30

I know it's customary to have run-on RUN commands in docker files to reduce steps/space. However, as these get long, I'd also like to add more comments to make the command clear.

FROM ubuntu:18.04
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \   # I WANT A COMMENT on what this step is doing
 && apt-get install -y software-properties-common # comments also don't work here, before the slash \

What's the docker/bash syntax or docker convention that would allow comments next to individual steps? If I put the comment where indicated above, I get the error

$ sudo docker build .
Sending build context to Docker daemon  4.608kB
Error response from daemon: Dockerfile parse error line 5: unknown instruction: &&

Which makes sense from a bash perspective but leaves me with few options for communicating the intent of the line.

2 Answers 2

39

You need to have a line with only the comment:

# comment 1
RUN apt-get update \
    # comment 2
    && apt-get install blabal blabla blabla \
    # comment 3
    && echo this is not a drill

docker removes the comment line with the newline.

See docker-nginx with examples.

Sign up to request clarification or add additional context in comments.

1 Comment

7

If you want comments on the same line as the commands, you can use this syntax:

RUN apt-get update -y          `# comment1` \ 
&& apt-get install -y          `# comment2` \
    software-properties-common `# comment3` \
    curl                       `# comment4`

or

RUN apt-get update -y          $(: comment1) \ 
&& apt-get install -y          $(: comment2) \
    software-properties-common $(: comment3) \
    curl                       $(: comment4)

1 Comment

I came up with this, too (only worse :-)), but the other answer is much cleaner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.