2

How can I access wpa_cli inside a docker?

I want to connect to wifi from inside a docker in Pi 3. For that I am using wpa_cli. But when I run wpa_cli inside the docker I get following error:

Could not connect to wpa_supplicant: (nil) - re-trying

Following is my docker-compose.yaml. I have given privileged access and set network to host.

version: '3.2' services:   blue:
    build: .
    container_name: blue
    network_mode: host
    restart: unless-stopped
    privileged: true
    logging:
      options:
        max-size: 50m

2 Answers 2

4

wpa_supplicant will not be running in the container unless you start it.

A major difference between docker containers and a normal environment is that there are no system services running in the container unless you run it with init or systemd as the base command, since it is init (systemd is an init implementation) that manages these.

Containers are intended to do specialised things, not simply duplicate the host environment. Rather than umpteen background processes running, you have only those that you explicitly start.

You do not need to need to use init to run wpa_supplicant, however -- and not having it running simplifies things considerably, because there are no networking daemons to interfere with your intentions.

wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/my.conf -B

Will start it, and go to the background if successful (-B). You may have to first:

ip link set wlan0 up

Both these require root privilleges. Wpa_supplicant and the wpa_cli interface use a local socket for communication, so should not require any other system services. You may need to indicate the socket; see man wpa_cli and ctrl_interface in the conf file.

Gotchas

If what you are trying to do is communicate with the wpa_supplicant already running in the host, you can't. This is not about permissions, this is about the logic of containerization. Although wpa_supplicant uses a socket to communicate, it is a unix local socket, meaning it is only accessible on the local system via a file path, not an inet address.

You should not need to do this. If you need to do things with the host networking, you need to do it from the host.

If that's not what you are up to (i.e., you want to actually initiate an internet connection from within the container), note wpa_supplicant is not the only component of such.

3
  • You answered what I asked for but I'm finding wpa_cli difficult to deal with. Maybe it would be better to just use wpa_supplicant to connect to a wifi with given SSID and Password. Is that possible? The problem is that after changing the conf file I have to restart Pi3.
    – Lokesh
    Commented Feb 11, 2019 at 6:38
  • 2
    I figured that out. I just hat to run the command you gave but before that I had to do kilall wpa_supplicant in the host. Then I could just change content of conf file and load wpa_supplicant to connect to desired wifi.
    – Lokesh
    Commented Feb 11, 2019 at 8:50
  • Yes, wpa_cli is in no way essential to the process.
    – goldilocks
    Commented Feb 11, 2019 at 13:28
0

I had the same issue on a Pi 4 and mounting the following volumes fixed the issue:

  • /usr/sbin/wpa_cli:/usr/sbin/wpa_cli
  • /etc/wpa_supplicant:/etc/wpa_supplicant
  • /var/run/wpa_supplicant:/var/run/wpa_supplicant
  • /tmp:/tmp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.