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
            It installs software packages, sets up the environment, and performs any necessary                                    configuration. The commands executed with RUN are not preserved after the image is built; they             contribute to the layers of the final image.
  • Example:
FROM ubuntu:latest
RUN 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 
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
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:

  • RUN is used during image build time to create image layers.
  • CMD and ENTRYPOINT are 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

Popular posts from this blog

Hibernate Many to Many Relationship

Why Integral Calculus limit tending to infinity a sacrilege

Introduction