skip to Main Content

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


  1. I found an already existing answer which explains it quite well.

    And if I quote the answer here:

    Running and thus creating a new container even if it terminates still keeps the resulting container image and metadata lying around which can still be linked to.

    So when you run docker run … /bin/true you are essentially creating a new container for storage purposes and running the simplest thing you can.

    In Docker 1.5 was introduced the docker create command so I believe you can now "create" containers without confusingly running something like /bin/true

    And I found an quick explanation from the best practices github page under section ‘#chaining-commands’ saying:

    The first and last commands of the block are special.

    If you would like to prepend or append a one-line command to the block, you will have to edit two lines – one that you are adding and the first or last commands. The first command is on the same line as the RUN directive, whereas the last command lacks the trailing backslash.

    Editing a line with a command that that you don’t want to change presents a risk of introducing a bug, and you also obscure the line’s history. This can be mitigated by having both the first and last commands true – they don’t do anything.

    Login or Signup to reply.
  2. 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 the docker 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

    RUN some_expensive_command http://server-a.example.com/input1
    RUN another_expensive_command http://server-b.example.com/input2
    

    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 a RUN true line between the two lines would break Docker’s layer caching, but only after the first command has run.

    # identical RUN line as before, from cache
    RUN some_expensive_command http://server-a.example.com/input1
    # not the same RUN line, so "executes" (but does nothing)
    RUN true
    # not running commands from cache any more
    RUN another_expensive_command http://server-b.example.com/input2
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search