mod_wsgi-docker icon indicating copy to clipboard operation
mod_wsgi-docker copied to clipboard

static files

Open evdel opened this issue 7 years ago • 16 comments

The static files of my Django application are not served.

evdel avatar Dec 08 '16 10:12 evdel

More information is required.

What do you have in your Dockerfile?

Have you added a build action hook? As what path in the source code? Is the file executable? What is in the file?

Has Django settings module had STATIC_ROOT set up and to what?

The output from doing the docker build would also be useful.

GrahamDumpleton avatar Dec 08 '16 20:12 GrahamDumpleton

Here is the content of my Dockerfile :

FROM grahamdumpleton/mod-wsgi-docker:python-2.7-onbuild CMD [ "stcypvacances/wsgi.py" ]

stcypvacances is the name of my Django project.

I don't know what a build action hook is.

In my Django settings.py I have :

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

The output of the build is :

Sending build context to Docker daemon 18.34 MB Step 1 : FROM grahamdumpleton/mod-wsgi-docker:python-2.7-onbuild Executing 2 build triggers... Step 1 : COPY . /app Step 1 : RUN mod_wsgi-docker-build ---> Running in b05a68794e50 -----> Installing dependencies with pip Collecting Django (from -r requirements.txt (line 1)) Downloading Django-1.10.4-py2.py3-none-any.whl (6.8MB) Collecting psycopg2 (from -r requirements.txt (line 2)) Downloading psycopg2-2.6.2.tar.gz (376kB) Installing collected packages: Django, psycopg2 Running setup.py install for psycopg2: started Running setup.py install for psycopg2: finished with status 'done' Successfully installed Django-1.10.4 psycopg2-2.6.2 You are using pip version 8.1.2, however version 9.0.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command. ---> a18c895ddd31 Removing intermediate container 24a166ac2567 Removing intermediate container b05a68794e50 Step 2 : CMD stcypvacances/wsgi.py ---> Running in 17240ba24686 ---> 056efb6a34a3 Removing intermediate container 17240ba24686 Successfully built 056efb6a34a3

evdel avatar Dec 08 '16 22:12 evdel

If you haven't already, look at:

  • https://github.com/GrahamDumpleton/mod_wsgi-docker/tree/master/demos/django-1.7

This shows what you would need for Django. The primary things are:

  • .whiskey/action_hooks/build (executable shell script) - https://github.com/GrahamDumpleton/mod_wsgi-docker/blob/master/demos/django-1.7/.whiskey/action_hooks/build - This is so that collectstatic is run.
  • .whiskey/server_args - https://github.com/GrahamDumpleton/mod_wsgi-docker/blob/master/demos/django-1.7/.whiskey/server_args - This sets additional arguments so that mod_wsgi-express knows where Django is and where static files are.
  • settings.py - https://github.com/GrahamDumpleton/mod_wsgi-docker/blob/master/demos/django-1.7/example/example/settings.py#L83 - Need to set STATIC_ROOT. Location needs to match path used in server_args.
  • Dockerfile - https://github.com/GrahamDumpleton/mod_wsgi-docker/blob/master/demos/django-1.7/Dockerfile - Done differently to how you are doing it.

Just note that I have the Django project pushed down into a sub directory. You would need to adjust some of the values to match what you have.

Now the question is whether you want to still use mod_wsgi-docker image as I have a newer image which is superseding this one. There isn't an official 1.0 version yet, but you still may be interested in it. Suggest you watch:

  • https://www.youtube.com/watch?v=y_vwvqgRZK0

I have also posted about it on my blog and there is:

  • http://warpdrive.readthedocs.io

I will be ramping up documentation and getting a 1.0 release done in the next month or so.

GrahamDumpleton avatar Dec 08 '16 23:12 GrahamDumpleton

I cloned the github.com/GrahamDumpleton/mod_wsgi-docker repo. Then I went to mod_wsgi-docker/demos/django-1.7 I ran sudo docker build . -t grahamdumpleton Docker said : Sending build context to Docker daemon 567.3 kB Step 1 : FROM grahamdumpleton/mod-wsgi-docker:python-2.7-onbuild Executing 2 build triggers... Step 1 : COPY . /app ---> Using cache Step 1 : RUN mod_wsgi-docker-build ---> Using cache ---> 9cdcd6d9c6fa Step 2 : USER $MOD_WSGI_USER:$MOD_WSGI_GROUP ---> Using cache ---> 2e073303b49c Successfully built 2e073303b49c Then I ran sudo docker run -it --rm -p 8001:80 --name grahamdumpletontest grahamdumpleton And Docker said : Server URL : http://localhost/ Server Root : /tmp/mod_wsgi-localhost:80:1001 Server Conf : /tmp/mod_wsgi-localhost:80:1001/httpd.conf Error Log File : /dev/stderr (warn) Startup Log File : /dev/stderr Request Capacity : 5 (1 process * 5 threads) Request Timeout : 60 (seconds) Startup Timeout : 15 (seconds) Queue Backlog : 100 (connections) Queue Timeout : 45 (seconds) Server Capacity : 20 (event/worker), 20 (prefork) Server Backlog : 500 (connections) Locale Setting : en_US.UTF-8 (13)Permission denied: AH00072: make_sock: could not bind to address [::]:80 (13)Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:80 no listening sockets available, shutting down AH00015: Unable to open logs

evdel avatar Dec 09 '16 12:12 evdel

Take out:

USER $MOD_WSGI_USER:$MOD_WSGI_GROUP

GrahamDumpleton avatar Dec 10 '16 06:12 GrahamDumpleton

Nice ! It worked ! I am happy.

evdel avatar Dec 10 '16 11:12 evdel

I think this has to be in the documentation.

evdel avatar Dec 10 '16 11:12 evdel

As I said already, this image is being superseded. So am investing time in the replacement instead.

I also don't understand why it didn't work with the USER statement at this point. The only thing I can think of right now is that your Docker system was set up to disallow certain Linux capabilities for images when run.

GrahamDumpleton avatar Dec 10 '16 19:12 GrahamDumpleton

Is there a clean way to run collectstatic when the container starts (not at build)? I would like to have a single image that has multiple django settings files (e.g. dev, qa, prod). The result of collectstatic is going to be slightly different depending on the settings file (i.e CDN location). Right now, I can run collectstatic with specified settings file with "docker exec" after the container starts, but wondering if there's a better way.

defmikekoh avatar Feb 17 '17 21:02 defmikekoh

Create an executable shell script at .whiskey/action_hooks/deploy in the repository and add the command in there. That script is run after the container is started, but before your application is started.

GrahamDumpleton avatar Feb 17 '17 21:02 GrahamDumpleton

Hi Graham,

Is the new version in docker hub?

I'm having trouble figuring out where to put static files for / so that they are served from apache directly. I tried adding a second --url-alias / frontend/ but then nothing goes to django. What I want to do is roughly:

  • if frontend$path is a file, serve it
  • else, send to django

i know how to do this with a .htaccess file but i'm a bit lost going through /tmp/mod_wsgi-localhost:80:0 . I found MOD_WSGI_STATIC_ONLY but I'm not sure how to use it.

Thanks, Jayen

jayenashar avatar Apr 09 '17 21:04 jayenashar

Use --document-root frontend.

This will set DocumentRoot to be that directory and it will be checked first for static files before routing anything else through to Django.

GrahamDumpleton avatar Apr 09 '17 21:04 GrahamDumpleton

Note that when running Django, you would only use this for things like favicon.ico etc where they have to be at root. Usually Django static files are at a sub directory set separately using --url-alias /static some/path/static.

GrahamDumpleton avatar Apr 09 '17 21:04 GrahamDumpleton

I've used react to build /, and I'm using django only for /rest and /admin (though both of those use /static). Looks like I need to move the /static react is using to something else, though otherwise --document-root does what I want. Thanks.

jayenashar avatar Apr 09 '17 21:04 jayenashar

Hi Graham, thanks for these docker images. I know this is an old thread but I have a somewhat related question.

Suppose you have --document-root frontend in .whiskey/server_args and the frontend directory has a directory called static. Let's say you have the mod_wsgi container running, and then you change those static assets (within frontend/static/), will the server reload those? AFAICT this requires restarting the container.

how can we specify that mod_wsgi reloads the static assets if they change or disable the caching of those static assets (if they are cached..)? (this is for development)

wgwz avatar Mar 20 '18 21:03 wgwz

There is no caching setup in Apache when used with mod_wsgi-express. Static files are always served direct off disk, so a restart/reload of Apache should not be required for changes to existing static files, or new files, to be visible.

GrahamDumpleton avatar Mar 21 '18 02:03 GrahamDumpleton