Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 79 of 8021. Container FundamentalsFree lesson

Inter-Process Communication via Unix Domain Sockets

Establish high-performance communication channels between containers by mounting host-level Unix domain sockets into isolated container filesystems.

Inter-Process Communication via Unix Domain Sockets

In this lesson, you will learn how to enable high-performance communication between isolated containers by sharing a common Unix Domain Socket (UDS) file on the host filesystem.

The Concept

Normally, containers are isolated by Linux Namespaces, meaning they cannot easily "see" each other's memory or processes. While we often use network ports (TCP/UDP) for inter-container communication, network traffic incurs overhead because it must travel through the kernel’s network stack.

A Unix Domain Socket is a file-based communication channel that allows processes to exchange data directly through the kernel's memory without using the network stack. By mounting the same directory from the host into two different containers, both containers can "see" the same socket file. This acts as a high-speed "mailbox" where one container sends a message to the file, and the other instantly retrieves it.

Practical Implementation

To connect two containers via a socket, we use a shared volume (a directory on the host) and pass it to both containers.

Imagine we have a socket-dir on our host. We can start a server container and a client container, mapping that host directory into both:

# Start a server container, mounting the host socket directory
docker run -d -v /tmp/sockets:/shared --name server my-app-server

# Start a client container, mounting the same host directory
docker run -d -v /tmp/sockets:/shared --name client my-app-client

Inside both containers, the directory /shared points to the exact same location. If the server creates a socket file at /shared/app.sock, the client can immediately connect to that same path. Because they are reading and writing to the same filesystem location, they bypass the need for IP addresses or port forwarding entirely.

This approach is highly performant and secure, as you can restrict access to that specific directory rather than opening network ports that could be scanned or attacked. It is the preferred method for local, high-frequency data exchange between services running on the same host.

Key Takeaway: By mounting a shared host directory as a volume, you allow isolated containers to communicate via Unix Domain Sockets, bypassing network overhead for faster, localized data exchange.