Docker

Assign static IP to Docker container

25 September 2026 · 6 min read

Assign static IP to Docker container

Assigning a static IP address to your Docker containers is crucial for establishing consistent network communication and simplifying service discovery within your application infrastructure. Whether you’re orchestrating a complex microservices architecture or deploying a single containerized application, understanding how to manage IP addressing is fundamental. This guide delves into various methods for assigning static IPs, empowering you to optimize your Docker networking setup for reliability and performance.

Why Static IPs for Docker Containers?

Dynamic IP allocation, while convenient, can introduce challenges when containers need to communicate predictably. Services relying on DNS might experience delays due to propagation times, and troubleshooting can become more complex when IP addresses change unexpectedly. A static IP provides stability, ensuring that your services remain accessible at a known address. This is particularly important for applications that expose ports to external networks or interact with other systems that depend on fixed IP addresses. For instance, if you’re running a database server in a Docker container, assigning it a static IP simplifies connecting your application to it.

Moreover, static IPs enhance the efficiency of inter-container communication. Instead of constantly resolving dynamic addresses, containers can directly connect using the pre-assigned static IP, reducing latency and improving overall performance.

Consider a scenario where you’re running a web server and a database server in separate Docker containers. With static IPs, the web server can directly connect to the database without relying on DNS resolution, resulting in faster response times and a more streamlined architecture.

Using Docker Compose for Static IP Assignment

Docker Compose simplifies the process of defining and managing multi-container applications. It allows you to specify static IPs for your containers directly within the docker-compose.yml file. This method provides a clean and centralized way to configure your networking, making it easy to manage and version control.

Within the docker-compose.yml file, you can use the networks section to define a custom network and then assign static IPs to individual containers using the ipam configuration. This gives you granular control over the IP addressing scheme within your application’s network.

For example:

version: "3.9" services: web: image: nginx:latest networks: my_network: ipv4_address: 172.20.0.2 networks: my_network: ipam: config: - subnet: 172.20.0.0/16 

Manual Network Configuration with Docker Run

For more fine-grained control or when working without Docker Compose, you can manually configure networking using the docker run command. This approach involves creating a Docker network with a specific subnet and then assigning a static IP to the container when you start it.

This method provides flexibility for advanced networking scenarios, but requires more manual steps compared to using Docker Compose. You’ll need to first create a Docker network using docker network create and then specify the IP address when running the container with the --ip flag.

For example:

docker network create --subnet=172.25.0.0/16 my_custom_network docker run --net my_custom_network --ip 172.25.0.10 -d my_image 

Best Practices for Static IP Assignment

When assigning static IPs, careful planning is essential to avoid IP conflicts and ensure efficient network management. Consider using a dedicated subnet for your Docker containers to prevent clashes with other devices on your network. Documenting your IP address assignments is crucial for maintainability and troubleshooting.

Using a clear and consistent naming convention for your networks and containers can significantly improve the readability and organization of your Docker setup. This simplifies network management and makes it easier to identify specific containers within your application.

Regularly reviewing your IP address allocations can help prevent issues and ensure efficient utilization of your IP address space. This is especially important in dynamic environments where containers are frequently created and destroyed.

  1. Plan your IP address scheme.
  2. Use Docker Compose for simplified management.
  3. Document your assignments.
  • Choose a dedicated subnet.
  • Use descriptive names for networks and containers.

Infographic Placeholder: Visualizing Static IP Assignment in Docker

Troubleshooting Static IP Issues

Occasionally, you might encounter issues with static IP assignments. Common problems include IP conflicts, incorrect network configurations, and connectivity problems. Understanding how to diagnose and resolve these issues is crucial for maintaining a stable and reliable Docker environment.

Start by verifying your network configuration and ensuring that there are no IP conflicts within your Docker network or with other devices on your network. Check the container logs for any error messages related to networking, and use the docker inspect command to examine the network settings of your containers.

Network tools like ping and traceroute can be helpful for diagnosing connectivity issues between containers and external networks. These tools can help you identify network bottlenecks and pinpoint the source of connectivity problems.

Assigning static IP addresses to your Docker containers offers numerous advantages for building robust and predictable applications. By implementing the techniques outlined in this guide, you can establish a stable networking environment that enhances communication, simplifies service discovery, and improves the overall performance of your containerized applications. You can learn more about networking in the official Docker documentation. Also, check out this article on advanced Docker networking concepts and this insightful blog post on best practices for static IP management. For further customization options within your Docker environment, explore advanced configuration techniques. By embracing these strategies, you can streamline your development workflows and create highly efficient containerized deployments.

FAQ

Q: Can I assign multiple static IPs to a single Docker container?

A: Yes, you can achieve this by adding multiple network interfaces to the container, each with its own static IP configuration.

Q: What happens if I assign the same static IP to two different containers?

A: This will result in an IP conflict, and one or both of the containers may fail to start or experience network connectivity issues.

Question & Answer :
I’m now trying to assign a static IP 172.17.0.1 when a Docker container be started up.

I use port 2122 as the ssh port of this container so that I let this container listen port 2122.

sudo docker run -i -t -p 2122:2122 ubuntu 

This command will run a Docker container with a random IP like 172.17.0.5, but I need to assign a specific IP to the container.

The following shell script is what I reference Docker documentation in advanced network settings.

pid=$(sudo docker inspect -f '{{.State.Pid}}' <container_name> 2>/dev/null) sudo rm -rf /var/run/netns/* sudo ln -s /proc/$pid/ns/net /var/run/netns/$pid sudo ip link add A type veth peer name B sudo brctl addif docker0 A sudo ip link set A up sudo ip link set B netns $pid sudo ip netns exec $pid ip link set eth0 down sudo ip netns exec $pid ip link delete eth0 sudo ip netns exec $pid ip link set dev B name eth0 sudo ip netns exec $pid ip link set eth0 address 12:34:56:78:9a:bc sudo ip netns exec $pid ip link set eth0 down sudo ip netns exec $pid ip link set eth0 up sudo ip netns exec $pid ip addr add 172.17.0.1/16 dev eth0 sudo ip netns exec $pid ip route add default via 172.17.42.1 

This shell script will assign a static IP 172.17.0.1 and link to the world fine. But whenever I try to ssh to this container from my local, it didn’t work. What’s the problem possibly I met?

Easy with Docker version 1.10.1, build 9e83765.

First you need to create your own docker network (mynet123)

docker network create --subnet=172.18.0.0/16 mynet123 

then, simply run the image (I’ll take ubuntu as example)

docker run --net mynet123 --ip 172.18.0.22 -it ubuntu bash 

then in ubuntu shell

ip addr 

Additionally you could use

  • --hostname to specify a hostname
  • --add-host to add more entries to /etc/hosts

Docs (and why you need to create a network) at https://docs.docker.com/engine/reference/commandline/network_create/