Building Custom Images
Creating Docker Images
To create a docker image, following steps define the process:
Create a Dockerfile. A Dockerfile is a plain text file with a few lines of configuration inside it. This configruration is going to define how the container behaves, what different programs it is going to contain and what it does when it starts as a container.
The Dockerfile is then passed to the docker client.
Next, the docker client will pass the file to the docker server. The docker server will use the Dockerfile to build a usable image.
The below diagram illustrates the above process:
Creating a Dockerfile has the following flow:
Building a Dockerfile
As an example, we will create a Dockerfile to create an image that runs redis-server
. Following is the content of the Dockerfile we will use to create an image:
Next, from the directory where the Dockerfile is created, run the following command:
The output of the command would look some thing like below:
Next, we use the id of the build image to create and start the container using docker run
:
Dockerfile Details
Following is the flow of what docker build
command does using the Dockerfile:
Tagging an Image
Rather than starting and running a container using a id, we can tag an image during the build process with a name. Following is how it can be done:
The convention from tagging an image with a name is as follows:
So, the flow of commands for building an image with a tag name and running a container from the built image goes like below:
Image Generation with Docker Commit
Till now, we created and started a container using a docker image. We can also go the other way and create a docker image using a running container.
Following is an example how to go about image generation using a running container:
As can be observed above, we started a container using an alpine image and installed redis inside the container. Next, we get the id of this running container and use it to generate an image from it.
Finally, using the id of the generated image, we can run a new container:
Last updated