扩展docker postgres映像以创建额外的数据库

I've already seen this topic : https://stackoverflow.com/a/26599273/2323245

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 is

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 app, i'm trying to reach the database with

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
DbStatus : %s", os.Getenv("HOSTNAME"), err)
        } else {
            c.String(http.StatusOK, "
hotstName : %s
DbStatus : connection ok !
", os.Getenv("HOSTNAME"))
        }
        defer db.Close()
    })

I tried different solutions like passing directly in the dockerfile env variables like that

ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker

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

I don't understand the problem

duandi2853
duandi2853 您是否可以使用psql连接到postgres并验证数据库是否存在?
3 年多之前 回复

2个回答



这是docker卷的问题。 </ p>

我必须 docker volume rm postgresvolume </ code>,然后再次执行 docker-compose up --build </ code> </ p>
\ n

这不是环境变量的问题,而是有理由这样的事实,即init.sql是现金形式的,并非一开始就运行。 这是由于我进行了一些测试而导致的项目开始</ p>
</ div>

展开原文

原文

It was a problem of docker volume.

I have to docker volume rm postgresvolume and do again a docker-compose up --build

It wasn't a problem of environment variables, but juste the fact that the init.sql was in cash and not run at the begining. It's due to some test I make a the start of the project



如果您想通过使用docker-compose传递ENV变量来创建数据库,则可以指定类似</ p>

  postgres:
容器名称:postgres
图像:库/ postgres
端口:
-“ 5432:5432”
环境:
-POSTGRES_DATABASE = docker
-POSTGRES_USER = docker
-撰写的YML文件中的POSTGRES_PASSWORD = docker
-POSTGRES_ADMIN_PASSWORD = docker
</ code> </ pre>


这将为您创建一个Postgres容器,其中包含带有用户/ pwd docker的DB docker ,不再需要您自己的dockerfile(因此您可以使用支持此行为的任何postgres映像)</ p>

运行compose之后(如果它无法按预期运行),您应该 查看日志,看看您的postgres容器是否可以正常工作,或者您的应用程序是否有问题。</ p>
</ div>

展开原文

原文

If you want to create the DB by passing ENV variables using docker-compose you can specify something like

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

in your compose YML file. This should create you a postgres container with an DB docker with user/pwd docker, no need for your own dockerfile anymore (so you can use any postgres image that supports this behavior)

After running compose (and if it doesn't work as you expect) you should have a look at the logs if your postgres container is working at all, or if there is an issue with your app.

doubu5035
doubu5035 我也尝试过(我会保持这种语法),但实际上这不是环境问题!
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问