![](/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.