为什么我的绑定挂载实际上没有绑定一些文件?

huangapple go评论73阅读模式
英文:

Why my bind mounts don't actually bind some files?

问题

我有一个Python应用程序,它使用绑定挂载将代码挂载到容器中,这样我就不必在每次代码更改时构建容器,像这样:

app:
    volumes:
      - type: bind
        source: ./findface
        target: /app/findface

这个一直运行得很好。但现在我还想绑定我的启动脚本,该脚本是从Dockerfile中调用的:

app:
    volumes:
      - type: bind
        source: ./findface
        target: /app/findface
      - type: bind
        source: ./startup.sh
        target: /app

但这个脚本却无法绑定。主机文件系统中实际存在该文件,但当我构建容器时它找不到:

Step 7/8 : RUN chmod +x startup.sh
 ---> Running in ecaae384b6e5
chmod: cannot access 'startup.sh': No such file or directory
ERROR: Service 'app' failed to build: The command '/bin/sh -c chmod +x startup.sh' returned a non-zero code: 1

我做错了什么? Dockerfile本身如下:

FROM jjanzic/docker-python3-opencv:latest

ENV PYTHONUNBUFFERED=1

RUN pip install --no-cache-dir gunicorn[eventlet]

WORKDIR /app

COPY ./requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

RUN chmod +x startup.sh

CMD "./startup.sh"
英文:

I've got some Python app which used bind mounts to mount code into container, so I don't have to build container on each code change, like this:

app:
    volumes:
      - type: bind
        source: ./findface
        target: /app/findface

And it was working fine. But now I also want to bind my startup script, which is invoked from Dockerfile:

app:
    volumes:
      - type: bind
        source: ./findface
        target: /app/findface
      - type: bind
        source: ./startup.sh
        target: /app

And this one just doesn't bind. There is an actual file in the host filesystem, but when I build the container it can't find it:

Step 7/8 : RUN chmod +x startup.sh
 ---> Running in ecaae384b6e5
chmod: cannot access 'startup.sh': No such file or directory
ERROR: Service 'app' failed to build: The command '/bin/sh -c chmod +x startup.sh' returned a non-zero code: 1

What am I doing wrong?

Dockerfile itself:

FROM jjanzic/docker-python3-opencv:latest

ENV PYTHONUNBUFFERED=1

RUN pip install --no-cache-dir gunicorn[eventlet]

WORKDIR /app

COPY ./requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

RUN chmod +x startup.sh

CMD "./startup.sh"

答案1

得分: 2

根据您的评论,如果文件在构建时不存在,则不需要在构建阶段设置权限。如果您不想在构建阶段复制文件,可以在主机上设置执行权限,然后使用docker run命令或您提到的yml配置文件进行绑定。

CMD "/app/startup.sh";

例如:

docker run -it --rm -v $PWD/startup.sh:/app/startup.sh my_image
英文:

Base on your comment, then you do not need set permission at build stage as the file is not exist at build time, if you do not want to copy at build stage, set permission on the host to make executable and then bind with docker run command or as per the yml config that you mentioned.

CMD "/app/startup.sh"

for example

docker run -it --rm -v $PWD/startup.sh:/app/startup.sh my_image

huangapple
  • 本文由 发表于 2020年1月3日 17:13:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定