Isolating Containers with Network Policies

Tutorial 4 of 5

1. Introduction

In this tutorial, we will be learning how to isolate Docker containers using network policies. The goal is to provide an understanding of how to set up policies that restrict the network connections that containers can establish.

By the end of this tutorial, you'll have a good understanding of:

  • What Docker network policies are and how they work
  • How to create and apply these policies to isolate Docker containers

Prerequisites

Before moving forward with this tutorial, make sure you have:

  • Basic knowledge of Docker and Docker commands
  • Docker installed on your system
  • Basic understanding of networking concepts

2. Step-by-Step Guide

Understanding Docker Network Policies

A Docker network policy is a set of rules that control the traffic flow between containers. With network policies, we can define which containers can communicate with others, effectively isolating certain containers as needed.

Creating a Network Policy

To create a network policy, we need to write a JSON file that defines the communications allowed between containers.

Applying the Network Policy

Once the network policy is defined, we can apply it to the Docker daemon using Docker commands.

3. Code Examples

Example 1: Creating a Network Policy

{
  "name": "isolated_policy",
  "Description": "This policy isolates a specific container",
  "ContainerSelector": { "role": "db" },
  "Ingress": [
    {
      "Ports": [
        {
          "Protocol": "TCP",
          "Port": "3306"
        }
      ],
      "From": [
        { "ContainerSelector": { "role": "frontend" } }
      ]
    }
  ]
}

This JSON file defines a network policy called isolated_policy, which applies to containers with the role of db. It only allows incoming traffic (Ingress) on port 3306 from containers with the role frontend.

Example 2: Applying the Network Policy

$ docker network create --driver overlay --subnet=192.168.0.0/16 --gateway=192.168.0.100 --ip-range=192.168.1.0/24 my_network
$ docker network connect --ip 192.168.1.5 my_network my_container

First, we create a network with a specific subnet, gateway, and IP range. Then, we connect a container to this network with a specific IP.

4. Summary

In this tutorial, we learned about Docker network policies and how they're used to control the communication between containers. We also learned how to create and apply these policies.

Next Steps

To further your knowledge, consider exploring:

  • Other types of Docker network drivers
  • How to manage Docker networks
  • How to debug network policy issues

Additional Resources

5. Practice Exercises

  1. Create a network policy that only allows incoming traffic from a specific IP address.
  2. Create a network policy that blocks all outgoing traffic from a specific container.

Solutions

  1. The following policy allows incoming traffic only from the IP address 192.168.1.5:
    { "name": "ip_policy", "Ingress": [ { "From": [ { "ipBlock": { "cidr": "192.168.1.5/32" } } ] } ] }
  2. The following policy blocks all outgoing traffic from the container with the role app:
    { "name": "block_policy", "ContainerSelector": { "role": "app" }, "Egress": [] }

Tips for Further Practice

  • Try to create more complex network policies, involving more containers and different types of traffic.
  • Experiment with different network drivers and see how they affect the network policy.