Mastering Docker: Practical Demonstration of CMD and ENTRYPOINT

Welcome back to another hands-on Docker tutorial. In this demo, we’ll dive into the practical use of CMD and ENTRYPOINT instructions in a Docker environment. These instructions are crucial for defining the behavior of your containers, so let’s jump right in.

For this demonstration, I’m using a Dockerfile that’s available in a directory called cmd_entrypoint. This Dockerfile is configured to use Ubuntu 20.10 as the base image and includes various commands to set up the environment.

Here’s a high-level overview of the Dockerfile:

  • It starts with a base image of Ubuntu 20.10.
  • It updates the system and installs several tools.
  • It sets the working directory to /etc.
  • The CMD instruction specifies that the default command to run when you start a container based on this image is “bash”.

In this setup, when we run the container, it launches a Bash shell, but there’s a caveat. Bash requires a terminal to attach to. If we run this container as-is, it will immediately start and exit because it can’t access a terminal.

Let’s begin by building this image. Open your terminal and execute the following command:

docker image build -t ubuntu_my_tools:v1 .

With the image built, you can now run a container using this image:

docker container run -it ubuntu_my_tools:v1

This container will start, but you’ll notice that it immediately exits because of the issue related to running Bash without a terminal.

If you want to run a specific command when the container starts, like df -h, you can do so with the following command:

docker container run -it ubuntu_my_tools:v1 df -h

This time, it executes the df command and then exits.

So, what if you want to create a Docker image that only runs the ping command, and you don’t want to specify a command each time you run it? Let’s modify our Dockerfile to achieve this.

In a new directory called entrypoint, we have a similar Dockerfile, but this time it uses the ENTRYPOINT instruction instead of CMD.

The modified Dockerfile looks like this:

FROM ubuntu:20.10
RUN apt-get update && apt-get install -y iputils-ping
ENTRYPOINT ["ping"]

This Dockerfile specifies that the ENTRYPOINT is “ping”. With this configuration, if you run a container using this image and don’t provide any additional command, it will execute the “ping” command with no arguments, which won’t work since ping requires an IP address or hostname as an argument.

Let’s build this image:

docker image build -t ubuntu_tools_ping:v1 .

Now, if you run a container from this image, you’ll encounter an issue because the “ping” command is missing the required argument. You can try it with the following command:

docker container run -it ubuntu_tools_ping:v1

To resolve this, you can provide an IP address or hostname as an argument:

docker container run -it ubuntu_tools_ping:v1 google.com

In this case, the provided argument replaces the default one in the ENTRYPOINT.

But what if you want to provide a default behavior when no argument is specified? You can achieve this by combining both ENTRYPOINT and CMD instructions.

In the directory named entrypoint_with_cmd, there’s a Dockerfile with the following content:

FROM ubuntu:20.10
RUN apt-get update && apt-get install -y iputils-ping
ENTRYPOINT ["ping"]
CMD ["127.0.0.1"]

This Dockerfile uses both ENTRYPOINT and CMD. It sets “ping” as the ENTRYPOINT and “127.0.0.1” as the CMD argument.

Now, build the image:

docker image build -t ubuntu_tools_ping:v2 .

With this setup, if you run the container without any additional arguments, it will execute “ping 127.0.0.1” by default. For instance:

docker container run -it ubuntu_tools_ping:v2

However, if you provide an argument, it will replace the CMD argument and run the “ping” command with your specified argument. Try it:

docker container run -it ubuntu_tools_ping:v2 google.com

To further enhance your Docker skills, you can replace the ENTRYPOINT of a running container with a different command using the --entrypoint option in the docker container run command. This enables you to override the default behavior set in the image.

Here’s an example of changing the entry point of a container:

docker container run --entrypoint=df ubuntu_tools_ping:v2

By executing this command, you replace the “ping” entry point with “df”, causing the container to run the “df” command and then exit.

In this practical demonstration, we’ve explored how to use CMD and ENTRYPOINT instructions in Docker to define the default behavior of containers, replace entry points, and create flexible container images tailored to your specific use cases. Understanding these fundamental Docker concepts is essential for mastering containerization. Stay tuned for more Docker tutorials to expand your knowledge further.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top