docker entrypoint script fails to run

Viewed 2780

I have a script startScript.sh that I want to run after the docker container completely starts (including loading all services that the container is supposed to start)

After that I want to run a script: startScript.sh

This is what I do:

sudo docker run -p 8080:8080 <docker image name> " /bin/bash -c ./startScript.sh"

However this gives me an error:

WFLYSRV0073: Invalid option '/bin/bash'

Even tried different shells still same error. Even tried passing just the script file name. Still did not help.

Note: I know that the above file is in the container in the root folder: / In fact I once entered the container by doing: sudo docker exec and manually ran that script file and it worked. But when I try to automatically do it as above, it does not work for me.

Some questions: 1. Please suggest what could be the issue. 2. I want to run that script after the container has started completely and is up and running - including all the services that are part of it. Is this the right way to even do it? Or does this try to run while the container is starting up?

1 Answers

When you pass arguments after the image name, you are not modifying the entrypoint, but the command (CMD). It seems your image has WFLYSRV0073 as entrypoint, which makes the actual executed binary be your entrypoint, with your command as arguments. Which makes WFLYSRV0073 fail when trying to parse /bin/bash as an argument.

To run just your script, you could override the image's entrypoint with an empty string, making it run your command's first element. Notice I also remove the quotes, or else Docker will search for a binary with the name containing spaces, which of course doesn't exist.

sudo docker run --entrypoint "" -p 8080:8080 <docker image name> /bin/bash -c ./startScript.sh

However this is probably not what you want: it won't run what the image should actually be running, only your setup script. The correct thing to do here is to modify the image's Dockerfile to run the setup script as the entrypoint, and at the end of it run the script's current entrypoint (the actual thing you want to run).

Alternatively, if you do not control the image you are running, you can use FROM <the current image> in a new Dockerfile to build another image based on it, setting the entrypoint to your script.

Edit:

An example of how the above can be done can be seen in MariaDB's entrypoint: you first start a temporary server, run your setup, then restart it, running the definitive service (which is the CMD) at the end.

The above solutions are good in case you want to perform initialization for an image, but if you just want to be able to run a script for development reasons instead of doing it consistently on the image's entrypoint, you can copy it to your container and then use docker exec <container name> your-command and-arguments to run it.

Related