2

I have a Dockerfile with a multi-stage build in which I'm trying to run collectstatic to get all the static files and copy them into the final docker image. At this moment I don't have a database to access, but as collectstatic is trying to access one with dummy values, I get an error.

In my opinion there should be no need for collectstatic to access a database. I have read through some Issues concerning this and I think that the maintainers of django are not planning on changing this. That's why I need to know if there is any way to prevent this access to a database.

Here's what I have:

ARG baseImage
FROM ${baseImage} AS base

USER root

ENV DB_HOST="example" \
    DB_NAME="example" \
    DB_USER="example" \
    DB_PASSWORD="example"

RUN python app/manage.py collectstatic --skip-checks --noinput --clear \
    && node_modules/.bin/gulp compile --destination "/tmp/staticcmp"


FROM nginx:1.11 AS cdn

COPY docker/cdn/etc/cdn-nginx.conf /etc/nginx/conf.d/default.template
COPY docker/cdn/robots.txt /usr/share/nginx/html/robots.txt
COPY docker/cdn/bin/ /usr/local/bin/
COPY --from=base /tmp/staticcmp/ /usr/share/nginx/html/static

ENV NGINX_ERROR_LOG="/dev/stdout" \
    NGINX_ACCESS_LOG="/dev/stdout"

EXPOSE 8000

ENTRYPOINT ["docker-cdn-entrypoint"]
GiftZwergrapper
  • 2,602
  • 2
  • 20
  • 40
  • 1
    manage.py commands will always check the db. But all collectstatic does is copy files from the source dirs to the output directory - you could easily implement that yourself either as a script or even directly in your dockerfile. – Daniel Roseman Nov 27 '19 at 08:32
  • @DanielRoseman That is not entirely true. `collectstatic` does additional work based on the `STATICFILES_STORAGE`. In my case this is `ManifestStaticFilesStorage` which adds copies with content hashing and resolves static paths in templates and in scss imports. If it would only copy files I would have written my own script. – GiftZwergrapper Dec 13 '19 at 10:58

1 Answers1

0

Unfortunately there is no way to prevent collectstatic from trying to access the database. Using a dummy database in certain situations could solve your problem. If this doesn't help you will probably have to implement it yourself.

  • I tried to use a dummy database but that leads to other problems. After that I implemented a similar functionality with different scripts. – GiftZwergrapper Apr 07 '20 at 08:09