Docker run override entrypoint with shell script which accepts arguments

DockerDocker Compose

Docker Problem Overview


I have entrypoint shell script which accepts arguments -a -b.

I have working docker-compose.yml file where I override tomcat's entrypoint with instruction:

entrypoint: /usr/local/tomcat/entrypoint.sh -a param1 -b param2

What is docker run alternative?

docker run --entrypoint "/usr/local/tomcat/entrypoint.sh -a param1 -b param2" tomcat:jre8

does not work

I get:

docker: Error response from daemon: 
invalid header field value "oci runtime error: container_linux.go:247: 
starting container process caused \"exec:
\\\"/usr/local/tomcat/entrypoint.sh -a param1 -b param2\\\": 
stat /usr/local/tomcat/entrypoint.sh -a param1 -b param2: 
no such file or directory\"\n".

FYI:

docker run --entrypoint "/usr/local/tomcat/entrypoint.sh" tomcat:jre8

works from Docker point of view, but obviously script fails

Docker Solutions


Solution 1 - Docker

It is because of the quotes you're using around your command.

When you run docker run --entrypoint "/usr/local/tomcat/entrypoint.sh -a param1 -b param2" tomcat:jre8 Docker treats whatever is within those quotes as a single script file.

As you can see from the error:

stat /usr/local/tomcat/entrypoint.sh -a param1 -b param2: 
no such file or directory\"\n".

It's trying to perform a stat on the file before running it, so it knows if it exists.

Place the arguments to your entrypoint at the end of your docker command like so:

docker run --entrypoint <entrypoint.sh> <image:tag> <arg1> <arg2> <arg3>

Your command becomes:

docker run --entrypoint /usr/local/tomcat/entrypoint.sh tomcat:jre8 -a param1 -b param2 

Have a look at the code snippets in the official documentation:

> The ENTRYPOINT of an image is similar to a COMMAND because it > specifies what executable to run when the container starts

https://docs.docker.com/engine/reference/run/#/entrypoint-default-command-to-execute-at-runtime

Solution 2 - Docker

I think this is also worth noting:

If you have a bunch of arguments to your docker run command, your --entrypoint should come first.

I don't know which of my arguments was the problem, but putting --entrypoint "/bin/bash" at the end did not prevent execution of the ENTRYPOINT from the Dockerfile. My arguments included:

  • 1x --rm
  • 1x --name
  • 1x -it
  • 3x -v
  • 6x -p
  • 4x -e

Solution 3 - Docker

N.B: Answering this old question because the proposed answer are not satisfying me as they are partly wrong.

Short answer: There is no way to override the entrypoint with multiples arguments but you can move them to the command (part after the image name) that will work


More detailed answer:

After some testing and reading the docs it's obvious that there is no way to mimic the dockerfile or docker-compose entrypoint behavior with docker run.

The proposed answers are overriding the entrypoint to a single binary (i.e. without args) and putting the originals arguments to the COMMAND.

That is often doing the job but there is really a conceptual difference that you can reveal using docker inspect.

In docker, ENTRYPOINT is the binary + the default args that will be launched at startup and that are not supposed to be changed by the user at normal use. COMMAND are the remaining arguments or parameters that you want user to change (ex: the targeted hostname of ping).So they are conceptually different.

Saying that in many case this is not a matter to move args from entrypoint to command for a one time container the resulting command will look the same.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPatrik MihalčinView Question on Stackoverflow
Solution 1 - DockerAdrian OpreaView Answer on Stackoverflow
Solution 2 - DockerBruno BronoskyView Answer on Stackoverflow
Solution 3 - DockerwebofmarsView Answer on Stackoverflow