简体   繁体   English

Docker:如何在两个容器之间共享文件夹

[英]Docker: How to share a folder between two containers

I am working on app that has a webserver running openresty and a application running on php wit the front-end in vue. 我正在开发一个具有运行openresty的网络服务器和一个在vue前端运行php的应用程序。

version: '3'
services:
  php:
    build: ./
    volumes:
      - ./services/php/php.ini:/usr/local/etc/php/php.ini

  web:
    build: ./services/openresty
    ports:
      - "8888:80"
    volumes:
      - ./services/openresty/company.conf:/usr/local/openresty/nginx/conf/nginx.conf
      - ./services/openresty/bodyconfig.conf:/etc/openresty/nginx/conf/bodyconfig.conf

When building the application I am building my vue code to a dist folder this happens inside the php container: 在构建应用程序时,我正在将我的Vue代码构建到dist文件夹中,这发生在php容器内:

FROM php:7.3-fpm-alpine

RUN apk update \
    && apk add  --no-cache git curl libmcrypt libmcrypt-dev openssh-client icu-dev \
    libxml2-dev bash freetype-dev libpng-dev libjpeg-turbo-dev nodejs npm libzip-dev g++ make autoconf postgresql-dev \
    && docker-php-source extract \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && docker-php-source delete \
    && docker-php-ext-install pdo_pgsql soap intl gd zip \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer \
    && rm -rf /tmp/*

CMD ["php-fpm", "-F"]

WORKDIR /var/www/company

COPY ./ ./

RUN bash ./docker_build.sh

EXPOSE 9000

docker_build.sh docker_build.sh

cd /var/www/company/js

npm install
npm run build

now inside the php docker container is a new folder dist under /var/www/dist. 现在,在php docker容器内的/ var / www / dist下是一个新文件夹dist。

nginx needs to use this folder because it contains a static index.html which is served by nginx like this nginx需要使用此文件夹,因为它包含一个静态的index.html,它由nginx像这样提供

location / {
    alias /var/www/company/dist/;
    try_files $uri $uri/ /index.html;
}

I am wondering how I can my web container access the build folder dist which is located inside the php container 我想知道我的Web容器如何访问位于php容器内的构建文件夹dist

Thanks in advance 提前致谢

You can use bind-mounts to mount that folder from the host. 您可以使用绑定挂载从主机挂载该文件夹。

Bind mount 绑定安装

When you use a bind mount, a file or directory on the host machine is mounted into a container. 使用绑定安装时,主机上的文件或目录将安装到容器中。 The file or directory is referenced by its full or relative path on the host machine. 文件或目录由主机上的完整或相对路径引用。

Or, you can use docker volume and attach that volume to both the containers. 或者,您可以使用docker卷并将该卷附加到两个容器。

Docker Volume Docker卷

Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. 卷是用于持久化由Docker容器生成和使用的数据的首选机制。 While bind-mounts are dependent on the directory structure of the host machine, volumes are completely managed by Docker. 尽管绑定安装取决于主机的目录结构,但是卷完全由Docker管理。

Edit: Link to Official document of docker-volume implementation 编辑:链接到docker-volume实现的正式文档

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

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