Skip to content

Instantly share code, notes, and snippets.

@christianberg
Last active January 10, 2023 16:58
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save christianberg/eaec4028fbb77a0c3c8c to your computer and use it in GitHub Desktop.
Save christianberg/eaec4028fbb77a0c3c8c to your computer and use it in GitHub Desktop.
Sharing a unix socket between a Docker container and it's host
  • Build the image from the Dockerfile above:
docker build --rm -t cberg-test .
  • Run a container, sharing the host dir /root/foo:
docker run --rm -v /root/foo:/foo cberg-test
  • In another shell, write something to the socket (which was created in /root/foo by the container):
echo 'hello docker' | socat - UNIX-CONNECT:/root/foo/bar.sock
  • See the output of the container in the first shell:
hello docker
FROM ubuntu
RUN apt-get update
RUN apt-get install -y socat
VOLUME /foo
CMD socat UNIX-LISTEN:/foo/bar.sock -
@dib130030
Copy link

FROM alpine
RUN apk --update add socat
CMD socat UNIX-LISTEN:/foo/bar.sock -

@younthu
Copy link

younthu commented Mar 25, 2019

in mac, i got connection refused error

@rctestbot
Copy link

docker run --name mysql-server -t
-v /var/run/mysqld:/var/run/mysqld
-d mysql:5.7
--socket=/var/run/mysqld/mysqld.sock
It does not work well ,when i ru the command docker logs mysql-server ,then found this:
[ERROR] Could not create unix socket lock file /var/run/mysqld/mysqld.sock.lock.

@aaszodi
Copy link

aaszodi commented Jan 10, 2023

in mac, i got connection refused error

Yes, because on the Mac Docker is running in a little Linux VM which is not the same machine as your Mac. Hence, no socket-based communication between the two. :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment