How to run two commands on Dockerfile? How to run two commands on Dockerfile? nginx nginx

How to run two commands on Dockerfile?


Try creating a script like this:

#!/bin/shnginx -g 'daemon off;' & gulp watch-dev

And then execute it in your CMD:

CMD /bin/my-script.sh

Also, notice your last line would not have worked:

CMD ["gulp watch-dev"]

It needed to be either:

CMD gulp watch-dev

or:

CMD ["gulp", "watch-dev"]

Also, notice that RUN is for executing a command that will change your image state (like RUN apt install curl), not for executing a program that needs to be running when you run your container. From the docs:

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.


The answer here is that RUN nginx -g 'daemon off;' is intentionally starting nginx in the foreground, which is blocking your second command. This command is intended to start docker containers with this as the foreground process. Running RUN nginx will start nginx, create the master and child nodes and (hopefully) exit with a zero status code. Although as mentioned above, this is not the intended use of run, so a bash script would work best in this case.