M
M
Millerish2021-01-26 19:25:45
Web development
Millerish, 2021-01-26 19:25:45

Docker: how to set up proxying?

I need to set up proxying between docker containers. In my variation of the container, they are accessible from the host machine, but they do not see each other. What am I doing wrong? How right?

version: "2.4"
services:
  nginx:
    image: nginx:1.17.2-alpine
    container_name: nginx
    volumes:
      - ./default.nginx:/etc/nginx/conf.d/default.conf
    ports:
      - 8989:8989

  frontend:
    container_name: frontend
    environment:
      - VIRTUAL_HOST=localhost:3000
    build:
      context: ./frontend
      dockerfile: Dockerfile
    ports:
      - 3000:3000

  backend:
    container_name: backend
    environment:
      - VIRTUAL_HOST=localhost:1337
    build:
      context: ./backend
      dockerfile: Dockerfile
    ports:
      - 1337:1337


front
FROM keymetrics/pm2:latest-alpine

ENV NPM_CONFIG_LOGLEVEL error

WORKDIR /usr/src/app

RUN apk update && apk upgrade && apk add --no-cache bash git openssh
RUN apk --no-cache add curl

COPY . .

RUN yarn install
RUN yarn build:ssr

EXPOSE 3000 1337

CMD [ "pm2-runtime", "start", "pm2.json" ]</blockquote>

back 
<blockquote>FROM strapi/base:latest

RUN yarn global add strapi

RUN mkdir /srv/app && chown 1000:1000 -R /srv/app
RUN apk --no-cache add curl

WORKDIR /srv/app

EXPOSE 1337 3000

COPY . .

RUN yarn

# CMD ["strapi", "develop"]
CMD ["strapi", "start"]


default.nginx
server {
    listen 8989;
    server_name localhost;

    location / {
        proxy_pass       http://frontend:3000;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location localhost:3000 {
        proxy_pass       http://frontend:3000;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    location localhost:3000/graphql {
        proxy_pass       http://backend:1337/graphql;
#         proxy_pass       http://backend:1337;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
      location ^~/graphql {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Frame-Options SAMEORIGIN;

#         proxy_pass http://backend:1337/graphql;
        proxy_pass http://localhost:1337/graphql;

      }

    location localhost:1337 {
        proxy_pass       http://backend:1337;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
    }


    # location ~ /\.ht {
    #     deny  all;
    # }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Millerish, 2021-01-27
@Millerish

Posted the correct configs on github:
https://github.com/StekolschikovV/Docker-nginx-str...

M
Mikhail Vasilyev, 2021-01-27
@vasilyevmn

Read here:
https://docs.docker.com/compose/networking/
In short, add to each container:

network:
    - mynetwork

And register this network:
networks:
  mynetwork:

D
D3lphi, 2021-01-26
@D3lphi

In docker-compose.yml for nginx, you need to add a dependency on other containers:

nginx:
    #...
    links:
        - frontend
        - backend

Thus, inside the nginx container, the frontend and backend domains will become available and you can refer to them in the nginx configuration.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question