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

Time:12-23

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.

CodePudding user response:

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.

  • Related