未找到:/static/frontend/styles.css(Docker、Django、Nginx)

问题描述 投票:0回答:0

我第一次尝试在生产环境中部署 docker,我遵循了一些 youtube 教程,但我成功地容器化了我的 Django 项目。但是当我运行它时,什么也没有出现,并且出现 404 错误,提示找不到 static/fronted/styles.CSS。请参阅随附的片段:

错误:

dlops-portal | Not Found: /static/frontend/styles.css
dlops-portal | Not Found: /static/frontend/runtime.js
dlops-portal | Not Found: /static/frontend/polyfills.js
dlops-portal | Not Found: /static/frontend/main.js
dlops-portal | Not Found: /static/frontend/scripts.js

这是我的 Docker 文件:

    FROM nikolaik/python-nodejs:python3.8-nodejs14-bullseye

    ENV PYTHONUNBUFFERED 1

    ENV LC_ALL C.UTF-8
    ENV LANG C.UTF-8

    RUN python3 -m pip install --upgrade pip
    RUN set -ex && mkdir /app

    WORKDIR /app

    # Copy the requirements file and install dependencies
    COPY d_requirements.txt /app/
    RUN pip install -r d_requirements.txt

    # Copy the application code
    COPY . /app/

    # Create the /opt/ai_sensei_data_source directory and set permissions
    RUN mkdir /opt/ai_sensei_data_source
    WORKDIR /opt
    RUN chmod a+x+w+r ai_sensei_data_source

    # Copy the .env file to /opt/ai_sensei_data_source/
    COPY /.env /opt/ai_sensei_data_source/.env

    # Change back to the frontend directory to install frontend dependencies
    WORKDIR /app/frontend

    # Install frontend dependencies
    RUN npm install -f
    RUN npm install -g @angular/cli
    RUN ng build --configuration production

    # Copy build static files to nginx conf dir & set permissions
    RUN mkdir -p /home/kchavan/Desktop/dlops-portal/static
    RUN cp -r /app/static/* /home/kchavan/Desktop/dlops-portal/static/
    # # RUN chown -R <user>:<group> /home/kchavan/Desktop/dlops-portal/ && \
    # #     chmod -R 755 /home/kchavan/Desktop/dlops-portal/

    # Change back to the /app directory as the final working directory
    WORKDIR /app

    # Install additional packages
    RUN apt-get update
    RUN apt-get install rsync -y
    RUN apt-get install lftp -y
    RUN apt-get install sshpass -y
    RUN apt-get install -y --no-install-recommends nano vim


    # Copy entrypoint script
    COPY ./entrypoint.sh /

    # Set entrypoint
    ENTRYPOINT [ "sh", "/entrypoint.sh" ]`

Docker 撰写文件:

    version: "3"

    services:
      web:
        build: .
        volumes:
          - static:/static
          - /home/data_source:/opt/ai_sensei_data_source/data_source
          - /home/.ssh:/home/.ssh

        container_name: "dlops-portal"
        ports:
          - "9000:8000"
        environment:
          - /opt/ai_sensei_data_source/.env 
        depends_on:
          - db
          - redis
      nginx:
        build: ./nginx
        volumes:
          - static:/static 
        ports:
          - "9073:80"
        container_name: "nginx-container"
        depends_on:
          - web
      db:
        image: postgres:12
        volumes:
          - /home/postgres_ai_sensei:/var/lib/postgresql/data
        environment:
          - /opt/ai_sensei_data_source/.env
          - POSTGRES_USER=pass
          - POSTGRES_PASSWORD=pass
      
        ports:
          - "5434:5432"
        container_name: "myapp-postgres-db"
        depends_on:
          - redis
      redis:
        image: redis:latest
        ports:
          - "6378:6379"
        container_name: "redis-dlops"
        environment:
          - /opt/ai_sensei_data_source/.env
    volumes:
      static:    `

我的入口点.sh 文件:


    #!/bin/bash
    python3 manage.py migrate --no-input
    python3 manage.py collectstatic --no-input 
    gunicorn dlops.wsgi:application --bind 0.0.0.0:8000 &
    celery multi start worker8 worker9 worker10 worker11 worker12 worker13 worker14 worker15            worker16 worker17 -A dlops -l info &
    celery -A dlops worker -l info 

Nginx docker 文件:

    `FROM nginx:1.21

    RUN rm /etc/nginx/conf.d/default.conf
    COPY ./nginx.conf /etc/nginx/conf.d/`




nginx conf file:


    upstream dlops {

        server web:8000;
    }

    server {

        listen 80;
    #  server_name
    #  error_log    
    #  error_page

        root /home/kchavan/dlops-portal/static/;

        location /static/{
            alias /static/;
            expires 1d; 
        }
        location / {
            proxy_pass http://dlops;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_redirect off;
    }

    }
`

我的settings.py文件:

    STATIC_ROOT = '/static/'

    STATIC_URL = '/static/'
    MEDIA_ROOT = 'Media/'
    MEDIA_URL = "/media/"
    media_directory = os.path.join(BASE_DIR, 'Media')
    os.makedirs(media_directory, exist_ok=True)

    ASSETS_ROOT = os.path.join(BASE_DIR, 'static/frontend/assets')
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'),
        os.path.join(BASE_DIR, 'assets')
    ]`

文件路径:

`  >project
    >backend
      >settings.py
    >frontend
    >nginx
      >dockerfile
      >nginx.conf
    >static
      >frontend
      >mail
    >dockerfile
    >dockercompose
    >entrypoint.sh`

我尝试在构建图像时将静态文件夹从本地系统复制到 nginx 容器,请参阅以下代码:

Docker 文件:

    `# Copy build static files to nginx conf dir & set permissions
    RUN mkdir -p /home/kchavan/Desktop/dlops-portal/static
    RUN cp -r /app/static/* /home/kchavan/Desktop/dlops-portal/static/`

但是没有成功

django dockerfile nginx-reverse-proxy nginx-location
© www.soinside.com 2019 - 2024. All rights reserved.