Hello, I'm Arthur.
Docker makes it easier to package applications and run them in isolated environments. However, when multiple containers need to communicate with each other, networking becomes an important part of the setup.
For example, a web application may run inside one container while its database runs inside another. Both services need to communicate, but the database does not necessarily need to be accessible from the public internet.
Understanding Docker networking helps developers build applications that are easier to manage, more secure, and simpler to troubleshoot.
In this guide, we'll explore Docker network types, container communication, port mapping, and practical networking commands.
1. Understand How Docker Networking Works
Docker provides networking capabilities that allow containers to communicate with each other and with external systems.
When you install Docker, it normally creates a default bridge network.
You can view the available networks with:
docker network ls
Example output:
NETWORK ID NAME DRIVER SCOPE
a1b2c3d4e5f6 bridge bridge local
b2c3d4e5f6a1 host host local
c3d4e5f6a1b2 none null local
Each network has a driver that determines how communication is handled.
The main Docker network types include:
- Bridge: Connects containers on the same Docker host.
- Host: Shares the host's network namespace.
- None: Disables normal network connectivity for the container.
- Overlay: Connects containers across multiple Docker hosts when supported by the deployment setup.
For many single-server applications, bridge networking is a practical starting point.
2. Create a Custom Bridge Network
Docker's default bridge network is useful for basic testing, but custom bridge networks provide better service discovery between containers.
Create a custom network:
docker network create app-network
Check that it exists:
docker network ls
You can inspect its configuration with:
docker network inspect app-network
Custom bridge networks allow containers to communicate using their container names or network aliases.
This makes it easier to build applications where multiple services need to communicate without relying on fixed IP addresses.
3. Connect Containers to the Same Network
Let's create two containers on the same network.
First, start a web container:
docker run -d \
--name web-app \
--network app-network \
nginx
Next, start another container:
docker run -d \
--name test-client \
--network app-network \
busybox sleep 3600
Both containers are now connected to app-network.
You can test communication between them using:
docker exec test-client ping -c 3 web-app
The container name can be resolved through Docker's embedded DNS on the custom network.
This is useful when an application needs to connect to another service without knowing its current container IP address.
4. Understand Port Mapping
Containers can run applications on internal ports without automatically exposing those ports to the host network.
For example, Nginx normally listens on port 80 inside its container.
To make it accessible through the host's port 8080, run:
docker run -d \
--name nginx-web \
-p 8080:80 \
nginx
The mapping follows this format:
HOST_PORT:CONTAINER_PORT
In this example, traffic reaching the host on port 8080 is forwarded to port 80 inside the container.
You can check the port mapping with:
docker port nginx-web
Then test the service locally:
curl http://localhost:8080
Port mapping is useful for applications that need to be accessed from outside the container.
However, exposing a port does not automatically make the application secure. Review firewall rules and application authentication before making a service publicly accessible.
5. Connect a Web Application to a Database
Consider a common application architecture:
- A web application handles user requests.
- A database stores application data.
- Both services run in separate containers.
You can create a custom network for these services:
docker network create backend-network
Start a database container:
docker run -d \
--name app-database \
--network backend-network \
-e MYSQL_ROOT_PASSWORD=change-this-password \
mysql:8.0
For a real deployment, use a securely managed password rather than placing credentials directly in command history or application files.
Now start a web application on the same network:
docker run -d \
--name app-web \
--network backend-network \
nginx
The web application can use app-database as the database hostname, provided the required database client and configuration are available.
This approach keeps the database separate from the web application while allowing internal communication.
For production applications, use dedicated credentials, persistent database storage, and appropriate network access controls.
6. Inspect Container Network Configuration
When networking does not work as expected, inspect the container configuration.
View a container's network details:
docker inspect web-app
You can also inspect a specific Docker network:
docker network inspect app-network
These commands help identify:
- Which network the container uses
- The container's IP address
- Connected containers
- Network aliases
- Port configuration
For a quick check, list running containers:
docker ps
Then review the relevant container's network settings.
7. Troubleshoot Common Docker Networking Problems
Docker networking problems can have several causes.
Containers cannot communicate
Check whether both containers are connected to the same custom network.
docker network inspect app-network
If a container is missing from the network, connect it:
docker network connect app-network container-name
The application cannot reach the database
Check the database container's status:
docker ps
Review its logs:
docker logs app-database
Confirm that the database service is running and accepting connections.
Also verify the hostname, port, username, and password configured in the application.
The application is not accessible externally
Check the container's port mapping:
docker port nginx-web
Confirm that the application is listening on the expected internal port.
Then review host firewall rules and any network restrictions that may block incoming traffic.
8. Plan Networking for Production Workloads
As an application grows, networking becomes more important.
A production environment may include multiple application containers, databases, background workers, and monitoring services.
A good networking design should define which services need to communicate and which should remain isolated.
For example:
- Public traffic should reach the web application.
- The web application should reach the database.
- The database should not be publicly accessible without a clear requirement.
- Internal services should communicate through appropriate networks.
For applications deployed on dedicated infrastructure, SeiMaxim's dedicated server hosting can be evaluated when planning the server resources needed to run Docker workloads.
The networking configuration should still be designed around the application's architecture, security requirements, and expected traffic.
9. Keep Networking Configuration Maintainable
As the number of containers increases, manually managing every container can become difficult.
Docker Compose allows developers to define services and networks in a configuration file.
For example:
services:
web:
image: nginx
ports:
- "8080:80"
networks:
- frontend
database:
image: mysql:8.0
networks:
- backend
networks:
frontend:
backend:
This configuration creates separate networks for the web and database services.
The web container is connected to the frontend network, while the database is connected to the backend network.
In this example, the two services do not share a network, so additional network configuration would be required if the web application needs to communicate with the database.
For larger deployments, SeiMaxim's server hosting solutions can be considered as part of the infrastructure planning process.
The key is to keep the network configuration understandable and avoid exposing services that do not need public access.
Final Thoughts
Docker networking is an important part of building reliable containerized applications.
Custom bridge networks allow containers to communicate using service names, while port mapping makes selected services accessible from outside the container.
By understanding network isolation, container discovery, and common troubleshooting commands, developers can build applications that are easier to manage and more secure.
Start with a simple network design, test communication between services, and expand the configuration as your application grows.
Top comments (0)