1

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:
7
  • ``` environment: - POSTGRES_USER=${POSTGRES_USER} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - POSTGRES_HOST_AUTH_METHOD=trust``` Commented Aug 29, 2022 at 13:45
  • It looks like your Go server is listening on port 8081, but your Compose setup is forwarding to port 8080 (the second ports: number needs to match what the process is listening on). Does changing this to match help, ports: ['8081:8081']? (Your sample code doesn't connect to the database at all; is any of the database setup required to reproduce the issue?) Commented Aug 29, 2022 at 14:16
  • @DavidMaze the database connection is not required at all. It's just part of the bigger project after all the containers are working. Even when I remove the postgres container I'm still having this problem. As for the matching ports I thought that it maps the {local_port:container_port} meaning that the server would have port 8081 open and listening and docker would transmit that to port 8080 for the container. I did try the same ports in an earlier troubleshooting attempt and it didn't change anything for me either. Commented Aug 29, 2022 at 15:18
  • @DavidMaze I lied and I just tried it again. It worked. Any idea why I can't use different ports here? Commented Aug 29, 2022 at 15:54
  • @DavidMaze I'm need to troubleshoot more. It seems to only work when I use 8081:8081 and no other port works. This could be some setting on the server I'm not aware of. I'm going to try this on another machine and see if I have the same issues, but you sent me in the right direction. Thank you. Commented Aug 29, 2022 at 16:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.