我已经成功地对我的Django应用进行了docker化,但是遇到了麻烦,对与Django应用连接的我的react应用进行了docker化。
这是我下面的Dockerfile
# Pull base image
FROM python:3
# Set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory, we can name it whatever we want
WORKDIR /djangobackend
# Install dependencies
# Copy requirement file from the docker workdir we defined named djangodocker
COPY requirements.txt /djangobackend/
RUN pip install -r requirements.txt
# Copy project
COPY . /djangobackend/
这是我的docker-compose.yml
文件
version: '3.2'
services:
db:
image: postgres:10.1-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
web:
build: .
command: python /djangobackend/backend/manage.py runserver 0.0.0.0:8000
volumes:
- .:/djangobackend
ports:
- 8000:8000
depends_on:
- db
volumes:
postgres_data:
一切正常。
后来,我试图通过dockerize进行如下反应:
Dockerfile
下面
# Pull base image
FROM python:3
# Set environment varibles
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory, we can name it whatever we want
WORKDIR /djangobackend
# Install dependencies
# Copy requirement file from the docker workdir we defined named djangodocker
COPY requirements.txt /djangobackend/
RUN pip install -r requirements.txt
# Copy project
COPY . /djangobackend/
# Use an official node runtime as a parent image
FROM node:8
WORKDIR /reactfrontend/
# Install dependencies
COPY package.json yarn.lock /reactfrontend/
RUN yarn
# Add rest of the client code
COPY . /reactfrontend/
EXPOSE 3000
CMD npm start
这是我的docker-compose.yml
文件
version: '3.2'
services:
db:
image: postgres:10.1-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
web:
build: ./djangobackend
command: python /djangobackend/backend/manage.py runserver 0.0.0.0:8000
volumes:
- .:/djangobackend
ports:
- 8000:8000
depends_on:
- db # it means, it will execuate after db is done
frontend:
build: ./reactfrontend
command: npm run build
volumes:
- .:/reactfrontend
ports:
- "3000:3000"
depends_on:
- web
volumes:
postgres_data:
任何人都可以告诉我上述代码无法在React APP中正常工作吗?
我已经成功运行命令docker build .
,但是如果我运行docker-compose.yml
,则会抛出错误:ERROR: build path /home/pyking/Desktop/drdn/djangobackend either does not exist, is not accessible, or is not a valid URL.
我不明白为什么当我在docker文件中添加一些代码进行React时会引发此错误,但是如果我不添加react的代码就不会引发错误,有人可以帮我将我的Django react app Docker化吗?
答案 0 :(得分:0)
web:
build: ./djangobackend
command: python /djangobackend/backend/manage.py runserver 0.0.0.0:8000
volumes:
- .:/djangobackend
由于Docker撰写中的volumes
导致了错误。
复制项目时
COPY . /djangobackend/
在Dockerfile中,然后为什么需要在Docker compose中添加volumes
。
删除Docker中的卷。
或将脚本的路径更新为
/djangobackend/djangobackend/backend/manage.py
在docker-compose中使用.
复制时