You should refer to this SO answer. In short, you should always apt-get update
before you apt-get install
. Keeping these statements on different lines means that Docker will cache them separately. If you re-run the Docker build, Docker may use the cached apt-get update
and thus install old packages.
This is not the same with pip - it will always try to install the latest package version available (unless you specified a version explicitly). In the case you have given, you can shorten the statement to RUN pip install -U pip opencv-python==4.4.0.46
.
As a matter of convenience, you might want to group your installs in different RUN
statemets, so that the Dockerfile is easier to read, and you don't have to reinstall everything in case you want to add/remove a package. So, for example, if you have pip
and opencv
on a single line and you add pytest
on that same line, when you run docker build
again, it will install pip
and opencv
as well. If, on the other hand, you split the lines like so:
RUN pip install -U pip opencv-python
RUN pip install -U pytest
and build, Docker will (by default) use the cached installs of pip
and opencv
and install only pytest
. If you have many packages this is a serious time-saver.
The same thing applies to apt-get
, by the way - the only catch is that, as explained, you would probably want to group apt-get install
and apt-get update
on one line for each group of package installs.
In case you don't use caching (i.e. you run something like docker build . --no-cache
), then it wouldn't matter whether you have everything on one line or on separate lines.