How to use nmcli command in a docker container?

Hi Balena support team,

I’m trying to deactivate and delete the wifi network connections configured by balena wifi-connect in the docker container.
I installed and run wifi-connect in my django container. I didn’t installed Network-Manager in the container. But I was able to execute wifi-connect by adding some settings(dbus mount and network host setting) to django service in my docker-compose.yml file.
Also, I was able to deactivate and delete the wifi network connection by running the following commands on host OS.

sudo nmcli connection down id “TP-LINK-REAL3D” || true
sudo nmcli connection delete id “TP-LINK-REAL3D” || true

Now what I want to is to deactivate and delete the wifi network connection in the contanier, not in host OS. However, when I run the nmcli commands above, the container doesn’t recognize nmcli command because network-manager is not installed in the container.
So I installed network manager in the container by the following command.

apt-get update && apt-get install -y network-manager && systemctl mask NetworkManager.service

But I still can’t use nmcli commands in the container.

  1. Is there any way to run nmcli commands directly in the container without installing network-manger in the container?
  2. Even after network manager is installed on the container i still can’t run nmcli command. How can I fix it?
    I’ll share my docker-compose.yml file as necessary.

Thanks in advance.

Anatoli

Hi, under balenaOS we have the option to expose the host OS D-Bus socket to the running containers: https://www.balena.io/docs/learn/develop/runtime/#dbus-communication-with-host-os. It looks like you need figure out how to do that under Raspbian as well in a similar fashion.
Thanks,
Zahari

Thanks for your quick reply. Previously, when I was having a hard time running wifi-connect in docker, someone(I think he is Zane) taught me how to get the host D-bus into the container. Here is my docker-compose.yml file.


However, I couldn’t run nmcli command in the container. I think there is an error anywhere.
I hope you can help me about this problem.
Thanks in advance.

I see, so you have the right D-Bus environment variable exported. Looking at the error more closely it seems to me that most probably it is some package that is not installed in the container.

Alternatively you can try using the NetworkManager D-Bus API directly and avoid nmcli altogether: https://github.com/NetworkManager/NetworkManager/tree/master/examples. I would definitely go that route if I need to work with NetworkManager from inside a container.

I would definitely go that route if I need to work with NetworkManager from inside a container.

I’m a little confused because of this. You mean it’s necessary to install NetworkManager in the container to using nmcli commands? Do I have to make any script to use nmcli command? Is it impossible to use nmcli commands from inside the container? I thought it will be possible because I executed wifi-connect successfully using NetworkManager installed on hostOS.
As for the Network-Manager git repository you shared, I’m not sure what to do.
Could you please lead me in detail on what to do? I really need to delete the already configured network connections before running it to use wifi-connect reasonably. I think if there is a way to run nmcli command in the container, it would be helpful.

When using wifi connect, I can only connected to the last wifi network configured by it. If I run the above two nmcli commands on host OS, I can see all networks on balena app and connect the pi to another network. I need to run those nmcli commands by clicking a button on web page running on the container. That’s why I want to run nmcli commands in the container. Does it make sense?

Thanks.
Anatoli.

You need to execute the network manager commands through DBUS, which means you can’t just do nmcli inside the container and expect it to work. You can see here: https://github.com/NetworkManager/NetworkManager/blob/master/examples/shell/disconnect-device.sh for example how the dbus-send command is used to send a command through DBUS to the NetworkManager. I think the code there is a great example on how to achieve what you want to do.

Thanks for your reply @sradevski
I have the following questions.

  1. So can I run this script in a container to remove all wifi network connections?
  2. In order to run this script in the container, is it necessary to install dbus package in the container?
    Isn’t necessary to install network manger?
  3. My container is built and run by docker-compose.yml file. To execute this script in the container without any issue, what settings do I have to add to my docker-compose.yml file?

Thanks
Anatoli

yes you can run it in a container, and you only need to install the dbus package. See here how to enable DBUS: https://www.balena.io/docs/learn/develop/runtime/#dbus-communication-with-host-os, which explains everything on how to use DBUS. Let us know if you are still stuck.