Docker Compose问题(ECONNREFUSED)

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

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:

  1. version: '3.7'
  2. services:
  3. backend_sql:
  4. image: mcr.microsoft.com/mssql/server:2017-GA-ubuntu
  5. container_name: testsql
  6. networks:
  7. - test_network
  8. ports:
  9. - "1440:1433"
  10. volumes:
  11. - testsqldata:/var/opt/mssql
  12. backend_api:
  13. image: ${DOCKER_REGISTRY-}backendapi
  14. container_name: testapi
  15. build:
  16. context: .
  17. dockerfile: backendAPI/Dockerfile
  18. networks:
  19. - test_network
  20. depends_on:
  21. - backend_sql
  22. ports:
  23. - "5000:80"
  24. networks:
  25. test_network:
  26. external:
  27. name: test_network
  28. volumes:
  29. testsqldata:
  30. driver: local
  31. name: testsqldata

前端 Docker Compose:

  1. version: "3.7"
  2. services:
  3. testapp:
  4. container_name: testapp
  5. build: .
  6. networks:
  7. - test_network
  8. ports:
  9. - "4200:4200"
  10. - "49153:49153"
  11. volumes:
  12. - "/app/node_modules"
  13. - ".:/app"
  14. networks:
  15. test_network:
  16. external:
  17. name: test_network

前端 Dockerfile:

  1. FROM node:10.16.3-alpine
  2. WORKDIR /app
  3. COPY package.json .
  4. RUN npm install
  5. COPY .
  6. EXPOSE 4200 49153
  7. CMD npm run start

package.json:

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

proxy.server.config.js:

  1. const PROXY_CONFIG = [
  2. {
  3. context: [
  4. '/api',
  5. ],
  6. target: 'http://localhost:5000',
  7. secure: false,
  8. logLevel: 'debug',
  9. ws: true,
  10. pathRewrite: {
  11. "^/api": ""
  12. }
  13. }
  14. ];
  15. 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:

  1. version: '3.7'
  2. services:
  3. backend_sql:
  4. image: mcr.microsoft.com/mssql/server:2017-GA-ubuntu
  5. container_name: testsql
  6. networks:
  7. - test_network
  8. ports:
  9. - "1440:1433"
  10. volumes:
  11. - testsqldata:/var/opt/mssql
  12. backend_api:
  13. image: ${DOCKER_REGISTRY-}backendapi
  14. container_name: testapi
  15. build:
  16. context: .
  17. dockerfile: backendAPI/Dockerfile
  18. networks:
  19. - test_network
  20. depends_on:
  21. - backend_sql
  22. ports:
  23. - "5000:80"
  24. networks:
  25. test_network:
  26. external:
  27. name: test_network
  28. volumes:
  29. testsqldata:
  30. driver: local
  31. name: testsqldata

frontend docker-compose:

  1. version: "3.7"
  2. services:
  3. testapp:
  4. container_name: testapp
  5. build: .
  6. networks:
  7. - test_network
  8. ports:
  9. - "4200:4200"
  10. - "49153:49153"
  11. volumes:
  12. - "/app/node_modules"
  13. - ".:/app"
  14. networks:
  15. test_network:
  16. external:
  17. name: test_network

frontend dockerfile:

  1. FROM node:10.16.3-alpine
  2. WORKDIR /app
  3. COPY package.json .
  4. RUN npm install
  5. COPY . .
  6. EXPOSE 4200 49153
  7. CMD npm run start

package.json:

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

proxy.server.config.js:

  1. const PROXY_CONFIG = [
  2. {
  3. context: [
  4. '/api',
  5. ],
  6. target: 'http://localhost:5000',
  7. secure: false,
  8. logLevel: 'debug',
  9. ws: true,
  10. pathRewrite: {
  11. "^/api": ""
  12. }
  13. }
  14. ];
  15. 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:

确定