Step by step guide to create Docker Image
We have, in our earlier tutorial discussed about the Dockerfile i.e. what are Dockerfiles & how to create one. We also mentioned three different examples , one each for CentOS, Fedora & Ubuntu to create docker image with Webserver (apache).
We will now use one of those Dockerfiles (CentOS one) to create Docker image & will than upload the created image to Docker Hub. To simply explain Docker Hub, its a public registry that has over 15000 images that can be directly used or can be used to create a custom Docker image. Docker Hub is directly maintained by Docker.
So let’s start with creating a Docker Image, but before we do that let’s discuss some pre-requisites,
( Recommended Read: Important Docker commands for Beginners )
To create docker image, we will need following things,
→ We need a dockerfile, which we created in our previous docker tutorial . Dockerfile should only be named “Dockerfile” without any extensions.
→ We also need a file named “.dockerignore” , this file contains a list of all file or folders that we don’t to include on image. Though its not necessary but required if we have multiple dockerfiles or some other content on same folder.
Note: It is recommended that we create a separate folder for each dockerfile/docker image.
→ We will also need an account for Docker Hub, we will need it to upload our Docker image to Docker Hub. If you don’t already have an account, create from following Link , DOCKER HUB.
When we have these things ready, we can move to next part of the tutorial i.e. building a docker image.
Building a Docker Image
Before we create the image, make sure that all the required files and folders are in available & are kept in separate folder with Dockerfile. Once we have all these things, open terminal in the folder & execute the following command,
$ docker build -t centos7_with_apache .
Image will than start to build, it will take some time depending internet speed. Also notice . (dot), that is used for creating an image in the same folder.
Once the image has been successfully built, we can check the image by executing,
$ docker images
Testing the Docker Image
Now we can test and use the newly created Docker image by running the following command,
$ docker run –name centos7_apache_instance_1 -t centos7_with_apache
here, with ‘–name’ we can set the local name for docker image & can create a number of machine using the same Docker image by using the different names.
Uploading to Docker Hub
You can also keep the image on local system for personal use but you can also contribute to the Docker community by uploading the created Docker image to Docker Hub.
Login to Docker hub using CLI by running the following command,
$ docker login
You will than be asked for your UserName, Password & Email Address, you will than get confirmation for successful login along with another message saying “WARNING: login credentials saved in /root/.docker/config.json”. That’s the file that contains your credentials in encrypted format & its location may also vary depending on user being used.
We will now first create a tag to created Docker image. To create tag run the following command,
$ docker tag 06de5ba7941b linuxtechlab/centos7_with_apache:1st_image
’06de5ba7941b ‘ is the IMAGE ID for the created Docker image, which we can get by running the command “docker images” (see screenshot above)
‘linuxtechlab’ is the username for Docker Hub, &
‘:1st_image’ is the tag we provided.
Now we can push the image to Docker hub using the following command,
$ docker push linuxtechlab/centos7_with_apache
We have now created a Docker image, used it locally on our machine & also uploaded it on Docker Hub. If you are facing issues on any step or have a query, please do reach us using the comment box below.