我已經看到了這個話題:https://stackoverflow.com/a/26599273/2323245擴展泊塢窗Postgres的圖像創建額外的數據庫
但我有以下問題:
postgres_1 | FATAL: role "docker" does not exist
app_1 | Error: Could not establish a connection with the database
這是我的搬運工,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.docker文件是
FROM library/postgres
MAINTAINER Samir Bouaked "[email protected]"
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("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()
})
到達數據庫我試着像直接在dockerfile ENV通過不同的解決方案這樣
ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB docker
變量,但總是有同樣的結果... 欲瞭解更多,您可以信息檢查這個回購https://github.com/neocase/docker-go-starter-kit
如果要使用經過ENV變量來創建數據庫,我不明白的問題
你能夠使用psql連接到postgres並驗證數據庫是否存在? – R0MANARMY