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

Golo Roden

In Dockerfiles there are two commands that look similar to me: CMD and ENTRYPOINT. But I guess that there is a (subtle?) difference between them - otherwise it would not make any sense to have two commands for the very same thing.

The documentation states for CMD

The main purpose of a CMD is to provide defaults for an executing container.

and for ENTRYPOINT:

An ENTRYPOINT helps you to configure a container that you can run as an executable.

So, what's the difference between those two commands?

creack

Docker has a default entrypoint which is /bin/sh -c but does not have a default command.

When you run docker like this: docker run -i -t ubuntu bash the entrypoint is the default /bin/sh -c, the image is ubuntu and the command is bash.

The command is run via the entrypoint. i.e., the actual thing that gets executed is /bin/sh -c bash. This allowed Docker to implement RUN quickly by relying on the shell's parser.

Later on, people asked to be able to customize this, so ENTRYPOINT and --entrypoint were introduced.

Everything after ubuntu in the example above is the command and is passed to the entrypoint. When using the CMD instruction, it is exactly as if you were doing docker run -i -t ubuntu <cmd>. <cmd> will be the parameter of the entrypoint.

You will also get the same result if you instead type this command docker run -i -t ubuntu. You will still start a bash shell in the container because of the ubuntu Dockerfile specified a default CMD: CMD ["bash"]

As everything is passed to the entrypoint, you can have a very nice behavior from your images. @Jiri example is good, it shows how to use an image as a "binary". When using ["/bin/cat"] as entrypoint and then doing docker run img /etc/passwd, you get it, /etc/passwd is the command and is passed to the entrypoint so the end result execution is simply /bin/cat /etc/passwd.

Another example would be to have any cli as entrypoint. For instance, if you have a redis image, instead of running docker run redisimg redis -H something -u toto get key, you can simply have ENTRYPOINT ["redis", "-H", "something", "-u", "toto"] and then run like this for the same result: docker run redisimg get key.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Difference between RUN and CMD in a Dockerfile

Dockerfile without CMD or Entrypoint

confused with CMD and ENTRYPOINT in dockerfile

Dockerfile: ENTRYPOINT and CMD

Understanding Dockerfile CMD/ENTRYPOINT

What is the difference between the "command" parameter in the docker-compose.yml and the CMD parameter in the Dockerfile?

What is the difference between % and %% in a cmd file?

Dockerfile entrypoint/cmd not executing on container import

Use here document with ENTRYPOINT/CMD in Dockerfile

CMD doesn't run after ENTRYPOINT in Dockerfile

Dockerfile - exec form of ENTRYPOINT and shell form of CMD

Combining ENTRYPOINT and CMD in Dockerfile targeting different executables

Dockerfile CMD unable to locate entrypoint file

What is the difference between the 'COPY' and 'ADD' commands in a Dockerfile?

What is the difference between CMD and Command prompt in windows?

What's the difference between the following cmd scripts?

what's the difference between command prompt and cmd?

What does "set --" do in this Dockerfile entrypoint?

Difference between CMD echo 'Hello world' and CMD ["echo", ''Hello world'] in a dockerfile?

dockerfile: how use CMD or ENTRYPOINT from base image

docker basics: what is the difference between using a Dockerfile and commit?

What's the difference between RUN and bash script in a dockerfile?

What's the difference between ADD and COPY instructions in the Dockerfile

What is the difference between package.json main and app.config.js expo.entryPoint?

difference between EntryPoint and handler in Spring Security

What's the difference between "bundle" and "includeInBundle" options for Sencha Cmd

What is the difference between PowerShell and cmd.exe command syntax?

What's the difference between CMD and PowerShell when deleting a file in Windows?

What is the difference between opening application by cmd/ps or by click?