从 Windows Docker 主机连接到 dockerized postgres [英] Connect to dockerized postgres from Windows Docker host

查看:34
本文介绍了从 Windows Docker 主机连接到 dockerized postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置是安装了 postgres(不是数据库,只有命令行工具)的 Windows 10 Pro 机器上的 Docker for Windows(版本 18.09.2).

my setup is Docker for Windows (Version 18.09.2) on a Windows 10 Pro machine with installed postgres (not a database, only the command line tools).

我使用 docker hub 运行官方 postgres 镜像

I run the official postgres image from docker hub with

docker run -it -v D:/postgres:/home/dump --name some_postgres -e POSTGRES_PASSWORD=root -d postgres

之后我可以通过连接

docker exec -it <docker-id> bash

然后运行

psql -U postgres

一切正常.现在我想从我的主机连接.我打电话

Everything works fine. Now I want to connect from my host. I call

psql -h <local-ip> -U postgres

得到了

psql: could not connect to server: Connection refused (0x0000274D/10061)
        Is the server running on host "192.168.116.74" and accepting
        TCP/IP connections on port 5432?

我很确定,可以访问数据库.因为如果我改变我收到的 IP

I am pretty sure, the database could be accessed. Because if I change the IP I receive

psql: could not connect to server: Connection timed out (0x0000274C/10060)
        Is the server running on host "192.168.116.11" and accepting
        TCP/IP connections on port 5432?

有人知道我如何解决这个问题吗?还是我做错了什么?

Has anyone any clue how I can fix this issue? Or what I am doing wrong?

推荐答案

要从您的主机连接到容器,您必须将端口发布到您的主机.这是使用端口发布和 -p 选项:

To connect to a container from your host you have to publish ports to your host. That is done using port publishing and -p option :

docker run -it -v D:/postgres:/home/dump --name some_postgres -p 5432:5432 -e POSTGRES_PASSWORD=root -d postgres

注意 -p 选项.现在,当您尝试连接到本地主机上的端口 5432 时,流量将被重定向到容器的端口 5432.

Notice the -p option. Now when you will be trying to connect to port 5432 on your localhost, the traffic will be redirected to the port 5432 of the container.

这篇关于从 Windows Docker 主机连接到 dockerized postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆