Docker Containers: Solving the Network Connectivity Loss After Host Restart
Learn how to persist network connectivity for Docker containers after a host restart. This post provides a comprehensive guide on tools, environment, and DevOps best practices to ensure uninterrupted container operation.

Introduction
Docker containers have revolutionized the way we deploy and manage applications. However, one common issue that Docker users face is the loss of network connectivity after a host restart. This can be frustrating, especially in production environments where high availability is crucial. In this post, we will explore the reasons behind this issue and provide practical solutions to persist network connectivity for Docker containers after a host restart.
Understanding the Problem
When a Docker container is started, it is assigned an IP address from the Docker bridge network. This IP address is used for communication between the container and the host, as well as with other containers on the same network. However, when the host is restarted, the Docker bridge network is recreated, and the IP address assigned to the container is lost. As a result, the container loses its network connectivity.
Docker Bridge Network
The Docker bridge network is a virtual network that connects multiple containers running on the same host. It is created by Docker when the first container is started. The bridge network is responsible for assigning IP addresses to containers and routing traffic between them.
1# Create a new Docker bridge network 2docker network create my-bridge-network 3 4# Inspect the bridge network 5docker network inspect my-bridge-network
Persisting Network Connectivity
To persist network connectivity for Docker containers after a host restart, we need to ensure that the Docker bridge network is preserved and the container's IP address is retained. Here are a few approaches to achieve this:
1. Using Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications. It allows us to define a network architecture for our application and preserve it across host restarts.
1# docker-compose.yml 2version: '3' 3services: 4 my-service: 5 image: my-image 6 networks: 7 - my-network 8networks: 9 my-network: 10 driver: bridge
1# Start the application using Docker Compose 2docker-compose up -d 3 4# Stop the application 5docker-compose down 6 7# Restart the host 8sudo reboot 9 10# Start the application again 11docker-compose up -d
2. Using Docker Network Create
We can create a Docker network with a specific name and driver, and then attach our container to it. This ensures that the network is preserved across host restarts.
1# Create a new Docker network 2docker network create --driver bridge my-persistent-network 3 4# Start a new container and attach it to the network 5docker run -d --name my-container --net my-persistent-network my-image
3. Using systemd
On Linux systems, we can use systemd to create a service that starts our Docker container and attaches it to a persistent network.
1# Create a new systemd service file 2sudo nano /etc/systemd/system/my-container.service 3 4# Add the following contents 5[Unit] 6Description=My Container Service 7After=docker.service 8 9[Service] 10Restart=always 11ExecStart=/usr/bin/docker run -d --name my-container --net my-persistent-network my-image 12 13[Install] 14WantedBy=default.target
1# Reload the systemd daemon 2sudo systemctl daemon-reload 3 4# Start the service 5sudo systemctl start my-container 6 7# Enable the service to start on boot 8sudo systemctl enable my-container
Common Pitfalls and Mistakes to Avoid
Here are some common pitfalls and mistakes to avoid when persisting network connectivity for Docker containers:
- Not specifying the
--net
option when starting a container, which can result in the container being attached to the default bridge network. - Not creating a persistent network before starting a container, which can result in the network being lost after a host restart.
- Not using Docker Compose or systemd to manage container startup and network attachment, which can result in inconsistent network configuration.
Best Practices and Optimization Tips
Here are some best practices and optimization tips for persisting network connectivity for Docker containers:
- Use Docker Compose to define and manage your container network architecture.
- Create persistent networks with specific names and drivers to ensure consistency across host restarts.
- Use systemd to create services that start your containers and attach them to persistent networks.
- Monitor your container logs and network configuration to detect any issues or inconsistencies.
Conclusion
In this post, we explored the issue of Docker containers losing network connectivity after a host restart and provided practical solutions to persist network connectivity. We discussed the use of Docker Compose, Docker network create, and systemd to ensure that container networks are preserved across host restarts. We also highlighted common pitfalls and mistakes to avoid and provided best practices and optimization tips for managing container networks.