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:
Prerequisites:
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
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
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
# Pull the latest Ubuntu image from Docker Hub
docker pull ubuntu:latest
# 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
# 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
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.
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.
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: