¿COPIAR/AGREGAR condicional en Dockerfile?


Dentro de mis Dockerfiles me gustaría COPIAR un archivo en mi imagen si existe, los requisitos.txt file para pip parece ser un buen candidato, pero ¿cómo se lograría esto?

COPY (requirements.txt if test -e requirements.txt; fi) /destination
...
RUN  if test -e requirements.txt; then pip install -r requirements.txt; fi

O

if test -e requirements.txt; then
    COPY requiements.txt /destination;
fi
RUN  if test -e requirements.txt; then pip install -r requirements.txt; fi
Author: Braiam, 2015-07-21

4 answers

Esto no está soportado actualmente (ya que sospecho que conduciría a una imagen no reproducible, ya que el mismo Dockerfile copiaría o no el archivo, dependiendo de su existencia).

Esto todavía se solicita, en número 13045, usando comodines: "COPY foo/* bar/" not work if no file in foo" (mayo de 2015).
No se implementará por ahora (julio de 2015) en Docker, sino otra herramienta de compilación como bocker podría apoyar esto.

 10
Author: VonC,
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
2015-07-21 07:20:06

Aquí hay una solución simple:

COPY foo file-which-may-exist* /target

Asegúrese de que foo existe, ya que COPY necesita al menos una fuente válida.

Si file-which-may-exist está presente, también se copiará.

NOTA: Debe tener cuidado de asegurarse de que su comodín no recoge otros archivos que no tiene la intención de copiar. Para tener más cuidado, puedes usar file-which-may-exist? en su lugar (? coincide con un solo carácter).

O incluso mejor, use una clase de caracteres como esta para asegurarse de que solo un archivo puede ser emparejado:

COPY foo file-which-may-exis[t] /target
 22
Author: jdhildeb,
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-10-18 02:44:03

Solución para solucionar el problema

Tenía el requisito de copiar CARPETA al servidor basado en variables ENV. Tomé la imagen vacía del servidor. se creó la estructura de carpetas de implementación requerida en la carpeta local. a continuación, se añade la siguiente línea a DockerFile copie la carpeta en el contenedor. I n última línea añadida punto de entrada para ejecutar init file.sh antes de que docker inicie el servidor.

#below lines added to integrate testing framework
RUN mkdir /mnt/conf_folder
ADD install /mnt/conf_folder/install
ADD install_test /mnt/conf_folder/install_test
ADD custom-init.sh /usr/local/bin/custom-init.sh
ENTRYPOINT ["/usr/local/bin/custom-init.sh"]

Luego crea el custom-init.sh archivo en local con script algo como a continuación

#!/bin/bash
if [ "${BUILD_EVN}" = "TEST" ]; then
    cp -avr /mnt/conf_folder/install_test/* /mnt/wso2das-3.1.0/
else
    cp -avr /mnt/conf_folder/install/* /mnt/wso2das-3.1.0/
fi;

In docker-compose file below lines.

Medio ambiente: - BUILD_EVN = TEST

Estos cambios copian la carpeta al contenedor durante la compilación de docker. cuando ejecutamos docker-compose up copia o implementa la carpeta real requerida en el servidor antes de que se inicie el servidor.

 5
Author: Santhosh Hirekerur,
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-12-21 06:16:02

Tal vez usted simplemente no necesita! Veamos su código de nuevo:

COPY (requirements.txt if test -e requirements.txt; fi) /destination
RUN  if test -e requirements.txt; then pip install -r requirements.txt; fi

Tal vez usted puede cambiar a:

COPY requirements.txt /requirements.txt
RUN  if test -e /requirements.txt; then pip install -r /requirements.txt; fi

Ahora tienes esto:

  • Si el archivo existe, será copiado por COPY y procesado por PIP dentro de RUN
  • Si el archivo no existe, la COPIA fallará (en silencio, supongo) y PIP no se ejecutará debido a la condición
 -6
Author: Danilo Aghemo,
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-06-02 07:48:02