Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOpsSchool!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to Assign a Static IP to a Docker Container in Bridge Mode

In this guide, you’ll learn how to assign a static IP address to a Docker container using a custom bridge network. This IP address will remain fixed even after your host system reboots.


✅ Why You Need This

By default, Docker assigns dynamic IPs to containers via its bridge network. If you restart your server or container, the IP may change — which breaks networking for apps relying on fixed addresses.

This tutorial helps solve that by:

  • Creating a custom bridge network
  • Assigning a fixed IP
  • Enabling auto-restart after reboot

🧰 Prerequisites

  • Docker installed on your Linux system (docker -v to verify)
  • Root or sudo access

🔧 Step 1: Create a Custom Bridge Network

The default Docker bridge does not support static IP assignment. So, first create a custom one:

docker network create \
  --driver bridge \
  --subnet 192.168.100.0/24 \
  --gateway 192.168.100.1 \
  my_custom_bridge

✅ This creates a new Docker bridge network:

  • Subnet: 192.168.100.0/24
  • Gateway: 192.168.100.1
  • Name: my_custom_bridge

🚀 Step 2: Run a Container with a Static IP

Now, start your container with a fixed IP and auto-restart policy:

docker run -dit \
  --name my_httpd \
  --network my_custom_bridge \
  --ip 192.168.100.10 \
  --restart unless-stopped \
  httpd
Code language: CSS (css)

✅ This ensures:

  • 192.168.100.10 is always assigned
  • The container restarts after reboot
  • It uses your custom bridge

🔄 Step 3: Test Persistence After Reboot

Reboot your host system:

sudo reboot

After the reboot, run:

docker ps
docker inspect my_httpd | grep IPAddress

✅ You should see your container up and running with the same IP.


📝 Additional Notes

  • Avoid IP conflicts by managing assigned IPs manually.
  • You can replace httpd with any image (e.g., nginx, mysql, your-custom-app).
  • Use docker network inspect my_custom_bridge to check connected containers.

🧠 Bonus Tip: Docker Compose Version

Create a docker-compose.yml:

version: '3.7'
services:
  web:
    image: httpd
    container_name: my_httpd
    restart: unless-stopped
    networks:
      mynet:
        ipv4_address: 192.168.100.10

networks:
  mynet:
    driver: bridge
    ipam:
      config:
        - subnet: 192.168.100.0/24
          gateway: 192.168.100.1
Code language: JavaScript (javascript)

Then run:

docker-compose up -d

✅ Conclusion

You’ve now:

  • Created a custom Docker network
  • Assigned a static IP to a container
  • Ensured it persists across reboots

This is especially helpful for microservices, reverse proxies, and apps that require stable internal networking.


Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x