Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose command doesn't override Dockerfile CMD

Docker-compose reference says:

command

Override the default command.

My Dockerfile

FROM adoptopenjdk/maven-openjdk8

RUN mkdir /config

COPY /src/main/resources/application.properties /config/application.properties
COPY /src/main/resources/logback.xml /config/logback.xml

# copy wait-for-it-sh tool to sync container starting
COPY wait-for-it.sh /
RUN chmod +x /wait-for-it.sh

VOLUME /tmp

ADD target/my-svc-1.jar app.jar

RUN sh -c 'touch /app.jar'

ENV JAVA_OPTS=""

CMD [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

Interesting part of my docker-compose.yml

version: '3'

services:
  ..
  my-svc:
    container_name: my-svc-container
    build:
      context: ./my-svc-folder
      dockerfile: Dockerfile
    ports:
      - "9100:9100"
      - "5005:5005"
    depends_on:
      - db
    command: ["./wait-for-it.sh", "my-svc-dbserver:3306", "--", "sh", "-c", "java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar /app.jar"]
  ...

Output of docker-compose build:

Building my-svc
Step 1/11 : FROM adoptopenjdk/maven-openjdk8
 ---> b3ad935008a1
Step 2/11 : RUN mkdir /config
 ---> Using cache
 ---> 5db347c13050
Step 3/11 : COPY /src/main/resources/application.properties /config/application.properties
 ---> Using cache
 ---> ade3653a018a
Step 4/11 : COPY /src/main/resources/logback.xml /config/logback.xml
 ---> Using cache
 ---> 94277fd95be1
Step 5/11 : COPY wait-for-it.sh /
 ---> Using cache
 ---> 80ac64c7c994
Step 6/11 : RUN chmod +x /wait-for-it.sh
 ---> Using cache
 ---> 1a0c99bc10ad
Step 7/11 : VOLUME /tmp
 ---> Using cache
 ---> 81864f3c9fda
Step 8/11 : ADD target/my-svc-1.jar app.jar
 ---> Using cache
 ---> 56cc5c149e37
Step 9/11 : RUN sh -c 'touch /app.jar'
 ---> Using cache
 ---> 9db3f5d983cc
Step 10/11 : ENV JAVA_OPTS=""
 ---> Using cache
 ---> a5c4828b6539
Step 11/11 : CMD [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
 ---> Using cache
 ---> cb1fd876d8d6
Successfully built cb1fd876d8d6
Successfully tagged src_my-svc:latest

So, the last step is shown as CMD [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ], the one that is in Dockerfile instead of the one in docker-compose.

What am I missing here?

like image 312
OzrenTkalcecKrznaric Avatar asked Sep 14 '20 11:09

OzrenTkalcecKrznaric


People also ask

Does Docker compose override Dockerfile CMD?

Docker-compose command doesn't override Dockerfile CMD.

How can I override CMD when running a Docker image?

As the operator (the person running a container from the image), you can override that CMD just by specifying a new COMMAND. If the image also specifies an ENTRYPOINT then the CMD or COMMAND get appended as arguments to the ENTRYPOINT. So to do what you want you need only specify a cmd, and override using /bin/bash .

Does Docker compose replace Dockerfile?

No, Dockerfile instructs Docker how to build your image(s). Docker Compose instructs Docker how to run your image(s). Thx, so I have to make a dockerfile just to have the copy command ?

Does Docker compose command override ENTRYPOINT?

All About Docker Compose Override Entrypoint Entrypoint helps use set the command and parameters that executes first when a container is run. In fact, the command line arguments in the following command become a part of the entrypoint command, thereby overriding all elements mentioned via CMD.


Video Answer


1 Answers

Overriding takes place at runtime, which is when you create a container based on an image and you then start it. That last step you see is during the building phase of the actual image which (correctly) follows the instructions of your Dockerfile.

So, if you continue with docker-compose up, the container that will be created and started, will have the "overridden" configuration provided in the docker-compose.yaml file.


From Overriding Dockerfile image defaults

Overriding Dockerfile image defaults

When a developer builds an image from a Dockerfile or when she commits it, the developer can set a number of default parameters that take effect when the image starts up as a container. Four of the Dockerfile commands cannot be overridden at runtime: FROM, MAINTAINER, RUN, and ADD. Everything else has a corresponding override in docker run.

like image 52
tgogos Avatar answered Oct 21 '22 09:10

tgogos