Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run two commands on Dockerfile?

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:[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?

like image 571
Paulo Avatar asked Jul 23 '19 17:07

Paulo


People also ask

How do I run multiple scripts in Dockerfile?

There's two ways to do this: Have a shell script that runs each service as a background job. Launch a full init system inside the container and launch the services under this.

Can we have 2 from in Dockerfile?

With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don't want in the final image.

How do I run multiple commands in docker exec?

In order to execute multiple commands using the “docker exec” command, execute “docker exec” with the “bash” process and use the “-c” option to read the command as a string. Note: Simple quotes may not work in your host terminal, you will have to use double quotes to execute multiple commands.

Can I run two processes in docker container?

It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.


1 Answers

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.

like image 168
Daniel Avatar answered Oct 01 '22 20:10

Daniel