Docker
Static IP
Container Networking
IP Address Configuration
Docker Container

Assign static IP to Docker container

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Docker containers are dynamic by nature. Each time you start a container, Docker assigns it a new IP address, unless explicitly configured otherwise. However, in certain scenarios—such as when managing databases or services that require consistent endpoint connectivity—you might need to assign a static IP address to a Docker container. This article will explore how to assign a static IP to a Docker container, highlighting technical details, examples, and best practices.

Networking in Docker

Before diving into static IPs, it's crucial to understand how Docker networking works. Docker provides several network drivers, such as bridge, host, and overlay. Here, we'll focus on the bridge network, which is the default for standalone containers.

Docker Network Types

  • Bridge Network: Default network driver, ideal for individual host containers.
  • Host Network: Removes network isolation between the container and Docker host; no IPAM (IP address management).
  • Overlay Network: Enables multi-host networking, allowing containers on different Docker hosts to communicate.

Assigning a Static IP

To assign a static IP, you need to create a user-defined bridge network. Docker does not allow assigning static IPs directly to containers on the default bridge network.

Steps to Assign a Static IP

  1. Create a User-defined Network
    Creating a bridge network with IPAM configuration enables setting custom subnets and IP addresses.
bash
   docker network create --subnet 172.18.0.0/16 custom_network

In this command, --subnet specifies the IP range for the network.

  1. Start a Container with a Static IP
    Use the --network and --ip flags to start a container with a static IP.
bash
   docker run -d --name web_app --network custom_network --ip 172.18.0.10 nginx

Here, --network connects the container to the custom_network, and --ip assigns the static IP 172.18.0.10.

Example: Static IP Assignment

Consider a scenario where you have a web service and a database. The web service needs to connect to the database using a consistent IP. Here's an example setup:

Create Network

bash
docker network create --subnet 172.20.0.0/24 my_app_network

Database and Web Service Containers

  • Database
bash
  docker run -d --name db_container --network my_app_network --ip 172.20.0.2 postgres
  • Web Service
bash
  docker run -d --name web_service --network my_app_network --ip 172.20.0.3 my_webapp_image

Verification

To verify, list the containers connected to the network:

bash
docker network inspect my_app_network

This will display the configuration, confirming the IP assignments.

Best Practices

  • Subnet Planning: Choose subnets that don't conflict with existing internal network ranges.
  • Version Control: Track your network configurations in version control (e.g., using Docker Compose).
  • Security: Limit your container's network connectivity to enhance security.

Key considerations for static IP assignments are summarized below:

Key PointDescription
Network TypeUse bridge for static IP assignment via user-defined network.
IP AssignmentUse Docker flags --network and --ip to define static IPs.
Network CreationCreate using docker network create --subnet.
Multiple NetworksContainers can connect to multiple networks if needed.
Conflict AvoidanceEnsure custom subnets do not overlap with host network subnets.

Conclusion

Assigning a static IP to a Docker container requires using a user-defined bridge network. By carefully planning network subnets and maintaining network configurations, you can achieve consistent and reliable connectivity between your containers. This setup is especially beneficial for applications that need stable network endpoints or wish to replicate traditional on-premise network behaviors in a containerized environment.


Course illustration
Course illustration

All Rights Reserved.