Mittenchops
Mittenchops

Reputation: 19724

How can I add comments in long docker RUN commands?

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.

Upvotes: 28

Views: 4983

Answers (2)

wisbucky
wisbucky

Reputation: 37953

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)

Upvotes: 7

KamilCuk
KamilCuk

Reputation: 141633

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.

Upvotes: 38

Related Questions