从 Docker 镜像的 django 集合部分制作静态文件



我想在Docker镜像中包含从python manage.py collectstatic生成的静态文件。

为此,我在Dockerfile中包含了以下行

CMD python manage.py collectstatic --no-input

但由于它在中间容器中运行命令,因此生成的静态文件不存在于STATIC_ROOT目录中。我可以在构建日志上看到以下几行。

Step 13/14 : CMD python manage.py collectstatic --no-input
---> Running in 8ea5efada461
Removing intermediate container 8ea5efada461
---> 67aef71cc7b6

我想在图像中包括生成的静态文件。我该怎么做才能做到这一点?

更新(解决方案(

我使用的是CMD,但相反,我应该使用RUN命令执行此任务,正如文档所说的

The RUN instruction will execute any commands in a new layer on top of the current image and commit the results. The resulting committed image will be used for the next step in the Dockerfile.

您需要将collectstatic的输出复制到您的最终容器中。

例如,我的dockerfile包含相同的概念(这不是完整的Dockerfle,只是相关的部分(

# Pull base image
FROM python:3.7.7-slim-buster AS python-base
COPY requirements.txt /requirements.txt
WORKDIR /project
RUN apt-get update && 
apt-get -y upgrade && 
pip install --upgrade pip && 
pip install -r /requirements.txt
FROM node:8 AS frontend-deps-npm
WORKDIR /
COPY ./package.json /package.json
RUN npm install
COPY . /app
WORKDIR /app
RUN /node_modules/gulp/bin/gulp.js

FROM python-base AS frontend-deps
COPY --from=frontend-deps-npm /app /app
WORKDIR /app
RUN python manage.py collectstatic -v 2 --noinput

FROM python-base AS app
COPY . /app
COPY --from=frontend-deps /app/static-collection /app/static-collection

最新更新