我正在练习制作一个与PostgreSQL数据库交互的Golang Web应用程序,它们各自在自己的容器中运行。
我使用
但是我好像无法正确设置postgres容器。
为了简洁起见,
我使用
docker-compose up
命令来运行这些容器。但是我好像无法正确设置postgres容器。
为了简洁起见,
Dockerfile
和其他设置文件的链接在此处(如果您想要的话,请让我知道)。version: '2'
services:
web_app:
build: dockerfiles/web_app
ports:
- "9000:9000"
volumes:
- .:/go/src/gitlab.com/repo/web_app
# links might be replaced by depends_on.
# links:
# - db
depends_on:
- db
# tty and stdin_open cause docker-compose to disconnect from docker-machine after 60sec.
# A fix is on the way.
# tty: true
# stdin_open: true
db:
build: dockerfiles/db
volumes:
- data:/var/lib/postgresql/data
volumes:
data: {}
docker-compose up
工作正常。但是当应用程序尝试使用以下命令打开数据库连接时:
var pgConf string = "user=web_app dbname=web_app sslmode=verify-full password=password"
db, err := sql.Open("postgres", pgConf)
我从Docker Compose得到以下错误:
Error creating new user: dial tcp [::1]:5432: getsockopt: connection refused
我该如何使两个容器相互通信?
提前感谢您的帮助。
depends_on
不能替代links
。前者仅确定启动顺序,而后者还可以通过名称在容器之间进行访问。 - Xiongbing Jin