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

Viewed 7993

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

2 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.

The (partial) answer is that the error that was thrown was not at all what the problem was. Running the following command: bash -c 'python code.py' worked fine. I still cant explain why there was a difference between CMD in Dockerfile and docker-compose "command" oprtion. but this solved it for me

Related