Creating a custom image using Dockerfile
What is docker image?
A docker image is an image of OS which is used to launch different containers with docker. It is a copy of the original OS with some basic functionalities. We need the docker image to launch the container of any OS we want. Initially we need to download the image before launching any container. Examples of docker images are- CentOS, Ubuntu, etc. Docker has a public repository/registry hub.docker.com where you will find all the docker images. When you visit the Docker Hub first, create your free account and login to explore more images. Command to download CentOS image:
docker pull centos:latest
Syntax for the respective command is:
docker pull <OS_name>: <version>
Problem with these images is that they come with very limited functionalities i.e. limited software. So to set the required environment, we have to download all the packages and software we need. Every time you launch a new container, you have to again set the environment by downloading the required software and packages which is a time consuming process. You can reuse a stopped container again having the environment you have set but that wont be the case always. Also once you have deleted the container the data you have and the environment you have set will also be lost. Rather there are use-cases where we have to launch new containers every time. Even at times To solve this issue we can create our own custom image with all are requirements.
There are two ways to create a custom image:
- Commit
- Dockerfile
In this blog I am going to discuss about creating a custom image using Dockerfile.
What is a Dockerfile?
Dockerfile is on of the way to create customized image as per one’s use-case. Dockerfile is a text document that contains all the required commands to set the environment i.e. which packages to be installed, which commands to be executed, etc. at the time of building the docker image.
How to create a Dockerfile?
- Create a new workspace/directory with some name and open the directory to create a Dockerfile inside it
mkdir /docker_ws
cd /docker_ws
2. Create a text file with the name ‘Dockerfile’(naming the text file ‘Dockerfile’ is mandotory)
vim Dockerfile
3. The text file should contained the required image and the commands to set up the environment with required docker ‘key words’ as shown. Here I am setting up a web server using Apache web server-httpd on top of docker
In the above picture ‘my_web.html’ is a html file created already which is then copied in the container
4.Building the docker image-
After creating the text file, initially we have to build the image which same as compiling the code before execution. Following is the command to build the docker image-
docker build /docker_ws/ -t web_os:v1
Following command will tell whether docker image is built or not
docker images
5. Running the container-
To run the created image, use-
docker -run -dit — name my_web web_os:v1
Command to check whether the container is executed-
docker ps
To visit the webpage we need the IP address of the container, use following command with container name to get IP address of the container-
docker inspect my_web
You can visit the webpage via your terminal or from the browser-
a. Via terminal-
curl 172.17.0.2/my_web.html
b. Via browser-
Why use Dockerfile instead of Commit?
One of the main key point to use Dockerfile is that dockerfile is editable. Every time we want to make any change in the container we simply open the Dockerfile text file and make the required changes. Just remember one thing, evrey time you make any change in the Dockerfile also build the image again. Its like whenever we change anything in the code, we always compile it again before executing it. While in commit its not possible to make any changes in the container once launched. Whenever we want to add something, we have to create a new container with commit. Thus, dockerfile is mutable whereas commit is immutable.
Keywords used in Dockerfile:
Here I am explaining the keywords used in above example. To use the keywords, always refer to Dockerfile Reference file- https://docs.docker.com/engine/reference/builder/
FROM: The FROM
instruction initializes a new build stage and sets the Base Image for subsequent instructions. As such, a valid Dockerfile
must start with a FROM
instruction. The image can be any valid image – it is especially easy to start by pulling an image from the Public
FROM [--platform=<platform>] <image>[:<tag>] [AS <name>]
RUN: The RUN
instruction will execute any commands in a new layer on top of the current image and commit the results. The RUN
keyword generally used when we to execute a command during the built time.
RUN <command>
WORKDIR: The WORKDIR
instruction sets the working directory for any RUN
, CMD
, ENTRYPOINT
, COPY
and ADD
instructions that follow it in the Dockerfile
. WORKDIR
keyword helps us to open the directory/file location as soon as the container is launched.
WORKDIR /path/to/workdir
COPY: The COPY
instruction copies new files or directories from <src>
and adds them to the filesystem of the container at the path <dest>
COPY [--chown=<user>:<group>] <src>... <dest>
ENTRYPOINT: An ENTRYPOINT
allows you to configure a container that will run as an executable. ENTRYPOINT
executes the command during the run time. As soon as the command executed through ENTRYPOINT
the user gets exited from the conatiner.
ENTRYPOINT ["executable", "param1", "param2"]
CMD: The main purpose of a CMD
is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT
instruction as well. Generally the CMD
keyword is used to pass default parameters for the ENTRYPOINT
instruction.
CMD ["executable", "para1", "para2"]
There are more keywords in Dockerfile, please refer to the Dockerfile documentation to use them.