dongshi9407
2016-03-27 03:35
浏览 150

如何将Docker Web应用程序容器连接到Docker PostgreSQL容器?

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.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 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 refused

What can I do to make both containers talk to each other?

Thank you in advance.

图片转代码服务由CSDN问答提供 功能建议

我正在练习制作一个与PostgreSQL数据库交互的Golang Web应用程序,每个应用程序都在各自的容器上运行。 / p>

我正在使用 docker-compose up

运行容器,但我似乎无法获取postgres容器

为简便起见,指向 Dockerfile s和其他设置文件的链接为在此要点上(让我知道是否要在这里使用它)。

 版本:'2'
服务:  
 web_app:
构建:dockerfiles / web_app 
端口:
-“ 9000:9000” 
卷:
-。:/ go / src / gitlab.com / repo / web_app 
#链接可能是 
#链接:
#-db 
depends_on:
-db 
#tty和stdin_open导致docker-compose在60sec之后与docker-machine断开连接。
#正在进行修复 。
#tty:true 
#stdin_open:true 
 db  :
构建:dockerfiles / db 
卷:
-数据:/ var / lib / postgresql / 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收到以下错误:

 创建新用户时出错:拨打tcp [:: 1]:5432:getsockopt:连接被拒绝
   
 
 

我该怎么做才能使两个容器都与之通信 彼此吗?

谢谢。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxing1853 2016-03-27 08:08
    已采纳

    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: nginx
    

    First 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_1
    

    Then 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 ms
    

    If 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_1
    

    Gives: (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"
        }
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题