扩展Docker的Postgres镜像以创建额外的数据库。

huangapple go评论98阅读模式
英文:

Extend docker postgres image to create extra database

问题

我已经看过这个主题:https://stackoverflow.com/a/26599273/2323245

但是我有以下问题:

postgres_1 | FATAL: 角色"docker"不存在
app_1 | 错误:无法与数据库建立连接

这是我的docker-compose.yml文件:

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:

我的postgres.dockerfile是:

FROM library/postgres

MAINTAINER Samir Bouaked "sbouaked@neocasesoftware.com"

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

这是我的init.sql文件:

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

在我的golang应用程序中,我尝试使用以下代码连接到数据库:

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("错误:数据源参数无效 - " + err.Error())
}
err = db.Ping()

if err != nil {
    log.Println("错误:无法与数据库建立连接")
    c.String(http.StatusOK, "hotstName : %s\nDbStatus : %s", os.Getenv("HOSTNAME"), err)
} else {
    c.String(http.StatusOK, "\nhotstName : %s\nDbStatus : 连接正常!", os.Getenv("HOSTNAME"))
}
defer db.Close()

})

我尝试了不同的解决方案,比如直接在dockerfile中传递环境变量,像这样:

ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker

但是结果始终相同...
如果需要更多信息,你可以查看这个仓库:https://github.com/neocase/docker-go-starter-kit

我不明白问题出在哪里。

英文:

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\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 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

答案1

得分: 3

这是一个关于Docker卷的问题。

我需要执行docker volume rm postgresvolume,然后再次运行docker-compose up --build

这不是环境变量的问题,而是因为init.sql文件被缓存了,没有在开始时运行。这是由于我在项目开始时进行了一些测试导致的。

英文:

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

答案2

得分: 1

如果你想通过传递环境变量使用docker-compose来创建数据库,你可以在compose YML文件中指定类似以下的内容:

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

这样会创建一个带有数据库的postgres容器,使用用户名/密码为docker,不再需要自己的dockerfile(因此你可以使用任何支持此行为的postgres镜像)。

在运行compose之后(如果它不能按照你的期望工作),你应该查看日志,看看你的postgres容器是否正常工作,或者是否存在与你的应用程序相关的问题。

英文:

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.

huangapple
  • 本文由 发表于 2016年11月21日 18:30:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/40717975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定