3

Hi i am wondering if it possible to run two scripts in same time automaticly on docker container start . First script have to run client application, and the second run server app as background.

ProgShiled
  • 154
  • 1
  • 1
  • 12
  • I achieved this using process manager (in my case [circus](https://circus.readthedocs.io/en/latest/), but there are other similar solutions). It makes sense for long-running processes (seems its your case). Also it adds benefit that if you do not handle some error in you application and it stops due to this exception, circus will restart it. As a downside (for some cases), it's python application, so it needs python runtime and needed libraries to be installed into you docker container. – kami Jan 17 '19 at 22:22
  • Can you not just use 2 containers? – Ben Jan 17 '19 at 22:30
  • Ok Kami but only one app is python. Ben i have to use one container – ProgShiled Jan 17 '19 at 22:30
  • @ProgShiled, it's not a problem that only one app is python. You can run any script/app with this approach. Actually I've been running Java applications with such a process manager in docker container. – kami Jan 18 '19 at 11:56

3 Answers3

8

You can use CMD in your Dockerfile and use command & to run two command in parallel:

CMD server_command & client_command

(where server_command is the command used to start the server and client_command is the command used to start the client)

Itai Steinherz
  • 777
  • 1
  • 9
  • 19
5

As mentioned having multiple processes is not a suggested practice.

Nevertheless, in some scenarios is required to have multiple processes. In those cases the usual approach is to use a process manager like supervisor.

Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58
4

Docker's official stance on this has always been that it is best to only have a single service running in a container. Having said that, they also maintain very robust documentation outlying possible solutions for getting multiple services into a single container.

https://docs.docker.com/config/containers/multi-service_container/

A quick summary is essentially that when you have multiple services, you need to have some type of "init" process to act as a parent for all the services in the container.

There's two ways to do this:

  1. Have a shell script that runs each service as a background job.
  2. Launch a full init system inside the container and launch the services under this.

Both are problematic. The first because bash is not an init system, and you can end up with all kinds of headaches when it doesn't act like one. The second because an init system is a pretty heavy duty thing to put into a docker container.

Having said all that, the best solution is to split your services into two containers.

Swiss
  • 5,556
  • 1
  • 28
  • 42