Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences Between Dockerfile Instructions in Shell and Exec Form

What is the difference between shell and exec form for

CMD:

CMD python my_script.py arg

vs.

CMD ["python", "my_script.py", "arg"]

ENTRYPOINT:

ENTRYPOINT ./bin/main

vs.

ENTRYPOINT ["./bin/main"]

and RUN:

RUN npm start

vs.

RUN ["npm", "start"]

Dockerfile instructions?

like image 594
Sheena Avatar asked Mar 15 '17 09:03

Sheena


People also ask

What is shell form and executable form in docker?

Firstly, They have both two different forms: shell form—For example, ENTRYPOINT node app. js. exec form—For example, ENTRYPOINT ["node","app. js"]

What are the differences between CMD and ENTRYPOINT Dockerfile instructions?

CMD vs ENTRYPOINT: Fundamental differencesCMD commands are ignored by Daemon when there are parameters stated within the docker run command. ENTRYPOINT instructions are not ignored but instead are appended as command line parameters by treating those as arguments of the command.

What is the difference between run and CMD in Dockerfile?

RUN is an image build step, the state of the container after a RUN command will be committed to the container image. A Dockerfile can have many RUN steps that layer on top of one another to build the image. CMD is the command the container executes by default when you launch the built image.

Which instruction in Dockerfile is used to execute a command with container?

CMD is an instruction that is best to use if you need a default command which users can easily override. If a Dockerfile has multiple CMDs, it only applies the instructions from the last one. On the other hand, ENTRYPOINT is preferred when you want to define a container with a specific executable.


1 Answers

There are two differences between the shell form and the exec form. According to the documentation, the exec form is the preferred form. These are the two differences:

The exec form is parsed as a JSON array, which means that you must use double-quotes (“) around words not single-quotes (‘).

Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, CMD [ "echo", "$HOME" ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: CMD [ "sh", "-c", "echo $HOME" ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.

Some additional subtleties here are:

The exec form makes it possible to avoid shell string munging, and to RUN commands using a base image that does not contain the specified shell executable.

In the shell form you can use a \ (backslash) to continue a single RUN instruction onto the next line.

There is also a third form for CMD:

CMD ["param1","param2"] (as default parameters to ENTRYPOINT)

Additionally, the exec form is required for CMD if you are using it as parameters/arguments to ENTRYPOINT that are intended to be overwritten.

like image 78
Matt Schuchard Avatar answered Oct 23 '22 21:10

Matt Schuchard