Beginner10 min readUpdated Jan 2025

How to Assign Static IP to a Docker Container — Simple Docker Networking Guide

Beginner-friendly visual explanation of how to assign a static IP address to a Docker container using Docker networks. Learn how Docker networking works internally and how to use a custom bridge network with a fixed IP.


1. Before Anything: Can Docker Containers Have Static IP?

Yes, but with a rule:

👉 A Docker container can only have a static IP if it is inside a user-defined custom network.

You cannot give a static IP in Docker’s default network.

So the process is:

Create custom network → Run container → Assign static IP

Let’s break everything down visually and simply.


2. Visual Understanding: Why You Need a Custom Network

Default Docker network (bridge) creates IPs automatically:

172.17.0.2  
172.17.0.3  
172.17.0.4  

You cannot control these.

So we create our own private mini-network:

Custom Network: 192.168.10.0/24  
Container IP:   192.168.10.20  

Visual diagram:

┌──────────────────────────────┐
│  Custom Docker Network       │ 192.168.10.0/24
│   ├── Container A (10.10)    │
│   ├── Container B (10.20)    │
│   └── Container C (10.30)    │
└──────────────────────────────┘

You now fully control IPs inside this network.


3. Step 1 — Create a Custom Bridge Network (with Subnet)

Run:

docker network create \
  --subnet=192.168.10.0/24 \
  mynetwork

Explanation:

  • 192.168.10.0/24 → your mini-private network
  • mynetwork → name of your network

Docker creates your own virtual LAN.


4. Step 2 — Run a Container With a Static IP

docker run -d \
  --name myapp \
  --net=mynetwork \
  --ip=192.168.10.20 \
  nginx

Meaning:

  • Container name → myapp
  • Network → mynetwork
  • Static IP → 192.168.10.20
  • Image → nginx

Check container:

docker inspect myapp | grep IPAddress

Output:

"IPAddress": "192.168.10.20"

Boom — static IP assigned 🎉


5. Visual Breakdown of the Whole Process

Create Network  
↓  
Choose IP range  
↓  
Run container with --ip  
↓  
Container gets fixed IP  
↓  
Other containers can reach it easily

This is clean and predictable.


6. Step 3 — Connect Multiple Containers With Static IP

Example:

docker run -d --name api --net=mynetwork --ip=192.168.10.21 node
docker run -d --name db  --net=mynetwork --ip=192.168.10.22 mysql
docker run -d --name web --net=mynetwork --ip=192.168.10.23 nginx

Visual Map:

API → 192.168.10.21  
DB  → 192.168.10.22  
Web → 192.168.10.23

This makes inter-container communication super easy.


7. Connect an Existing Container to Static IP (Optional)

If container is already created:

  1. Disconnect from old network
docker network disconnect bridge myapp
  1. Connect to custom network
docker network connect --ip=192.168.10.20 mynetwork myapp

Now container has static IP without recreating it.


8. When Should You Use Static IP?

✔ Good use cases:

  • When building a multi-container app
  • When containers need to talk to each other
  • When running local development environments
  • When doing networking experiments
  • When setting up internal DNS or proxies

❌ Avoid static IP when:

  • Using Docker Compose (names work better)
  • Scaling containers automatically
  • Deploying to production (dynamic networks are safer)

9. 5-Second Visual Summary

Static IP only works in custom networks  
↓  
Create network with subnet  
↓  
Run container with --ip  
↓  
Container now has permanent IP  

Done.


10. FAQs (SEO-Friendly + Beginner Questions)

Q1: Can I give static IP in default Docker network?

No. Only in user-defined networks.


Q2: What if the static IP is already taken?

Docker will throw an error — choose another IP.


Q3: Can multiple containers share the same static IP?

No. Each container must have a unique IP.


Q4: Can I change the IP of a running container?

Yes — disconnect and reconnect with docker network connect --ip.


Q5: Does static IP work in Docker Compose?

Yes, but not recommended — Compose usually uses service names instead of IPs.


🏁 Final Words (In Your Tone)

Giving a Docker container a static IP is basically like saying:

“Bro, don’t roam around with random IPs. Stay fixed at this address.”

Static IPs make networking predictable, clean, and easy to test. But remember: static IP is useful mostly for learning, local development, and controlled environments.

Once you understand this, Docker networking becomes much easier.


Mastered this concept?

Take the next step in your journey to becoming a senior developer.