Docker Compose Django,Webpack和构建静态文件

4

我正在尝试弄清楚如何在生产构建期间使用Node和Webpack构建静态文件,并将它们作为卷挂载,以供Django Web应用程序使用并用于Django collectstatic。

我已经将所有服务分离到各自的容器中,并且每个容器都有自己的Dockerfile。

目前的问题是,我无法在Django应用程序中访问Webpack生成的访问文件。问题是,我是否可以使用单独的Node和Django Dockerfile来实现这一点,还是应该在一个Dockerfile中完成?

Node Dockerfile

FROM node:alpine
WORKDIR ./code
COPY ./package.json ./yarn.lock /code/
COPY ./webpack.base.config.js ./webpack.prod.config.js /code/
RUN yarn install --production
ADD static /code/static/
RUN yarn run prod

Python应用程序Dockerfile

FROM python:3.6.2-alpine
ENV PYTHONUNBUFFERED 1

RUN apk update \
  && apk add \
    bash \
    curl \
    build-base \
    postgresql-dev \
    postgresql-client \
    libpq \
    tzdata

    WORKDIR /code
    ADD requirements.txt /code
    RUN pip install --upgrade pip
    RUN pip install -r requirements.txt
    ADD ./ /code
    ENV TZ=Europe/London

    EXPOSE 8000

Docker Compose 用于生产环境

version: '3'

services:

  frontend:
    build: docker/services/node
    volumes:
      - static_files:/code/static

  webapp:
    build: .
    env_file:
      - .env
    expose:
      - "8000"
    volumes:
      - ./public:/code/public/
      - static_files:/code/static
    command: ["./docker/scripts/wait-for-it.sh", "database:5432", "--", "./docker/services/webapp/run-prod.sh"]
    depends_on:
      - frontend
      - database

  database:
    image: postgres
    env_file:
      - .env
    expose:
      - "5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data/

  nginx:
    build: docker/services/nginx
    env_file:
      - .env
    ports:
      - "80:80"
    volumes:
      - ./public:/www/public/
    depends_on:
      - webapp
    healthcheck:
      test: ["CMD", "curl", "-f", "http://0.0.0.0:8000"]
      interval: 30s
      timeout: 10s
      retries: 3

volumes:
  postgres_data:
  static_files:
1个回答

2
您可以使用多阶段构建来完成此操作。在您的情况下,第一阶段将生成静态文件,而第二阶段将打包您的Python应用程序并从node.js Docker镜像中复制静态文件。生成的映像将仅包含Python依赖项。
以下是多阶段Dockerfile,文档可在此处找到:https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
# static files build stage
FROM node:alpine as static # named as static for easy reference
WORKDIR /code
COPY ./package.json ./yarn.lock /code/
COPY ./webpack.base.config.js ./webpack.prod.config.js /code/
RUN yarn install --production
ADD static /code/static/
RUN yarn run prod

# python app package stage 
FROM python:3.6.2-alpine as final # named as final because it's final :)
ENV PYTHONUNBUFFERED 1

RUN apk update \
  && apk add \
    bash \
    curl \
    build-base \
    postgresql-dev \
    postgresql-client \
    libpq \
    tzdata

WORKDIR /code
ADD requirements.txt /code
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ADD ./ /code
# This next command has access to the file contents of the previous stage. 
# Ideally you should rewrite the paths to copy the static files from where they have been generated to where they should end up
# The 'from' clause is used to reference the first build stage 
COPY --from=static /code/static/path/to/static/files /code/desired/location


ENV TZ=Europe/London

EXPOSE 8000

您可以在docker-compose文件中使用此单个映像。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接