4

I have a named pipe that's been created in a Ubuntu Docker container (ubuntu-python). I'd like to send data into this pipe from the host operating system and read data from the pipe from the container.

Within Container:

I created the pipe within the container using

mkfifo stream

I then read from the stream using

cat stream

As expected, the command appears to hang because it's waiting for some input

Docker file

I then shared the named pipe to the host operating system using a volume

    volumes:
      - ./audio:/app/audio # stream is inside /app/audio

Host OS:

I ran

sudo bash -c "echo test > stream"

I expect this to write to the stream, this should cause the original cat stream command to unhang and print test, but both the input and the output commands continue to hang, showing that the data isn't actually being moved into the container. The reason I use bash -c is because I need the redirection to run as root.

This series of commands running on the host OS works perfectly, but once I introduce the volume and the movement of data between the host and the container, it no longer works. How can I write data to a named pipe from the host OS and have it show up in the container?

For reference, the host is WSL running Ubuntu 20.04, but I plan to deploy this on a Raspberry Pi 3B+ running Raspbian Lite. I also want to distribute this to other users of an open source project, so the solution should be host agnostic (atleast for hosts that can actually run a Ubuntu container).

Byte11
  • 204
  • 4
  • 12
  • Can you provide the exact commands you ran, and the exact order you ran them in? "I ran `mkfifo` in a container" with the Compose bind mount fragment after that are throwing me off a little. – David Maze Dec 21 '20 at 00:43
  • These are the commands I ran in the order I ran them. I left out semantic commands like `docker-compose up` and removed the full file locations, but that's it. When I say I ran `mkfifo` in a container, I used `docker exec -it /bin/bash` to access the shell and ran that command in the `/app/audio` folder which is mounted using a volume to the host OS. Hope that clears it up. – Byte11 Dec 21 '20 at 02:37

1 Answers1

0

Using mkfifo -m a+rw stream will allow any user to write to the stream instead of just root users. This is because the -m allows you to specify permissions on the pipe similar to chmod. As a result, the redirection no longer needs to run as root.

Now we can simply use echo test > stream from the host OS, and the output will appear properly in the container.

I'm not sure why writing to the pipe using bash -c didn't actually move the data into the container, but the -m flag for mkfifo means we don't need to use it.

Byte11
  • 204
  • 4
  • 12