在此示例中,如何使用 docker-compose 允许 2 个容器正确共享 .sock 文件?

How do i use docker-compose to allow for 2 containers to share a .sock file correctly in this example?

我正在处理我的 AWS EC2 实例,现在 docker正在调整我的应用程序以使其为生产做好准备。我有一个用于后端和 React 前端的 Django api。目的是使用 gunicorn 作为后端服务于 api 和 nginx 服务于 react 静态文件。我有 2 个 docker 文件,一个用于后端,一个用于前端。后端在 docker 中与 gunicorn 一起工作,也与 docker-compose 一起工作。

我一直在为前端问题苦苦挣扎。 docker 本身会 运行 使用 npm start(而不是使用 nginx),但是拒绝使用 docker-compose,请您根据下面的代码提出我需要更正的建议? (因为我也在写这个 post 我发现 docker 前端的 copose 本身并没有最终启动)

在此之后,我将尝试让它再次与 nginx 一起工作,因此不会在产品中使用 npm start。

此外,我想知道如何共享 gunicorn 创建的 .sock 文件,以便 nginx 在 2 个单独的 docker 中使用。如果这不是连接我的前端和后端的正确方法,请提供最佳方法,我们将不胜感激。

我试过: - 对 docker 组合的各种添加和更改,包括向前端服务添加 depends_on 以依赖于后端。 - 具有相同网络和容量的不同解决方案的组合,并且认为它无论如何都没有帮助。

终端输出:

(project) ubuntu@XXXXXXXXXXX:~/django-react-app$ docker-compose up
Starting django-react-app_frontend_1 ... done
Starting django-react-app_backend_1  ... done
Attaching to django-react-app_frontend_1, django-react-app_backend_1
backend_1   | [2019-06-08 16:04:51 +0000] [1] [INFO] Starting gunicorn 19.9.0
backend_1   | [2019-06-08 16:04:51 +0000] [1] [INFO] Listening at: http://0.0.0.                               0:8000 (1)
backend_1   | [2019-06-08 16:04:51 +0000] [1] [INFO] Using worker: sync
backend_1   | [2019-06-08 16:04:51 +0000] [8] [INFO] Booting worker with pid: 8
backend_1   | /usr/local/lib/python3.7/site-packages/psycopg2/__init__.py:144: U                               serWarning: The psycopg2 wheel package will be renamed from release 2.8; in orde                               r to keep installing from binary please use "pip install psycopg2-binary" instea                               d. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-f                               rom-pypi>.
backend_1   |   """)

docker-compose.yml 文件

version: "3"
services:

  frontend:
    build:
      context: .
      dockerfile: frontend_docker
    ports:
      - "3000:3000"
    volumes:
      - codevolume:/code
    networks:
      - backend

  backend:
    build:
      context: .
      dockerfile: backend_docker
    ports:
      - "8000:8000"
    volumes:
      - codevolume:/code
    networks:
      - backend

volumes:
  codevolume: 
# Networks to be created to facilitate communication between containers
networks:
  backend: 

frontend_dockerfile

FROM node:11.15

USER root

SHELL ["/bin/bash", "-c"]

RUN apt-get update -y && \
 apt-get install -y nginx && \
 apt-get clean && \
 mkdir /code/ && \
 rm -rf /tmp/*

WORKDIR /code

COPY source/frontend/ /code/

RUN npm install && \
 npm cache clear --force && \
 rm -rf /tmp/*

RUN npm run-script build && \
 rm -rf /tmp/*

#will only be using one of these with nginx and removing the other once i resolve this Whosebug issue :)
EXPOSE 8000
EXPOSE 3000

COPY configs/nginx.conf /tmp/

RUN mv /tmp/nginx.conf /etc/nginx/sites-available/myreactfrontend && \
 ln -s /etc/nginx/sites-available/myreactfrontend /etc/nginx/sites-enabled

#will ideally be switching to nginx afterward, may even look into multi stage builds if people think this would be recommended
#ENTRYPOINT ["/bin/bash", "-c", "nginx -g 'daemon off;'"]
ENTRYPOINT ["/bin/bash", "-c", "npm start"]

下面的文件工作正常,所以不需要编辑,我提供文件以便在上下文中有意义。

backend_docker

FROM python:3

USER root

SHELL ["/bin/bash", "-c"]

RUN mkdir /code

WORKDIR /code

COPY requirements/base.txt /code/

COPY source/backend/ /code/

ENV PYTHONUNBUFFERED 1

ENV DJANGO_SETTINGS_MODULE=mydjangoapi.settings.base

RUN pip install -r base.txt && \
 python manage.py makemigrations && \
 python manage.py migrate

EXPOSE 8000

ENTRYPOINT ["/bin/bash", "-c", "gunicorn --bind 0.0.0.0:8000 mydjangoapi.wsgi"] 

简而言之,由于 OP 正在通过 docker-compose.yml 文件中的 dockerfile 构建容器,因此解决方案是 运行 docker-compose build,并且不仅仅是 'turning images on' 通过 docker-compose