dongrong7883 2016-11-21 10:30
浏览 283
已采纳

扩展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

  • 写回答

2条回答 默认 最新

  • duanlao1552 2016-11-22 08:31
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongshuo2752 2016-11-21 13:49
    关注

    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.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Tpad api账户 api口令
  • ¥30 ppt进度条制作,vba语言
  • ¥15 stc12c5a60s2单片机测光敏ADC
  • ¥15 生信simpleaffy包下载
  • ¥15 请教一下simulink中S函数相关问题
  • ¥15 在二层网络中,掩码存在包含关系即可通信
  • ¥15 端口转发器解析失败不知道电脑设置了啥
  • ¥15 Latex算法流程图行号自定义
  • ¥15 关于#python#的问题:我在自己的电脑上运行起来总是报错,希望能给我一个详细的教程,(开发工具-github)
  • ¥40 基于51单片机实现球赛计分器功能