Skip to content

Instantly share code, notes, and snippets.

@lukaszlach
Created February 24, 2021 19:17
Show Gist options
  • Save lukaszlach/180cc44cfd476d75387c3cd47a20918e to your computer and use it in GitHub Desktop.
Save lukaszlach/180cc44cfd476d75387c3cd47a20918e to your computer and use it in GitHub Desktop.
Communication between containers - disk, shared memory, unix sockets
— term (network)
1$ docker run -it alpine:3
2$ docker run -it centos
1$ ip a
2$ ip a
2$ ping 172.17.0.3
1$ apk -U add nginx
1$ nginx -g 'daemon off;'
1$ mkdir -p /run/nginx
1$ nginx -g 'daemon off;'
2$ curl 172.17.0.3
— term (shared directory)
1$ docker volume create shared-volume
1$ docker run -it -v shared-volume:/shared -w /shared ubuntu
2$ docker run -it -v shared-volume:/shared -w /shared fedora
1$ touch file
1$ echo $RANDOM
2$ tail -F file
1$ echo $RANDOM >> file # multiple times
— term (shared memory)
1$ docker run -it --name alpine --ipc=shareable alpine:3
2$ docker run -it --ipc container:alpine alpine:3
1$$ apk add util-linux
2$$ apk add util-linux
2$$ ipcs -m
1$$ ipcmk -M 12345
2$$ ipcs -m
— term (fifo)
1$ docker volume create fifo
1$ docker run -it -v fifo:/fifo alpine:3
2$ docker run -it -v fifo:/fifo alpine:3
1$ mkfifo shared-fifo
2$ ls
2$ tail -F shared-fifo
1$ echo $(date) > shared-fifo # multiple times
3$ docker run -it -v fifo:/fifo alpine:3
3$ tail -F shared-fifo
1$ echo $(date) > shared-fifo # multiple times
2$ ^C
1$ echo $(date) > shared-fifo # multiple times
— term (unix socket)
1$ docker volume create unix-socket
1$ docker run -it -v unix-socket:/unix -w /unix debian:buster
1$$ apt-get update
1$$ apt-get install netcat-openbsd
1$$ nc -lkU socket.sock
2$ docker run -it -v unix-socket:/unix -w /unix alpine:3
2$$ ls
2$$ apk -U add curl
2$$ curl --unix-socket socket.sock url/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment