This article talks about how to create a container with static ip address on QNAP NAS.

create container with static ip in qnap

When we create a container on the same layer of network as the QNAP itself based on “qnet” driver provided by QNAP Container Station, it will assign a new ip address every restart of the container.

Sometimes we want to access our service use a static ip. How to do it?

Jeager shares two ideas here:

Idea 1. Join a static network and specify the ip address when the container is initialized.

Create a “qnet_static” network, specify “ipv4_address” inside “qnet_static” network when the container is created.

docker compose

  1. write docker-compose.yml
version: '3'
services:
  static_sample:
    image: alpine
    command: ifconfig eth0
    networks:
      qnet-static:
        ipv4_address: 192.168.1.100

networks:
  qnet-static:
    driver: qnet
    driver_opts:
      iface: "eth0"
    ipam:
      driver: qnet
      options:
        iface: "eth0"
      config:
        - subnet: 192.168.1.0/24
          gateway: 192.168.1.1
  1. execute
$ docker compose up

Ideas 2. Join a dynamic network (which means using DHCP) and bind ip with MAC on the router in advance.

Diffrence between the static network and the dynamic network is whether specify subnet/gateway in network configuration or not.

  1. write docker-compose.yml
version: '3'
services:
  ubuntu:
    image: ubuntu:20.04
    container_name: ubuntu
    volumes:
      - /share/Container/ubuntu/root:/root
    command: "tail -f /dev/null"
    mac_address: 02:42:EC:99:57:43
    networks:
      qnet-dhcp
    restart: unless-stopped

networks:
  qnet-dhcp:
    driver: qnet
    driver_opts:
      iface: "eth0"
    ipam:
      driver: qnet
      options:
        iface: "eth0"

  1. execute
$ docker compose up
  1. Bind ip and MAC in router
  1. Restart docker
$ docker restart *****

QNET driver in QTS, refer to this LINK