Service Discovery
100%

Docker Compose · Lesson 3

Service Discovery

Learn how Compose services reach each other by name and how host access uses a published port.

A browser on the Docker host reaches the web service at its published address. A second container in the project has a different route. Compose normally connects services in the same project to a shared network, where they can look each other up by service name.

Requesting the Page from the Toolbox

Return to the practice folder that contains compose.yaml. If the toolbox has exited since the previous lesson, start it again with docker compose start toolbox. With both services running, use docker compose exec to run a command in the existing toolbox service. The BusyBox wget command requests a page. The -q option suppresses progress messages and -O - writes the page to the terminal.

$ docker compose exec -T toolbox wget -q -O - http://web:80/

The -T option disables allocation of an interactive terminal. The URL names the web service and its container port. Success returns the Nginx page HTML.

HTML returned inside the toolbox after requesting the web service by name

Choosing an Address from the Host

On the Docker host, open http://127.0.0.1:8080/ if you kept the original mapping. If you changed the host port in the previous lesson, use that port in the browser address. The toolbox still uses http://web:80/ because it connects inside the Compose network, where the service name web is available.

Suppose you change the host side of the mapping to 8081. Which example address also needs to change?

Keeping a Stable Service Name

When a container is replaced, its network address can change. Its Compose service name stays the same. A client can resolve the name again and reconnect. An already open connection to a replaced container may end.

The web container was recreated and now has a different address. What can the toolbox use for its next request?

Lesson complete

You finished Service Discovery

You can now select a service address based on where a request starts.

  • Use a service name and container port within the project network.

  • Use a published address for the browser on the host.

  • Resolve the service name again after a container is replaced.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Next Lesson
Back to Docker Compose