Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add comments in long docker RUN commands?

Tags:

bash

docker

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.

like image 398
Mittenchops Avatar asked Nov 30 '18 20:11

Mittenchops


People also ask

How do I put comments in a Dockerfile?

Like programming languages, you can write comments in your Dockerfiles. Comments in Dockerfiles are denoted by using the hash or pound symbol # at the beginning of the line. You should note that it only supports one-line comments, hence to write multi-line comments, you'll use the hash symbol on each line.

How do I comment multiple lines in Dockerfile?

Docker treats lines that begin with # as a comment, unless the line is a valid parser directive. A # marker anywhere else in a line is treated as an argument. Line continuation characters are not supported in comments.

How do I run multiple commands in docker?

Multiple commands can be executed in a running Docker container using the docker exec command. If the Docker container is stopped, before running the docker exec command it should be started using the docker run command.

Can we write multiple CMD in Dockerfile?

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect. If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format.


2 Answers

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.

like image 140
KamilCuk Avatar answered Oct 25 '22 21:10

KamilCuk


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)
like image 26
wisbucky Avatar answered Oct 25 '22 22:10

wisbucky