Docker Compose问题(ECONNREFUSED)

huangapple go评论74阅读模式
英文:

Docker compose problems (ECONNREFUSED)

问题

我有两个用于后端(.NET Core)和前端 Angular 的 Docker Compose 文件。它们应该相互通信。为此,我尝试了一个网络。但我总是收到以下消息:

[HPM] 尝试代理请求从 localhost:4200 到 http://localhost:5000 时发生错误 (ECONNREFUSED)

两个容器都在运行中。后端的 Compose 通过 Visual Studio 启动。前端的 Compose 通过 PowerShell 启动。

我尝试过前端使用端口 80 和 5000。结果都一样。

对于网络,我尝试过不使用外部网络。对于外部网络,我手动创建了名为 "test_network" 的网络。

后端 Docker Compose:

version: '3.7'

services:
  backend_sql:
    image: mcr.microsoft.com/mssql/server:2017-GA-ubuntu
    container_name: testsql
    networks:
      - test_network
    ports:
      - "1440:1433"
    volumes:
      - testsqldata:/var/opt/mssql

  backend_api:
    image: ${DOCKER_REGISTRY-}backendapi
    container_name: testapi
    build:
      context: .
      dockerfile: backendAPI/Dockerfile
    networks:
      - test_network
    depends_on:
      - backend_sql
    ports:
      - "5000:80"

networks:
  test_network:
    external:
      name: test_network

volumes:
  testsqldata:
    driver: local
    name: testsqldata

前端 Docker Compose:

version: "3.7"
services:
  testapp:
    container_name: testapp
    build: .
    networks:
      - test_network
    ports:
      - "4200:4200"
      - "49153:49153"
    volumes:
      - "/app/node_modules"
      - ".:/app"

networks:
  test_network:
    external:
      name: test_network

前端 Dockerfile:

FROM node:10.16.3-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY .
EXPOSE 4200 49153
CMD npm run start

package.json:

"scripts": {
  "ng": "ng",
  "start": "ng serve --host 0.0.0.0 --port 4200 --poll 500 --disableHostCheck",
  "serve": "ng serve --proxy-config src/proxy.server.config.js"
}

proxy.server.config.js:

const PROXY_CONFIG = [
  {
    context: [
      '/api',
    ],
    target: 'http://localhost:5000',
    secure: false,
    logLevel: 'debug',
    ws: true,
    pathRewrite: {
      "^/api": ""
    }
  }
];

module.exports = PROXY_CONFIG;

希望这些信息对你有帮助。如果需要进一步的帮助,请随时告诉我。

英文:

I have two docker-compose for the backend (.net Core) and for the Angular frontend. These should communicate with each other. For this I tried a network. But I always get the following message:

[HPM] Error occurred while trying to proxy request /appinfo from localhost:4200 to http://localhost:5000 (ECONNREFUSED)

Both containers are running. The backend-compose is started via Visual Studio. The frontend-compose is started using the powershell.

I have tried both port 80 and 5000 in the frontend. The result is the same.

For the network I tried it without external. For the external I created the network manually with "docker network create test_network".

backend docker-compose:

version: '3.7'

services:
  backend_sql:
	image: mcr.microsoft.com/mssql/server:2017-GA-ubuntu
	container_name: testsql
	networks:
	  - test_network
	ports:
	  - "1440:1433"
	volumes:
	 - testsqldata:/var/opt/mssql

  backend_api:
	image: ${DOCKER_REGISTRY-}backendapi
	container_name: testapi
	build:
	  context: .
	  dockerfile: backendAPI/Dockerfile
	networks:
	  - test_network
	depends_on:
	 - backend_sql
	ports:
	 - "5000:80"

networks:
  test_network:
	external:
		name: test_network

volumes:
  testsqldata:
	driver: local
	name: testsqldata

frontend docker-compose:

version: "3.7"
services:
  testapp:
	container_name: testapp
	build: .
	networks:
	  - test_network
	ports:
	  - "4200:4200"
	  - "49153:49153"
	volumes:
	  - "/app/node_modules"
	  - ".:/app"

networks:
  test_network:
	external:
	  name: test_network

frontend dockerfile:

FROM node:10.16.3-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 4200 49153
CMD npm run start

package.json:

"scripts": {
	"ng": "ng",
	"start": "ng serve --host 0.0.0.0 --port 4200 --poll 500 --disableHostCheck",
	"serve": "ng serve --proxy-config src/proxy.server.config.js",	

proxy.server.config.js:

const PROXY_CONFIG = [
	{
		context: [
			'/api',
		],
		target: 'http://localhost:5000',
		secure: false,
		logLevel: 'debug',
		ws: true,
		pathRewrite: {
			"^/api": ""
		}
	}
];

module.exports = PROXY_CONFIG;		

答案1

得分: 1

Docker Compose网络文档中,

> 现在,每个容器都可以查找主机名web或db,并返回相应容器的IP地址。例如,web的应用程序代码可以连接到URL postgres://db:5432 并开始使用Postgres数据库。

您应该使用容器名称作为主机名。

英文:

From the Docker Compose networking documentation,
> Each container can now look up the hostname web or db and get back the appropriate container’s IP address. For example, web’s application code could connect to the URL postgres://db:5432 and start using the Postgres database.

You should use the container names as hostname.

huangapple
  • 本文由 发表于 2020年1月3日 16:37:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定