getting docker networking working with ros2

Deprecated

This tutorial is deprecated because

it uses ipvlan instead of macvlan, making it not useful for wifi nics it’s about installing docker inside two vms… I have better examples now.

Virtualbox guest master setup

  • Networking
    • Select Internal networking

      according to here, internal networking fully supports broadcast. I suspect bridged will too…

  1. create a virtual machine

  2. install ubuntu 22.04 server edition

  3. update

  4. remove firefox and snapd

  5. install helpful tools

    sudo apt install -y net-tools
    
  6. install ubuntu-desktop-minimal

    sudo apt install -y ubuntu-desktop-minimal
    
  7. install docker, ensuring to add docker group to your user.

  8. install vm guest additions prerequisites

    sudo apt install -y gcc make perl
    
  9. install vm guest additions

    1. go to virtualbox menu and select “install guest additions”
    2. navigate to the cdrom and run autorun.sh
  10. restart

  11. pull your desired ros2 image. For example:

    docker pull osrf/ros:galactic-desktop
    
  12. shutdown the guest

  13. save a snapshot

  14. create two linked clones, regenerating all network interface ids and MACs.

Internal Networking

Guest 1 Setup

configuration

select “internal network”

netplan

sudo cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak
cat << EOT sudo tee /etc/netplan/00-installer-config.yaml 
network:
  ethernets:
    enp0s3:
      addresses:
      - 192.168.0.120/24
      dhcp4: no
      dhcp6: no
  version: 2
EOT

docker-compose

mkdir ~/docker
cd ~/docker
cat << EOT >docker-compose.yml
version: '2'

services:
  ros:
    container_name: ros2
    image: osrf/ros:galactic-desktop
    hostname: ros-host
    domainname: test
    networks:
      network:
        ipv4_address: 192.168.0.121
    command: ros2 run demo_nodes_cpp talker
    restart: unless-stopped

networks:
  network:
    driver: ipvlan
    driver_opts:
      parent: enp0s3
      ipvlan-mode: l2
    ipam:
      driver: default
      config:
        - subnet: 192.168.0.0/24
          gateway: 192.168.0.1
          ip_range: 192.168.0.1/24
EOT

Guest 2

configuration

select “internal network”

netplan

sudo cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak
cat << EOT sudo tee /etc/netplan/00-installer-config.yaml 
network:
  ethernets:
    enp0s3:
      addresses:
      - 192.168.0.130/24
      dhcp4: no
      dhcp6: no
  version: 2
EOT

docker-compose

mkdir ~/docker
cd ~/docker
cat << EOT >docker-compose.yml
version: '2'

services:
  ros:
    container_name: ros2
    image: osrf/ros:galactic-desktop
    hostname: ros-host
    domainname: test
    networks:
      network:
        ipv4_address: 192.168.0.131
    command: ros2 run demo_nodes_cpp listener
    restart: unless-stopped

networks:
  network:
    driver: ipvlan
    driver_opts:
      parent: enp0s3
      ipvlan-mode: l2
    ipam:
      driver: default
      config:
        - subnet: 192.168.0.0/24
          gateway: 192.168.0.1
          ip_range: 192.168.0.1/24
EOT

Run

in clone 1 and 2, navigate to ~/docker and type

docker compose up

when done, please ensure you clean up your network interfaces on each guest clone by typing

docker compose down

Bridged Networking with a physical router

This example assumes a subnet of 192.168.8.xxx from the router. Go to both guests and select bridged networking. The guests ip addresses themselves can be assigned by the router’s dhcp server in this example.

clone 1 netplan

cat << EOT sudo tee /etc/netplan/00-installer-config.yaml 
network:
  ethernets:
    enp0s3:
#      addresses:
#      - 192.168.8.10/24
      dhcp4: yes
      dhcp6: no
  version: 2
EOT

clone 1 docker-compose.yml

cat << EOT >~/docker/docker-compose.yml
version: '2'

services:
  ros:
    container_name: ros2
    image: osrf/ros:galactic-desktop
    hostname: ros-host
    domainname: test
    networks:
      network:
        ipv4_address: 192.168.8.11
    command: ros2 run demo_nodes_cpp talker
    restart: unless-stopped

networks:
  network:
    driver: ipvlan
    driver_opts:
      parent: enp0s3
      ipvlan-mode: l2
    ipam:
      driver: default
      config:
        - subnet: 192.168.8.0/24
          gateway: 192.168.8.1
          ip_range: 192.168.8.1/24
EOT

clone 2 netplan

cat << EOT sudo tee /etc/netplan/00-installer-config.yaml 
# This is the network config written by 'subiquity'
network:
  ethernets:
    enp0s3:
#      addresses:
#      - 192.168.8.20/24
      dhcp4: yes
      dhcp6: no
  version: 2
EOT

clone 1 docker-compose.yml

cat << EOT >~/docker/docker-compose.yml
version: '2'

services:
  ros:
    container_name: ros2
    image: osrf/ros:galactic-desktop
    hostname: ros-host
    domainname: test
    networks:
      network:
        ipv4_address: 192.168.8.21
    command: ros2 run demo_nodes_cpp listener
    restart: unless-stopped

networks:
  network:
    driver: ipvlan
    driver_opts:
      parent: enp0s3
      ipvlan-mode: l2
    ipam:
      driver: default
      config:
        - subnet: 192.168.8.0/24
          gateway: 192.168.8.1
          ip_range: 192.168.8.1/24
EOT