![](/img/trans.png)
[英]How do I share host volumes between containers in docker-compose?
[英]Cannot share volumes between containers in a docker-compose file
我想将我的反应容器生成的文件构建共享到我的 Nginx 容器。
react:
stdin_open: true
build:
dockerfile: ./react.dockerfile
context: ../frontend
volumes:
- /app/node_modules
- files_built:/app/build
networks:
- default
nginx:
stdin_open: true
build:
dockerfile: ./nginx.dockerfile
context: ../frontend
volumes:
- /app/node_modules
- ../frontend/nginx:/config
- files_built:/app/build
networks:
- default
environment:
- PUID=1000
- PGID=1000
- TZ=Europe/London
- URL=myurl.com
- VALIDATION=http
- STAGING=true #optional
ports:
- 443:443
- 80:80 #optional
depends_on:
- react
volumes:
public:
db-data:
files_built:
networks:
default:
当我进入反应容器时,构建文件位于文件夹 /app 中。 我正在尝试在 react 和 Nginx 容器中使用命名卷(files_build)。 但是使用下面的代码,我在虚拟机中找不到任何文件夹 files_built 或在 Nginx 容器中的构建文件(使用命令 find / -name "build")。
我究竟做错了什么?
谢谢你的帮助,雨果
对于典型的 React 应用程序,您会将其编译为静态文件并提供这些静态文件; 没有特别需要运行服务器。 您可以为此使用多阶段构建。 您的 Dockerfile 大致如下所示:
FROM node:lts AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY ./ ./
RUN npm build
FROM nginx
COPY --from=build /app/dist /usr/share/nginx/html
# base image contains the right CMD already
这意味着您可以从 Compose 设置中删除react
容器并将docker-compose.yml
文件减少到
version: '3.8'
services:
nginx:
build: ../frontend
volumes:
- ../frontend/nginx:/config
environment:
- TZ=Europe/London
- URL=myurl.com
- VALIDATION=http
- STAGING=true #optional
ports:
- 443:443
- 80:80 #optional
您不需要volumes:
这里。 使用卷有几个值得注意的问题:由于它们的内容永远不会更新,它们很容易用旧内容覆盖您的应用程序,并且它们不能很好地转换为像 Kubernetes 这样的集群环境。 将内容COPY
到 Docker 映像中比尝试在容器之间共享文件要好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.