Pull an image from Docker Hub and run it as a container
๐งฐ Prerequisites
- You have Docker installed on your machine.
- Check with:
docker --version
- If not installed, follow the official Docker installation guide for your OS: https://docs.docker.com/get-docker/
sudo apt install docker.io -y
- Check with:
โ
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:
docker
: The main Docker CLI command.pull
: Tells Docker to download an image from Docker Hub.hello-world
: The name of the image to pull.
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:
REPOSITORY
: Name of the image (hello-world
)TAG
: Version tag (default islatest
)IMAGE ID
: Unique identifier for the imageSIZE
: Size of the image
๐น Run the Image as a Container
Now, run the image using the docker run
command:
docker run hello-world
๐ก Explanation:
run
: Tells Docker to create and start a new container from the specified image.hello-world
: The image to use for creating the container.
๐ก What Happens?
- Docker checks if the image exists locally.
- If yes (which it does), it starts a new container from that image.
- The container runs the default command defined in the image (in this case, prints a welcome message).
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
Command | Description |
---|---|
docker pull <image> | Downloads an image from Docker Hub |
docker images | Lists all images on your system |
docker run <image> | Runs an image as a container |
docker ps | Lists running containers |
docker ps -a | Lists 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:
-d
: Run container in detached mode (in background)-p 8080:80
: Map port 8080 (your computer) to port 80 (inside the container)nginx
: Official NGINX image
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:
๐งน 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:
docker build
: Command to build an image.-t my-flask-app
: Tags the image with a name (my-flask-app
)..
: Tells Docker to use the current directory as the build context (where it looks for files likeDockerfile
,app.py
, etc.).
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:
-d
: Run the container in detached mode (in background).-p 5000:5000
: Map port5000
on your host to port5000
in the container.my-flask-app
: The image to run.
๐ Test the App
Open your browser and go to:
You should see:
Hello from Docker!
๐ 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
Command | Description |
---|---|
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 /path | Sets the working directory inside the container |
COPY . /app | Copies 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
- Always use
.dockerignore
to exclude unnecessary files (like__pycache__
,.git
,venv
, etc.) - Avoid installing unnecessary dependencies
- Use multi-stage builds for more complex apps (advanced topic)
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:
docker tag
: Renames or tags an existing image.my-flask-app
: The local image name you built earlier.your-dockerhub-username/my-flask-app
: The new name for Docker Hub.
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: ...
โ 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
Command | Description |
---|---|
docker login | Log 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