11

I have to execute two commands on the docker file, but both these commands are attached to the terminal and block the execution from the next.

dockerfile:

FROM sinet/nginx-node:latest

RUN mkdir /usr/src/app

WORKDIR /usr/src/app

RUN git clone https://name:pass@bitbucket.org/joaocromg/front-web-alferes.git
WORKDIR /usr/src/app/front-web-alferes

RUN npm install 
    
RUN npm install bower -g 
RUN npm install gulp -g 
RUN bower install --allow-root 
    
COPY default.conf /etc/nginx/conf.d/

RUN nginx -g 'daemon off;' & # command 1 blocking
 
CMD ["gulp watch-dev"] # command 2 not executed

Someone know how can I solve this?

riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106
Paulo
  • 577
  • 3
  • 8
  • 23
  • Try to concatinate them with a && – arnonuem Jul 23 '19 at 17:32
  • I tried but not work. :\ – Paulo Jul 23 '19 at 17:32
  • 2
    Possible duplicate of [Running nginx on Alpine](https://stackoverflow.com/questions/56946225/running-nginx-on-alpine) – Efrat Levitan Jul 23 '19 at 18:00
  • The usual answer to this question is “in two separate containers”; there’s a standard `nginx` image that can help you run that part. – David Maze Jul 23 '19 at 20:42
  • @DavidMaze i know the standard is separate in two containers, but in my case doesnt make sense (i dont develop this system, i'm just trying use container). because "gulp watch-dev" is just to manager static file and is not a different service. – Paulo Jul 24 '19 at 11:05

3 Answers3

14

Try creating a script like this:

#!/bin/sh
nginx -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.

Daniel
  • 21,933
  • 14
  • 72
  • 101
2

I suggest you try supervisord in this case. http://supervisord.org/

Edit: Here is an dockerized example of httpd and ssh daemon: https://riptutorial.com/docker/example/14132/dockerfile-plus-supervisord-conf

dschuldt
  • 657
  • 4
  • 8
1

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.

huberu
  • 45
  • 7