In previous articles, I have explained about Docker Container Introduction and how to use Docker Container custom Networking. In this article, I am going to explain you about the Docker Custom Image creation. This whole document will provide you an idea of Docker Custom Image creation. so let’s get start with the quick introduction of Docker Image.
Suggestable Read: Basics of Docker Container and the installation
Also, Read: Docker Container | Customize the docker Network
Docker image can be described as a template with all required configurations. Whereas a container is a running instance of Docker image. Like containers, an image is not bound to the states i.e. Image does not have states.
There are different images available from the OS/Application vendor along with the custom images from the community.
When working on container a DevOps/Application engineer generally create their own Docker image with all the customizations, this enables them to launch a container quickly.
Methods for custom image creation:
In this way, you can download the base Docker OS image -> create container -> manually launch a shell -> perform the customization -> commit the changes.
This process will save your container to a Docker image and that image can be stored/distributed.
The automated method using Dockerfile:
Dockerfile is a text file with the directives/instructions for the image creation. “docker build” command is used to build the image which creates/configures the image automatically by reading the dockerfile. Dockerfile accept the information in the following format
In the last session on “Quick Introduction to Docker”, we have covered the “interactive way” of image creation and in this session, we are going to create a Docker image using the dockerfile method.
Using the Dockerfile, we are going to create an Apache HTTPD Web server on CentOS 7 image. At the high level below configuration will be performed/applied to the Docker image.
- Download the official CentOS7 image.
- Perform package update on the image.
- Install Apache HTTP server.
- Add a directive to include/copy the index.html from the Docker mgmt server to document root ( i.e. /var/www/html ) of the image.
- Enable port 80 for automatically whenever a container created from this image.
- Configure the auto start-up of Apache HTTPD service.
Below are the directives we are going to use in the dockerfile.
FROM: this directive tells which base image to be used to create the custom image, example centos/ubuntu/fedora etc.
RUN: this directive is used to define the commands to be executed during the image build.
ADD: this directive is used to define the files/directories to be copied from the source ( local server) to the image during image build.
ENTRYPOINT: this directive defines the container as executable.
CMD: this directive is used to define the arguments for the ENTRYPOINT command.
EXPOSE: this directive defines the network ports on which container will listen.
Also, Check out the Video for more details:
Sample dockerfile: Docker Custom Image creation
|# use latest centos7 image.|
# add the image maintainer name and email id
MAINTAINER Yogesh Mehta email: [email protected]
# update the centos image with latest available updates
RUN yum update -y
RUN yum clean all
# install network utilities, such as ( ifconfig,netstat, etc)
RUN yum install net-tools –y
# install apache httpd web server
RUN yum install httpd -y
RUN yum clean all
# copy the index.html file from current directory to image’s document root
ADD index.html /var/www/html/
# define image to allow listen on port 80 (whenever a container created)
# define the commands to be executed when container boots (created from this image)
ENTRYPOINT [ “/usr/sbin/httpd” ]
CMD [ “-D”, “FOREGROUND” ]
# docker build -t [repository/image_name]:[tag] .
Test the newly created image by creating a container.
# docker run -it -d -P [image id]
# curl [container IP]:80
Conclusion: I hope this article and the video will give you a basic knowledge of Docker Custom Image creation and how can you build your own custom image to create Docker Containers.