Using Docker Hub to Pull and Push Images

Tutorial 4 of 5

1. Introduction

This tutorial aims to guide you on using Docker Hub to pull and push Docker images. Docker images are templates that include the runtime and the code of your application, which you can use to create Docker containers. Docker Hub, on the other hand, is a central repository where you can store and distribute Docker images.

By the end of this tutorial, you will learn how to:

  • Pull an image from Docker Hub
  • Make changes to this image
  • Push the modified image back to Docker Hub

Prerequisites:

  • Basic knowledge of Docker
  • Docker installed on your system
  • Docker Hub account

2. Step-by-Step Guide

Pulling an Image

To pull an image from Docker Hub, you use the docker pull command followed by the name of the image. For instance, to pull the latest official image of Ubuntu, you use:

docker pull ubuntu:latest

Making Changes to the Image

After pulling the image, you can run a container from it and make changes. To do this, use the docker run command followed by the image name. In the container, you can install new software, modify files, etc. Here's how to run a bash shell in an Ubuntu container:

docker run -it ubuntu:latest /bin/bash

Pushing the Image Back to Docker Hub

Once you've made your changes, exit the container and use the docker commit command to create a new image from the container. Finally, use the docker push command to push this new image to Docker Hub:

docker commit container_id username/new_image_name
docker push username/new_image_name

3. Code Examples

Example 1: Pulling an Image

# Pull the latest Ubuntu image from Docker Hub
docker pull ubuntu:latest

Example 2: Running a Container and Making Changes

# Run a container from the Ubuntu image
docker run -it ubuntu:latest /bin/bash

# Once in the container, install Node.js
apt-get update
apt-get install -y nodejs

Example 3: Committing and Pushing the Image

# Commit the changes to a new image
docker commit container_id username/ubuntu_node

# Push the new image to Docker Hub
docker push username/ubuntu_node

4. Summary

In this tutorial, you learned how to pull an image from Docker Hub, make changes to it, and push the modified image back to Docker Hub. The next step could be learning how to automate the creation of Docker images using Dockerfiles.

5. Practice Exercises

  1. Pull the latest image of Alpine Linux from Docker Hub and install Python in it. Then, push the resulting image to your Docker Hub account.

  2. Create a Docker image with the latest version of Node.js and Express installed. Push this image to Docker Hub.

Here are some tips for further practice:

  • Try to create Docker images with different software installed.
  • Learn how to use Dockerfiles to automate the image creation process.
  • Explore other Docker commands and their options.