Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

285
Views
Bloque condicional en Docker

He estado usando docker desde hace algún tiempo. Me encontré con una situación en la que necesito ejecutar instrucciones presentes en el Dockerfile en función de alguna condición. Por ejemplo, aquí está el fragmento de Dockerfile

 FROM centos:centos7 MAINTAINER Akshay <akshay@dm.com> # Update and install required binaries RUN yum update -y \ && yum install -y which wget openssh-server sudo java-1.8.0-openjdk \ && yum clean all #install Maven RUN curl -Lf http://archive.apache.org/dist/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz -o /tmp/apache-maven-3.3.9.tar.gz RUN tar -xzf /tmp/apache-maven-3.3.9.tar.gz -C /opt \ && rm /tmp/apache-maven-3.3.9.tar.gz ENV M2_HOME "/opt/apache-maven-3.3.9" ENV PATH ${PATH}:${M2_HOME}/bin: # Install Ant ENV ANT_VERSION 1.9.4 RUN cd && \ wget -q http://archive.apache.org/dist/ant/binaries/apache-ant-${ANT_VERSION}-bin.tar.gz && \ tar -xzf apache-ant-${ANT_VERSION}-bin.tar.gz && \ mv apache-ant-${ANT_VERSION} /opt/ant && \ rm apache-ant-${ANT_VERSION}-bin.tar.gz ENV ANT_HOME /opt/ant ENV PATH ${PATH}:/opt/ant/bin ......

Entonces, como puede ver en mi archivo docker, tengo instrucciones de instalación para maven y ant. Pero ahora tengo que instalar cualquiera de ellos según una condición. Sé que puedo usar la instrucción ARG en Dockerfile para obtener el argumento durante el tiempo de compilación, pero el problema es que no pude encontrar ningún documento sobre cómo incluirlos en un bloque if/else .

También he leído algunas otras publicaciones de stackoverflow con respecto a esto, pero en esas veo que han pedido usar declaraciones condicionales dentro de una instrucción, por ejemplo RUN if $BUILDVAR -eq "SO"; then export SOMEVAR=hello; else export SOMEVAR=world; fi Link , o escribiendo un archivo de script separado como este Pero como puede ver, mi caso es diferente. No puedo hablar de esto ya que también tendría un montón de otras instrucciones que dependerían de ese argumento. tengo que hacer algo como esto

 ARG BUILD_TOOL if [ "${BUILD_TOOL}" = "MAVEN" ]; then --install maven elif [ "${BUILD_TOOL}" = "ANT" ]; then --install ant fi .... other instructions .... if [ "${BUILD_TOOL}" = "MAVEN" ]; then --some other dependent commands elif [ "${BUILD_TOOL}" = "ANT" ]; then --some other dependent commands fi
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Si no desea usar todas esas declaraciones RUN if , puede crear un script bash con el procedimiento de configuración y llamarlo desde Dockerfile. Por ejemplo:

 FROM centos:centos7 MAINTAINER Someone <someone@email.com> ARG BUILD_TOOL COPY setup.sh /setup.sh RUN ./setup.sh RUN rm /setup.sh

Y el archivo setup.sh (no olvides hacerlo ejecutable):

 if [ "${BUILD_TOOL}" = "MAVEN" ]; then echo "Step 1 of MAVEN setup"; echo "(...)"; echo "Done MAVEN setup"; elif [ "${BUILD_TOOL}" = "ANT" ]; then echo "Step 1 of ANT setup"; echo "(...)"; echo "Done ANT setup"; fi

Luego puede construirlo usando docker build --build-arg BUILD_TOOL=MAVEN . (o ANT ).

Tenga en cuenta que usé un script de shell aquí, pero si tiene otros intérpretes disponibles (por ejemplo, python o ruby), también puede usarlos para escribir el script de configuración.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!