✍️ In a previous post, I explained how to enable IPv6 for docker and build a network using the command-line
💪 Here I detail how to do the same with docker compose.
👉 Firstly, I build the network via the command-line then docker compose to attach a container to this network.
🔥 Then, I build the network and container service, all in docker compose.
💻 This demo was uses Ubuntu 21.04 server.
Install docker compose
The docker compose installation notes are here. They worked for me without issue.
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Example 1: Use an existing network with docker compose
-
Manually create a network using the command-line. This is a user-defined bridge network. See my previous post for details about this type of network.
docker network create --ipv6 \ --subnet="2001:db8:1::/64" \ --gateway="2001:db8:1::1" \ mynetv6-1
-
Create a docker-compose.yaml file that utilises the existing
mynetv6-1
network.
version: "3.9" services: alp1: image: alpine:latest command: ping6 -c 4 2001:db8:1::1 networks: - mynetv6-1 networks: mynetv6-1: external: true
Notes
- This file uses version 3.9.
- A service
alp1
is created that uses thealpine:latest
image. This will ping the network gateway to demonstrate successful network & container build. - There is a
networks
subcommand under services to assign the service to themynetv6-1
network. - The dedicated
networks
section references the existingmynetv6-1
, and indicates that this network is already live with theexternal:true
attribute. Without this, docker compose will attempt to create a new network for thealp1
service.
Verify
joe@ub5:~/docker-test$ docker-compose up
Creating docker-test_alp1_1 ... done
Attaching to docker-test_alp1_1
alp1_1 | PING 2001:db8:1::1 (2001:db8:1::1): 56 data bytes
alp1_1 | 64 bytes from 2001:db8:1::1: seq=0 ttl=64 time=0.087 ms
alp1_1 | 64 bytes from 2001:db8:1::1: seq=1 ttl=64 time=0.141 ms
alp1_1 | 64 bytes from 2001:db8:1::1: seq=2 ttl=64 time=0.051 ms
alp1_1 | 64 bytes from 2001:db8:1::1: seq=3 ttl=64 time=0.140 ms
alp1_1 |
alp1_1 | --- 2001:db8:1::1 ping statistics ---
alp1_1 | 4 packets transmitted, 4 packets received, 0% packet loss
alp1_1 | round-trip min/avg/max = 0.051/0.104/0.141 ms
docker-test_alp1_1 exited with code 0
Example 2: Build a network and service with docker compose
This example simply requires a docker-compose.yml file.
version: "2.4"
services:
alp2:
image: alpine:latest
command: ping6 -c 4 2001:db8:a::1
networks:
- net2
networks:
net2:
name: net2
enable_ipv6: true
ipam:
config:
- subnet: 2001:db8:a::/64
gateway: 2001:db8:a::1
Notes
- Note the version number,
enable_ipv6
is not supported in version 3. - This creates a service, with the latest alpine image, attached to the
net2
network, and pings the gateway. - The dedicated network section creates
net2
, enables IPv6, and addresses the subnet and gateway.
Verify
joe@ub5:~/docker-test$ docker-compose up --remove-orphans
Creating network "net2" with the default driver
Recreating docker-test_alp2_1 ... done
Attaching to docker-test_alp2_1
alp2_1 | PING 2001:db8:a::1 (2001:db8:a::1): 56 data bytes
alp2_1 | 64 bytes from 2001:db8:a::1: seq=0 ttl=64 time=2054.280 ms
alp2_1 | 64 bytes from 2001:db8:a::1: seq=1 ttl=64 time=1053.982 ms
alp2_1 | 64 bytes from 2001:db8:a::1: seq=2 ttl=64 time=53.688 ms
alp2_1 | 64 bytes from 2001:db8:a::1: seq=3 ttl=64 time=0.052 ms
alp2_1 |
alp2_1 | --- 2001:db8:a::1 ping statistics ---
alp2_1 | 4 packets transmitted, 4 packets received, 0% packet loss
alp2_1 | round-trip min/avg/max = 0.052/790.500/2054.280 ms
docker-test_alp2_1 exited with code 0
Final note: Want to keep the container running?
Seeing as this is focused on docker networking, you might want to keep the container up and running, to ping about the network and such like.
- Just add
command: tail -F anything
to your service. - See here for details.
Top comments (1)
thanks, it works for me.