Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker image error: "pa_context_connect() failed: Connection refused" with audiotest #74

Open
auwsom opened this issue Nov 22, 2021 · 3 comments

Comments

@auwsom
Copy link

auwsom commented Nov 22, 2021

Fresh docker image following the install docker at link below, so I would expect it to work out of the box. Using docker exec -it mycroft /bin/bash to start. Then mycroft-start audiotest and I get the error in the title. I've seen this error, I think when setting up Mycroft in a VM and it was because I had installed as root. I'm a little confused here because the Docker container doesnt have a user beside root.

The packages are installed into /opt so I might assume I'm supposed to create a user, so I did, and use the command below to start the container with that user, but I get another error:

Changing ownership of /opt/mycroft to user: with group: abc
sudo: effective uid is not 0, is /usr/bin/sudo on a file system with the 'nosuid' option set or an NFS file system without root privileges?
Starting audiotest
Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 183, in _run_module_as_main
....
  File "/opt/mycroft/mycroft/configuration/config.py", line 17, in <module>
    import inflection
ModuleNotFoundError: No module named 'inflection'

I'm working through the errors for clues, but either way, the instructions for Docker below are lacking the necessary steps to make it work.

https://mycroft-ai.gitbook.io/docs/using-mycroft-ai/get-mycroft/docker

@krisgesling
Copy link
Contributor

Hey not sure why inflection is not installed - but if you're still using this image - to get past this specific error you can run mycroft-pip install inflection within the Docker container.

We'll need to look at why it's not getting installed though.

@krisgesling krisgesling transferred this issue from MycroftAI/mycroft-core Nov 22, 2021
@auwsom
Copy link
Author

auwsom commented Nov 29, 2021

Hang in there with me for a bit.. this is going to be a lengthy post with a lot of details about a journey to get the docker image working.

  • The missing packages was because of the virtual environment not being activated. I thought this might be the case after thinking about it, but it's worth keeping on this thread for others with the same error. Installing inflection only results in having another package not found.
  • The biggest challenge with the docker-mycroft image is that there is no user other than root set. Pulseaudio gives some hints tha its not intended to run as root, and in some tests I had the error in the title resolve by just typing pulseaudio as a user instead of root.
  • I have about a week's worth of progress to document, so I'll start at the conclusion and work back for others with similar errors, with random hints as I clean up open tabs.
  • I got the container working using a standardized and commonly used image (lower chances of malware) 'minideb' image from DockerHub. The minideb images are curated by Docker itself I believe. There is an Ubuntu image of a similar size around 50mb that also has a good record and has been hardened for security by Canonical. I'll probably build a fresh version.
  • https://hub.docker.com/r/bitnami/minideb https://hub.docker.com/_/ubuntu
  • These minimal images dont have a lot of utilities. minideb doesnt even have ps, even though it runs on SysV instead of SystemD. ubuntu has ps, but not wget, etc.
  • After getting the initial 'Connection refused' error to resolve the first time, by installing a user (see commands below), it is essential to add that user to the 'audio' group. (adding user 'mycroft' would work, but 'ubuntu' is used in the mycroft code (will find) so using that)
  • Installing alsa-utils (~7mb) will give tools like aplay and arecord. aplay -l should show the same soundcard for root as user 'ubuntu' after its been added to the audio group. It should try to play a test sound from alsa-utils with the command below.
  • With a user and alsa happy, pulseaudio needs several args passed to it with the container creation.
  • The key here was this post: pa_context_connect() failed when X11 socket and DISPLAY variable are passed in docker run TheBiggerGuy/docker-pulseaudio-example#1
  • ..for this docker test image: https://hub.docker.com/r/thebiggerguy/docker-pulseaudio-example/
  • ..it is specifically to test audio in a basic docker image. The default scripts dont work, but the code in the issue does.
  • The args in that issue are similar to the docker setup from Mycroft docs: https://mycroft-ai.gitbook.io/docs/using-mycroft-ai/get-mycroft/docker
  • The last round of problems was getting errors like: "Unable to autolaunch a dbus-daemon without a $DISPLAY for X11" and "Unable to contact D-Bus: : /usr/bin/dbus-launch docker" and errors about "/root/.config/pulse/cookie"
  • Checking this cookie reveal a directory, which is because "If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v will create the endpoint for you. It is always created as a directory." https://stackoverflow.com/a/47099098/4240654
  • Deleting that dir and a making a new cookie file, and chmodding to 777 removed the cookie errors.
  • And finally, runing the docker container from a non-root user on the host got rid of the "Opening a playback stream with sample specification 's16le 2ch 44100Hz' and channel map 'front-left,front-right'. .. Connection failure: Connection refused ..pa_context_connect() failed: Connection refused" error!
  • One more step was to add the host root cookie to the guest user.
(after the building using dev_setup.sh from the mycroft-core.git)
useradd --create-home --shell /bin/bash --password ubuntu ubuntu 
usermod -aG audio ubuntu
apt install alsa-utils
su ubuntu && cd ~
aplay -l
aplay /usr/share/sounds/alsa/Noise.wav
pulseaudio 
(pulseaudio --help, if necessary)
export PATH="/home/ubuntu/mycroft-core/bin:$PATH"
  • Of course, run the mycroft-start audiotest command to make sure the mic is also working.
  • Oh, yeah, if there is a "sudo: effective uid is not 0, is /usr/bin/sudo on a file system with the 'nosuid" error, mine was because I symlinked the /var/lib/docker dir to another partition, and it had 'nosuid' as a default arg for security reasons.
  • On a VM: If there are "no soundcards found" with aplay -l as root, run sudo modprobe snd-hda-intel to see if the kernel has audio modules. A lot of the server image kernels dont, to keep them lightweight. If you see "OSError: [Errno -9996] Invalid input device (no default output device)" you may need to apt install pulseaudio.

sudo docker run -t --detach --device /dev/snd --volume /media/username/programs/mycroft:/root/.mycroft --env PULSE_SERVER=unix:${XDG_RUNTIME_DIR}/pulse/native -v ${XDG_RUNTIME_DIR}/pulse/native:${XDG_RUNTIME_DIR}/pulse/native -v ~/.config/pulse/cookie:/root/.config/pulse/cookie -v ~/.config/pulse/cookie:/home/mycroft/.config/pulse/cookie -p 8181:8181 --name mini-mycroft minideb-mycroft
To enter: (needs sudo when as normal user) sudo docker exec -it mini-mycroft /bin/bash -c "su mycroft" then cd ~/mycroft-core"

  • Portainer.io is a great docker container manager for linux - web-based.
  • pip can be upgraded with python -m pip install --upgrade pip to remove that error. There is another error for 'behave' that is for the testing suite Mycroft is using. https://mycroft.ai/blog/the-voight-kampff-test/
  • it would be great to have some of this info added to the Mycroft Docker installation docs

@auwsom
Copy link
Author

auwsom commented Nov 29, 2021

I'm creating a working version of the Mycroft docker image from here: https://hub.docker.com/r/mycroftai/docker-mycroft/
I will upload this to DockerHub here:

Here are the exact commands necessary. They could be added to the Mycroft DockerHub repo dockerfile.
First create the container with this command. MUST BE RUN AS NON-ROOT USER:
sudo docker run --tty --detach --device /dev/snd -e PULSE_SERVER=unix:${XDG_RUNTIME_DIR}/pulse/native --volume ${XDG_RUNTIME_DIR}/pulse/native:${XDG_RUNTIME_DIR}/pulse/native -v ~/.config/pulse/cookie:/root/.config/pulse/cookie -p 8181:8181 --name mycroft-docker mycroftai/docker-mycroft
Enter the container from a normal non-root user:
sudo docker exec -it mycroft-docker /bin/bash

You should hear the Mycroft pairing prompt within a minute or two of this. You can then run the CLI tool: mycroft-start cli

If you want to add a non-root user, add the pulse cookie either before or after creating the container -v ~/.config/pulse/cookie:/home/user/.config/pulse/cookie. If you add it before, a user directory will be created and you'll need the following commands to create the user. If adding after, you shouldn't need to chown the directory. Symlinking /opt/mycroft to the user home dir worked fine with my testing.

useradd -d /home/user --shell /bin/bash --password user user
usermod -aG sudo user
usermod -aG audio user
su - user
cd /opt/mycroft
sudo chown -R user:user /home/user

The documentation here is correct, except it must be run as a non-root user or else the cookie will come from the root dir and pulseaudio will give the error in the title of this issue. The documentation should really include a "sudo" in front of the docker command so it is clear that it must be run as a non-root user.
https://mycroft-ai.gitbook.io/docs/using-mycroft-ai/get-mycroft/docker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants