Docker postgres image extension to create an additional database

I have already seen this topic: https://stackoverflow.com/a/312929/

But I have the following problem:

postgres_1  | FATAL:  role "docker" does not exist
app_1       | Error: Could not establish a connection with the database

This is my docker-compose.yml file

version: "2"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfiles/app.dockerfile
    links:
      - postgres
    depends_on:
      - postgres
    ports:
      - "8080:8080"
    environment:
      PORT: 8080
    networks:
      - neo_dev
    restart: always

  postgres:
    build:
          context: .
          dockerfile: Dockerfiles/postgres.dockerfile
    networks:
      - neo_dev
    volumes:
      - postgresql:/var/lib/postgresql
      # This needs explicit mapping due to https://github.com/docker-library/postgres/blob/4e48e3228a30763913ece952c611e5e9b95c8759/Dockerfile.template#L52
      - postgresql_data:/var/lib/postgresql/data
    ports:
      - "5430:5432"  #in case you already have postgres running in your host machine

networks:
    neo_dev:
      driver: bridge

volumes:
  postgresql:
  postgresql_data:

My postgres.dockerfile

FROM library/postgres

MAINTAINER Samir Bouaked "sbouaked@neocasesoftware.com"

ADD Dockerfiles/init.sql /docker-entrypoint-initdb.d/

And this is my init.sql file

CREATE USER docker with password 'docker';
CREATE DATABASE docker;
GRANT ALL PRIVILEGES ON DATABASE docker TO docker;

In my golang application, I am trying to get to the database using

router.GET("/db", func(c *gin.Context) {
        db, err := sql.Open("postgres", "host=postgres port=5432 user=docker dbname=docker password=docker sslmode=disable")
        if err != nil {
            log.Fatal("Error: The data source arguments are not valid - " + err.Error())
        }
        err = db.Ping()

        if err != nil {
            log.Println("Error: Could not establish a connection with the database")
            c.String(http.StatusOK, "hotstName : %s\nDbStatus : %s", os.Getenv("HOSTNAME"), err)
        } else {
            c.String(http.StatusOK, "\nhotstName : %s\nDbStatus : connection ok !\n", os.Getenv("HOSTNAME"))
        }
        defer db.Close()
    })

I tried different solutions, such as transferring directly to dockerfile env files like

ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker

But they always have the same result ... For more information, you can check out this repo https://github.com/neocase/docker-go-starter-kit

I do not understand the problem

+4
source share
2

.

docker volume rm postgresvolume a docker-compose up --build

, , init.sql . -

+2

, ENV, docker-compose, -

  postgres:
    container_name: postgres
    image: library/postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DATABASE=docker
      - POSTGRES_USER=docker
      - POSTGRES_PASSWORD=docker
      - POSTGRES_ADMIN_PASSWORD=docker

YML. postgres - DB - user/pwd, dockerfile ( postgres, ).

compose ( , ), , postgres , .

+1

Source: https://habr.com/ru/post/1661445/


All Articles