Skip to main content

How to Assign a Static IP Address to Docker Containers

·500 words·3 mins
Docker Networking Containers
Table of Contents

By default, Docker assigns IP addresses to containers dynamically. This means a container may receive a different IP address each time it restarts. While this behavior works well for most applications, certain scenarios—such as fixed inter-container communication or legacy system integration—require containers to use static IP addresses.

This guide explains how to assign static IPs to Docker containers using custom networks and Docker Compose.

🧱 Using a User-Defined Docker Bridge Network
#

Docker’s default bridge network does not support fixed IP assignment. To use static IPs, you must create a user-defined bridge network with a predefined subnet.

Create a Custom Bridge Network
#

Use the following command to create a bridge network with a specific IP range:

docker network create --subnet=192.168.100.0/24 my_custom_network

This creates a network named my_custom_network with a dedicated subnet, allowing you to manually assign IP addresses to containers.

Run a Container with a Static IP
#

When starting a container, specify both the network and the desired IP address:

docker run -d --name my_container --net my_custom_network --ip 192.168.100.10 nginx

In this example, the Nginx container is assigned the fixed IP address 192.168.100.10.

Verify the Assigned IP Address
#

To confirm the container’s IP address, run:

docker inspect my_container | grep "IPAddress"

The output should show the assigned static IP:

"IPAddress": "192.168.100.10",

Advantages and Limitations
#

  • Advantages: Flexible subnet control and predictable container networking.
  • Limitations: IP addresses must be managed manually, and the configuration applies only within the custom network.

🧩 Assigning Static IPs with Docker Compose
#

For applications that consist of multiple containers, Docker Compose provides a cleaner and more maintainable approach to static IP assignment.

Define the Network and IP in docker-compose.yml
#

Below is an example configuration that assigns a static IP to a service:

version: '3'

services:
  web:
    image: nginx
    container_name: web_container
    networks:
      sec_network:
        ipv4_address: 192.168.100.10

networks:
  sec_network:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 192.168.100.0/24

This file defines a custom bridge network and assigns a fixed IP to the web service.

Start the Services
#

Deploy the containers using:

docker-compose up -d

Verify the Container IP
#

Check the assigned IP address with:

docker inspect web_container | grep "IPAddress"

Advantages and Limitations
#

  • Advantages: Ideal for multi-container setups and easy to manage through version-controlled configuration.
  • Limitations: Applies only to containers managed within the same Compose project.

🌐 Other Docker Network Modes
#

Docker also supports alternative network modes such as host and overlay. Overlay networks, typically used with Docker Swarm, can provide consistent networking across multiple hosts. However, these setups are more complex and generally intended for large-scale or production orchestration environments.


📌 Key Takeaways
#

Docker does not assign static IPs by default, but user-defined networks make fixed addressing possible. The appropriate method depends on your deployment needs:

  • For simple or single-container use cases, a custom bridge network with docker run is sufficient.
  • For multi-container applications, Docker Compose offers a structured and scalable solution.

By understanding Docker’s networking model, you can design container environments with predictable and reliable network behavior.

Related

How to Configure Static IP Addresses on Linux Distributions
·475 words·3 mins
Linux Networking IP Configuration
Understanding Network Port Mapping and Port Forwarding
·623 words·3 mins
Infrastructure Connectivity Cybersecurity Networking
Private vs Public IP Address Ranges Explained
·537 words·3 mins
IP Address Infrastructure Internet Protocols Networking