简体   繁体   English

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

[英]docker-compose: use file from volume in Dockerfile

I defined a volume in my docker-compose.yml. 我在docker-compose.yml中定义了一个卷。 I want to use one of these files from the volume in my Dockerfile, but I get the error: "No such file or directory" 我想从我的Dockerfile中的卷中使用其中一个文件,但是我收到错误:“没有这样的文件或目录”

If I create the container without the access to the files in the Dockerfile I will see all files from the volume inside the container at the specific location from the docker-compose.yml file. 如果我创建容器而无法访问Dockerfile中的文件,我将在docker-compose.yml文件的特定位置看到容器内容器中的所有文件。

Is this how it should work or do I something wrong? 这是它应该如何工作或我做错了什么? I think I am missing something. 我想我错过了什么。

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

docker-compose.yml: 泊坞窗,compose.yml:

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

Dockerfile: 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"]

Summarising discussion in comments: 总结评论中的讨论:

RUN directive has no access to volume because it's not mounted yet. RUN指令无法访问卷,因为它尚未安装。 Docker creates build context only, which is neccessary to use ADD directive. Docker仅创建构建上下文 ,这是使用ADD指令所必需的。 But in this way the files will remain in the compiled container so you will need a rebuild to update those. 但是这样文件将保留在编译容器中,因此您需要重建才能更新这些文件。

After build is finished, triggered by "build: ." 构建完成后,由“build:”触发 in docker-compose.yml, docker launches the container and adds a volume. 在docker-compose.yml中,docker启动容器并添加一个卷。 But it's too late in your case. 但是你的情况为时已晚。

Suggested mechanism is to use ENTRYPOINT with a scipt which launches your stuff. 建议的机制是使用带有scipt的ENTRYPOINT来启动你的东西。 It's being executed after the build in the phase of launch, so you'll have access to the volume. 它是在发布阶段构建之后执行的,因此您可以访问该卷。

Another approach, which seems to me a bit cleaner is to use command directive of docker-compose. 另一种方法,在我看来更清洁一点是使用docker-compose的命令指令。 You can put the same script inside. 你可以把相同的脚本放在里面。 It depends of the way you're doing deployment and the way you're using docker in the development environment. 这取决于您进行部署的方式以及您在开发环境中使用docker的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM