In this tutorial, we will learn how to use Docker Hub for pulling and pushing images. Docker Hub is a cloud-based registry service that allows you to link to code repositories, build your images and test them, stores manually pushed images, and links to Docker Cloud so you can deploy images to your hosts.
By the end of this tutorial, you will know how to:
- Pull Docker images from Docker Hub
- Push your images to Docker Hub
Prerequisites:
- Basic understanding of Docker
- Docker installed on your machine
- Docker Hub account
To pull an image from Docker Hub, you can use the command docker pull
. The syntax of the command is as follows:
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
For example, if you want to download the latest Ubuntu image, you can use the following command:
docker pull ubuntu:latest
Before you can push an image to Docker Hub, you need to tag it with the username, repository, and version. Use the command docker tag
to achieve this:
docker tag local-image:tagname reponame:tagname
After tagging the image, you can push it to Docker Hub using the docker push
command:
docker push reponame:tagname
Let's pull the latest version of the nginx image:
# Pull the latest version of the nginx image
docker pull nginx:latest
After running the command, Docker will download the nginx image from Docker Hub.
First, let's create a Dockerfile:
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory in the container to /app
WORKDIR /app
# Add the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Now, build an image from the Dockerfile:
docker build -t friendlyhello .
Then, tag the image:
docker tag friendlyhello YOUR_DOCKERHUB_NAME/friendlyhello:tagname
Finally, push the image to Docker Hub:
docker push YOUR_DOCKERHUB_NAME/friendlyhello:tagname
After running the command, Docker will upload the friendlyhello image to Docker Hub.
In this tutorial, we've learned how to pull images from Docker Hub and push our own images to Docker Hub. This is an essential skill for any developer using Docker, because it allows them to share their images with others and use images created by others.
Next, you might want to learn how to use Docker Compose to manage multi-container applications, or how to use Docker Swarm for clustering and scheduling Docker containers.
Solutions to these exercises can be found on the Docker documentation page. Keep practicing with different images and Dockerfiles to become more comfortable with Docker Hub.