[英]Cannot share volumes between containers in a docker-compose file
I want to share the file build that is generated by my react container to my Nginx container.我想将我的反应容器生成的文件构建共享到我的 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:
When I go in the react container, the build file is in the folder /app.当我进入反应容器时,构建文件位于文件夹 /app 中。 I am trying to use a named volume (files_build) in both the react and the Nginx containers.
我正在尝试在 react 和 Nginx 容器中使用命名卷(files_build)。 But with the code bellow, I cannot find any folder files_built in the VM or build file in the Nginx container (using the command find / -name "build").
但是使用下面的代码,我在虚拟机中找不到任何文件夹 files_built 或在 Nginx 容器中的构建文件(使用命令 find / -name "build")。
What am I doing wrong?我究竟做错了什么?
Thanks for your help, Hugo谢谢你的帮助,雨果
For a typical React application, you'll compile it down to static files and serve those static files;对于典型的 React 应用程序,您会将其编译为静态文件并提供这些静态文件; there's no specific need for a running server.
没有特别需要运行服务器。 You can use a multi-stage build for this.
您可以为此使用多阶段构建。 Your Dockerfile would look roughly like:
您的 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
That means you can remove the react
container from your Compose setup and reduce the docker-compose.yml
file to这意味着您可以从 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
You do not need volumes:
here.您不需要
volumes:
这里。 There are a couple of notable problems with using volumes: since their content never gets updated they are prone to overwriting your application with old content, and they do not translate well to clustered environments like Kubernetes.使用卷有几个值得注意的问题:由于它们的内容永远不会更新,它们很容易用旧内容覆盖您的应用程序,并且它们不能很好地转换为像 Kubernetes 这样的集群环境。 It's better to
COPY
content into Docker images than to try to share files between containers.将内容
COPY
到 Docker 映像中比尝试在容器之间共享文件要好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.