How to run two commands on Dockerfile?

19,723

Solution 1

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

Solution 2

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.

Share:
19,723

Related videos on Youtube

Paulo
Author by

Paulo

An gamer alcoholic and DEV in free time

Updated on September 15, 2022

Comments

  • Paulo
    Paulo over 1 year

    I have to execute two commands on docker file, but both theses commands atached 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:[email protected]/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?