我正在Docker Compose中使用NextJS,Django,Postgres,Nginx部署项目,它在本地(ubunutu 18.04)上运行良好。但是在服务器(ubuntu 20.04)中,它会引发Error: ENOTEMPTY: directory not empty, rmdir '/frontend/out/node_modules/cacache/node_modules/.bin'
错误。如何解决这个问题。
附言我正在使用wait-for.sh的前端来等待api服务准备就绪(否则我也会得到构建错误),而我的本地docker版本是Docker version 19.03.12, build 48a66213fe
,我的服务器docker版本是Docker version 19.03.11, build dd360c7
也许是可能的错误原因?
我的前端服务的Dockerfile:
USER root
WORKDIR /frontend
COPY . /frontend
# Add wait-for-it
COPY wait-for.sh wait-for.sh
RUN chmod +x wait-for.sh
RUN yarn
我的docker-compose.yml:
services:
db:
image: postgres:12.0-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=purple_postgres_user
- POSTGRES_PASSWORD=purple_p@ssW0rd
- POSTGRES_DB=purple_db
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
# command: ./manage.py purple.wsgi:application --bind 0.0.0.0:8000
volumes:
- ./backend/:/home/purple_user/purple/
- static_volume:/home/purple_user/purple/static/
- media_volume:/home/purple_user/purple/media/
expose:
- 8000
ports:
- 8000:8000
env_file:
- ./.env.dev
depends_on:
- db
nginx:
build: ./nginx
ports:
- 80:80
volumes:
- static_volume:/var/www/staticfiles/
- media_volume:/var/www/mediafiles/
- build_folder:/var/www/frontend/
depends_on:
- web
frontend:
container_name: frontend
build:
context: ./frontend
volumes:
- build_folder:/frontend/out
depends_on:
- web
command: ./wait-for.sh web:8000 -- yarn build
volumes:
postgres_data:
static_volume:
media_volume:
build_folder:```
最佳答案
Docker中的命名卷是持久性的。当您的docker-compose.yml
文件包含
volumes:
- build_folder:/frontend/out
它将先前构建的结果存储在build_folder
中,并且在下次运行此容器时它们仍然存在。这就是导致您看到错误消息的原因。我避免设置Compose服务只是为了为其他服务构建文件。相反,在图像构建时使用multi-stage build来构建 Artifact 。相反,在您的Nginx镜像Dockerfile中,构建前端应用程序:
FROM node:12 AS frontend
WORKDIR /frontend
COPY frontend/package.json frontend/yarn.lock .
RUN yarn install
COPY frontend .
RUN yarn build
FROM nginx
# ... whatever this Dockerfile had before ...
# (except, change `COPY stuff /image/path` to `COPY nginx/stuff /image/path`)
COPY --from=frontend /frontend/out /var/www/frontend
您可以使用类似的技术对静态文件COPY
进行--from
生成web
图像;或者使用附加的构建阶段,直接从构建上下文中对其进行COPY
编码,或者对单独的构建镜像的名称COPY --from
编码。 (由于Compose不会对依赖的构建进行排序,因此很难进行编排。)这避免了类似的问题,即static_volume
的内容在两个图像中都隐藏了更改的静态文件。services:
nginx:
# The build context must be an ancestor of anything that
# it needs to `COPY` in. Since we `COPY` from both the `frontend`
# and `nginx` directories, we need to specify
build: .
ports: ['80:80']
depends_on: [web]
# No `volumes:`; the image is self-contained
# No build-only `frontend` container
关于node.js - 如何解决 yarn 生成错误-错误:ENOTEMPTY:目录不为空,rmdir '/frontend/out/node_modules/cacache/node_modules/.bin',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64628315/