Skip to content

Instantly share code, notes, and snippets.

@duruyao
Last active February 23, 2022 02:28
Show Gist options
  • Save duruyao/5a17209817c6e77aff4f94b619f858ed to your computer and use it in GitHub Desktop.
Save duruyao/5a17209817c6e77aff4f94b619f858ed to your computer and use it in GitHub Desktop.
The shortcut to run docker container.
#!/usr/bin/env bash
## date: 2021-12-06
## author: duruyao@gmail.com
## desc: the shortcut to attach docker container
##
## usage: dk-attach [CONTAINER]
##
data_file_path="${HOME}"/.dk_detach_containers
if [ ! -f "${data_file_path}" ]; then
echo -n >"${data_file_path}"
fi
container="$(tail -1 "${data_file_path}")"
if [ -n "$1" ]; then
container="$1"
fi
cmd="docker exec -it --user ${USER} ${container} /bin/bash"
echo "[COMMAND]"
echo "${cmd}"
echo ""
${cmd}
#!/usr/bin/env bash
## date: 2022-01-12
## author: duruyao@gmail.com
## desc: the shortcut to show information of docker containers launched by dk-run-detach
##
## usage: dk-list
##
data_file_path="${HOME}"/.dk_detach_containers
if [ ! -f "${data_file_path}" ]; then
echo -n >"${data_file_path}"
fi
docker ps -a | grep NAMES
grep -v '^ *#' <"${data_file_path}" | while IFS= read -r line; do
if [ -n "${line}" ]; then
docker ps -a | grep "${line}"
fi
done
#!/usr/bin/env bash
## date: 2021-12-06
## author: duruyao@gmail.com
## desc: the shortcut to run docker container in the background
##
## usage: dk-run-detach [DOCKER_IMAGE] [CONTAINER_NAME] [MOUNT_DIR] [PORT_1] [PORT_2]
##
data_file_path="${HOME}"/.dk_detach_containers
if [ ! -f "${data_file_path}" ]; then
echo -n >"${data_file_path}"
fi
docker_image="duruyao/vimicro-ai:cpu-dev"
if [ -n "$1" ]; then
docker_image="$1"
fi
container_name="$(echo "${USER}.${docker_image}" | sed "s/^${USER}\..*\//${USER}./" | sed "s/\:/./")"
if [ -n "$2" ]; then
container_name="$2"
fi
mount_src_dir="/opt/share0/duruyao/project/AI_NPU"
if [ -n "$3" ]; then
mount_src_dir=$(readlink -f "$3")
fi
port_1="7776"
if [ -n "$4" ]; then
port_1="$4"
fi
port_2="7778"
if [ -n "$5" ]; then
port_2="$5"
fi
mount_dst_dir=/tmp/$(basename "${mount_src_dir}")
if [ -n "$(command -v nvidia-docker)" ]; then
docker_exec="nvidia-docker"
else
docker_exec="docker"
fi
cmd="${docker_exec} run --name=${container_name} --security-opt seccomp=unconfined --security-opt apparmor=unconfined -itd -e NEW_USER=${USER} -e NEW_USER_ID=$(id -u "${USER}") -e NEW_GROUP_ID=$(id -g "${USER}") -p ${port_1}:22 -p ${port_2}:7777 --rm -v ${mount_src_dir}:${mount_dst_dir} ${docker_image}"
echo "[COMMAND]"
echo "${cmd}"
echo ""
echo "[CONTAINER ID]"
${cmd} || exit
## write container name to file
echo "${container_name}" >>"${data_file_path}"
hostname=($(hostname -I))
echo ""
echo "[COMMAND TO ATTACH THE DOCKER CONTAINER]"
echo "dk-attach ${container_name}"
echo ""
echo "[COMMAND TO LOG INTO THE DOCKER CONTAINER]"
echo "ssh ${USER}@${hostname[0]}:${port_1}"
#!/usr/bin/env bash
## date: 2021-12-06
## author: duruyao@gmail.com
## desc: the shortcut to stop docker container
##
## usage: dk-stop [CONTAINER]
##
data_file_path="${HOME}"/.dk_detach_containers
if [ ! -f "${data_file_path}" ]; then
echo -n >"${data_file_path}"
fi
container="$(tail -1 "${data_file_path}")"
if [ -n "$1" ]; then
container="$1"
fi
cmd="docker stop ${container}"
echo "[COMMAND]"
echo "${cmd}"
echo ""
echo "[CONTAINER NAME]"
${cmd} || exit
sed -i "/${container}/d" "${data_file_path}"
#!/usr/bin/env bash
## date: 2022-01-12
## author: duruyao@gmail.com
## desc: the shortcut to show version of docker image
##
## usage: dk-version [DOCKER_IMAGE]
##
docker_image="duruyao/vimicro-ai:cpu-dev"
if [ -n "$1" ]; then
docker_image="$1"
fi
cmd="docker run --rm ${docker_image} VERSION"
echo "[COMMAND]"
echo "${cmd}"
echo ""
${cmd}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment