我在 docker 容器中的服务器上安装了 Postgresql.我如何从外部连接到它,即从我的本地计算机?我应该应用什么设置来允许它?
I have Postgresql on a server in a docker container. How can I connect to it from the outside, that is, from my local computer? What setting should I apply to allow that?
推荐答案你可以这样运行 Postgres(映射端口):
You can run Postgres this way (map a port):
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres现在你已经将容器的 5432 端口映射到服务器的 5432 端口.-p <host_port>:<container_port> .所以现在你的 postgres 可以从你的 public-server-ip:5432
So now you have mapped the port 5432 of your container to port 5432 of your server. -p <host_port>:<container_port> .So now your postgres is accessible from your public-server-ip:5432
测试:运行 postgres 数据库(上面的命令)
To test: Run the postgres database (command above)
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 05b3a3471f6f postgres "/docker-entrypoint.s" 1 seconds ago Up 1 seconds 0.0.0.0:5432->5432/tcp some-postgres进入你的容器并创建一个数据库:
Go inside your container and create a database:
docker exec -it 05b3a3471f6f bash root@05b3a3471f6f:/# psql -U postgres postgres-# CREATE DATABASE mytest; postgres-# q转到您的本地主机(您有一些工具或 psql 客户端).
Go to your localhost (where you have some tool or the psql client).
psql -h public-ip-server -p 5432 -U postgres(密码 mysecretpassword)
(password mysecretpassword)
postgres=# l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+------------+------------+----------------------- mytest | postgres | UTF8 | en_US.utf8 | en_US.utf8 | postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres因此,您正在从本地主机访问数据库(在服务器上的 docker 中运行).
So you're accessing the database (which is running in docker on a server) from your localhost.
在这篇文章详细解释了.
更多推荐
从外部连接到 docker 容器中的 Postgresql
发布评论