¿Cómo puedo hacer un comentario en un Dockerfile?


Estoy escribiendo un Dockerfile y quiero saber si hay alguna manera de hacer comentarios en este archivo? ¿Docker tiene una opción de comentario que toma el resto de una línea y la ignora?

Author: BMitch, 2016-04-19

4 answers

Puedes usar # para comentar una línea.

# Everything on this line is a comment
 207
Author: Ranjeet,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-08 20:37:27

Como otros han mencionado, los comentarios son referenciados con un # y son documentados aquí. Sin embargo, a diferencia de algunos idiomas, el # debe estar al principio de la línea. Si ocurren a través de la línea, se interpretan como un argumento y pueden resultar en un comportamiento inesperado.

# This is a comment

COPY test_dir target_dir # this is not a comment, it is an arg to COPY

RUN echo hello world # this is an argument to RUN but the shell may ignore it

También debe tenerse en cuenta que las directivas del analizador se han añadido recientemente al Dockerfile que tienen la misma sintaxis que un comentario. Tienen que aparecer en la parte superior de la archivo, antes de cualquier otro comentario o comando. En la actualidad, la única directiva es para cambiar el carácter de escape para soportar windows:

# escape=`

FROM microsoft/nanoserver
COPY testfile.txt c:\
RUN dir c:\

La primera línea, si bien parece ser un comentario, es una directiva parser para cambiar el carácter de escape a un backtick para que los comandos COPY y RUN puedan usar la barra invertida en la ruta.

 29
Author: BMitch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-08 20:56:44

Utilice la sintaxis # para los comentarios

De: https://docs.docker.com/engine/reference/builder/#format

# My comment here
RUN echo 'we are running some cool things'
 15
Author: edhurtig,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-19 06:46:21

Los comentarios de Dockerfile comienzan con '#' al igual que python. He aquí un buen ejemplo : https://github.com/kstaken/dockerfile-examples/blob/master/mongodb/Dockerfile

# Install a more up to date mongodb than what is included in the default ubuntu repositories.

FROM ubuntu
MAINTAINER Kimbro Staken

RUN apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
RUN echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | tee -a /etc/apt/sources.list.d/10gen.list
RUN apt-get update
RUN apt-get -y install apt-utils
RUN apt-get -y install mongodb-10gen

#RUN echo "" >> /etc/mongodb.conf

CMD ["/usr/bin/mongod", "--config", "/etc/mongodb.conf"] 
 1
Author: DhruvPathak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-19 06:45:37