连接被拒绝:拨号tcp 127.0.0.1:8080: 连接被拒绝。使用Docker的Go应用程序。

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

dial tcp 127.0.0.1:8080: connect: connection refused. go docker app

问题

我有两个用Go语言编写的应用程序。首先是user_management应用程序,我首先运行(docker-compose up --build),然后运行(docker-compose up --build)sport_app。sport_app依赖于user_management应用程序。

sport_app的Dockerfile文件如下所示。

FROM golang:alpine

RUN apk update && apk upgrade && apk add --no-cache bash git openssh curl

WORKDIR /go-sports-entities-hierarchy

COPY . /go-sports-entities-hierarchy/
RUN rm -rf /go-sports-entities-hierarchy/.env
RUN go mod download
RUN chmod +x /go-sports-entities-hierarchy/scripts/*
RUN ./scripts/build.sh

ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait

ENV GIN_MODE="debug" \
    GQL_SERVER_HOST="localhost" \
    GQL_SERVER_PORT=7777 \
    ALLOWED_ORIGINS=* \
    USER_MANAGEMENT_SERVER_URL="http://localhost:8080/user/me" \
    # GQLGen config
    GQL_SERVER_GRAPHQL_PATH="graphql" \
    GQL_SERVER_GRAPHQL_PLAYGROUND_ENABLED=true \
    GQL_SERVER_GRAPHQL_PLAYGROUND_PATH="playground" \
# Export necessary port
EXPOSE 7777

CMD /wait && ./scripts/run.sh

sport_app的docker-compose.yml文件如下所示。

version: '3'

volumes:
  postgres_data:
      driver: local
services:
  go-sports-entities-hierarchy:
      restart: always
      build:
        dockerfile: Dockerfile
        context: .
      environment:
        WAIT_HOSTS: postgres:5432
        # Web framework config
        GIN_MODE: debug
        GQL_SERVER_HOST: go-sports-entities-hierarchy
        GQL_SERVER_PORT: 7777
        ALLOWED_ORIGINS: "*"
        USER_MANAGEMENT_SERVER_URL: http://localhost:8080/user/me
        # GQLGen config
        GQL_SERVER_GRAPHQL_PATH: graphql
        GQL_SERVER_GRAPHQL_PLAYGROUND_ENABLED: "true"
        GQL_SERVER_GRAPHQL_PLAYGROUND_PATH: playground
      ports:
        - 7777:7777
      depends_on:
        - postgres
        - redisearch
  go-sports-events-workflow:
      restart: always
      build:
        dockerfile: Dockerfile
        context: .
      environment:
        WAIT_HOSTS: postgres:5432
        # Web framework config
        GIN_MODE: debug
        GQL_SERVER_HOST: go-sports-events-workflow
        GQL_SERVER_PORT: 7778
        ALLOWED_ORIGINS: "*"
        # GQLGen config
        GQL_SERVER_GRAPHQL_PATH: graphql
        GQL_SERVER_GRAPHQL_PLAYGROUND_ENABLED: "true"
        GQL_SERVER_GRAPHQL_PLAYGROUND_PATH: playground
      depends_on:
        - postgres
        - redisearch
        - go-sports-entities-hierarchy

user_management应用程序的Dockerfile文件如下所示。

FROM golang:alpine

RUN apk update && apk add --no-cache git ca-certificates && update-ca-certificates

# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64

# Move to working directory /build
WORKDIR /build

# Copy and download dependency using go mod
COPY go.mod .
COPY go.sum .
RUN go mod download

# Copy the code into the container
COPY . .

# Build the application
RUN go build -o main .

# Move to /dist directory as the place for resulting binary folder
WORKDIR /dist

# Copy binary from build to main folder
RUN cp -r /build/html .
RUN cp /build/main .

# Environment Variables
ENV DB_HOST="127.0.0.1" \
    APP_PROTOCOL="http" \
    APP_HOST="localhost" \
    APP_PORT=8080 \
    ALLOWED_ORIGINS="*"

# Export necessary port
EXPOSE 8080

ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait

# Command to run when starting the container
CMD /wait && /dist/main

user_management应用程序的docker-compose.yml文件如下所示。

version: '3'

volumes:
  postgres_data:
      driver: local

services:
  postgres:
      image: postgres
      volumes:
        - postgres_data:/var/lib/postgresql/data
      ports:
        - 5432:5432
  go-user-management:
      restart: always
      build:
        dockerfile: Dockerfile
        context: .
      environment:
        # Postgres Details
        DB_PORT: 5432
        # APP details
        APP_PROTOCOL: http
        APP_HOST: localhost
        APP_PORT: 8080
        # System Configuration Details
        ALLOWED_ORIGINS: "*"
      ports:
        - 8080:8080
      depends_on:
        - postgres

在sport_app中,我编写了以下代码并出现错误:

client := resty.New()
resp, err := client.R().SetHeader("Content-Type", "application/json").SetHeader("Authorization", "Bearer "+token).Get("http://localhost:8080/user/me")

错误是:Get "http://localhost:8080/user/me": dial tcp 127.0.0.1:8080: connect: connection refused
这个API(http://localhost:8080/user/me)是在user_management应用程序中编写的,并且它是正常工作的,我用Postman进行了检查。
我已经阅读了这个问题answers,但无法解决我的问题。
我对Docker还不熟悉,请帮忙解决。

英文:

I have two apps in go language. user_management app, which I run (docker-compose up --build) first, then I run(docker-compose up --build) sport_app. sport_app is dependent from user_management app.

sport_app Dockerfile file as below.

FROM golang:alpine
RUN apk update && apk upgrade && apk add --no-cache bash git openssh curl
WORKDIR /go-sports-entities-hierarchy
COPY . /go-sports-entities-hierarchy/
RUN rm -rf /go-sports-entities-hierarchy/.env
RUN go mod download
RUN chmod +x /go-sports-entities-hierarchy/scripts/*
RUN ./scripts/build.sh
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
ENV GIN_MODE="debug" \
GQL_SERVER_HOST="localhost" \
GQL_SERVER_PORT=7777 \
ALLOWED_ORIGINS=* \
USER_MANAGEMENT_SERVER_URL="http://localhost:8080/user/me" \
# GQLGen config
GQL_SERVER_GRAPHQL_PATH="graphql" \
GQL_SERVER_GRAPHQL_PLAYGROUND_ENABLED=true \
GQL_SERVER_GRAPHQL_PLAYGROUND_PATH="playground" \
# Export necessary port
EXPOSE 7777
CMD /wait && ./scripts/run.sh

sport_app docker-compose.yml file as below.

version: '3'
volumes:
postgres_data:
driver: local
services:
go-sports-entities-hierarchy:
restart: always
build:
dockerfile: Dockerfile
context: .
environment:
WAIT_HOSTS: postgres:5432
# Web framework config
GIN_MODE: debug
GQL_SERVER_HOST: go-sports-entities-hierarchy
GQL_SERVER_PORT: 7777
ALLOWED_ORIGINS: "*"
USER_MANAGEMENT_SERVER_URL: http://localhost:8080/user/me
# GQLGen config
GQL_SERVER_GRAPHQL_PATH: graphql
GQL_SERVER_GRAPHQL_PLAYGROUND_ENABLED: "true"
GQL_SERVER_GRAPHQL_PLAYGROUND_PATH: playground
ports:
- 7777:7777
depends_on:
- postgres
- redisearch
go-sports-events-workflow:
restart: always
build:
dockerfile: Dockerfile
context: .
environment:
WAIT_HOSTS: postgres:5432
# Web framework config
GIN_MODE: debug
GQL_SERVER_HOST: go-sports-events-workflow
GQL_SERVER_PORT: 7778
ALLOWED_ORIGINS: "*"
# GQLGen config
GQL_SERVER_GRAPHQL_PATH: graphql
GQL_SERVER_GRAPHQL_PLAYGROUND_ENABLED: "true"
GQL_SERVER_GRAPHQL_PLAYGROUND_PATH: playground
depends_on:
- postgres
- redisearch
- go-sports-entities-hierarchy

user_management app Dockerfile as below:

FROM golang:alpine
RUN apk update && apk add --no-cache git ca-certificates && update-ca-certificates
# Set necessary environmet variables needed for our image
ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64
# Move to working directory /build
WORKDIR /build
# Copy and download dependency using go mod
COPY go.mod .
COPY go.sum .
RUN go mod download
# Copy the code into the container
COPY . .
# Build the application
RUN go build -o main .
# Move to /dist directory as the place for resulting binary folder
WORKDIR /dist
# Copy binary from build to main folder
RUN cp -r /build/html .
RUN cp /build/main .
# Environment Variables
ENV DB_HOST="127.0.0.1" \
APP_PROTOCOL="http" \
APP_HOST="localhost" \
APP_PORT=8080 \
ALLOWED_ORIGINS="*"
# Export necessary port
EXPOSE 8080
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
# Command to run when starting the container
CMD /wait && /dist/main

user_management app docker-compose.yml file as below:

version: '3'
volumes:
postgres_data:
driver: local
services:
postgres:
image: postgres
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- 5432:5432
go-user-management:
restart: always
build:
dockerfile: Dockerfile
context: .
environment:
# Postgres Details
DB_PORT: 5432
# APP details
APP_PROTOCOL: http
APP_HOST: localhost
APP_PORT: 8080
# System Configuration Details
ALLOWED_ORIGINS: "*"
ports:
- 8080:8080
depends_on:
- postgres

In sport_app I write below code and get error:

client := resty.New()
resp, err := client.R().SetHeader("Content-Type", "application/json").SetHeader("Authorization", "Bearer "+token).Get("http://localhost:8080/user/me")

Error is: Get "http://localhost:8080/user/me": dial tcp 127.0.0.1:8080: connect: connection refused:"
This API(http://localhost:8080/user/me) is written in the user_management app and this is working, I check with the postman.
I already read this question answers, but can not solve my problem.
I am new to docker, please help.

答案1

得分: 7

用于在多个docker-compose客户端之间进行通信,您需要确保要相互通信的容器位于同一网络中。

例如,(为简洁起见进行了编辑)这里有一个docker-compose.yml示例:

# sport_app docker-compose.yml
version: '3'
services:
go-sports-entities-hierarchy:
...
networks:
- some-net
go-sports-events-workflow
...
networks:
- some-net
networks:
some-net:
driver: bridge

和另一个docker-compose.yml

# user_management app docker-compose.yml
version: '3'
services:
postgres:
...
networks:
- some-net
go-user-management
...
networks:
- some-net
networks:
some-net:
external: true

注意:您的应用程序的网络根据项目名称命名,该名称基于其所在目录的名称,在本例中添加了前缀user_

然后,它们可以使用服务名称进行通信,例如go-user-management等。

您可以在运行docker-compose up --build命令后运行docker network ls命令来查看它,然后运行docker network inspect bridge等命令。

英文:

For communicating between multiple docker-compose clients, you need to make sure that the containers you want to talk to each other are on the same network.

For example, (edited for brevity) here you have one of the docker-compose.yml

# sport_app docker-compose.yml
version: '3'
services:
go-sports-entities-hierarchy:
...
networks:
- some-net
go-sports-events-workflow
...
networks:
- some-net
networks:
some-net:
driver: bridge

And the other docker-compose.yml

# user_management app docker-compose.yml
version: '3'
services:
postgres:
...
networks:
- some-net
go-user-management
...
networks:
- some-net
networks:
some-net:
external: true

Note: Your app’s network is given a name based on the project name, which is based on the name of the directory it lives in, in this case a prefix user_ was added.

They can then talk to each other using the service name, i.e. go-user-management, etc.

You can, after running the docker-compose up --build commands, run the docker network ls command to see it, then docker network inspect bridge, etc.

huangapple
  • 本文由 发表于 2021年12月22日 03:40:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/70440797.html
匿名

发表评论

匿名网友

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

确定