Getting started with Docker Hub, Docker and Docker Compose — Part 2

Godfrey Odenigbo
7 min readJul 19, 2023

--

Abstract
In Part 1 of this series, we explored the fundamentals of Docker, Docker Hub, and Docker Compose. We learnt how to create and manage containers, as well as how to build and run applications using Docker and Docker Compose. In this part, we will delve deeper into Docker Hub and discuss how to utilize it to streamline your containerization process. This article aims to get you up and working fast with docker, docker compose by providing a basic guide to using Docker Hub, incorporating images from Docker Hub into your Docker and Docker Compose workflows, and searching for base images for your Dockerfile.

Table of Contents:
1. Abstract
2. Introduction
3. Docker and Docker Compose with Docker Hub
4. Docker create container using image from Docker Hub
5. Docker Compose create container using an image from Docker Hub.
6. Searching for base images on Docker Hub
7. Conclusion

Introduction
Before we dive into the specifics, let’s recap the basics. Docker Hub is a cloud-based repository provided by Docker that allows you to store and share Docker images. Docker itself is an open-source platform that enables developers to automate the deployment of applications inside containers. Docker Compose, on the other hand, is a tool that allows you to define and manage multi-container Docker applications.

Docker and Docker Compose with Docker hub

Firstly, let’s set up a new docker hub new account setup.

Head over here to create a Docker Hub account or head over here to Login to Docker Hub, then generate a security token next.

Generate a security token

To interact with Docker Hub programmatically or authenticate with the Docker command-line interface (CLI), you’ll need a security token. Here’s how you can obtain one:
- Log in to Docker Hub and navigate to your account settings.
- Select the “Security” tab.
- Click on the “New Access Token” button.
- Provide a description for the token and select the desired access scopes (you can go with the default selection on the docker hub website).
- Click on the “Create” button to generate the token.
- Make sure to copy and securely store the token since it will not be shown again and to get a new one, you would have to repeat this process.

Next, Creating a Repository

Image above shows a docker account with no repositories yet
Image above shows a docker account with no repositories yet

- Head over to the repositories page, which should not have any repositories to show you.
- On the right hand side of the page, there’s a button “Create repository” which we would click to create our first repository.
- Fill out the form containing basic information about your docker repository. Information like the repo name and a description field which is not compulsory.
- Once done click “Create”. Before clicking create, there’s a selection to be made. You have to select the repository visibility i.e a private repo or a public one. By default, the repos are public, also free accounts can create only one private repo.

Pushing Images to Docker Hub Repositories
Now we have a Docker repository for our file on the cloud, and we can push it to the repository with the command ‘docker push gvgodfrey/expressjs:latest’. (Note: ‘gvgodfrey’ is my Docker Hub username, and ‘expressjs’ is the name of the repository. These values would be different for your username and project.) Here’s what each part of the command means:

  • docker push’: This command is used to push Docker images to a registry. It instructs Docker to upload the specified image to a registry so that others can access and use it.
  • gvgodfrey/expressjs’: This is the name of the Docker repository or image. It follows the format ‘<username>/<repository>’, where ‘gvgodfrey’ is the username, and ‘expressjs’ is the repository name.
  • :latest’: This is the tag assigned to the Docker image. Tags are used to identify different versions or variations of an image. In this case, ‘latest’ refers to the most recent or current version of the ‘expressjs’ image.

Note: If you already have your Docker image built, you can proceed. However, in my last article, we built an image locally. Now, to be able to run the command above, we need to execute some additional Docker commands. It is assumed that you have installed Docker; if you have not, check out this awesome article by James Walker. If you do have Docker installed, proceed to run this command: ‘docker build . -f ./docker/Dockerfile -t <username>/<image name>’, which follows the project folder structure from the last article, with ‘-f ./docker/Dockerfile’ pointing to the location of our Dockerfile. For me, the command I run is ‘docker build . -f ./docker/Dockerfile -t gvgodfrey/expressjs’, and it should return a similar response to the image, which will be shown below.

Let’s break down the addition to the build command above as we have touched this in my previous article, A Beginner’s Guide to Starting with Docker and Docker Compose. In the addition to the command above, the “-t” flag would be covered.

The -t or — tag flag: This flag option takes two arguments, the name of the image and the tag e.g myimage:v1.0. The tag is an optional identifier that you can use to track different versions of your image.

Image above shows a successfully built docker image
Image above shows a successfully built docker image

Once the command is run, and the image is built, you can proceed to run the push command from earlier, and it should push the image to the repo on Docker Hub. If you are facing a login issue, don’t panic; we will log in now via the CLI using the token generated earlier.

To log in, please run this command in the terminal: ‘docker login -u <username>’ where ‘<username>’ should be replaced with your appropriate username. You will be prompted to enter your password, where you should supply the token generated and copied earlier.

Docker create container using an image from Docker Hub

Building a container using an image from Docker Hub is straightforward. Follow these steps:

  1. Open a terminal or command prompt and ensure Docker is installed and running.
  2. Use the following command to pull an image from Docker Hub: docker pull <image_name>:tagin in my case docker pull gvgodfrey/expressjs:latest
  3. Once the image is pulled, you can create and run a container using the following command: docker run gvgodfrey/expressjs.
Image above shows the output docker pull and run commands.
Image above shows the output docker pull and run commands.

Docker compose create container using an image from Docker Hub.

Building a container using an image from Docker Hub with docker-compose is also quite easy. We will use the docker-compose file from the previous article and make some necessary modifications to achieve the desired outcome as shown below:

image above shows updated YAML file for our docker compose using the image from dockerhub

Where the new YAML keys added are:

1) image: This key takes the name of the image as its value in this case, gvgodfrey/expressjs. The build command was also removed as this was its replacement. This key informs the container of what image to use.
2) restart: This key specifies how you want Docker Compose to restart a service if it fails. The YAML values that can be provided is between the following:

- no: The service will not be restarted if it fails.
- on-failure: The service will be restarted if it fails.
- always: The service will be restarted regardless of whether it fails or not.
- unless-stopped: The service will be restarted unless it’s stopped manually.

The restart key is a powerful tool that can help you to keep your applications running smoothly. By specifying the restart key, you can ensure that your services are restarted if they fail, even if the failure is caused by a transient error.

Searching for base images on Docker Hub

When creating your own Docker images, you often start with a base image. Docker Hub provides a vast collection of pre-built images that can serve as a foundation for your containers. Here’s how you can search for base images on Docker Hub:
- Visit the Docker Hub website.
- In the search bar, enter keywords related to the base image you’re looking for e.g node.
- Browse through the search results and choose an image that suits your needs.
- Take note of the image name and version for later use in your Dockerfile e.g node:18.13.0-alpine.

Conclusion

In this article, we explored the practical aspects of using Docker Hub, Docker, and Docker Compose. We learned how to set up a new repository on Docker Hub, obtain a security token, build containers using images from Docker Hub. Additionally, we discovered how to search for base images on Docker Hub to serve as a starting point for creating our own Docker images. Armed with this knowledge, you are now equipped to harness the power of Docker Hub and leverage its vast ecosystem of container images. Happy containerizing! Stay tuned!

--

--

Godfrey Odenigbo

Full Stack Engineer | NodeJs | .Net | Containers | Mongo | Postgres