我试图运行docker compose来设置nginx,golang服务器和postgresql。问题是我无法连接到数据库:
最初,我试图通过以下指令连接到postgres:
db, err = gorm.Open("postgres", "host=db port=5432 user=sigbrian password=example sslmode=disable")
使用此docker-compose文件:
version: '3.2'
# volumes:
# database_data:
# driver: local
services:
db:
image: postgres
restart: always
environment:
- POSTGRES_DB:sigdb
- POSTGRES_USER:sigbrian
- POSTGRES_PASSWORD:example
ports:
- '5432:5432'
# volumes:
# - database_data:/var/lib/postgresql/data
golang-app:
build:
context: .
dockerfile: Dockerfile
# command: ["./wait-for-it.sh", "db:5432"]
# ports:
# - '80:8888'
expose:
- '8080'
depends_on:
- "db"
links:
- "db"
nginx-proxy-and-webserver:
image: nginx:1.15.5-alpine
ports:
- '80:80'
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./static:/var/www/sig/static
depends_on:
- "golang-app"
links:
- "golang-app"
但是当我从文件中注释postgres用户,数据库和密码环境变量,并尝试通过这种方式建立连接时:
db, err = gorm.Open("postgres", "host=db port=5432 user=postgres sslmode=disable")
仍然会引发相同的错误。感谢您的帮助。
答案 0 :(得分:0)
我正在像您一样将docker与postgres和golang一起使用,这是我使用的docker-compose.yml文件
version: '2'
services:
redis:
image: redis:latest
command: --appendonly yes
postgres:
image: postgres:latest
environment:
- POSTGRES_USER=amine
- POSTGRES_PASSWORD=amine
- POSTGRES_DB=0000
app:
image: ahilaly/amine.in:v0.1.3
command: -wait
ports:
- 7000:7000
depends_on:
- postgres
- redis
links:
- postgres:postgres
- redis:redis