Upload your own image on Docker Hub

One of the very first steps in the containerization process of a service or application is constructing the image that is going to be used for every component. A component may require a plain image that is just an Apache server, but in the majority of the cases, you'll have to add packages or apply configurations to the images already provided in Docker Hub. This will result in creating your own images that you may eventually share on Docker Hub.    

In this post, we're going to follow the process of creating our own image and then publishing it.

First, we need to decide the image on top of which we're going to build our own. To make this call, you need to know what services and applications are going to be running on your container. Take WordPress for example. You can start with the official image, one of the very popular Bitnami images, or with just a PHP-FPM image. In this example, we're going to build on top of the PHP Apache image and save all of our files in a directory called myImage.

Docker-file
The first step of the process is to create the Dockerfile file. Use your favorite text edition to create the file in the image folder. The name of the file should be Dockerfile, without extension.

The contents of this file are the instructions to be followed by docker when creating the image. More information on Dockerfiles is available here

In our case, the contents of the Dockerfile would be:

# Create an Apache server
FROM php:7.4-apache

# Prepare apt
RUN apt-get update

We are using the image php:7.4-apache and we execute the update command upon creation.

Image Build
Now it's time to create the image on our system. From within our image directory, we're going to issue the docker build -t myimage . command. There's a period at the end of the command that represents the build context to use, in our case the directory we're working in.


If you get the list of images in the system, you should see the new image:

Docker Account PAT
To upload the image to your docker account, you need to create a personal access token. Log in to Docker Hub and go to Account Settings from the drop-down menu on the top left (your username). Now switch to the Security tab and click on the New Access Token button as shown below:

Add a description to the token so that you can later identify and remove it if necessary and go with the default permissions (Read / Write / Delete).

After clicking Generate, a screen with the value of the token is shown. Make sure you copy the value and store it somewhere safe!

To login to your account via terminal, execute the docker login -u <DockerHubAccount> command and use the value of the token as the password, where <DockerHubAccount> is your account alias on Docker Hub:

Image Tag & Push
Now that you've logged into your account using the terminal, you can tag and push your image. For the purposes of this guide, I'm going to tag the image with the latest tag using the command: docker image tag <imageName> <DockerHubAccount>/<imageName>:latest

Replace <imageName> with the name of your image and again <DockerHubAccount> with the name of your account in docker hub. Listing the images will show an additional image, but with different tags.

The image has been tagged and is ready to be pushed to docker hub using the command: docker image push <DockerHubAccount>/<imageName>:latest

When the push process completes, the image should be available on your Docker Hub account:


To test that everything went fine, you can try to pull the image:
If you issue the pull command from the same machine you created the image, there shouldn't be any changes!

Containers
You may now create containers based on that image using: docker create cpolydorou/myimage:latest 

The container has been created successfully, using your own image!

From this point on, the only thing left to do is to learn more about how to construct your dockerfile and decide on a tagging strategy for your images. 


Popular posts from this blog

Domain Controller Machine Password Reset

Managing Active Directory User Certificates using PowerShell

Configuring a Certificate on Exchange Receive Connector