[英]Docker connect to postgres with specific host address
I want to connect to container with postgres DB by some unique name instead 127.0.0.1 address.我想通过一些唯一的名称而不是 127.0.0.1 地址连接到带有 postgres DB 的容器。
There is specific flag --add-host=name:ipaddrs
that will add routing to /etc/hosts file internally in container, but it isn't that I want.有特定的标志
--add-host=name:ipaddrs
将路由添加到容器内部的 /etc/hosts 文件,但这不是我想要的。
docker run --rm --add-host=postgresdb:127.0.0.1 -p 5430:5432 -d --name=docker-postgres-image -e DB_HOST=postgresdb -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=pspostgres -e POSTGRES_DB=app postgres
How I can change default routing to connect to container from localhost(127.0.0.1):5430 to any character words for ex.如何将默认路由更改为从 localhost(127.0.0.1):5430 连接到容器的任何字符,例如 ex。 postgres-db-service:5430?
postgres-db-服务:5430?
I hope understand you.我希望能理解你。 I think you need ahostname in your container, you can do this using the flag --hostname.
我认为您的容器中需要一个主机名,您可以使用 --hostname 标志来执行此操作。 So your command will look like this:
因此,您的命令将如下所示:
docker run -d --rm --hostname postgres-db-service \
-p 5430:5432 \
--name=docker-postgres-image \
-e DB_HOST=postgres-db-service \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=pspostgres \
-e POSTGRES_DB=app \
postgres
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.