Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a dockerfile, can we have multiple RUN commands into one RUN command?

I understand that each RUN command creates a layer. Suppose I have the following RUN commands:

RUN python -m pip install --upgrade pip
RUN python -m pip install --upgrade setuptools
RUN pip install -r requirements.txt 

I wish to run all the command in one run command. Is the below OK to use?

RUN python -m pip install --upgrade pip; python -m pip install --upgrade setuptools; pip install -r requirements.txt 

If I use the following, then it gives me an error "The token '&&' is not a valid statement separator in this version.":

RUN python -m pip install --upgrade pip && python -m pip install --upgrade setuptools && pip install -r requirements.txt
like image 342
variable Avatar asked Nov 05 '25 07:11

variable


1 Answers

Yes you can and its a good practice

Instead of doing this

RUN python -m pip install --upgrade pip
RUN python -m pip install --upgrade setuptools
RUN pip install -r requirements.txt 

Try this

RUN python -m pip install --upgrade pip &&\
    python -m pip install --upgrade setuptools &&\
    pip install -r requirements.txt 

Advantages with that approach

Each instruction in the Dockerfile adds an extra layer to the docker image The number of instructions and layers should be kept to a minimum as it ultimately affects the build performance and time

like image 200
UDIT JOSHI Avatar answered Nov 07 '25 08:11

UDIT JOSHI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!