Using Docker Hub to Pull and Push Images

Tutorial 1 of 5

1. Introduction

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

2. Step-by-Step Guide

Pulling Docker Images

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

Pushing Docker Images

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

3. Code Examples

Example 1: Pulling an Image

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.

Example 2: Pushing an Image

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.

4. Summary

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.

5. Practice Exercises

  1. Pull a specific version of the ubuntu image from Docker Hub, not the latest one.
  2. Build a Docker image from a Dockerfile, tag it and push it to Docker Hub.
  3. Pull an image from Docker Hub, make changes to it, and then push the updated image back to Docker Hub.

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.