We IT people LOVE tinkering with new tech stuff, and Docker is no exception. As a matter of fact, Docker was ranked as the #1 most wanted platform, #2 most loved platform, and #3 most broadly used platform in the 2019 Stack Overflow Developer Survey, and #1 most wanted platform, #2 most loved platform and #3 most popular platform in the 2020 edition of the same survey.
If you've bumped into this article, chances are that you have been bamboozled by the
fruit doggo difference between the
ENTRYPOINT instructions defined in a Dockerfile, used to describe how a Docker container should be run.
Containers are lightweight, standalone, executable packages of software designed for running specific tasks, no matter if short-lived or long-running. Therefore, a container's lifecycle depends on the ongoing process inside of it.
Once the process stops, the container stops as well.
Containers are dependent on images and use them to construct a run-time environment in order to run an application. A Dockerfile is a script which role is to define the process which leads to the creation of a final image, and how to run a container bound to said image.
The process of creating a Docker image usually begins with a
FROM command, defining a base image to build our image upon. We usually follow up with a bunch of
RUN instructions, used to run commands which modify the underlying filesystem.
RUNinstruction creates a new image layer that contains a modification to the filesystem.
ENTRYPOINT are two different types of instructions used to define how a Docker container should be run.
Let's dive into the details of each one:
CMD instruction defines the default command used to run a Docker container from an image.
Given a simple image like the following:
FROM alpine CMD ["echo", "Hello, World!"]
Running it with:
docker container run my-container
Will yield the following result:
A Dockerfile can contain multiple
CMDinstructions, but every single one of them, except for the last one, will be ignored.
CMD instruction can be overridden by the arguments passed to
docker container run, like:
docker container run my-container echo "Hello again!"
ENTRYPOINT instructions define, just like their CMD siblings, the default command used to start a container.
FROM alpine ENTRYPOINT ["echo", "Hello, World!"]
Well, the main difference is that, if both a
ENTRYPOINT instructions are defined inside a Dockerfile, the
CMD instruction will be concatenated to the
FROM alpine ENTRYPOINT ["echo"] CMD ["Hello, World!"]
Wil lead to us seeing:
CMD instructions are, by design, made do be easily overridden by passing arguments to
docker container run commands,
ENTRYPOINT have purposely been made harder to override manually, by forcing user to use a
This design leads us to a consistent pattern. And we like patterns, patterns are good for our mental health:
We can use
ENTRYPOINT to define a base command which always gets executed, and
CMD to define default arguments for this command, easily overridable by passing custom args to
docker container run.