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


โœ… Step-by-Step Guide

๐Ÿ”น Step 1: Open Terminal (or Command Prompt)

Open your terminal or command prompt. This is where youโ€™ll run all Docker commands.


๐Ÿ”น Step 2: 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.


๐Ÿ”น Step 3: 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:


๐Ÿ”น Step 4: 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.
...

๐Ÿ”น Step 5: 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!


Youโ€™re encountering this 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

๐Ÿช› Step 1: Find the Process Using Port 8080

Run this command in your terminal:

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.


๐Ÿšซ Step 2: 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.


โ–ถ๏ธ Step 3: 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.


๐Ÿ“Œ Summary

ProblemSolution
Port 8080 is already in useKill the conflicting process or use another port
Canโ€™t find which process is using itUse lsof -i :8080 or netstat
Donโ€™t want to kill the processRun NGINX on a different port: -p 8000:80

๐Ÿ™Œ Final Tip

Once youโ€™re comfortable with these basics, explore Docker Hub (https://hub.docker.com) to find thousands of pre-built images for apps like MySQL, Redis, Python apps, etc.


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


๐Ÿ“ Step 1: 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.


๐Ÿ“„ Step 2: Create a Simple Application

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

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

touch app.py

Now open app.py in a text editor and 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 requirements.txt file:

echo "flask" > requirements.txt

This tells Docker which Python packages to install.


๐Ÿณ Step 3: Create a Dockerfile

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

Create the Dockerfile:

touch Dockerfile

Open it in your editor and add the following content:

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

# Set the working directory inside the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the outside world
EXPOSE 5000

# Define environment variable (optional)
ENV NAME="Docker"

# Run app.py when the container launches
CMD ["python", "app.py"]

๐Ÿ—๏ธ Step 4: 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.


โ–ถ๏ธ Step 5: Run the Container

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

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

๐Ÿ’ก Explanation:


๐ŸŒ Step 6: 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


๐Ÿ” Step 1: 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.


๐Ÿ—๏ธ Step 2: 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.


๐Ÿš€ Step 3: 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.


๐ŸŒ Step 4: 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
How to Connect a Netlify sites to a Custom Domain or subdomain Using Cloudflare with SSL Certificate