Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c4da71c

Browse files
dwjftMisty Stanley-Jones
authored andcommitted
docs change for compose/networking (docker#3094)
* docs change for compose/networking Without this clarification, I was tripped up for a while because it wasn't easy to understand why containers were getting connection refused errors on ports defined for the host, not the container.
1 parent 415a358 commit c4da71c

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

compose/networking.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ For example, suppose your app is in a directory called `myapp`, and your `docker
2828
- "8000:8000"
2929
db:
3030
image: postgres
31+
ports:
32+
- "8001:5432"
3133

3234
When you run `docker-compose up`, the following happens:
3335

@@ -42,7 +44,15 @@ get back the appropriate container's IP address. For example, `web`'s
4244
application code could connect to the URL `postgres://db:5432` and start
4345
using the Postgres database.
4446

45-
Because `web` explicitly maps a port, it's also accessible from the outside world via port 8000 on your Docker host's network interface.
47+
It is important to note the distinction between `HOST_PORT` and `CONTAINER_PORT`.
48+
In the above example, for `db`, the `HOST_PORT` is `8001` and the container port is
49+
`5432` (postgres default). Networked service-to-service
50+
communication use the `CONTAINER_PORT`. When `HOST_PORT` is defined,
51+
the service is accessible outside the swarm as well.
52+
53+
Within the `web` container, your connection string to `db` would look like
54+
`postgres://db:5432`, and from the host machine, the connection string would
55+
look like `postgres://{DOCKER_IP}:8001`.
4656

4757
## Updating containers
4858

0 commit comments

Comments
 (0)