Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Docker ARG allowed within CMD instruction

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}]}
like image 644
DarVar Avatar asked Feb 22 '16 18:02

DarVar


People also ask

How do I pass ARG in Dockerfile?

ARG instruction defines a variable that can be passed at build time. Once it is defined in the Dockerfile you can pass with this flag --build-arg while building the image. We can have multiple ARG instruction in the Dockerfile. ARG is the only instruction that can precede the FROM instruction in the Dockerfile.

What does the CMD instruction in a Dockerfile do?

When building a Dockerfile, the CMD instruction specifies the default program that will execute once the container runs. A quick point to note: CMD commands will only be utilized when command-line arguments are missing.

Can we use CMD and ENTRYPOINT together?

Example of using CMD and ENTRYPOINT togetherIf both ENTRYPOINT and CMD are present, what is written in CMD is executed as an option of the command written in ENTRYPOINT. If an argument is added at the time of docker run , the contents of CMD will be overwritten and the ENTRYPOINT command will be executed.


1 Answers

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}
like image 61
JesusTinoco Avatar answered Oct 16 '22 10:10

JesusTinoco