0

I am trying to develop a web application using Django in Docker.

I made a DockerFile and a docker-compose.yml file as below to check the code in the development environment.

Even though I'm using the runserver command in the file, when I start the container and access the localhost, the CSS for the admin page isn't working.

enter image description here

How should I change the codes to make it work?


Here are the codes:

Dockerfile

FROM python:3.8.3-alpine

WORKDIR /usr/src/app

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN pip install --upgrade pip

COPY ./requirements.txt .

RUN pip install -r requirements.txt

COPY . .

docker-compose.yml

version: '3.7'

services:
  web:
    build: ./django_project
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - ./django_project/:/usr/src/app/

    ports:
      - 8000:8000

docker desktop: 4.1.1

Python: 3.8

Django: 3.0

Tio
  • 944
  • 3
  • 15
  • 35
  • Have you run `./manage.py collectstatic`? – STerliakov Jul 02 '22 at 17:02
  • @ SUTerliakov, Yes, I've already tried it, but CSS doesn't work – Tio Jul 02 '22 at 17:05
  • Well, it is slightly difficult to debug. At least provide your folder structure. Have a look at [deployment guideline](https://docs.djangoproject.com/en/4.0/howto/deployment/) and [don't use `runserver` to deploy](https://docs.djangoproject.com/en/4.0/ref/django-admin/#runserver) – STerliakov Jul 02 '22 at 17:07
  • The `volumes:` block hides everything in your image, so if you do any setup there it will be lost. You shouldn't need a Compose `command:` override normally either; the Dockerfile `CMD` should be sufficient. Do you see the browser application trying to fetch the CSS file; can you make the same request with a command-line tool like `curl`? Is there anything interesting in the container logs? If you do inspect the container filesystem, is the CSS file there somewhere? – David Maze Jul 02 '22 at 17:11
  • Does this answer your question? [Docker Django 404 for web static files, but fine for admin static files](https://stackoverflow.com/questions/38627393/docker-django-404-for-web-static-files-but-fine-for-admin-static-files) – tbhaxor Jul 02 '22 at 17:45
  • Does it work without containerization? Like with `runserver` on a host machine, without docker. – Ivan Starostin Jul 03 '22 at 09:00
  • @Ivan Starostin, Yes when I use `runserver` without docker css works well. – Tio Jul 03 '22 at 13:38
  • Please show your folder structure as already asked above and settings.py part related to the static files. Also please grab generated static file url from rendered page when running in docker container and put this url into browser address bar, open it manually and add specific http status/ error message to your question too. – Ivan Starostin Jul 03 '22 at 17:59

0 Answers0