Skip to content

Instantly share code, notes, and snippets.

@duruyao
Last active October 18, 2022 08:51
Show Gist options
  • Save duruyao/ea8312f4420c7697f2562e2c6c53acb3 to your computer and use it in GitHub Desktop.
Save duruyao/ea8312f4420c7697f2562e2c6c53acb3 to your computer and use it in GitHub Desktop.
Check path permissions before starting the docker image.
#!/usr/bin/env bash
## date: 2022-10-13
## author: duruyao@gmail.com
## file: docker-checker.sh
## desc: check path permissions before starting the docker image
set -euo pipefail
function error_ln() {
# usage: error_ln "error message"
printf "\033[1;32;31m%s\n\033[m" "${1}"
}
function warning_ln() {
# usage: warning_ln "warning message"
printf "\033[1;32;33m%s\n\033[m" "${1}"
}
function info_ln() {
# usage: info_ln "info message"
printf "\033[1;32;32m%s\n\033[m" "${1}"
}
function debug_ln() {
# usage: debug_ln "debug message"
printf "%s\n" "${1}"
}
DOCKER_CMD=()
DOCKER_EXEC=("/usr/bin/docker")
DOCKER_OPTIONS=("$@")
read_only_dirs=()
admin_mail="duruyao@vimicro.com"
report_error=${DOCKER_COMMAND_CHECKER_REPORT_ERROR-true}
while (($#)); do
case "${1}" in
-v | --volume)
host_dir="${2//\:*/}"
if [ -n "${host_dir}" ]; then
if ! test -w "${host_dir}"; then
read_only_dirs+=("${2//\:*/}")
warning_ln "Warning: Read-only directory on the host: '${host_dir}'"
fi
fi
shift 2
;;
*)
shift 1
;;
esac
done
if [ ${#read_only_dirs[@]} -gt 0 ]; then
if ${report_error}; then
error_ln "Error: Do not mount the read-only directories on the host to the container"
debug_ln "Try run the command: 'export DOCKER_COMMAND_CHECKER_REPORT_ERROR=false'"
info_ln "Send mail to ${admin_mail} for more information"
exit 1
fi
warning_ln "Warning: Do not write to the read-only directories on the host in the container"
info_ln "Send mail to ${admin_mail} for more information"
printf "Are you sure you want to continue running the command? [Y/n] "
read -r continue
if echo "${continue}" | grep -q -E "n|N|no|No|NO"; then exit 0; fi
fi
DOCKER_CMD=(${DOCKER_EXEC[@]+"${DOCKER_EXEC[@]}"} ${DOCKER_OPTIONS[@]+"${DOCKER_OPTIONS[@]}"})
echo ${DOCKER_CMD[@]+"${DOCKER_CMD[@]}"} >"${HOME}"/.docker_command
${DOCKER_CMD[@]+"${DOCKER_CMD[@]}"}
#!/usr/bin/env bash
## date: 2022-10-17
## author: duruyao@gmail.com
## file: setup-docker-checker.sh
## desc: setup the docker-command-checker.sh
set -euo pipefail
prefix="${HOME}"/bin
mkdir -p "${prefix}"
curl -ksSL "https://gist.githubusercontent.com/duruyao/ea8312f4420c7697f2562e2c6c53acb3/raw/7892276e55a62a37e155fd88b15a548062e9af35/docker-command-checker.sh" -o "${prefix}"/docker-command-checker.sh
chmod +x "${prefix}"/docker-command-checker.sh
echo "
export PATH=\"${prefix}:\$PATH\"
alias docker=\"docker-command-checker.sh\"
#unalias docker
" >>"${HOME}"/.bashrc
source "${HOME}"/.bashrc
@duruyao
Copy link
Author

duruyao commented Oct 14, 2022

1. Setup

bash -c "$(curl -fksSL https://gist.githubusercontent.com/duruyao/ea8312f4420c7697f2562e2c6c53acb3/raw/a356b496e7df6e22073372f1b795dff3fdb28ae9/setup-docker-command-checker.sh)" && source ~/.bashrc

2. Usage

$ docker run -it --rm -v /opt:/opt -v $HOME:$HOME -v /var:/var ubuntu:18.04 bash
Warning: Read-only directory on the host: '/opt'
Warning: Read-only directory on the host: '/var'
Error: Do not mount the read-only directories on the host to the container
Try run the command: 'export DOCKER_COMMAND_CHECKER_REPORT_ERROR=false'
Send mail to duruyao@vimicro.com for more information

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