英文:
Why can docker not find copied files?
问题
我尝试了很多不同的解决方案,包括在这里和其他地方提到的相似或相同的问题,但仍然无法工作。我的目标是运行多个Docker容器,用于我的后端和前端(还有一些其他容器,但我在这里剪切了它们,因为它们可以正常启动和运行)。由于Docker似乎无法找到manage.py
(用于运行后端)和package.json
(用于安装前端依赖项)文件,后端和前端容器无法启动。
我的文件夹结构:
- docker-compose.yml
- frontend/
- Dockerfile
- package.json
- 节点项目文件...
- backend/
- Dockerfile
- manage.py
- 更多的Django项目文件...
我的docker-compose.yml文件:
version: '3.4'
services:
backend:
build:
context: ./backend
dockerfile: Dockerfile
container_name: 'backend'
command: python manage.py runserver 0.0.0.0:8080
volumes:
- ./data/backend:/backend_app
ports:
- '8080:8080'
restart: always
frontend:
image: node:18.14.2
build:
context: ./frontend
dockerfile: Dockerfile
container_name: 'frontend'
command: npm run preview
ports:
- '3000:3000'
volumes:
- ./data/frontend:/frontend_app
frontend/Dockerfile:
FROM node:18.14.2
EXPOSE 3000
WORKDIR /frontend_app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "preview"]
backend/Dockerfile:
FROM python:3.10-slim
EXPOSE 8080
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /backend_app
COPY requirements.txt .
RUN python -m pip install --upgrade pip
RUN python -m pip install -r requirements.txt
COPY . .
CMD ["python", "manage.py", "runserver", "0.0.0.0:8080"]
我运行docker compose up
时的部分Docker控制台输出:
frontend | npm ERR! code ENOENT
frontend | npm ERR! syscall open
frontend | npm ERR! path /frontend_app/package.json
frontend | npm ERR! errno -2
frontend | npm ERR! enoent ENOENT: no such file or directory, open '/frontend_app/package.json'
frontend | npm ERR! enoent This is related to npm not being able to find a file.
frontend | npm ERR! enoent
frontend |
frontend |
frontend | npm ERR! A complete log of this run can be found in:
frontend | npm ERR! /root/.npm/_logs/2023-07-17T22_35_21_039Z-debug-0.log
backend | python: can't open file '/backend_app/manage.py': [Errno 2] No such file or directory
backend exited with code 2
英文:
I've tried a bunch of different solutions to similar/identical questions on here and other places and it still won't work. My goal is to run multiple docker containers for my backend and frontend (and a few others but i cut them out here since they start and run fine). The backend and frontend containers won't start up since docker cannot seem to be able to find the manage.py
(for running the backend) and package.json
(for installing the frontend dependencies) files.
My folder structure:
- docker-compose.yml
- frontend/
- Dockerfile
- package.json
- node project files...
- backend/
- Dockerfile
- manage.py
- more django project files...
My docker-compose.yml file:
version: '3.4'
services:
backend:
build:
context: ./backend
dockerfile: Dockerfile
container_name: 'backend'
command: python manage.py runserver 0.0.0.0:8080
volumes:
- ./data/backend:/backend_app
ports:
- '8080:8080'
restart: always
frontend:
image: node:18.14.2
build:
context: ./frontend
dockerfile: Dockerfile
container_name: 'frontend'
command: npm run preview
ports:
- '3000:3000'
volumes:
- ./data/frontend:/frontend_app
frontend/Dockerfile:
FROM node:18.14.2
EXPOSE 3000
WORKDIR /frontend_app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "preview"]
backend/Dockerfile:
FROM python:3.10-slim
EXPOSE 8080
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /backend_app
COPY requirements.txt .
RUN python -m pip install --upgrade pip
RUN python -m pip install -r requirements.txt
COPY . .
CMD ["python", "manage.py", "runserver", "0.0.0.0:8080"]
My (partial) docker console output when running docker compose up
:
frontend | npm
frontend | ERR! code ENOENT
frontend | npm ERR! syscall open
frontend | npm ERR! path /frontend_app/package.json
frontend | npm
frontend | ERR! errno -2
frontend | npm ERR! enoent ENOENT: no such file or directory, open '/frontend_app/package.json'
frontend | npm ERR! enoent This is related to npm not being able to find a file.
frontend | npm ERR! enoent
frontend |
frontend |
frontend | npm ERR! A complete log of this run can be found in:
frontend | npm ERR! /root/.npm/_logs/2023-07-17T22_35_21_039Z-debug-0.log
backend | python: can't open file '/backend_app/manage.py': [Errno 2] No such file or directory
backend exited with code 2
答案1
得分: 2
你正在挂载./data/{frontend,backend}
,覆盖了构建时的任何操作。你的目录结构并不表明这是正确的路径。
你应该挂载./{frontend,backend}
。
另外,由于在构建时获取依赖项(node_modules),当你执行docker compose up
时,它们将不可用,因为你用挂载的主机目录覆盖了容器文件系统。因此,你需要确保安装依赖项,以使它们存在于主机系统上。总之,混合构建和挂载卷似乎有点奇怪。
英文:
You are mounting ./data/{frontend,backend}
, overwriting anything you did on build. Your directory structure does not suggest that this is the right path.
You should mount ./{frontend,backend}
instead.
Additionally, due to fetching the dependencies (node_modules) on build, they will not be available when you do docker compose up
, since you overwrite the container file system with the mounted host directory. So you need to ensure that you install the dependencies such that they will exist on the host system. All in all, mixing the build with the volume mounts seems a bit strange.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论