0

I have a Docker container which runs a web service. After the container process is started, I need to run a single command. How can I do this automatically, either by using Docker Compose or Docker?

I'm looking for a solution that does not require me to substitute the original container process with a Bash script that runs sleep infinity etc. Is this even possible?

Magnus
  • 589
  • 8
  • 26
  • 1
    Have you tried to use `command` directive in the `docker-compose.yml`? – funnydman Jan 06 '20 at 18:47
  • 1
    It’s not possible unless designed this way in the entrypoint. – Mike Doe Jan 06 '20 at 19:44
  • 1
    Do you want your webservice to have process id 1? If not, you can make a script that first runs the single command and then starts your webserver. – Maaike Jan 06 '20 at 21:37
  • @Maaike, I wish this was an option, but as stated in the question, the command must be run *after* the web service has started. – Magnus Jan 07 '20 at 22:57
  • 1
    @Magnus Then still you can use a script to start the webserver and then run your command, but that is only possible if the webserver does not need to have process id 1. You can just launch a script instead of your webserver. The script launches the webserver, waits until the webserver is started and ready, and then runs the command. But your script will then have process id 1, which is not always a problem, but good to be aware of. – Maaike Jan 08 '20 at 14:53
  • @Maaike, I know. Regrettably, it's not an option for my particular use case. My Docker image is fed into a third party black box platform, and the web server process needs id 1. Or at the very least, it must be guaranteed that the Docker container dies when the web server dies. I know I could make some hacky script that tries to detect this state, but rather than go that path, I'm going to attack the problem from some other angle entirely. – Magnus Jan 09 '20 at 17:06

0 Answers0