djangofan
djangofan

Reputation: 29669

Not able to start 2 tasks using Dockerfile CMD

I have a question about Dockerfile with CMD command. I am trying to setup a server that needs to run 2 commands in the docker container at startup. I am able to run either 1 or the other service just fine on their own but if I try to script it to run 2 services at the same time, it fails. I have tried all sorts of variations of nohup, &, linux task backgrounding but I haven't been able to solve it.

Here is my project where I am trying to achieve this: https://djangofan.github.io/mountebank-with-ui-node/

#entryPoint.sh
#!/bin/bash
nohup /bin/bash -c "http-server -p 80 /ui" &
nohup /bin/bash -c "mb --port 2525 --configfile /mb/imposters.ejs --allowInjection" &
jobs -l

Displays this output but the ports are not listening:

djangofan@MACPRO ~/workspace/mountebank-container (master)*$ ./run-container.sh 
f5c50afd848e46df93989fcc471b4c0c163d2f5ad845a889013d59d951170878
f5c50afd848e46df93989fcc471b4c0c163d2f5ad845a889013d59d951170878   djangofan/mountebank-example   "/bin/bash -c /scripts/entryPoint.sh"   Less than a second ago   Up Less than a second   0.0.0.0:2525->2525/tcp, 0.0.0.0:4546->4546/tcp, 0.0.0.0:5555->5555/tcp, 2424/tcp, 0.0.0.0:9000->9000/tcp, 0.0.0.0:2424->80/tcp   nervous_lalande
[1]-     5 Running                 nohup /bin/bash -c "http-server -p 80 /ui" &
[2]+     6 Running                 nohup /bin/bash -c "mb --port 2525 --configfile /mb/imposters.ejs --allowInjection" &

And here is my Dockerfile:

FROM node:8-alpine

ENV MOUNTEBANK_VERSION=1.14.0
RUN apk add --no-cache bash gawk sed grep bc coreutils
RUN npm install -g http-server
RUN npm install -g mountebank@${MOUNTEBANK_VERSION} --production
EXPOSE 2525 2424 4546 5555 9000
ADD imposters /mb/
ADD ui /ui/
ADD *.sh /scripts/

# these work when ran 1 or the other
#CMD ["http-server", "-p", "80", "/ui"]
#CMD ["mb", "--port", "2525", "--configfile", "/mb/imposters.ejs", "--allowInjection"]

# this doesnt yet work
CMD ["/bin/bash", "-c", "/scripts/entryPoint.sh"]

Upvotes: 0

Views: 1726

Answers (1)

nickgryg
nickgryg

Reputation: 28633

One process inside docker container has to run not in background mode, because docker container is running while main process inside it is running.

The /scripts/entryPoint.sh should be:

#!/bin/bash
nohup /bin/bash -c "http-server -p 80 /ui" &
nohup /bin/bash -c "mb --port 2525 --configfile /mb/imposters.ejs --allowInjection"

Everything else is fine in your Dockerfile.

Upvotes: 2

Related Questions