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:
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?){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.8081:8081and 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.