Anterior: , Acima: Contêineres do Sistema Guix   [Conteúdo][Índice]


4.2.2 Rede em contêineres

What good is a Guix System running a PostgreSQL database service as a container when we can only talk to it with processes originating in the container? It would be much better if we could talk to the database over the network.

The easiest way to do this is to create a pair of connected virtual Ethernet devices (known as veth). We move one of the devices (ceth-test) into the net namespace of the container and leave the other end (veth-test) of the connection on the host system.

pid=5983
ns="guix-test"
host="veth-test"
client="ceth-test"

# Attach the new net namespace "guix-test" to the container PID.
sudo ip netns attach $ns $pid

# Create the pair of devices
sudo ip link add $host type veth peer name $client

# Move the client device into the container's net namespace
sudo ip link set $client netns $ns

Then we configure the host side:

sudo ip link set $host up
sudo ip addr add 10.0.0.1/24 dev $host

…and then we configure the client side:

sudo ip netns exec $ns  ip link set lo up
sudo ip netns exec $ns  ip link set $client up
sudo ip netns exec $ns  ip addr add 10.0.0.2/24 dev $client

At this point the host can reach the container at IP address 10.0.0.2, and the container can reach the host at IP 10.0.0.1. This is all we need to talk to the database server inside the container from the host system on the outside.

$ psql -h 10.0.0.2 -U test
psql (14.4)
Type "help" for help.

test=> CREATE TABLE hello (who TEXT NOT NULL);
CREATE TABLE
test=> INSERT INTO hello (who) VALUES ('world');
INSERT 0 1
test=> SELECT * FROM hello;
  who
-------
 world
(1 row)

Now that we’re done with this little demonstration let’s clean up:

sudo kill $pid
sudo ip netns del $ns
sudo ip link del $host

Anterior: Um banco de dados de contêineres, Acima: Contêineres do Sistema Guix   [Conteúdo][Índice]