Sumeet
Sumeet

Reputation: 29

Connection refused while accessing REST Api from Go client when both client and api are on same docker container

I am super new to docker and Go. I've created a client app in Go which test an REST API. The API is provided as part of docker image. I am supposed to add tests in client app and make my client app available as part of same docker-compose.yml.

On local I am able to run go test -v ./... -cover and all tests are able to access api http://localhost:8095/v1/organisation/accounts

When I run docker compose up I get error in below screenshot.

Somehow the client app is not able to access api url within container but its able to access it from my host laptop

Any advice will be very helpful on how to get this done. My instructions for this task are that test written in client app against the api endpoint should run as part of docker-compose.yml file.

Docker Compose Error

I added new Dockerfile for me client app as below and then included it in docker-compose.yml.

Dockerfile:

FROM golang:alpine

ENV GO111MODULE=on \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64

WORKDIR /build

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

COPY . .

WORKDIR /build/accountsapi

RUN go test ./... -cover

docker-compose.yml

version: '3'

services:
  # App Service
  app:
    # Configuration for building the docker image for the service
    build:
      context: . 
      dockerfile: Dockerfile
    restart: unless-stopped
    depends_on: 
      - accountapi
  accountapi:
    image: cooltech/accountapi:v1.0.0-50-g48935
    restart: on-failure
    depends_on:
      - postgresql
      - vault
    environment:
  - PSQL_USER=root
  - PSQL_PASSWORD=password
  - PSQL_HOST=postgresql
  - PSQL_PORT=5432
  - STACK_NAME=accountapi
  - DATABASE-HOST=postgresql
  - DATABASE-SSL-MODE=disable
  - DATABASE-USERNAME=api_user
  - DATABASE-PASSWORD=XXX
ports:
  - 8095:8080
...more services

Here is sample test and actual method

func TestGetAccountByIdExpectsNoAccount(t *testing.T) { 
    // act
    resp := GetAccountById(getUUID())

    defer resp.Body.Close()

    //assert
    if status := resp.StatusCode; status != http.StatusNotFound {
        t.Errorf("handler returned wrong status code: got %v want %v",
            status, http.StatusOK)
    }
}

const Baseurl = "http://localhost:8095/v1/organisation/accounts"
func GetAccountById(id string) *http.Response {

    var url = fmt.Sprintf("%s/%s", Baseurl, id)
    resp, err := http.Get(url)
    if err != nil {
        fmt.Println(err)
    }

    return resp
}

Upvotes: 0

Views: 1132

Answers (1)

Sumeet
Sumeet

Reputation: 29

Finally, I found the issue after @Rengas pointed out in comments that I am trying to run the test before my API service is up which is part of docker-compose. So I removed the go test -v ./... from my Dockerfile and added it as part of docker-compose which looks like below now

accountapitesting:
    build:
      context: . 
      dockerfile: Dockerfile
    restart: on-failure
    command: go test -v ./...
    depends_on: 
      - accountapi

Upvotes: 1

Related Questions