Skip to content

Commit 841eb40

Browse files
author
Craig Ringer
committed
simple example of docker client/server network
1 parent 01490ad commit 841eb40

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

docker/network_partition/Dockerfile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM debian:10
2+
ENV DEBIAN_FRONTEND noninteractive
3+
RUN apt-get -y update && \
4+
apt-get -y install iproute2 socat iptables
5+
ADD server /usr/local/bin/server
6+
ADD client /usr/local/bin/client
7+
RUN chmod a+x /usr/local/bin/server /usr/local/bin/client

docker/network_partition/README.md

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
A demo of docker networking between containers with ability to isolate
2+
communication between the nodes while not granting excessive priviliges.
3+
4+
Uses user defined locally scoped bridge network.
5+
6+
## Run the demo
7+
8+
Run with
9+
10+
docker-compose up
11+
12+
You'll see chatter between the nodes on stderr. Now you can mess with their
13+
networking externally using the methods below.
14+
15+
## Detach and attach containers
16+
17+
To break connectivity:
18+
19+
docker disconnect unstable network_partition_client_1
20+
21+
to resume connectivity
22+
23+
docker connect unstable network_partition_client_1
24+
25+
This will make the containers nonroutable. If there are existing TCP
26+
connections they won't break straight away, they'll keep retrying until their
27+
timeouts are hit, and they'll buffer sent data for retry.
28+
29+
## Messing with connectivity using iptables
30+
31+
You can use iptables within each container. Because each container has its own
32+
network namespace, this won't mess up the host's iptables rules.
33+
34+
Within either container, it's possible to do things like:
35+
36+
# blackhole from other container
37+
iptables -A INPUT -s client -j DROP
38+
39+
# delete blackhole
40+
iptables -D INPUT 1
41+
42+
# send TCP RST for other container
43+
iptables -A INPUT -s client -j DROP
44+
45+
# restore connectivity
46+
iptables -D INPUT 1
47+
48+
You can run this with `docker exec` e.g.
49+
50+
docker exec network_partition_client_1 iptables -A INPUT -s server -j DROP
51+
52+
## Docker-compose not required
53+
54+
Example uses docker compose, but it's simple to do with plain docker using the
55+
`docker network create` command and the `--network` and `--cap-add NET_ADMIN`
56+
flags to `docker run`.
57+
58+
e.g.
59+
60+
docker build -t net-demo .
61+
62+
docker network create unstable
63+
64+
docker run \
65+
--detach -n network_partition_client_1 \
66+
--entrypoint /usr/local/bin/client \
67+
--network unstable \
68+
net-demo
69+
70+
docker run \
71+
--detach -n network_partition_server_1 \
72+
--entrypoint /usr/local/bin/server \
73+
--network unstable \
74+
net-demo
75+
76+
## Note on docker and network namespaces
77+
78+
Docker uses network namespaces, but `ip netns` won't see them. Docker tracks it
79+
in `/var/run/docker/netns` but `ip netns` tracks them in `/var/run/netns`.
80+
81+
You can hack around this with
82+
83+
sudo rmdir /var/run/netns
84+
sudo ln -s /var/run/docker/netns /var/run/netns
85+

docker/network_partition/client

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
while : ; do
3+
( while : ; do date -Isec; sleep 1; done ) | socat -d -v STDIN tcp:server:9999
4+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: "3.9"
2+
services:
3+
server:
4+
build: .
5+
networks:
6+
- unstable
7+
entrypoint: /usr/local/bin/server
8+
cap_add:
9+
- NET_ADMIN
10+
client:
11+
build: .
12+
networks:
13+
- unstable
14+
entrypoint: /usr/local/bin/client
15+
cap_add:
16+
- NET_ADMIN
17+
networks:
18+
unstable:
19+
name: unstable
20+
driver: bridge

docker/network_partition/server

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
while : ; do
3+
socat -d -v tcp-listen:9999 stdout
4+
done

0 commit comments

Comments
 (0)