I'm trying to learn about how to make a docker container with a simple golang API that relies on a postgres database. My problems are two-fold:
Attempting to connect to the postgres DB would yield me a "password authentication failed for user postgres":
Meanwhile, attempting to do so via psql would get me this:
This is via my PC, not via the docker container. Attempting to access the docker container gets me this:
Googling the "the input device is not a TTY" issue tells me I should just remove the -ti
, but doing so doesn't really do anything, the CLI just outputs the next line.
Now trying to connect to it via my golang app, the app just fails with the last log being:
dial tcp 172.29.0.2:5432: connect: connection refused
So I'm not really sure what's going on here. Here's my docker-compose.yml
version: '3.8'
services:
go_db:
container_name: go_db
image: postgres:12
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: apploggerstorage
ports:
- 5432:5432
volumes:
- pgdata:/var/lib/postgresql/data
app_logger:
container_name: app_logger
image: lanceguinto/app_logger:1.0.0
build: .
environment:
DATABASE_URL: "host=go_db user=postgres password=postgres dbname=apploggerstorage sslmode=disable"
ports:
- 8000:8000
depends_on:
- go_db
My Dockerfile:
FROM golang:1.21.6
WORKDIR /app
COPY . .
RUN go get -d -v ./...
RUN go build -o applogreader .
EXPOSE 8000
CMD ["./applogreader"]
And here's my code for the main.go.
Did I miss a step somewhere? I've been mostly following this tutorial on it, but I couldn't get very far on what is to my understanding a pretty basic API. Any help would be much appreciated.