首页 文章

docker-compose:在Dockerfile中使用卷中的文件

提问于
浏览
1

我在docker-compose.yml中定义了一个卷 . 我想从我的Dockerfile中的卷中使用其中一个文件,但是我收到错误:“没有这样的文件或目录”

如果我创建容器而无法访问Dockerfile中的文件,我将在docker-compose.yml文件的特定位置看到容器内容器中的所有文件 .

这是它应该如何工作或我做错了什么?我想我错过了什么 .

存储库:https://github.com/Lightshadow244/OwnMusicWeb

泊坞窗,compose.yml:

version: '3'
services:
  ownmusicweb:
   build: .
   container_name: ownmusicweb
   hostname: ownmusicweb
   volumes:
       - ~/OwnMusicWeb/ownmusicweb:/ownmusicweb
   ports:
    - 83:8000
   tty: true

Dockerfile:

FROM ubuntu:latest
WORKDIR /ownmusicweb
RUN ["apt-get", "update"]
RUN ["apt-get", "install", "-y", "python-pip"]
RUN ["pip", "install", "--upgrade", "pip"]
RUN ["pip", "install", "Django", "eyeD3", "djangorestframework", "markdown", "django-filter"]
RUN ["python", "/ownmusicweb/manage.py", "migrate"]
RUN ["python", "/ownmusicweb/manage.py", "runserver", "0.0.0.0:8000"]

1 回答

  • 1

    总结评论中的讨论:

    RUN 指令无法访问卷,因为它尚未挂载 . Docker仅创建build context,这是使用ADD指令所必需的 . 但是这样文件将保留在编译容器中,因此您需要重建才能更新这些文件 .

    构建完成后,由docker-compose.yml中的 "build: ." 触发,docker启动容器并添加一个卷 . 但是你的情况为时已晚 .

    建议的机制是使用带有scipt的 ENTRYPOINT 来启动你的东西 . 它's being executed after the build in the phase of launch, so you'将有权访问该卷 .

    另一种方法,在我看来更清洁一点是使用docker-compose的 command 指令 . 你可以把相同的脚本放在里面 . 这取决于您在开发环境中使用docker的方式 .

相关问题