Câu hỏi phỏng vấn Docker
Câu hỏi

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

Câu trả lời

In a Dockerfile, both CMD and ENTRYPOINT are instructions used to define the command that runs when a container starts, but they serve different purposes and have distinct behaviors.

CMD (Command)

  • Purpose: The CMD instruction sets default commands and/or parameters that can be overridden from the Docker command line interface (CLI) when the container is run.
  • Overriding: If you provide a command when running the container (docker run <image> <command>), the CMD instruction will be overridden by this command.
  • Forms: CMD can be written in two forms:
    • Shell form: CMD command param1 param2
    • Exec form: CMD ["executable", "param1", "param2"]
  • Example:
    Dockerfile Copy
    FROM ubuntu
    CMD ["echo", "Hello, World!"]
    Running docker run <image> will execute echo Hello, World!. However, running docker run <image> echo Goodbye will override the CMD and execute echo Goodbye instead[1][3][4][5].

ENTRYPOINT

  • Purpose: The ENTRYPOINT instruction configures a container to run as an executable. It sets a command that will always be executed when the container starts.
  • Overriding: Unlike CMD, the ENTRYPOINT command cannot be overridden by the command line arguments provided when running the container. Instead, any arguments provided will be appended to the ENTRYPOINT command.
  • Forms: ENTRYPOINT can also be written in two forms:
    • Shell form: ENTRYPOINT command param1 param2
    • Exec form: ENTRYPOINT ["executable", "param1", "param2"]
  • Example:
    Dockerfile Copy
    FROM ubuntu
    ENTRYPOINT ["echo"]
    CMD ["Hello, World!"]
    Running docker run <image> will execute echo Hello, World!. Running docker run <image> Goodbye will execute echo Goodbye, appending the argument to the ENTRYPOINT command[2][3][4][5][6].

Using CMD and ENTRYPOINT T...

middle

middle

Gợi ý câu hỏi phỏng vấn

senior

How virtualization works at low level?

expert

Name some limitations of containers vs VM

junior

What is the difference between a Docker image and a container?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào