Is Docker ARG allowed within CMD instruction

DockerDocker Compose

Docker Problem Overview


I have a Dockerfile where an ARG is used in the CMD instruction:

ARG MASTER_NAME
CMD spark-submit --deploy-mode client --master ${MASTER_URL}

The arg is passed via docker-compose:

  spark:
    build:
      context: spark
      args:
        - MASTER_URL=spark://master:7077

However, the ARG does not seem to get expanded for CMD. After I docker-compose up.

Here's what inspect shows:

docker inspect  -f "{{.Name}} {{.Config.Cmd}}" $(docker ps -a -q)
/spark {[/bin/sh -c spark-submit --deploy-mode client --master ${MASTER_URL}]}

Docker Solutions


Solution 1 - Docker

The thing is that args only can be used at build time, and the CMD is executing at run time. I guess that the only approach now to achieve what you want is setting an environment variable in the Dockerfile with the MASTER_NAME value.

ARG MASTER_NAME
ENV MASTER_NAME ${MASTER_NAME}
CMD spark-submit --deploy-mode client --master ${MASTER_NAME}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarVarView Question on Stackoverflow
Solution 1 - DockerJesusTinocoView Answer on Stackoverflow