skip to Main Content

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":

enter image description here

Meanwhile, attempting to do so via psql would get me this:
enter image description here

This is via my PC, not via the docker container. Attempting to access the docker container gets me this:

enter image description here

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.

2

Answers


  1. Chosen as BEST ANSWER

    Fixed it. For some reason, the user postgres just wasn't working, so I elected to try and make another user instead.

    environment:
      POSTGRES_PASSWORD: apploggerpw0987
      POSTGRES_USER: apploggeruser
      POSTGRES_DB: apploggerstorage
    

    This didn't work as is, because of the image: postgres:12 in the docker-compose.yml, I had to change that to image: postgres which would use the latest which supports this feature of creating a user. Had to kill the docker volume as well.


  2. First of all, your first image (PgAdmin) there’s a typo in the maintenance DB, you wrote applogstorage,
    according to the compose file your database name should be apploggerstorage.

    connection from cli:

    psql -U postgres -h localhost -p 5432 -d apploggerstorage
    

    connect via docker exec

    docker exec -it go_db psql -U postgres -d apploggerstorage
    

    I have just tried out your compose file and it works.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search