我正在按照https://registry.hub.docker.com/_/postgres/的官方postgres图像尝试docker postgres.
在文档中,它运行以下命令并使其工作:
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
docker run -it --link some-postgres:postgres --rm postgres sh -c 'exec psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'
所以我的问题是$POSTGRES_PORT_5432_TCP_PORT和$POSTGRES_PORT_5432_TCP_ADDR来自哪里?
最佳答案
它们可通过Docker链接机制启动到第二个容器.
从https://docs.docker.com/userguide/dockerlinks/#environment-variables开始:
When two containers are linked,Docker will set some environment
variables in the target container to enable programmatic discovery of
information related to the source container.…
The pattern followed is:
…
- PORT__ADDR will contain just the IP address from the URL (e.g. WEBDB_PORT_8080_TCP_ADDR=172.17.0.82).
- PORT__PORT will contain just the port number from the URL (e.g. WEBDB_PORT_8080_TCP_PORT=8080).