拨打 tcp 127.0.0.1:8080: 连接:连接被拒绝。去码头应用程序

Fua*_*nov 7 go docker dockerfile docker-compose

我有两个 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

Run Code Online (Sandbox Code Playgroud)

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

Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

在 sport_app 中,我编写以下代码并收到错误:

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

错误是:Get "http://localhost:8080/user/me": dial tcp 127.0.0.1:8080: connect: connection returned:" 这个 API(http://localhost:8080/user/me) 是这样写的user_management 应用程序,这正在工作,我与邮递员核实。我已经阅读了这个问题的答案,但无法解决我的问题。我是 docker 的新手,请帮忙。

Ell*_*lie 7

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

\n

例如,(为简洁而编辑)这里您有以下之一docker-compose.yml

\n
# sport_app docker-compose.yml\nversion: '3'\nservices:\n  go-sports-entities-hierarchy:\n    ...\n    networks:\n      - some-net\n  go-sports-events-workflow\n    ...\n    networks:\n      - some-net\nnetworks:\n  some-net:\n    driver: bridge\n
Run Code Online (Sandbox Code Playgroud)\n

和另一个docker-compose.yml

\n
# user_management app docker-compose.yml\nversion: '3'\nservices:\n  postgres:\n    ...\n    networks:\n      - some-net\n  go-user-management\n    ...\n    networks:\n      - some-net\nnetworks:\n  some-net:\n    external: true\n
Run Code Online (Sandbox Code Playgroud)\n

注意:您的 app\xe2\x80\x99s 网络的名称基于project name,它基于其所在目录的名称,在本例中user_添加了前缀。

\n

go-user-management然后,他们可以使用服务名称(即等)相互交谈。

\n

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

\n