When I took over a project, I found a command "RUN true" in the Dockerfile.
FROM xxx
RUN xxx
RUN true
RUN xxx
I don’t know what this command does, can anyone help explain. In my opinion, this command makes no sense, but i’m not sure if there is any other use.
There is doc about Creating Images, you can see it:
RUN true
&& dnf install -y --setopt=tsflags=nodocs
httpd vim
&& systemctl enable httpd
&& dnf clean all
&& true
@David Maze
test for it. docker file:
FROM centos:7.9.2009
RUN yum install tmux -y
RUN yum install not_exists -y
build log:
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM centos:7.9.2009
---> eeb6ee3f44bd
Step 2/3 : RUN yum install tmux -y
---> Running in 6c6e29ea9f2c
...omit...
Complete!
Removing intermediate container 6c6e29ea9f2c
---> 7c796c2b5260
Step 3/3 : RUN yum install not_exists -y
---> Running in e4b7096cc42b
...omit...
No package not_exists available.
Error: Nothing to do
The command '/bin/sh -c yum install not_exists -y' returned a non-zero code: 1
modify dockefile:
FROM centos:7.9.2009
RUN yum install tmux -y
RUN yum install tree -y
build log:
Sending build context to Docker daemon 2.048kB
Step 1/3 : FROM centos:7.9.2009
---> eeb6ee3f44bd
Step 2/3 : RUN yum install tmux -y
---> Using cache
---> 7c796c2b5260
Step 3/3 : RUN yum install tree -y
---> Running in 180b32cb44f3
...omit...
Installed:
tree.x86_64 0:1.6.0-10.el7
Complete!
Removing intermediate container 180b32cb44f3
---> 4e905ed25cc2
Successfully built 4e905ed25cc2
Successfully tagged test:v0
you can see Using cache 7c796c2b5260
. without a command "RUN true", but the first "RUN" cache is reusged.
2
Answers
I found an already existing answer which explains it quite well.
And if I quote the answer here:
And I found an quick explanation from the best practices github page under section ‘#chaining-commands’ saying:
RUN true
as a standalone command does absolutely nothing and it’s safe to delete it./bin/true
is a standard shell command. It reads no input, produces no output, and neither reads nor writes files; it just exits with a status code of 0 ("success"). Running it as a Docker step will have no effect on the final image other than inserting an additional layer into thedocker history
.The one clever use I can think of for this is to cause a later part of a Dockerfile to re-run. Imagine a Dockerfile like
If the second input changes, you could want to rebuild this image.
docker build --no-cache
will re-run the first step too, though, and this could take longer than you want. Inserting aRUN true
line between the two lines would break Docker’s layer caching, but only after the first command has run.