A hostname is the human-readable name used to identify a host or service during network communication. In Docker Compose, a service name becomes the hostname other containers use. section 6.4.1
Compose creates a shared network for services. A container can reach another service by name, even when that service has no port exposed to the host machine. section 6.4.1
Calling an NGINX service by hostname
Define services named web and my-nginx-service, then enter the web container and run curl http://my-nginx-service or curl http://my-nginx-service:80. The web container receives NGINX’s standard HTML response through the Compose network. section 6.4.1
Using service names as hostnames provides several practical deployment behaviors:
- Avoids requiring every internal service to publish a port on the host. section 6.4.1
- Lets application services connect to databases such as Redis through the database service name. section 6.4.2
- Requires services to share the Compose network and use the declared service name. section 6.4.1
Listing 6.9 NGINX service in Docker Compose
services:
my-nginx-service:
image: nginx
web:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"
environment:
- PORT=8080
volumes:
- mywebdata:/app/data
volumes:
mywebdata:
Inspect the Compose definition that declares my-nginx-service and web, then relate those names to the curl hostname. section 6.4.1

