As per Docker documentation: There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.
I wish to execute a simple bash script(which processes docker environment variable) before the CMD command(which is init in my case).
Is there any way to do this?
Use a custom entrypoint
Make a custom entrypoint which does what you want, and then exec's your CMD at the end.
entrypoint.sh:
Dockerfile:
Docker will run your entrypoint, using CMD as arguments. If your CMD is
init
, then:The
exec
at the end of the entrypoint script takes care of handing off to CMD when the entrypoint is done with what it needed to do.Why this works
The use of ENTRYPOINT and CMD frequently confuses people new to Docker. In comments, you expressed confusion about it. Here is how it works and why.
The ENTRYPOINT is the initial thing run inside the container. It takes the CMD as an argument list. Therefore, in this example, what is run in the container is this argument list:
It is not required that an image have an ENTRYPOINT. If you don't define one, Docker has a default:
/bin/sh -c
.So with your original situation, no ENTRYPOINT, and using a CMD of
init
, Docker would have run this:In the beginning, Docker offered only CMD, and
/bin/sh -c
was hard-coded as the ENTRYPOINT (you could not change it). At some point along the way, people had use cases where they had to do more custom things, and Docker exposed ENTRYPOINT so you could change it to anything you want.In the example I show above, the ENTRYPOINT is replaced with a custom script. (Though it is still ultimately being run by
sh
, because it starts with#!/bin/sh
.)That ENTRYPOINT takes the CMD as is argument. At the end of the entrypoint.sh script is
exec "$@"
. Since$@
expands to the list of arguments given to the script, this is turned intoAnd therefore, when the script is finished, it goes away and is replaced by
init
as PID 1. (That's whatexec
does - it replaces the current process with a different command.)How to include CMD
In the comments, you asked about adding CMD in the Dockerfile. Yes, you can do that.
Dockerfile:
Or if there is more to your command, e.g. arguments like
init -a -b
, would look like this:Thanks to Dan for his answer.
Although I found I had to do something like this within the Dockerfile:
NOTE: I named the script startup.sh as opposed to entrypoint.sh
The key here was that I needed to provide 'sh' otherwise I kept getting "no such file..." errors coming out of 'docker logs -f container_name'.
See: https://github.com/docker/compose/issues/3876
Dan's answer was correct, but I found it rather confusing to implement. For those in the same situation, here are code examples of how I implemented his explanation of the use of ENTRYPOINT instead of CMD.
Here are the last few lines in my Dockerfile:
Here are the contents of the mergeandlaunch bash shell script:
Here is how the code gets executed:
mergeandlaunch
shell scriptexec
command.