Skip to content

Instantly share code, notes, and snippets.

@toonetown
Last active May 20, 2016 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toonetown/cbb006b81e3e54eccbec301af9dbf7bf to your computer and use it in GitHub Desktop.
Save toonetown/cbb006b81e3e54eccbec301af9dbf7bf to your computer and use it in GitHub Desktop.
Allows passing of data from the host to a docker container
#!/bin/bash
#
# A script to facilitate passing data from host to container in a daemon
#
# To read a variable (from within the container):
# VAR="$(docker_pipe read)"
#
# !!! Note that reading a variable will block until it is written to !!!
#
# To write to the variable that is being read (from the host):
# echo "Value" | docker exec -i <CONTAINER_ID> docker_pipe write
: ${FIFO_NAME:=/tmp/docker_pipe}
function cleanup { rm -f "${FIFO_NAME}"; exit ${1}; }
case "${1}" in
"read")
trap cleanup SIGHUP SIGINT SIGTERM EXIT
mkfifo "${FIFO_NAME}" || exit $?
cat "${FIFO_NAME}" || exit $?
;;
"write")
[ -p "${FIFO_NAME}" ] || { echo "${FIFO_NAME} does not exist."; exit 1; }
cat /dev/stdin > "${FIFO_NAME}"
;;
*)
echo "Usage: ${0} <read|write>" >&2
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment