DEV Community

Raunak Jain
Raunak Jain

Posted on

What is the difference between CMD and ENTRYPOINT in a Dockerfile?

A Dockerfile is a set of instructions to build a Docker image. In a Dockerfile you use commands like CMD and ENTRYPOINT. They might seem similar but they have different roles. In simple terms, both commands help run a container, but they work in different ways.

When you build an image, you write these commands to tell Docker what to do when a container starts. It is important to know the differences. With clear ideas, you can decide which one is best for your needs.

We also talk about Docker images and containers. If you want to learn more about Docker images, please visit Understanding Docker Images.


What is CMD?

CMD is a Dockerfile instruction that sets the default command for the container. When the container runs, if you do not give any other command, Docker will use CMD.

How CMD Works

  • Default Behavior: CMD tells Docker what to run when the container starts.
  • Overridable: If you pass a different command at runtime, CMD is replaced.
  • Format Options: There are three formats to write CMD. The most common are the exec form and the shell form.

Example of CMD

Here is an example of using CMD in a Dockerfile:

FROM ubuntu:20.04
CMD ["echo", "Hello from CMD"]
Enter fullscreen mode Exit fullscreen mode

In this example, when you run the container, it will print "Hello from CMD". If you run the container with a different command, that command will take over.

CMD is useful when you want to provide a default behavior but allow users to change it easily.


What is ENTRYPOINT?

ENTRYPOINT is another Dockerfile instruction that sets the command to run when a container starts. Unlike CMD, ENTRYPOINT is not easily replaced. It makes the container behave like an executable.

How ENTRYPOINT Works

  • Fixed Behavior: ENTRYPOINT is used when you want the container to run a specific command always.
  • Not Overridable: Although you can add extra parameters at runtime, the main command from ENTRYPOINT remains.
  • Combining with CMD: CMD can work with ENTRYPOINT to add default parameters.

Example of ENTRYPOINT

Below is an example of using ENTRYPOINT:

FROM ubuntu:20.04
ENTRYPOINT ["echo", "Hello from ENTRYPOINT"]
Enter fullscreen mode Exit fullscreen mode

This command will always run when the container starts. Even if you pass extra arguments, Docker will add them to the command. For instance, if you run the container with an extra word, the output becomes a combination of the two.


Key Differences Between CMD and ENTRYPOINT

Now that we see what CMD and ENTRYPOINT do, let us compare them.

1. Command Replacement

  • CMD: The CMD instruction is easily overridden by supplying another command during the container start.
  • ENTRYPOINT: The ENTRYPOINT command is fixed and remains even if you pass extra parameters.

2. Use Cases

  • CMD: Use CMD when you want to set default arguments. For example, you may want your container to run a shell script by default but allow users to change the script or pass new parameters.
  • ENTRYPOINT: Use ENTRYPOINT when you want your container to act as a dedicated executable. This is useful in scenarios where you do not want the command to be replaced.

3. Combining CMD and ENTRYPOINT

You can combine CMD and ENTRYPOINT in one Dockerfile. In this case, CMD will provide additional default arguments to ENTRYPOINT. Consider this Dockerfile:

FROM ubuntu:20.04
ENTRYPOINT ["echo"]
CMD ["Hello from CMD with ENTRYPOINT"]
Enter fullscreen mode Exit fullscreen mode

When you run the container, Docker executes:

echo Hello from CMD with ENTRYPOINT
Enter fullscreen mode Exit fullscreen mode

If you pass an argument during runtime, it will override CMD, but the ENTRYPOINT command remains.

For more on combining these instructions, you might check out an article like How do Docker images get created?.


Detailed Comparison

Let us break down the details to see how each command is used in real-world projects.

Default Command vs. Fixed Command

  • CMD as Default Command:

    CMD is like a suggestion for what to do when the container runs. For example, if you write a Dockerfile with CMD and later run your container without extra commands, CMD is executed. However, if a user supplies another command, Docker will ignore CMD.

  • ENTRYPOINT as Fixed Command:

    ENTRYPOINT is like a fixed instruction. It tells Docker that this command must always run. Even if you supply extra arguments, they are simply added to the fixed command.

How They Behave on the Command Line

Consider you have this Dockerfile:

FROM ubuntu:20.04
CMD ["echo", "Hello from CMD"]
Enter fullscreen mode Exit fullscreen mode

If you run:

docker run myimage
Enter fullscreen mode Exit fullscreen mode

It prints "Hello from CMD". Now if you run:

docker run myimage "Extra text"
Enter fullscreen mode Exit fullscreen mode

It will print "Extra text" because the command line argument overrides CMD.

Now compare it with an ENTRYPOINT Dockerfile:

FROM ubuntu:20.04
ENTRYPOINT ["echo", "Hello from ENTRYPOINT"]
Enter fullscreen mode Exit fullscreen mode

If you run:

docker run myimage
Enter fullscreen mode Exit fullscreen mode

It prints "Hello from ENTRYPOINT". And if you run:

docker run myimage "Extra text"
Enter fullscreen mode Exit fullscreen mode

It prints "Hello from ENTRYPOINT Extra text". Notice how ENTRYPOINT does not get replaced. It just appends the extra parameter.

Flexibility vs. Strictness

  • CMD gives you flexibility.

    You can change the command at runtime easily. This is good for development when you may need to run different commands without changing the Dockerfile.

  • ENTRYPOINT gives you strict control.

    Use ENTRYPOINT if you want the container to always run a specific application or command. This is common in production environments where consistency is important.

For more details on Docker container behavior, you can read about How a Docker container operates.


Real-World Examples

Let us see two real-world examples that show how CMD and ENTRYPOINT can be used.

Example 1: A Simple Web Server

Suppose you have a simple web server script that you want to run in a container. You can set up your Dockerfile like this:

FROM python:3.8-slim
COPY app.py /app/app.py
WORKDIR /app
CMD ["python", "app.py"]
Enter fullscreen mode Exit fullscreen mode

Here, CMD runs the web server by default. But if you need to run a different script, you can override CMD when starting the container.

Example 2: A Dedicated Service

Now imagine you want your container to always run a specific script that should not be replaced. In this case, use ENTRYPOINT:

FROM python:3.8-slim
COPY worker.py /app/worker.py
WORKDIR /app
ENTRYPOINT ["python", "worker.py"]
Enter fullscreen mode Exit fullscreen mode

Even if someone runs the container with additional arguments, the worker script will always run. This makes sure the container behaves as expected.

For more insight into running applications inside containers, you might want to see How to work with Docker containers using the Docker CLI.


When to Use CMD and When to Use ENTRYPOINT

Choosing between CMD and ENTRYPOINT depends on what you want your container to do.

Use CMD When:

  • You need a default command that can be easily overridden.
  • You want to provide default parameters.
  • The container is used for different purposes and may need flexibility.

Use ENTRYPOINT When:

  • You want your container to behave like an executable.
  • You do not want the main command to be changed by users.
  • You need to ensure a specific command always runs, even when extra parameters are provided.

Combining Both

Sometimes the best option is to combine CMD and ENTRYPOINT. In this case, ENTRYPOINT sets the command and CMD provides default arguments. This way you get a fixed command with some flexibility. For example:

FROM node:14
WORKDIR /app
COPY . /app
ENTRYPOINT ["node", "server.js"]
CMD ["--port", "3000"]
Enter fullscreen mode Exit fullscreen mode

When you run this container without extra parameters, it will run:

node server.js --port 3000
Enter fullscreen mode Exit fullscreen mode

If you run it with extra arguments, they will override CMD but not ENTRYPOINT. This combination is very common in many production environments.

For a look into how images are built with these instructions, check out How to build a Docker image from a Dockerfile.


Troubleshooting and Common Pitfalls

Sometimes new users can get confused between CMD and ENTRYPOINT. Here are a few tips to avoid common mistakes:

  • Remember the Override Behavior: CMD is replaced if you pass a command at runtime. ENTRYPOINT is not replaced but gets extra parameters.
  • Check Your Dockerfile Format: Use the exec form (with square brackets) to avoid shell interpretation issues.
  • Test Your Container: Run your container with and without extra arguments to see how the command behaves.
  • Combine Carefully: When using both CMD and ENTRYPOINT, always test how extra parameters are handled.

If you have trouble with Docker commands or want to explore more about container best practices, consider reading What are the benefits of using Docker in development?.


Best Practices

Here are some best practices to follow when working with CMD and ENTRYPOINT:

  1. Keep It Simple:

    Write clear and short commands. Avoid complex instructions that are hard to override.

  2. Document Your Dockerfile:

    Add comments to explain why you chose CMD or ENTRYPOINT. This helps others understand your intent.

  3. Use the Exec Form:

    Always write CMD and ENTRYPOINT using the JSON array syntax. It avoids issues with shell processing.

  4. Combine Wisely:

    When you combine CMD and ENTRYPOINT, be clear which part provides the main command and which part gives default parameters.

  5. Test Thoroughly:

    Always run your container with different arguments. Make sure it behaves as you expect in every case.

For more on Docker installation and getting started, check out How to install Docker on different operating systems.


Summary

In this article we compared CMD and ENTRYPOINT in a Dockerfile. We learned that:

  • CMD is used to set a default command which can be easily overridden at runtime.
  • ENTRYPOINT is used to set a fixed command that always runs when the container starts.
  • You can combine both to create a flexible yet controlled container behavior.
  • The choice between them depends on your needs. Use CMD when you want flexibility. Use ENTRYPOINT when you need strict control.

By understanding these differences, you can build better Docker images and create containers that work exactly as you expect.


Final Thoughts

Understanding the difference between CMD and ENTRYPOINT is essential for creating reliable Docker images. With the simple examples and clear explanations above, you should be able to choose the right command for your Dockerfile. Remember that CMD gives you an easy way to provide defaults, while ENTRYPOINT is best for fixed behavior.

As you continue to work with Docker, keep testing your images. Adjust your Dockerfile based on your needs and always document your changes. Learning these details will help you build robust containerized applications.

If you are interested in more advanced topics or want to see how Docker integrates with other tools, you can explore further articles. For example, if you want to know how to push and pull images, visit How do you push and pull Docker images from Docker Hub?.

Thank you for reading this guide on CMD vs ENTRYPOINT in a Dockerfile. We hope this article has helped you understand the key differences and given you practical examples for your projects. Happy containerizing and keep learning!

Top comments (0)