No static files after deploying Django app

I've deployed a multi container Django application to AWS EB with ECS running on 64bit Amazon Linux 2/3.2.3 platform. Here's the Dockerrun.aws.json file.

{
    "AWSEBDockerrunVersion": "2",
    "containerDefinitions": [
      {
        "essential": true,
        "image": "${AWS_ACOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${IMAGE_NAME}:${IMAGE_TAG}",
        "mountPoints": [
          {
            "containerPath": "/code/static",
            "sourceVolume": "web"
          }
        ],
        "name": "web",
        "hostname": "web",
        "memoryReservation": 1200,
        "portMappings": [
          {
            "containerPort": 8000,
            "hostPort": 8000
          }
        ]
      },
      {
        "name": "nginx-proxy",
        "image": "nginx",
        "essential": true,
        "memoryReservation": 128,
        "portMappings": [
          {
            "containerPort": 80,
            "hostPort": 80
          }
        ],
        "mountPoints": [
          {
            "sourceVolume": "nginx-proxy-conf",
            "containerPath": "/etc/nginx/nginx.conf"
          }
        ],
        "links": ["web"]
      }

    ],
    "volumes": [
      {
        "name": "web"
      },
      {
        "name": "nginx-proxy-conf",
        "host": {
          "sourcePath": "/var/app/current/nginx.conf"
      }
    }
    ]
}

Here's the nginx.conf

user                    nginx;
events {
    worker_connections        1024;
}
http {
    server {
        listen 80;
        location /static/ {
            alias /static/;
        }

        location / {
            proxy_pass http://web:8000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }
    }
}

This is the Dockerfile.

FROM python:3.10

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN apt-get update
RUN apt-get upgrade -y

COPY ./requirements.txt requirements.txt
RUN apt-get install -y realmd
RUN pip install -r requirements.txt

WORKDIR /code

EXPOSE 8000

COPY . .

RUN chmod +x entrypoint.sh
ENTRYPOINT ["./entrypoint.sh"]

and the entrypoint.sh

#!/bin/bash
python manage.py migrate --noinput >/tmp/migrate 2> /tmp/migrate_err

python manage.py collectstatic --no-input

yum install realmd

gunicorn \
    --bind 0.0.0.0:8000 \
    pos.wsgi:application

So CodePipieline builds the image and deploys to the EB(with t3.small EC2). After deploying the status is "OK", when I enter link generated by EB and add /admin at the end I can see the admin panel but without styles.[![enter image description here][1]][1]. Now the django app is connected to an RDS db and when I try to login to the admin panel I get 504 Gateway Time-out or 502 Bad Gateway. I've tried to add or remove some settings from the nginx.conf but still can't see the static files. When I exec to the Docker container after ssh ing to the EC2 instance I can see the /static/ directory.
I have these lines in my settings.py

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "static")

Let me know if there's anything else to share. [1]: https://i.stack.imgur.com/cyA3z.png

You did not specify it clearly but If your static files are not on nginx image, those will not be served correctly.

There are two option to fix it.

  • map static folder between containers (similar nginx.conf)
  • Remove /static/ location from nginx.conf, pass static files requests to proxied upstream (django container).

I think you not collect Django defaults statics file

Setps:

 - run command python manage.py collectstatic

 - urlpatterns = [
    path("", include("myapp.urls")),
    path('admin/', admin.site.urls),

]

urlpatterns+=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns+=static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Back to Top