docker
docker-compose
hostname
configuration
containerization

How do I set hostname in docker-compose?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Setting Hostname in Docker Compose

When deploying applications using Docker Compose, you may encounter scenarios where you need to set a specific hostname for your container. Hostnames can be essential for networking within a Docker network, as they allow easy reference between services by name. This article will guide you through setting hostnames in Docker Compose, providing you with practical examples and technical insights.

Understanding Docker Compose and Hostnames

Docker Compose is a tool for defining and managing multi-container Docker applications. A docker-compose.yml file is used to configure the application's services, networks, and volumes. Each service in your Compose file can connect to a network by a unique hostname, enabling seamless communication between services.

A hostname in Docker Compose can be set using the hostname key under a service definition. This key allows you to define a custom name for your container that overrides the default behavior, where Docker automatically generates a hostname.

Practical Example of Setting a Hostname

Consider a simple application with two services, app and database. You want the app container to connect to the database container using a specific hostname.

Here's how you can configure the docker-compose.yml:

yaml
1version: '3.8'
2
3services:
4  app:
5    image: myapp:latest
6    hostname: app-service
7    depends_on:
8      - database
9    networks:
10      - app-network
11
12  database:
13    image: postgres:latest
14    hostname: db-service
15    environment:
16      POSTGRES_USER: user
17      POSTGRES_PASSWORD: password
18    networks:
19      - app-network
20
21networks:
22  app-network:

In this example:

  • The app service is assigned the hostname app-service.
  • The database service is assigned the hostname db-service.

This configuration allows app to communicate with database using the hostname db-service.

Technical Considerations

  • DNS Resolution: Within the Docker network, services can reference each other using hostnames. Docker’s internal DNS server resolves the service name to the appropriate IP address within the network.
  • Hostname Resolution Order: The service's hostname is used as part of the DNS resolution process. The order of lookups is hostname, then service name, then container ID.

Additional Subtopics

Using Environment Variables for Dynamic Hostnames

You can dynamically assign hostnames using environment variables. This is useful when your application environment requires dynamic configuration.

yaml
1version: '3.8'
2
3services:
4  web:
5    image: nginx:latest
6    hostname: "${HOSTNAME:-default-web}"
7

In this setup, the hostname will be default-web unless overridden by the environment variable $HOSTNAME.

Configuring a Custom Domain Name

You can also create a custom domain within the Docker network by using the domainname key. This can be beneficial in testing environments simulating real-world DNS configurations.

yaml
1version: '3.8'
2
3services:
4  app:
5    image: myapp:latest
6    hostname: app-service
7    domainname: mycompany.local
8

Here, the full hostname becomes app-service.mycompany.local.

Summary Table

Key PointExplanation
hostnameSets a specific name for your service container.
DNS ResolutionDocker's internal DNS resolves the set hostname to IP addresses.
Dynamic HostnamesUse environment variables to configure hostnames dynamically.
Full Domain ConfigurationCombine hostname and domainname for complete domain names.
Hostname Usage OrderHostname resolution follows hostname, service name, container ID.

Conclusion

Properly setting hostnames in Docker Compose can greatly enhance the networking capability of your application, ensuring seamless service communication and easier management of inter-service relationships. Leveraging features like environment variables and domain name configurations can provide additional flexibility and capabilities, suitable for various development and testing environments.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.