我正在练习制作一个与PostgreSQL数据库进行交互的Golang Web应用程序,每个都运行在自己的容器上。
我正在运行 docker-compose up
但是我似乎没有让Postgres容器正确设置。
为简洁起见,链接到 Dockerfile 和其他设置文件是这个要点(让我知道你想要它在这里)。
版本:'2'服务: web_app:构建:dockerfiles / web_app 端口: - 9000:9000卷: - 。:/ go / src / gitlab / repo / web_app #链接可能被depends_on替换。 #links:# - db depends_on: - db #tty和stdin_open导致docker-compose在60秒后与docker-machine断开连接。 #一个修复是在路上。 #tty:true #stdin_open:true db: build:dockerfiles / db volumes: - data:/ var / lib / postgresql / data 卷: data:{}docker-compise up 工作正常。但是当应用程序尝试使用以下方式打开数据库连接时:
var pgConf string =user = web_app dbname = web_app sslmode = verify -full password = password db,err:= sql.Open(postgres,pgConf)我从docker撰写了以下错误:
创建新用户时出错:拨号tcp [: :1]:5432:getsockopt:连接拒绝我可以做什么来使两个容器与每个其他?
提前谢谢。
解决方案docker-compose v2,不需要在服务之间创建链接。 Docker 1.9和1.10允许您通过名称连接到相同(自定义)网络上的其他容器。
您应该可以使用服务或容器的名称作为主机名。鉴于容器的名称是由docker-compose生成的,因此使用起来并不方便,因此,docker-compose还向每个容器添加了具有服务名称的别名。 / p>
采取这个非常简单的例子。为了方便起见,我使用了一个Nginx容器,但同样适用于您的情况;
版本:'2'服务: web_app:图像:nginx db:图像:nginx pre>首先启动项目(假设;
$ docker- compose --project-name = test up -d 使用默认驱动程序创建网络test_default创建test_db_1 创建test_web_app_1然后从 test_web_app_1 容器内的db服务ping:
$ docker exec -it test_web_app_1 ping -c 2 db PING db(172.18.0.2):56个数据字节从172.18开始64个字节。 0.2:icmp_seq = 0 ttl = 64 time = 0.108 ms 从172.18.0.2开始的64个字节:icmp_seq = 1 ttl = 64 time = 0.243 ms --- db ping statistics --- 发送2个数据包,2个数据包接收,0%丢包往返最小/ avg / max / stddev = 0.108 / 0.175 / 0.243 / 0.068 ms如果您检查 test_db_1 container,你可以看到docker-compose自动为 test_db_1 容器添加了一个db别名;
$ docker inspect test_db_1提供:(只有 NetworkSettings.Networks part)
Networks:{test_default:{IPAMConfig:null,链接:null,别名:[db, 002b1875e61f,NetworkID:0f9e2cddeca79e5a46c08294ed61dee273828607f99014f6410bda887626be70,EndpointID:a941ab95586a8fdafc5075f9c5c44d745f974e5790ef6048b9e90115a22fb31f,网关:172.18.0.1, IPAddress:172.18.0.2,IPPrefixLen:16,IPv6Gateway:,GlobalIPv6Address:,GlobalIPv6PrefixLen :0,MacAddress:02:42:ac:12:00:02} }
I'm practicing making a Golang web app that interacts with a PostgreSQL database, each running on their own container.
I'm running the containers with docker-compose up
But I seem to be failing on getting the postgres container properly set up.
For brevity, links to Dockerfiles and other settings files are on this gist (let me know if you want it here instead).
version: '2' services: web_app: build: dockerfiles/web_app ports: - "9000:9000" volumes: - .:/go/src/gitlab/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 works fine. But when the application tries to open a database connection with:
var pgConf string = "user=web_app dbname=web_app sslmode=verify-full password=password" db, err := sql.Open("postgres", pgConf)I get the following error from docker compose:
Error creating new user: dial tcp [::1]:5432: getsockopt: connection refusedWhat can I do to make both containers talk to each other?
Thank you in advance.
解决方案When using the docker-compose v2, it's not needed to create links between services. Docker 1.9 and 1.10 allows you to connect to other containers on the same (custom) network through their name.
You should be able to connect using either the name of the service or the name of the container as a hostname. Given that the name of the container is generated by docker-compose, this is not really convenient to use, so for that reason, docker-compose also adds an alias with the service name to each container.
Take this very simple example. I've used an Nginx container for convenience, but the same should apply to your situation;
version: '2' services: web_app: image: nginx db: image: nginxFirst start the project (assuming;
$ docker-compose --project-name=test up -d Creating network "test_default" with the default driver Creating test_db_1 Creating test_web_app_1Then ping the "db" service from inside the test_web_app_1 container:
$ docker exec -it test_web_app_1 ping -c 2 db PING db (172.18.0.2): 56 data bytes 64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.108 ms 64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.243 ms --- db ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max/stddev = 0.108/0.175/0.243/0.068 msIf you inspect the test_db_1 container, you can see that docker-compose automatically added a "db" alias for the test_db_1 container;
$ docker inspect test_db_1Gives: (just the NetworkSettings.Networkspart)
"Networks": { "test_default": { "IPAMConfig": null, "Links": null, "Aliases": [ "db", "002b1875e61f" ], "NetworkID": "0f9e2cddeca79e5a46c08294ed61dee273828607f99014f6410bda887626be70", "EndpointID": "a941ab95586a8fdafc5075f9c5c44d745f974e5790ef6048b9e90115a22fb31f", "Gateway": "172.18.0.1", "IPAddress": "172.18.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:12:00:02" } }
更多推荐
如何将Docker Web应用程序容器连接到Docker PostgreSQL容器?
发布评论