I want to include static files generated from python manage.py collectstatic
in the Docker image.
For this, I included the following line in my Dockerfile
CMD python manage.py collectstatic --no-input
But since it runs the command in an intermediate container, the generated static files aren't present STATIC_ROOT
directory. The following lines I can see on the logs of build.
Step 13/14 : CMD python manage.py collectstatic --no-input
---> Running in 8ea5efada461
Removing intermediate container 8ea5efada461
---> 67aef71cc7b6
I'd like to include the generated static files in the image. What shall I do to achieve this?
I was using CMD but instead, I should use RUN command for this task as the docs say
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.
You need to copy the output of collectstatic
into your final container.
For example, my dockerfile contains the same concept (this isn't the complete dockerfile, just the relevant pieces)
# 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