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

Viewed 407

I am working on my AWS EC2 instance, and now dockerising my applicaiton to get it ready for prod. I have a Django api for the backend and React frontend. The aim is to use gunicorn for the backend to serve the api and nginx to serve the react static files. I have got 2 docker files one for the backend and one for the frontend. The backend is working with gunicorn in the docker and also waorks with docker-compose.

I have been struggling with the frontend one. The docker itself will run with npm start(not with nginx), however refuses to work with docker-compose, could you please advise based on the code below what I need to correct? (as I was also writing this post I found that the docker copose for the frontend by itself does not end up launching)

After this I will try to get get it working with nginx again so won't be using npm start in prod.

Additionally, I would like to know how I can share the .sock file created by gunicorn to be able to be used by nginx in the 2 separate dockers. If this is not the correct way to connect my frontend and backend, please advise the best approach as it would be highly appreciated.

I have tried: - a variety of additons and changes to the docker compose including adding a depends_on to the frontend servcie to depend on the backend. - a combination of different solutions to have the same network and volumes and don't think it has helped in anyway yet.

output in terminal:

(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 file

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 stackoverflow 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"]

Below file is working fine so so shouldnt need editing, I'm providng the file so it makes sense in the context.

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"] 
1 Answers

In short, the fix for this was running docker-compose build due to the fact OP is building the containers via a dockerfile inside of the docker-compose.yml file, and not just 'turning images on' via docker-compose.

Related