Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

nginx - Docker: how to set up proxying?

I need to set up proxying between docker containers. In my variations, the containers are accessible from the host machine, but they cannot see each other.

Check the availability of containers via curl.

What am I doing wrong? How is it correct?

docker-compose.yml

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;
    # }
}

I ask for help in this matter, because I'm a beginner. If its ready-made configuration for my task, please provide.

question from:https://stackoverflow.com/questions/65905292/docker-how-to-set-up-proxying

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First, check if the ICC (Inter Container Communication) flag is set to true. If it's true check if the containers are in the same network.

  1. List the network:
docker network ls
  1. Inspect the containers in the network:
docker network inspect <network_name> -f "{{json .Containers }}"
  1. If they are on different networks, then remove the container from one network and move it in the other network using:
docker network disconnect <network_name_1> <container_name>

docker network connect <network_name_2> <container_name>

If you don't want containers in the same network you can bridge the network or create the link between the containers using:

docker run --name <container_name> -p <port_ext>:<port_in> -d --network <network_1> <network_2>

You can also add links in your docker-compose file:

links:
  - "<network_1>"
  - "<network_2>"

Reads: How to link containers in docker?, How to find the network my container is in?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...