We have, in our earlier tutorial discussed 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 images with Webserver (apache).
We will now use one of those Dockerfiles (CentOS one) to create a Docker image & will then upload the created image to Docker Hub. To simply explain Docker Hub, it's 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.
Recommended Read: Important Docker commands for Beginners
Also Read: Learn how to use Docker Compose (docker-compose.yml)
So let's start with our tutorial on how to create Docker Image, but before we do that let's discuss some pre-requisites,
Pre-requisites
To create a docker image, we will need the 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 files or folders that we don't include on the image. Though it's not necessary but required if we have multiple dockerfiles or some other content on the 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 the following Link, DOCKER HUB.
When we have these things ready, we can move to the 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 available & are kept in a separate folder with Dockerfile. Once we have all these things, open the terminal in the folder & execute the following command,
$ docker build -t centos7_with_apache .
The image will then start to build, it will take some time depending on internet speed. Also, notice the '.' (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 the docker image & can create a number of machines using the same Docker image by using the different names.
Uploading to Docker Hub
You can also keep the image on the local system for personal use but you can also contribute to the Docker community by uploading the created Docker image to Docker Hub.
Log in to Docker hub using CLI by running the following command,
$ docker login
You will then be asked for your UserName, Password & Email Address, you will then get a 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 the user being used.
We will now first create a tag to create a Docker image. To create a 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 the 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.
We are giving you exclusive deals to try Linux Servers for free with 100$ credit, check these links to claim your 100$,
DigitalOcean - 100$ free credit & Linode - 100$ free credit
Check some Exclusive Deals, HERE.
Also, check out DevOps Book You should read section.