Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update compose networking for container-container communication #21481

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion content/manuals/compose/how-tos/networking.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ Within the `web` container, your connection string to `db` would look like
`postgres://db:5432`, and from the host machine, the connection string would
look like `postgres://{DOCKER_IP}:8001` for example `postgres://localhost:8001` if your container is running locally.

## Container to container communication

Those familiar with using `http://localhost:3000` to reference a local development URL, use `http://servicename:3000` instead when using docker compose.

Example: In local development you have a frontend node app running at `http://localhost:3000` which references a backend api running at `http://localhost:8080/api`. In Docker networking, you would replace the node app api reference to the backend api with `http://apiservice:8080/api`. Similarly, the frontend app would be referenced by service name `http://myapp:3000`
Comment on lines +64 to +66
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should clarify that this DNS resolution only works between compose services. E.g., if you would like to view your node frontend app in your browser, you would still need to publish the port from the container to the host and access it via loopback.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great idea, wouldnt want to confuse learners further by having people type the wrong port or something likehttp://servicename:3000. I think the sentence you wrote should be good, because the content just above in /compose/how-tos/networking/ describes host ports and container ports. Should I add a small example if it helps?

We should clarify that this DNS resolution only works between compose services. For example, if you would like to view your node frontend app in your browser running at container port 80 and host port 3000, you would still need to publish the port from the container to the host then you can view the node app at http://localhost:3000

Also I haven't heard of loopback before, what should I look into and should I mention loopback as well?


## Update containers on the network

If you make a configuration change to a service and run `docker compose up` to update it, the old container is removed and the new one joins the network under a different IP address but the same name. Running containers can look up that name and connect to the new address, but the old address stops working.
Expand Down Expand Up @@ -178,7 +184,7 @@ networks:

Instead of attempting to create a network called `[projectname]_default`, Compose looks for a network called `my-pre-existing-network` and connects your app's containers to it.

## Further reference information
## Further reference information

For full details of the network configuration options available, see the following references:

Expand Down
2 changes: 2 additions & 0 deletions content/manuals/engine/network/drivers/bridge.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ network.**

- **User-defined bridges provide automatic DNS resolution between containers**.

> **See also**: Learn how to communicate between containers by name (containername:port instead of localhost:port) in: [Container to container communication in Docker Compose](/manuals/compose/how-tos/networking.md#container-to-container-communication).

Containers on the default bridge network can only access each other by IP
addresses, unless you use the [`--link` option](../links.md), which is
considered legacy. On a user-defined bridge network, containers can resolve
Expand Down