Skip to content
Go back

Pull, Build an image using a Dockerfile and Push the image to Docker Hub.

Published:

Pull an image from Docker Hub and run it as a container


๐Ÿงฐ Prerequisites


โœ… Add user to the docker group for avoid typing sudo on every command.

Run the following command once with sudo:

sudo usermod -aG docker $USER

Then, log out and log back in (or reboot) for the group changes to take effect.

For immediately:

newgrp docker

๐Ÿ”น Pull an Image from Docker Hub

Use the docker pull command:

docker pull hello-world

๐Ÿ’ก Explanation:

This pulls the official hello-world image, which is small and used for testing Docker installations.


๐Ÿ”น List Images on Your Machine

After pulling the image, check if it was successfully downloaded:

docker images

๐Ÿ’ก Output will look like this:

REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f22117a   2 months ago   13.3kB

๐Ÿ’ก Explanation:


๐Ÿ”น Run the Image as a Container

Now, run the image using the docker run command:

docker run hello-world

๐Ÿ’ก Explanation:

๐Ÿ’ก What Happens?

You should see output like:

Hello from Docker!
This message shows that your installation appears to be working correctly.
...

๐Ÿ”น List Running Containers

To see containers currently running:

docker ps

But since hello-world exits immediately after printing the message, it wonโ€™t show up here.

To see all containers, including stopped ones:

docker ps -a

๐Ÿ’ก Output will look like:

CONTAINER ID   IMAGE         COMMAND    ...   STATUS                     NAMES
abc123xyz      hello-world   "/hello"   ...   Exited (0) 10 seconds ago   youthful_banach

๐Ÿ”น Optional: Remove the Container

If you want to clean up:

docker rm abc123xyz

Replace abc123xyz with your actual container ID or name.


๐Ÿ”น Optional: Remove the Image

To remove the image from your system:

docker rmi hello-world

๐Ÿ“Œ Summary of Commands

CommandDescription
docker pull <image>Downloads an image from Docker Hub
docker imagesLists all images on your system
docker run <image>Runs an image as a container
docker psLists running containers
docker ps -aLists all containers (including stopped ones)
docker rm <container>Removes a specific container
docker rmi <image>Removes a specific image

๐ŸŽฏ Example with Another Image (nginx Web Server)

Letโ€™s try something more practical โ€” running a real web server:

docker run -d -p 8080:80 nginx

๐Ÿ’ก Explanation:

Now open your browser and go to:
๐Ÿ‘‰ http://localhost:8080
You should see the NGINX welcome page!


If youโ€™re encountering this port error:

Bind for 0.0.0.0:8080 failed: port is already allocated

๐Ÿ” What does this mean?

This error occurs because port 8080 on your machine is already being used by another process, so Docker canโ€™t bind to it.


โœ… Step-by-Step Fix

๐Ÿช› Find the Process Using Port 8080

Run this command in your terminal:

sudo lsof -i :8080

Or if you donโ€™t have lsof, use:

sudo netstat -tulpn | grep :8080

Example Output:

COMMAND   PID   USER    FD   TYPE DEVICE SIZE/OFF NODE NAME
node    12345   user   20u  IPv6 123456      0t0  TCP *:8080 (LISTEN)

Here, the process with PID 12345 is using port 8080.


๐Ÿšซ Stop the Conflicting Process

Use the kill command with the PID from above:

sudo kill -9 12345

Replace 12345 with the actual PID you found.

โš ๏ธ Be careful not to kill important system processes. Make sure you know what the process is before killing it.


โ–ถ๏ธ Try Running the Docker Command Again

Now try starting NGINX again:

docker run -d -p 8080:80 nginx

It should now work and return a container ID like:

a14767df4f989392399558bee003840abe43350569edbe2c121ca304a683f1af

You can verify itโ€™s running with:

docker ps

And visit http://localhost:8080 in your browser.


๐Ÿ”„ Alternative: Use a Different Host Port

If you donโ€™t want to stop the existing service using port 8080, you can map NGINX to a different port on your host โ€” for example, 8000:

docker run -d -p 8000:80 nginx

Then open:

๐Ÿ‘‰ http://localhost:8000


๐Ÿงน Optional: Clean Up Unused Containers/Ports

To avoid future conflicts, clean up unused containers:

docker stop $(docker ps -aq)
docker rm $(docker ps -aq)

โš ๏ธ This will remove all running/stopped containers! Only do this if youโ€™re okay with that.


Build an image using a Dockerfile and Run the image as a container


๐Ÿ“ Create a Project Directory

Open your terminal and create a new folder for your project:

mkdir my-docker-app
cd my-docker-app

This will be the working directory where youโ€™ll create your Dockerfile and application files.


๐Ÿ“„ Create a Simple Application

For this example, weโ€™ll create a very simple Python web app using Flask.

๐Ÿ”น Create a file named app.py:

nano app.py

Now paste the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello from Docker!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

This is a basic Flask app that responds with "Hello from Docker!" when accessed via a browser.


๐Ÿณ Create a Dockerfile

A Dockerfile is a script containing instructions to build a Docker image.

Create the Dockerfile:

nano Dockerfile

Now paste the following code:

# Use an official Python runtime as the base image
FROM python:3.9-slim

# Set the working directory inside the container
WORKDIR /app

# Copy only the application file
COPY app.py /app/

# Install Flask directly
RUN pip install --no-cache-dir flask

# Expose the Flask default port
EXPOSE 5000

# Optional environment variable
ENV NAME="Docker"

# Run the application
CMD ["python", "app.py"]

๐Ÿ—๏ธ Build the Docker Image

From inside your project directory (my-docker-app), run:

docker build -t my-flask-app .

๐Ÿ’ก Explanation:

Youโ€™ll see output showing each step being executed โ€” downloading Python, installing Flask, copying files, etc.


โ–ถ๏ธ Run the Container

Once the image is built, run it as a container:

docker run -d -p 5000:5000 my-flask-app

๐Ÿ’ก Explanation:


๐ŸŒ Test the App

Open your browser and go to:

๐Ÿ‘‰ http://localhost:5000

You should see:

Hello from Docker!

Output-Dockerfile

๐ŸŽ‰ Youโ€™ve successfully built a Docker image and run a web app inside a container!


๐Ÿ—‘๏ธ Optional: Clean Up

To stop and remove the container:

docker stop <container_id>
docker rm <container_id>

Use docker ps to find the container ID.


๐Ÿ“‹ Summary of Key Commands

CommandDescription
docker build -t <image-name> .Builds a Docker image using the Dockerfile in current directory
docker run -d -p <host-port>:<container-port> <image>Runs a container from the image
FROM <base-image>Base image to start from (e.g., Python, Ubuntu)
WORKDIR /pathSets the working directory inside the container
COPY . /appCopies files from local machine to container
RUN <command>Runs a shell command during image build
EXPOSE <port>Documents which port the container listens on
CMD ["cmd", "arg1"]Default command to run when container starts

โœ… Bonus Tip: Best Practices


Push the docker image to Docker Hub


๐Ÿ” Log in to Docker Hub

In your terminal, run:

docker login

Youโ€™ll be prompted to enter your Docker Hub username and password.

Example:

Username: your-dockerhub-username
Password: **********
Login Succeeded

โœ… If successful, youโ€™re now logged in and ready to push images.


๐Ÿ—๏ธ Tag Your Image with Docker Hub Username

Before pushing an image to Docker Hub, you need to tag it with your Docker Hub username, like this:

docker tag my-flask-app your-dockerhub-username/my-flask-app

Replace your-dockerhub-username with your actual Docker Hub username.

๐Ÿ’ก Explanation:

You can verify the tagging by running:

docker images

You should see both my-flask-app and your-dockerhub-username/my-flask-app listed.


๐Ÿš€ Push the Image to Docker Hub

Now push your image:

docker push your-dockerhub-username/my-flask-app

This uploads the image to Docker Hub.

Example Output:

The push refers to repository [docker.io/your-dockerhub-username/my-flask-app]
...
Pushed
latest: digest: sha256:... size: ...

Output-docker image push to docker hub

โœ… Success! Your image is now available on Docker Hub.


๐ŸŒ Test Pulling Your Image from Docker Hub

To confirm everything works, stop and remove your local image:

docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker rmi your-dockerhub-username/my-flask-app
docker rmi my-flask-app

Then pull it back from Docker Hub:

docker pull your-dockerhub-username/my-flask-app

And run it:

docker run -d -p 5000:5000 your-dockerhub-username/my-flask-app

Go to http://localhost:5000 โ€” you should still see:

Hello from Docker!

๐ŸŽ‰ Youโ€™ve successfully pushed your image to Docker Hub and pulled it again!


๐Ÿ“‹ Summary of Key Commands

CommandDescription
docker loginLog in to Docker Hub
docker tag <local-image> <username>/<repo>Tag image for Docker Hub
docker push <username>/<repo>Upload image to Docker Hub
docker pull <username>/<repo>Download image from Docker Hub

๐Ÿ“ Optional: Add a Tag (e.g., Version)

Instead of always using latest, you can version your image:

docker tag my-flask-app your-dockerhub-username/my-flask-app:v1.0
docker push your-dockerhub-username/my-flask-app:v1.0

Then pull with:

docker pull your-dockerhub-username/my-flask-app:v1.0

๐Ÿงผ Bonus: Clean Up Local Images

To free up space:

docker images prune -a

Or just remove specific images:

docker rmi your-dockerhub-username/my-flask-app


Suggest Changes

Previous Post
Containers and Containerization - Docker
Next Post
Linux Command Line and Shell Scripting Cheat Sheet for Beginners and Practitioner