如何在同一实例上从一个容器向另一个容器发出 http 请求

时间:2021-07-02 13:00:41

标签: javascript amazon-web-services docker dockerfile amazon-elastic-beanstalk

我在 aws beanstalk 中运行了一个多容器 docker 实例,我有 4 个容器在运行,一个 nginx 代理接收请求并将它们转发到正确的微服务和节点中接收这些请求的 3 个微服务。但是现在我需要一个微服务在本地调用另一个,这可能吗?我尝试了所有可能的配置,我在容器之间创建了一个链接,一个网络,但我总是收到以下错误

getaddrinfo ENOTFOUND http://user:5003

这里是实例的设置

Dockerrun.aws.json

{
  "AWSEBDockerrunVersion": 2,
  "containerDefinitions": [
    {
      "name": "auth",
      "image": "xxxxxx.amazonaws.com/auth-api",
      "hostname": "auth",
      "essential": false,
      "memory": 128,
      "links": [
        "profile",
        "user"
      ]
    },
    {
      "name": "profile",
      "image": "xxxxxx.amazonaws.com/profile-api",
      "hostname": "profile",
      "essential": false,
      "memory": 128
    },
    {
      "name": "user",
      "image": "xxxxxx.amazonaws.com/user-api",
      "hostname": "user",
      "essential": false,
      "memory": 128
    },
    {
      "name": "nginx",
      "image": "xxxxxx.amazonaws.com/nginx",
      "hostname": "nginx",
      "essential": true,
      "portMappings": [
        {
          "hostPort": 80,
          "containerPort": 80
        },
        {
          "hostPort": 443,
          "containerPort": 443
        }
      ],
      "links": [
        "auth",
        "profile",
        "user"
      ],
      "memory": 64
    }
  ]
}

docker-compose.yml

version: "3.8"

networks:
  isolation-network:
    driver: bridge

services:
  nginx:
    image:  ********.amazonaws.com/nginx
    restart: always
    build:
      context: ./nginx
    volumes:
      - ../etc/pki/tls/certs:/etc/nginx/certs  
    ports:
      - "80:80"
      - "443:443"
    networks:
      - isolation-network
  auth:
    image:  ********.amazonaws.com/auth-api
    build:
        context: ./auth-api
    volumes:
      - /app/node_modules
      - ./auth-api:/app
    ports:
      - "5001:5001"
    depends_on:
      - user
    networks:
      - isolation-network
  profile:
    image: ********.amazonaws.com/profile-api
    build:
        context: ./profile-api
    volumes:
      - /app/node_modules
      - ./profile-api:/app
    ports:
      - "5002:5002"
    networks:
      - isolation-network
  user:
    image: ********.amazonaws.com/user-api
    build:
        context: ./user-api
    volumes:
      - /app/node_modules
      - ./user-api:/app
    ports:
      - "5003:5003"
    networks:
      - isolation-network

Dockerfile(身份验证微服务)

FROM **********.amazonaws.com/node-image

WORKDIR /usr/src/app

COPY package*.json ./
COPY tsconfig.json ./

COPY src /usr/src/app/src

RUN ls -a

RUN npm install
RUN npm run build

EXPOSE 5001

CMD [ "node", "./dist/server.js" ]

Dockerfile(用户微服务)

FROM ******.amazonaws.com/node-image

RUN apk update

WORKDIR /usr/src/app

COPY package*.json ./
COPY tsconfig.json ./

COPY src /usr/src/app/src

# WORKDIR /usr/src/app/dist/uploads

RUN ls -a

RUN npm install
RUN npm run build

EXPOSE 5003

CMD [ "node", "./dist/server.js" ]

0 个答案:

没有答案