Home Explore Blog CI



docker

2nd chunk of `content/manuals/compose/how-tos/networking.md`
367e3d930399705560ec90c4d016fc2b76465079a36881b70000000100000936
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.

If any containers have connections open to the old container, they are closed. It is a container's responsibility to detect this condition, look up the name again and reconnect.

> [!TIP]
>
> Reference containers by name, not IP, whenever possible. Otherwise you’ll need to constantly update the IP address you use.

## Link containers

Links allow you to define extra aliases by which a service is reachable from another service. They are not required to enable services to communicate. By default, any service can reach any other service at that service's name. In the following example, `db` is reachable from `web` at the hostnames `db` and `database`:

```yaml
services:

  web:
    build: .
    links:
      - "db:database"
  db:
    image: postgres
```

See the [links reference](/reference/compose-file/services.md#links) for more information.

## Multi-host networking

When deploying a Compose application on a Docker Engine with [Swarm mode enabled](/manuals/engine/swarm/_index.md),
you can make use of the built-in `overlay` driver to enable multi-host communication.

Overlay networks are always created as `attachable`. You can optionally set the [`attachable`](/reference/compose-file/networks.md#attachable) property to `false`.

Consult the [Swarm mode section](/manuals/engine/swarm/_index.md), to see how to set up
a Swarm cluster, and the [Getting started with multi-host networking](/manuals/engine/network/tutorials/overlay.md)
to learn about multi-host overlay networks.

## Specify custom networks

Instead of just using the default app network, you can specify your own networks with the top-level `networks` key. This lets you create more complex topologies and specify [custom network drivers](/engine/extend/plugins_network/) and options. You can also use it to connect services to externally-created networks which aren't managed by Compose.

Each service can specify what networks to connect to with the service-level `networks` key, which is a list of names referencing entries under the top-level `networks` key.

Title: Advanced Networking in Compose: Links, Multi-host, and Custom Networks
Summary
When a service is updated, the old container is replaced with a new one that has a different IP address but the same name. Containers are responsible for reconnecting. Links allow you to define extra aliases for services. For multi-host deployments with Swarm mode enabled, the overlay driver enables communication across hosts. Custom networks can be defined using the `networks` key, allowing for more complex topologies and the use of custom network drivers or externally created networks.