Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

command/CMD in docker-compose is not equivalent to CMD in Dockerfile

I have a container that uses a volume in its entrypoint. for example

CMD bash /some/volume/bash_script.sh

I moved this to compose but it only works if my compose points to a Dockerfile in the build section if I try to write the same line in the command section is not acting as I expect and throws file not found error.

I also tried to use docker-compose run <specific service> bash /some/volume/bash_script.sh which gave me the same error.

The question is - Why dont I have this volume at the time that the docker-compose 'command' is executed? Is there anyway to make this work/ override the CMD in my dockerfile?

EDIT: I'll show specifically how I do this in my files:

docker-compose:

version: '3'
services:
  base:
    build: 
      context: ..
      dockerfile: BaseDockerfile
    volumes:
      code:/volumes/code/
  my_service:
    volumes:
      code:/volumes/code/
    container_name: my_service
    image: my_service_image
    ports:
      - 1337:1337
    build: 
      context: ..
      dockerfile: Dockerfile

volumes: code:

BaseDockerfile:

FROM python:3.6-slim

WORKDIR /volumes/code/

COPY code.py code.py
CMD tail -f /dev/null

Dockerfile:

FROM python:3.6-slim

RUN apt-get update && apt-get install -y redis-server \
alien \
unixodbc 

WORKDIR /volumes/code/


CMD python code.py;

This works.

But if I try to add to docker-compose.yml this line:

command: python code.py

Then this file doesnt exist at the command time. I was expecting this to behave the same as the CMD command

like image 557
NotSoShabby Avatar asked Dec 16 '18 14:12

NotSoShabby


People also ask

What is CMD command in Dockerfile?

The CMD command​ specifies the instruction that is to be executed when a Docker container starts.

Does docker compose override Dockerfile CMD?

Docker-compose command doesn't override Dockerfile CMD.

Is docker compose same as Dockerfile?

Difference between docker-compose and Dockerfile. The key difference between the Dockerfile and docker-compose is that the Dockerfile describes how to build Docker images, while docker-compose is used to run Docker containers.

Is CMD needed in Dockerfile?

Both ENTRYPOINT and CMD are essential for building and running Dockerfiles—it simply depends on your use case. As a general rule of thumb: Opt for ENTRYPOINT instructions when building an executable Docker image using commands that always need to be executed.


1 Answers

Hmm, nice point!
command: python code.py is not exactly the same as CMD python code.py;!
Since the first one is interpreted as a shell-form command, where the latter is interpreted as an exec-form command.
The problem is about the differences in these two types of CMDs. (i.e. CMD ["something"] vs CMD "something").
For more info about these two, see here.

But, you may still be thinking of what's wrong with your example?
In your case, based on the specification of YAML format, python code.py in the command: python code.py will be interpreted as a single string value, not an array!
On the other hand, as you've probably guessed, python code.py; in the above-mentioned Dockerfile is interpreted as an array, which provides an exec-form command.

like image 153
Emran BatmanGhelich Avatar answered Oct 21 '22 21:10

Emran BatmanGhelich