简体   繁体   English

从 dockerfile 访问 docker-compose.yml 中设置的卷

[英]access volumes set in docker-compose.yml from dockerfile

I have my project architecture like this:我的项目架构是这样的:

.
├── app/
├── context/
│   ├── Dockerfile
│   ├── .dockerignore
│   └── php.ini
├── database/
├── http/
├── composer.json
└── docker-compose.yml

and in docker-compose.yml I have the following configuration:docker-compose.yml我有以下配置:

version: '3.8'
services: 
  app:
    container_name: "ERP"
    restart: always
    build: 
      context: ./context
      dockerfile: Dockerfile
    stdin_open: true
    tty: true
    ports: 
      - '8000:80'
    links: 
      - db_server
    volumes:
      - .:/usr/src/app
    working_dir: /usr/src/app
  db_server:
    container_name: "db_server"
    image: 'mysql:8.0'
    ports: 
    - '3306:3306'

But when I set the Dockerfile content to set up the application with docker-compose up , having Dockerfile content as this:但是,当我设置Dockerfile内容以使用docker-compose up设置应用程序时,具有Dockerfile内容如下:

FROM ubuntu:20.04
WORKDIR /usr/src/app
RUN cat composer.json

It says "No such file or directory composer.json" .它说"No such file or directory composer.json" Why?为什么?

UPDATE更新

I managed to solve the problem based on ENTRYPOINT configuration..我设法根据ENTRYPOINT配置解决了这个问题..

as far I understand - I'm new to docker -, the ENTRYPOINT defines an entry script running at the beginning of a container, thus, starting that script will be definitely in the run time of the container and after the initializations specified by docker-compost.yml file.. so the contents of the actual context will be available to the script to see it in the time it runs..据我了解 - 我是 docker 的新手 - ENTRYPOINT 定义了一个在容器开头运行的入口脚本,因此,启动该脚本肯定会在容器的运行时和 docker 指定的初始化之后 - compost.yml 文件.. 所以实际上下文的内容将可供脚本在运行时查看..

Thank you all for your answers.谢谢大家的答案。

That's because you define the context to be "./context", so you are stuck into this folder, where composer.json isn't.那是因为你将上下文定义为“./context”,所以你被困在这个文件夹中,composer.json 不是。

Use "."利用 ”。” for the context.为上下文。 and context/Dockerfile for the dockerfile. dockerfile 的上下文/Dockerfile。 Then mounting '.'然后安装“。” will mount the whole directory, and not only the./context one.将挂载整个目录,而不仅仅是 ./context 一个。

The build process to create an image occurs before the runtime process that takes that image and runs a container.创建映像的构建过程发生在获取该映像并运行容器的运行时过程之前。 The compose file includes a build section to allow you to easily build the image before running it, but all of the other parts of the compose file define runtime configurations like volume mounts and networking. compose 文件包含一个构建部分,可让您在运行之前轻松构建映像,但 compose 文件的所有其他部分都定义了运行时配置,例如卷安装和网络。

At build time, you do not get to specify the volume sources, at most you can define a required volume target in the image with a VOLUME step.在构建时,您无需指定卷源,最多可以通过 VOLUME 步骤在映像中定义所需的卷目标。 (Note if you do that, future RUN steps within the Dockerfile may have issues modifying that directory since many build tools mount an anonymous volume as you've requested, but only capture the changes to the container filesystem, not the volume filesystem). (请注意,如果您这样做,Dockerfile 中的未来 RUN 步骤可能会在修改该目录时遇到问题,因为许多构建工具会按照您的要求安装匿名卷,但仅捕获对容器文件系统的更改,而不是对卷文件系统的更改)。

If you need the contents of a file or directory in your image, you must perform a COPY or ADD step in the Dockerfile to copy from the build context (typically imported as . or the current directory) into the image.如果您需要映像中文件或目录的内容,则必须在 Dockerfile 中执行 COPY 或 ADD 步骤,以从构建上下文(通常导入为.或当前目录)复制到映像中。

Key build.context define a path to a directory containing a Dockerfile .build.context定义包含Dockerfile的目录的路径。 This is a context of building image, and during build process docker doesn't have access to composer.json file (it is out of the context).这是构建映像的上下文,在构建过程中 docker 无法访问composer.json文件(它不在上下文中)。

Command RUN runs command during build.命令RUN在构建期间运行命令。 If you want to run it when container is starting, you should use CMD .如果你想在容器启动时运行它,你应该使用CMD

FROM ubuntu:20.04
WORKDIR /usr/src/app
CMD cat composer.json

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

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