为什么端口绑定与 Docker SDK 一起使用不起作用



我对Postgres镜像和Docker SDK(Python(有一个问题。如果我使用默认的Postgres端口5432-一切都很好。如果我将端口更改为8133-错误:

"Connection to localhost:8133 refused. Check that the host and port are correct and that the postmaster is accepting TCP/IP connections."

操作系统:Windows 11

Python:3.8

dockerpython包:vers。6.0.0

Dockerfile:

FROM postgres:13.8
COPY . /docker-entrypoint-initdb.d/

Python代码:

import dockerfile
client = docker.from_env()
...
client.images.build(path=docker_root, tag='my-image')
client.containers.run(
image='my-image', 
environment=[
'POSTGRES_USER=mytest',
'POSTGRES_PASSWORD=qwerty',
'POSTGRES_DB=testdb'
], 
ports={'8133/tcp': 5432},
volumes=['C:/Users/.../AppData/Local/Temp/test-postgres':/var/lib/postgresql/data'], 
name='my-container'
)

如果ports={'5432/tcp': 5432}-正常

如果ports={'8133/tcp': 5432}-无连接

我哪里搞错了?

您向后拥有ports:语法。查看client.containers.run()文档:

字典的键是要绑定到容器内部的端口。。。。字典的值是要在主机上打开的相应端口。。。。

PostgreSQL的容器端口总是5432,这需要是冒号之前的字典键。

client.containers.run(
image='my-image', 
ports={'5432/tcp': 8133}, # with 5432 on the left
...
)

最新更新