What is Docker?
Published On: 2019/10/04
A docker is an open source tool created to help developers and operations to package, deploy and run applications easly by using containers. Have you ever purchased a salad or a meal box from a restaurant? If yes, then you can easly relate it to a docker container.
What is a container ?
A docker container image is a unit packaged with application code, required runtime for the application to run, system tools, system libraries and settings. I would relate this with a salad box where you get salad, toppings and cutlery in a box. A docker image becomes a container when it is deployed and running in the Docker engine. A container is a lightweight unit which uses the underlying OS kernel and is run as an isolated process in user space.
Is it same as Virtual Machine (VM) ?
The docker container is different from a virtual machine. For a better understanding we can compare it to a serviced apartment (docker container) and an unfurnished apatment (VM). Just like in the case of a serviced aparment where the tenant can make use of the household items provided by the owner, the docker container can use the underlying operating system and its functions to run the application code. In the case of a Virtual Machine, an operating system has to be installed to run the application code just like the unfurnished apartment where the tenant is expected to bring his own household items.
Dockerfile and .dockerignore file
A Dockerfile contains a set of instructions which will be called by the docker build tool, in the given order, while creating the docker image of your application. Some of the frequently seen commands are given below. I would compare this file to the instruction leaflet which is enclosed in the “Assemble-It-Yourself” products. The .dockerignore file contains a list of rules for excluding files and folders while building the docker image. This is very much similar to a .gitignore file used in GIT version control system.
FROM, RUN, CMD and Entrypoint in Docker
The RUN instruction in the docker file is something like adding an extra layer on top of the current image. Just relate it to preparing a burger, where each RUN command add an extra layer to the burger and put it into a box.
The main purpose CMD instruction is to provide defaults for an executing container. These defaults can include an executable, or can be without an executable and in that case we must provide the ENTRYPOINT instruction in the dockerfile. CMD should be used as a way of defining default arguments for an ENTRYPOINT command or for executing an ad-hoc command in a container.
The RUN instruction will be executed at the time of building an image but the CMD instruction will set the intended command for the image and this command will be executed during the container spin up.
If we are just specifying the parameters to the executable in the CMD instruction, then we must add the ENTRYPOINT instruction in the docker file. An ENTRYPOINT allows you to configure a container that will run as an executable.
Both CMD and ENTRYPOINT has two forms.
exec form :: This is the preferred form of both CMD and ENTRYPOINT instruction. In this form arguments are passed as JSON array format.
CMD ["executable","param1","param2"] ENTRYPOINT ["executable", "param1", "param2"]
In this form you must give the absolute path of the executable as it is not starting a shell to execute the executable. Note that in the exec form variable substitution will not work so you need to provide the exact value which must be used as the parameter.shell form :: In the shell form the executable command and the parameters are not enclosed in JSON array format instead directly given next to the CMD or ENTRYPOINT instruction.
The disadvantage of the shell form is that the ENTRYPOINT instruction will be executed in a shell so that the executable will not be the containers PID 1 process. As a result of this the executable will not recieve signal SIGTERM from “docker stop”.CMD command param1 param2 ENTRYPOINT command param1 param2
docker-compose
docker-compose.yml is a file which describes the services that make your application. I would compare it with a cooking recipe which a chef uses to create a dish. This file is kind of a configuration file which the compose tool uses and internally triggers required commands to allocate necessary operational resources.
Conclusion
In this article i have provided you an overview on docker. If you would like to learn more on docker and its commands visit the site https://docs.docker.com.