Installing Docker

  • Following Docker’s official documentation, first uninstall all conflicting packages. apt-get may indicate some packages are not installed.

    Ubuntu:

    for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done
    

    Debian:

    for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done
    
  • Set up Docker’s apt repository:

    Ubuntu:

    # Add Docker's official GPG key:
    sudo apt-get update
    sudo apt-get install ca-certificates curl
    sudo install -m 0755 -d /etc/apt/keyrings
    sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
    sudo chmod a+r /etc/apt/keyrings/docker.asc
    
    # Add the repository to Apt sources:
    echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
      $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
      sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
    

    Debian:

    # Add Docker's official GPG key:
    sudo apt-get update
    sudo apt-get install ca-certificates curl
    sudo install -m 0755 -d /etc/apt/keyrings
    sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
    sudo chmod a+r /etc/apt/keyrings/docker.asc
    
    # Add the repository to Apt sources:
    echo \
      "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
      $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
      sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
    
  • Install Docker:

    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    

Building

I won’t go into detail about compilation. I use GitHub cloud compilation. After completion, you can find the openwrt-x86-64-generic-rootfs.tar.gz file in the Release section. Download it to create a Docker image.

Creating a Docker Image

This step can be fully integrated into GitHub Actions, but for now I’m doing it manually and will update GitHub Actions later.

  • Create Dockerfile:

    FROM scratch
    ADD openwrt-x86-64-generic-rootfs.tar.gz /
    WORKDIR /
    CMD ["/sbin/init"]
    
  • Build Docker image:

    docker build -t openwrt:latest .
    

Deploying on Linux

Due to macvlan’s limitations on communication between host and Docker containers, I was unable to ping the OpenWrt IP address after running these commands. The following is just for personal reference.

  • Assuming my lan interface is enp0s1, enable promiscuous mode:

    ip link set enp0s1 promisc on
    
  • Set up macvlan:

    docker network create -d macvlan \
        --subnet=192.168.2.0/24 \
        --gateway=192.168.2.1 \
        -o parent=enp0s1 \
        wrt_lan
    
  • Start the OpenWrt image:

    docker run -d --name openwrt \
        --net wrt_lan \
        --ip 192.168.2.66 \
        --privileged \
        openwrt /sbin/init
    
  • Enter the OpenWrt container and change the LAN interface to a static IP address:

    docker exec -it openwrt sh
    

    Then edit /etc/config/network:

    vi /etc/config/network
    

    Change it to:

    config interface 'lan'
            option type 'bridge'
            option ifname 'eth0'
            option proto 'static'
            option netmask '255.255.255.0'
            option ip6assign '60'
            option ipaddr '192.168.2.66'	# OpenWrt IP address
            option gateway '192.168.2.1'	# Main router IP
            option dns '192.168.2.1'		# Main router IP
    
  • Restart the OpenWrt network service:

    /etc/init.d/network restart