Docker diff between CMD,RUN and ENTRYPOINT
In Docker, ENTRYPOINT, RUN, and CMD are instructions used in a Dockerfile to define the behavior of the container. Each serves a different purpose:
RUN
- Purpose:
RUN is used to execute commands during the build process of the Docker image.
- Usage:
RUN are not preserved after the image is built; they contribute to the layers of the final image.- Example:
FROM ubuntu:latestRUN apt-get update && apt-get install -y curl
CMD
Purpose: CMD specifies the default command to run
when the container starts.
Usage:
It is often used to provide default arguments
It is often used to provide default arguments
to the
ENTRYPOINT instruction or to specify the main command
to be executed by the container
if
ENTRYPOINT is not defined. Only the last
CMD instruction in the Dockerfile is used.Example:
FROM ubuntu:latest
CMD ["echo", "Hello, world!"]
ENTRYPOINT
Purpose: ENTRYPOINT defines the command that will always run
when the container starts.
Usage:
Usage:
It is used to set up the primary command to run within the container,
which cannot be overridden at runtime.
However, you can append additional arguments to the
ENTRYPOINT command when running the container.FROM ubuntu:latest
ENTRYPOINT ["echo"]
Differences and Use Cases
Persistence:
RUNis used during image build time to create image layers.CMDandENTRYPOINTare used to configure the runtime behavior of the container.
Overriding Behavior:
CMD can be overridden by providing arguments
to
docker run.ENTRYPOINT is less easily overridden,
as it defines the main command to run,
but it can take arguments from
the
docker run command.Combining ENTRYPOINT and CMD:
When both ENTRYPOINT and CMD are specified, CMD provides default arguments to the ENTRYPOINT.
FROM ubuntu:latest
ENTRYPOINT ["echo"]
CMD ["Hello, world!"]
Running this container will result in the command echo Hello, world!
Comments
Post a Comment