Using FIFOs (a.k.a. named pipes) is a handy way for implementing IPC (inter-process communication). Because you can send to or receive from other processes like writing/reading ordinary files. Besides, FIFOs can also be stored in Docker’s volumes. That means we can use FIFOs to communicate between containers. Let’s take a look at a quick example. We’ll create a container that reads from a FIFO and display it.
volume=fifo-ipc-example
docker volume create "$volume"
docker run --rm -it -v ${volume}:/var/run/${volume} debian:11-slim mkfifo /var/run/fifo-ipc-example/message
docker run --rm -it -v ${volume}:/var/run/${volume} debian:11-slim cat /var/run/fifo-ipc-example/message
The docker volume
command creates a volume. We’ll use it to share a FIFO file. The first docker run
command creates a FIFO file (message
) on the volume. The second docker run
waits for input to the FIFO. If another process writes to the FIFO, the cat
command will display it. Let’s open a new terminal and write something to the FIFO from another container.
volume=fifo-ipc-example
docker run --rm -it -v ${volume}:/var/run/${volume} debian:11-slim bash -c "echo 'Hello.' > /var/run/fifo-ipc-example/message"
Then you will see the string “Hello.” in the first terminal. That’s it. Don’t forget to clean the volume up after you are done.
docker volume rm fifo-ipc-example