• Discover
  • Collections
  • Board
  • Create
  • Profile
  • Settings
Paths

Lesson 1: Docker Container Networking

Lesson 1: Docker Container Networking

3 min read

Introduction

For applications to be truly useful, they need to communicate. In Docker, this means understanding how containers connect to each other and to the outside world. This lesson will explain Docker's networking model, focusing on the default bridge network, user-defined bridge networks, and key concepts like DNS resolution within Docker environments. Mastering networking is critical for building multi-service applications.

Key Concepts

Default Bridge Network

When Docker is installed, a default bridge network named bridge is created. All containers that you run without specifying a network are automatically attached to this bridge network. Containers on the default bridge network can communicate with each other via their IP addresses, but not by container name.

  • Isolation: Provides network isolation for containers.

  • IP Addressing: Containers receive an internal IP address from the bridge network's subnet.

  • Limited DNS: Containers can only resolve other containers on the same default bridge by IP address, not by hostname.

User-Defined Bridge Networks

For more robust and flexible networking, Docker allows you to create your own user-defined bridge networks. These networks offer significant advantages over the default bridge network:

  • Automatic DNS Resolution: Containers on a user-defined network can discover and communicate with each other using their container names as hostnames.

  • Better Isolation: Provides explicit isolation for different application stacks.

  • Configurable: Allows for custom subnets, gateways, and other network options.

Network Drivers

Docker supports several network drivers, each suited for different use cases:

  • bridge: The default network driver. Suitable for single-host applications.

  • host: Removes network isolation between the container and the Docker host. The container directly uses the host's network stack.

  • null: Disables networking for the container.

  • overlay: Used for multi-host container communication (e.g., in Docker Swarm).

  • macvlan: Assigns a MAC address to a container, making it appear as a physical device on your network.

Example/Code

Inspecting Docker Networks

List all Docker networks:

bash
docker network ls

Inspect a specific network (e.g., the default bridge):

bash
docker network inspect bridge

Creating and Using a User-Defined Network

  1. Create a new bridge network:
    bash
    undefined

docker network create my-app-network ```

  1. Run containers on the new network: Now, launch two Nginx containers, ensuring they are connected to my-app-network.
    bash
    undefined

docker run -d --name web1 --network my-app-network nginx docker run -d --name web2 --network my-app-network nginx ```

  1. Verify communication: You can now access web2 from web1 using its name.

For example, from web1's shell:

bash
docker exec -it web1 bash # Inside web1 container capt-get update && apt-get install -y curl curl http://web2 # This should return Nginx's welcome page exit ``` ## Summary/Key Takeaways - Docker uses networks to enable communication between containers and the host. - The default `bridge` network connects containers but lacks name resolution. - User-defined bridge networks (`docker network create`) provide automatic DNS resolution and better isolation. - Different network drivers (`bridge`, `host`, `overlay`, etc.) cater to various architectural needs.
End of lesson
👏Well done!
Previous Lesson
Quiz: Module 2: Dockerfile and Image Management
Next Lesson
Lesson 2: Persistent Data with Docker Volumes

Course Content

0% Complete0/20 Lessons

Lesson 1: Introduction to Containers and Docker

Lesson 2: Installing Docker and Basic Commands

Lesson 3: Docker Images and Registries

Lesson 4: Running Containers and Port Mapping

Quiz

Lesson 1: Docker Container Networking

Lesson 2: Persistent Data with Docker Volumes

Lesson 3: Bind Mounts vs. Volumes

Lesson 4: Docker Compose for Multi-Container Applications

Quiz

Course Content

0% Complete0/20 Lessons

Lesson 1: Introduction to Containers and Docker

Lesson 2: Installing Docker and Basic Commands

Lesson 3: Docker Images and Registries

Lesson 4: Running Containers and Port Mapping

Quiz

Lesson 1: Docker Container Networking

Lesson 2: Persistent Data with Docker Volumes

Lesson 3: Bind Mounts vs. Volumes

Lesson 4: Docker Compose for Multi-Container Applications

Quiz