Home > Blockchain >  Golang project and postgres image with docker compose up doesn't fail, but doesn't work ei
Golang project and postgres image with docker compose up doesn't fail, but doesn't work ei

Time:08-30

not sure what I did wrong here. I'm trying to make a golang/postgres dockerized project with a persistent db. Below are the files. When I run go run main.go then curl http://localhost:8081/ I get the expected output, but when I try this with docker compose up I'm having issues, but everything seems to be working because I don't see any error messages postgres-1 | 2022-08-29 05:31:59.703 UTC [1] LOG: database system is ready to accept connections, but when I try curl http://localhost:8081/ I'm getting an error curl: (56) Recv failure: Connection reset by peer. I tried removing the postgres part entirely and I'm still getting the same problem. I can see that docker is up and running and the port is listening

sudo lsof -i -P -n | grep 8081
docker-pr 592069            root    4u  IPv4 1760430      0t0  TCP *:8081 (LISTEN)

I'm using this on Ubuntu 22.04

main.go:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "this can be anything")
    })

    http.HandleFunc("/try-it", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "it worked!")
    })

    log.Fatal(http.ListenAndServe(":8081", nil))

}

Dockerfile:

FROM golang:1.19-alpine3.16

WORKDIR /app

COPY go.mod ./
RUN go mod download

COPY . .

RUN go build -o main .

RUN go build -v -o /usr/local/bin/app ./...

EXPOSE 8081
CMD [ "./main" ]

docker-compose.yml:

version: '3.9'
services:
  api:
    restart: unless-stopped
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8081:8080"
    depends_on:
      - postgres
    networks:
      - backend
  postgres:
    image: postgres
    restart: unless-stopped
    ports:
      - "5001:5432"
    volumes:
      - psqlVolume:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    networks:
      - backend

networks:
  backend:

volumes:
  psqlVolume:

CodePudding user response:

As @DavidMaze and @Brits explained in the comments the docker container is up and running, but the ports need to be mapped in both the container and the application. For example in main.go this method http.ListenAndServe(":8084", nil) in the app would need to map with the container port (in this case :8084)

version: '3.9'
services:
  api:
    image: api
    restart: unless-stopped
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8083:8084" #host_port:container_port

a curl request could be made on the host port where docker is listening (in this case it's :8083). For example curl http://localhost:8083/. This would make a request to the host machine and that request would be captured by docker which is listening on port :8083 then transmit the request to the container which is listening on :8084 as specified in the docker-compose.yml. If the port mapping isn't correct then curl will return curl: (56) Recv failure: Connection reset by peer.Thank you for the learning experience and I appreciate all your help.

CodePudding user response:

Try to use

postgres:
image: postgres:12.4-alpine
shm_size: 256m
environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_HOST_AUTH_METHOD=trust
ports:
      - "5001:5432"
healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
api:
image: api
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile
stop_signal: SIGTERM
ports:
    - "8081:8080"
deploy:
   mode: replicated
   replicas: 1
depends_on:
   - postgres
networks:
   - backend
  • Related