Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 184 of 8023. Docker Orchestration and StorageFree lesson

Configuring Static IP Addresses for Services

Assign fixed IP addresses to specific containers to support legacy applications that require persistent network identifiers within a bridge network.

In this lesson, you will learn how to assign a fixed, static IP address to a container within a custom Docker bridge network to satisfy the requirements of legacy applications.

Why Static IPs?

In modern containerized environments, we typically rely on Docker’s embedded DNS for service discovery, where containers find each other by name (e.g., http://database). However, some legacy applications are "hard-coded" to communicate with specific IP addresses. They cannot perform DNS lookups or dynamic service discovery, and they will fail if their target service changes its IP address every time it restarts.

To support these applications, we can manually assign a permanent IP address to a container on a custom bridge network.

Configuring Static IPs in Docker Compose

To define a static IP, you must first create a custom network that supports IPAM (IP Address Management) configurations. You then assign a specific address from that network’s subnet to your service.

Here is how you configure this in your docker-compose.yml:

services:
  legacy-db:
    image: postgres:12
    networks:
      app-net:
        ipv4_address: 172.20.0.10

networks:
  app-net:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.0/24

Important Considerations

When you manually manage IP addresses, you take on the responsibility of ensuring no two containers are assigned the same IP. If you assign 172.20.0.10 to two different services, one of them will fail to start due to an address conflict.

Furthermore, ensure that the IP you select falls within the subnet range defined in your network configuration. While this technique is vital for legacy compatibility, it bypasses the flexibility of Docker’s automatic network management, so it should be reserved strictly for applications that cannot function with standard DNS-based discovery.

Key Takeaway: By defining a custom bridge network with a specific subnet and assigning an ipv4_address to a service in Docker Compose, you can provide legacy applications with the persistent network identity they require to function.