Golang: `dial tcp 172.20.0.7:8081: connect: connection refused`, error at random times between docker containers / services

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

Golang: `dial tcp 172.20.0.7:8081: connect: connection refused`, error at random times between docker containers / services

问题

我有一个包含以下内容的docker-compose文件:

# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)

version: '3'

services:

  john:
    build:
      context: .
      dockerfile: Dockerfiles/cowboys/John/Dockerfile
      args:
        - COWBOY_NAME_JOHN
        - CONTAINER_PORT_JOHN
    ports:
      - "8081:8081" # 将容器上的暴露端口转发到主机上的端口
    restart: unless-stopped
    networks:
      - fullstack
    depends_on:
      db:
        condition: service_healthy
    links:
      - db


  philip:
    build:
      context: .
      dockerfile: Dockerfiles/cowboys/Philip/Dockerfile
      args:
        - COWBOY_NAME_PHILIP
        - CONTAINER_PORT_PHILIP
    ports:
      - "8085:8085"
    restart: unless-stopped
    networks:
      - fullstack
    depends_on:
      db:
        condition: service_healthy
    links:
      - db

  db:
    build:
      context: Dockerfiles/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
    restart: on-failure
    networks:
      - fullstack
    ports:
      - "3306:3306"
    healthcheck:
      test: mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD

networks:
  fullstack:
    driver: bridge

当我运行docker-compose up --build(或者在下一次运行时重新初始化数据库时运行docker-compose up --build --force-recreate)时,我会得到以下错误:

sam_1 | panic: Put "http://john:8081/shoot": dial tcp 172.20.0.7:8081: connect: connection refused

johnservices下的一个服务名称。这是完整的屏幕截图:

Golang: `dial tcp 172.20.0.7:8081: connect: connection refused`, error at random times between docker containers / services

包含环境变量和相关Dockerfile的详细信息的存储库链接 - https://github.com/sitetester/distributed-cowboys

请问有人可以解释如何解决这个错误吗?它只是在随机时间发生。不确定如何修复。提前感谢!

英文:

I've a docker-compose file with contents like this

# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)

version: '3'

services:

  john:
    build:
      context: .
      dockerfile: Dockerfiles/cowboys/John/Dockerfile
      args:
        - COWBOY_NAME_JOHN
        - CONTAINER_PORT_JOHN
    ports:
      - "8081:8081" # Forward the exposed port on the container to port on the host machine
    restart: unless-stopped
    networks:
      - fullstack
    depends_on:
      db:
        condition: service_healthy
    links:
      - db


  philip:
    build:
      context: .
      dockerfile: Dockerfiles/cowboys/Philip/Dockerfile
      args:
        - COWBOY_NAME_PHILIP
        - CONTAINER_PORT_PHILIP
    ports:
      - "8085:8085"
    restart: unless-stopped
    networks:
      - fullstack
    depends_on:
      db:
        condition: service_healthy
    links:
      - db

  db:
    build:
      context: Dockerfiles/mysql
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
    restart: on-failure
    networks:
      - fullstack
    ports:
      - "3306:3306"
    healthcheck:
      test: mysqladmin ping -h 127.0.0.1 -u $$MYSQL_USER --password=$$MYSQL_PASSWORD

networks:
  fullstack:
    driver: bridge

When I run docker-compose up --build ( or docker-compose up --build --force-recreate to reinitialise db on next run), I get an error like this

sam_1 | panic: Put "http://john:8081/shoot": dial tcp 172.20.0.7:8081: connect: connection refused

john is one of the services name under services. Here is complete screenshot:

Golang: `dial tcp 172.20.0.7:8081: connect: connection refused`, error at random times between docker containers / services

Link to repository where all details could be seen including environment variables & relevant Dockerfile(s) - https://github.com/sitetester/distributed-cowboys

Could anyone please explain how to get rid of this error ? It just happens/occurs at random time. Not sure how to fix it. Thanks in advance!

答案1

得分: 1

已解决:删除现有的容器/镜像并清理 Docker 卷解决了该问题。

英文:

Solved: removing existing containers/images and pruning docker volumes fixed the issue.

huangapple
  • 本文由 发表于 2022年2月13日 00:41:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/71093829.html
匿名

发表评论

匿名网友

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

确定