2022-06-07

CMD vs. ENTRYPOINT in Dockerfile

Introduction

This article aims to provide an understanding of two essential Dockerfile instructions – CMD and ENTRYPOINT.

CMD

CMD, short for Command, is a Dockerfile instruction that allows you to set a default command to be executed when a container is run from the resulting image. This command can be overridden by providing a different command at runtime. CMD is useful for providing default behavior or additional parameters for your container.

Syntax

Dockerfile
CMD ["executable", "param1", "param2"]

or

Dockerfile
CMD command param1 param2

Example

Dockerfile
FROM ubuntu
CMD ["echo", "Hello, World!"]

In this example, the default command for the container is to echo "Hello, World!". When the container is run, this command will be executed unless a different command is specified at runtime.

ENTRYPOINT

ENTRYPOINT is another Dockerfile instruction that specifies the default executable for a container. Unlike CMD, ENTRYPOINT does not allow the command to be easily overridden at runtime. Instead, any additional arguments provided at runtime are appended to the ENTRYPOINT command. This makes ENTRYPOINT particularly useful when you want to enforce a specific executable or application to run in your container.

Syntax

Dockerfile
ENTRYPOINT ["executable", "param1", "param2"]

or

Dockerfile
ENTRYPOINT command param1 param2

Example

Dockerfile
FROM ubuntu
ENTRYPOINT ["tail", "-f", "/dev/null"]

In this example, the ENTRYPOINT command ensures that the container will always execute the tail command with the -f and /dev/null parameters. Any additional parameters passed at runtime will be appended to this command.

Key Differences

  • Overriding behavior
    CMD can be easily overridden by providing a new command at runtime, while ENTRYPOINT is designed to enforce a specific executable, with additional runtime arguments appended to the command.

  • Flexibility
    CMD is more flexible for users, allowing them to define or change the default behavior of a container at runtime, while ENTRYPOINT is more rigid, enforcing a specific application or executable.

  • Intended use
    CMD is best suited for providing default behavior or additional parameters, while ENTRYPOINT is ideal for ensuring that a specific application is always executed within the container.

Using CMD and ENTRYPOINT Together

CMD and ENTRYPOINT can be used together to create versatile and powerful container configurations. When combined, ENTRYPOINT sets the default executable, while CMD provides default arguments that can be overridden at runtime.

Dockerfile
FROM python:3.8
ENTRYPOINT ["python", "app.py"]
CMD ["--host", "0.0.0.0", "--port", "8000"]

In this example, the ENTRYPOINT specifies that the python executable should be used to run the app.py script. The CMD instruction then provides default arguments for the container, setting the host to 0.0.0.0 and the port to 8000. When the container is run, any additional arguments provided at runtime will replace the default CMD arguments.

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!