Maxime Krier
Maxime Krier

Reputation: 101

nmcli in a docker container

i currently looking for running nmcli in a docker container

nmcli work great on my host but if i'm starting a privileged container nmcli does not work.

to start my container

sudo docker run --privileged --net host -it image_with_network-manager /bin/bash

and then running nmcli

nmcli dev wifi  => Error: Could not create NMClient object: Could not connect: No such file or directory

ifconfig inside my container is ok , i have eth0 and wlan0 of the host

Upvotes: 9

Views: 12997

Answers (4)

Auspex
Auspex

Reputation: 2254

It's not necessary to run the docker container as privileged:

My Dockerfile is:

FROM python

WORKDIR /app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# You need network-manager to get nmcli, but you DON'T WANT NetworkManager running in your container
# from https://docs.balena.io/reference/OS/network/#changing-the-network-at-runtime
RUN apt update && apt install -y network-manager iproute2 && systemctl mask NetworkManager.service.

COPY . .

CMD  [ "python3", "./program.py" ]

As noted in the comments, if you already have NM running on the host, you don't want it running in the container. Thus the systemctl mask.

docker-compose.yml is:

services:
  nm-service:
    build: .    
    security_opt:
      - apparmor:unconfined
    volumes:
      - /var/run/dbus:/var/run/dbus
    network_mode: host

The important bits are binding the DBUS volume (which enables sending to the host's DBUS) and security_opt which prevents AppArmor from denying access to DBUS

Upvotes: 0

try in docker, this create own dbus in docker

docker run -it --net=host image_name
        
dbus-uuidgen > /var/lib/dbus/machine-id
mkdir -p /var/run/dbus
dbus-daemon --config-file=/usr/share/dbus-1/system.conf --print-address

Upvotes: 2

weefwefwqg3
weefwefwqg3

Reputation: 1001

I run my container like this:

docker run -d -it --privileged=true --net host --volume /var/run/dbus:/var/run/dbus [other args here] on Ubuntu18.04 and it works.

Note: --volume /var/run/dbus:/var/run/dbus but not --volume /var/run/dbus, and the above volume sharing flag is used to share the outer system's bus. If you need the container to have its own bus, you will need to configure differently.

Upvotes: 6

Adam Mischke vanities
Adam Mischke vanities

Reputation: 41

The thing about networkmanager is that it runs on dbus. I did the same thing (stracing and seeing what was needed)

Try mounting /var/run/dbus as a volume.

sudo docker run --privileged --net host -it --volume /var/run/dbus image_with_network-manager /bin/bash

Upvotes: 3

Related Questions