Difference Between CMD and ENTRYPOINT

Difference Between CMD and ENTRYPOINT

In Docker, `CMD` and `ENTRYPOINT` are both instructions used in a Dockerfile to specify the command that runs when a container starts. However, they have different purposes and behaviors. Understanding the differences between these two instructions is crucial for effectively managing containerized applications.

CMD and ENTRYPOINT

CMD and ENTRYPOINT

Purpose

   – The `CMD` instruction provides default arguments for the container. It sets the command to be executed when the container starts if no command is provided at runtime. If a command is specified when running the container, it will override the `CMD` instruction.

Syntax:
   - `CMD` can be used in three formats:
     - Shell Form: `CMD command param1 param2`
       ```Dockerfile
       CMD echo "Hello, World!"
       ```
     - Exec Form: `CMD ["executable", "param1", "param2"]`
       ```Dockerfile
       CMD ["java", "-jar", "myapp.jar"]
       ```
     - Default Parameters: Used in conjunction with `ENTRYPOINT`.
       ```Dockerfile
       CMD ["param1", "param2"]
       ```

Overriding:
   - When you start a container, you can override the `CMD` instruction by providing a different command in the `docker run` command.

   Example:
   ```sh
   docker run my-image echo "Different Command"
   ```
   This command will override the `CMD` defined in the Dockerfile.

Use Case:

   – Use `CMD` when you want to set default arguments for your container but still allow users to override them if necessary.

ENTRYPOINT CMD and ENTRYPOINT

1. Purpose:

   – The `ENTRYPOINT` instruction sets the main command that will be executed when the container starts. Unlike `CMD`, the command specified in `ENTRYPOINT` cannot be easily overridden at runtime; it is always executed.

Use Case:
2. Syntax:
   - `ENTRYPOINT` can be used in two formats:
     - Exec Form: `ENTRYPOINT ["executable", "param1", "param2"]`
       ```Dockerfile
       ENTRYPOINT ["java", "-jar", "myapp.jar"]
       ```
     - Shell Form: `ENTRYPOINT command param1 param2`
       ```Dockerfile
       ENTRYPOINT java -jar myapp.jar
       ```

Example
3. Overriding:
   - While you can override the default parameters specified in `ENTRYPOINT` by providing additional arguments, you cannot override the `ENTRYPOINT` command itself.

   Example:
   ```sh
   docker run my-image --help
   ```
   This command passes `--help` as an argument to the `ENTRYPOINT` command, but the `ENTRYPOINT` itself (e.g., `java -jar myapp.jar`) remains unchanged.

4. Use Case:
   - Use `ENTRYPOINT` when you want to ensure that a specific command is always executed when the container starts, and you want to provide default arguments that can be modified.

Combining CMD and ENTRYPOINT
You can use `CMD` and `ENTRYPOINT` together to provide a default command with arguments that can be overridden:

- Dockerfile Example:

```Dockerfile
# Use an official OpenJDK runtime as the base image
FROM openjdk:17-jdk-alpine

# Set the working directory inside the container
WORKDIR /app

# Copy the JAR file to the container
COPY target/myapp.jar /app/myapp.jar

# Set the entry point for the container
ENTRYPOINT ["java", "-jar", "myapp.jar"]

# Set default arguments that can be overridden
CMD ["--help"]
```

Running the Container: By default, the container will run java -jar myapp.jar --help. You can override the default arguments while keeping the ENTRYPOINT command:

docker run my-image –version

This command will run java -jar myapp.jar --version, overriding the default CMD arguments.

Conclusion

  • CMD specifies default arguments or commands that can be overridden at runtime. It is flexible and can be used for default behavior.
  • ENTRYPOINT specifies the command that will always be executed when the container starts. It provides a fixed command, but you can pass arguments to modify its behavior.

Homepage

Readmore