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 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.
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With