Docker

Docker Images – Creating Docker Images

Begin with creating docker images

Now, after going through almost all the required basics of docker, we need to go deeper and to do so it is necessary to learn about docker images. In order to understand that, we are going to see different methods of creating docker images.

There a basically two ways with which we can create docker images:

  1. Using “commit” argument
  2. Using “Dockerfile

Using “commit”

First of all, we need to check what images we are currently having, and as the output of the below command shows that there are none.

list docker images

sudo docker image ls

Now, let us create a container which has following:

  • Name = container1
  • Base Image = alpine
  • On Execution = run “ash

Note: Because we are not having the alpine image locally, the docker is going to first check the image in local storage and if not found then it is going to pull it from the “hub.docker.com” and run as shown below.

run docker image using alpine image

sudo docker container run -i -t –name container1 alpine ash

After getting the “a shell’s” prompt, we are going to install “elinks” in the container running based on “alpine image“.

Explanation of command used below:

  • apk = it is package manager for alpine
  • add = argument stating for install something
  • –no-cache = while downloading, do not cache the index locally
install elinks in alpine

apk add –no-cache elinks

Question: Why we have used “–no-cache” option?

Answer: Because we want that our final image must of small size and this cache is not required at all, because “here our main task is only to create a image based on alpine image having elinks utility installed in it”.

Now, we have to first test the “installation of elinks” by opening the following webpage.

access a website using elinks

elinks google.com

As can be seen from the output of above command, elinks is running properly and we have successfully accessed the google.com homepage.

In addition, it is now time to check that whether is there any container running or not, and it is seen clearly from picture below that yes a container named as “container1” is running smoothly.

listing docker containers

sudo docker container ls

Now, “open a second terminal” because it is time to create the “final image” from the running container, and as shown below we have used a commit argument to create an image from running container “container1

Explanation of command used below:

  • commit = create an image from the current running state of the specified container.
  • container1 = this is the name of the running container from which we are going to create our final image having “elinks” installed in it.
  • container-elinks = this the name of our final image.
create docker image using commit

sudo docker container commit container1 container-elinks

After executing the above command, it is now time to check our manually created image, and by using the command below and checking its output, we can see that the image has been successfully created.

listing docker images

sudo docker image ls

Question: Why is there a size difference in above two images?

Answer: It is because the image named as “container-elinks” is having “elinks” installed in it, and the one which is named as “alpine” it is only the base image upon which the “container-elinks” is created.

Checking our manually created image

Running the following command and then accessing the google.com using “elinks” makes is 100% clear that our “manually created image” named as “container-elinks” is successful.

run docker container from a custom image

sudo docker container -i -t –name elinks-installed container-elinks ash

access a website using elinks

elinks google.com

listing docker containers

sudo docker container ls

Output of above command cross-verifies this that the custom image is used for this.

Using “Dockerfile”

This is the most widely used method to create docker images, and to do so:

  • First we need to create a directory named as “playground“.
print working directory in linux

pwd

  • Create a file under it using CAT command, and name it as “Dockerfile” and add the following contents to it.
use cat command to view a file

cat <filename>

  • Crosscheck the file with the following command or “ls -l” or “ls“.
long listing of files in linux

“ll” OR “ls -l” OR “ls”

After doing the above steps, check the output of the command below, currently we have only two images in our system, which are created earlier in this post.

listing docker images

sudo docker image ls

Now, in order to create an image, first make sure your the in the “playground” where you have created your “Dockerfile“, after navigating to it, execute the following command in order to create the image from it.

Explanation of command used below:

  • image = giving the context to the docker.
  • build = stating that we want to build something.
  • -t = with this flag we can name our final image or tag it.
  • curl = this is going to be the name of our custom image created with the help of “Dockerfile”.
  • . = this “dot” states that the “reference Dockerfile” is present in the current directory, which in this case is “playground”.

Do closely look the picture below, the custom image is being created in steps:

Step 1/2: FROM alpine:3.8 => This is what we have written as the first line in our above created “Dockerfile“. Please notice that the base image in this process has an id of “3f53bb00af94” which is image id of “alpine:3.8“.

Step 2/2: RUN apk add –no-cache curl => For futher step and process, an intermediate image having an id of “6450b14ea4d7” has been created and the top of it “elinks” is going to be installed. Please notice once again that after the installation of “elinks“, the intermediate container/image is going to be removed and a final image having an id of “715583b5c31f” has been created, this is our final custom docker image tagged as “curl:latest“.

 create docker image using dockerfile with build argument

sudo docker image build -t curl .

We can confirm that on execution of above command, we have successfully created our custom image by running the following command:

  • curl = name of the image.
  • latest = tag of the image
  • 715583b5c31f = image id

sudo docker image ls

Checking our manually created image

By executing the command below and checking the “curl’s installation” by accessing the “google.com” we can see that the output is provided by the “curl” is actually the “html code of google.com”, it states that our custom image is working properly.

sudo docker container run -i -t –name container-curl curl ash

More on Docker:

Comment here