On dockerfile syntax

  docker, question

Looking at a dockerfile file, variables were previously set through ARG and ENV.

ARG INSTALL_WORKSPACE_SSH=false
ENV INSTALL_WORKSPACE_SSH ${INSTALL_WORKSPACE_SSH}

Later, the value of INSTALL_WORKSPACE_SSH is determined by reading the environment variables

if [ ${INSTALL_WORKSPACE_SSH} = true ]

Isn’t that a problem? The value has been defined before, so what’s the point of reading the environment later?

#####################################
 # ssh:
 #####################################
 ARG INSTALL_WORKSPACE_SSH=false
 ENV INSTALL_WORKSPACE_SSH ${INSTALL_WORKSPACE_SSH}
 
 ADD insecure_id_rsa /tmp/id_rsa
 ADD insecure_id_rsa.pub /tmp/id_rsa.pub
 
 RUN if [ ${INSTALL_WORKSPACE_SSH} = true ];  then \
 rm -f /etc/service/sshd/down && \
 cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys \
 && cat /tmp/id_rsa.pub >> /root/.ssh/id_rsa.pub \
 && cat /tmp/id_rsa >> /root/.ssh/id_rsa \
 && rm -f /tmp/id_rsa* \
 && chmod 644 /root/.ssh/authorized_keys /root/.ssh/id_rsa.pub \
 && chmod 400 /root/.ssh/id_rsa \
 ;  fi

The variables defined by the arg instruction are valid only during compilation, and can be overwritten by the external parameter-—build-arg name = value during build. Therefore, it is necessary to re-judge whether it is the desired value or not.