在Django React应用中运行Docker容器时找不到静态文件

时间:2020-09-15 21:16:01

标签: reactjs django docker dockerfile gunicorn

我试图运行我的Docker映像以在浏览器上看到它,但出现的只是一个空白屏幕,经检查,我看到以下错误:

[2020-09-12 17:31:42 +0000] [12] [INFO] Starting gunicorn 20.0.4
[2020-09-12 17:31:42 +0000] [12] [INFO] Listening at: http://0.0.0.0:8000 (12)
[2020-09-12 17:31:42 +0000] [12] [INFO] Using worker: sync
[2020-09-12 17:31:42 +0000] [14] [INFO] Booting worker with pid: 14
Not Found: /static/js/2.8ba00362.chunk.js
Not Found: /static/js/main.8eee128b.chunk.js
Not Found: /static/css/2.7253a256.chunk.css
Not Found: /static/css/main.a85cbe2c.chunk.css

我运行的命令是: docker build . -t projecthubdocker run -d -p 8000:8000 --name theprojecthub projecthub:latest

我非常不确定为什么还要寻找这些静态文件,我似乎无法在我的代码中找到对其的任何引用。有人知道我该怎么做吗?

这是我的Dockerfile:

FROM myfriendsaccount/django-npm:latest

RUN mkdir usr/src/app
WORKDIR /usr/src/app

COPY frontend ./frontend
COPY backend ./backend

WORKDIR /usr/src/app/frontend

RUN npm install
RUN npm run build

WORKDIR /usr/src/app/backend

ENV DJANGO_PRODUCTION=True
RUN pip3 install -r requirements.txt

EXPOSE 8000

CMD python3 manage.py collectstatic && \
    python3 manage.py makemigrations && \
    python3 manage.py migrate && \
    gunicorn project_hub.wsgi --bind 0.0.0.0:$PORT

以下是我的settings.py文件的相关部分:

DEBUG = True

if DEBUG:
    APP_URL = "http://localhost:8000/"
else:
    APP_URL = "https://" + PRODUCTION_URL + "/"

PRODUCTION_URL = 'mywebsite.com'

ALLOWED_HOSTS = ['http://localhost:3000', 'localhost', 'http://localhost:8000', '127.0.0.1', '[::1]', PRODUCTION_URL, '0.0.0.0']

...

CORS_ORIGIN_WHITELIST = [
    'http://localhost:3000', 'http://localhost:8000',
]

STATIC_URL = '/static/'

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

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')


这是我的frontend / build / static内部的内容:

frontend/build/static files

0 个答案:

没有答案