9

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

4

2 回答 2

4

嗯,好点!
command: python code.py不完全一样CMD python code.py;
由于第一个被解释为shell-form命令,而后者被解释为exec-form命令。
问题在于这两种类型的CMD的差异。(即CMD ["something"]vs CMD "something")。
有关这两者的更多信息,请参见此处

但是,您可能仍在思考您的示例有什么问题?
在您的情况下,根据YAML格式的规范,python code.pyincommand: python code.py将被解释为单个字符串值,而不是数组!
另一方面,你可能已经猜到了,python code.py;在上面提到的 Dockerfile 被解释为一个数组,它提供了一个exec-form命令。

于 2020-02-22T16:53:42.183 回答
2

(部分)答案是抛出的错误根本不是问题所在。运行以下命令:bash -c 'python code.py'工作正常。我仍然无法解释为什么 Dockerfile 中的 CMD 和 docker-compose “command” 选项之间存在差异。但这为我解决了

于 2018-12-23T14:37:51.373 回答