Commit 45b5bdd8 authored by Hermann Mayer's avatar Hermann Mayer

BashRC: Refactored all docker helpers.

parent 2cad379e
......@@ -9,12 +9,12 @@
function docker-remove-stopped-containers()
{
if [ -z "$(docker ps -a -q)" ]; then
if [ -z "$(docker ps -a -f 'status=exited' -q)" ]; then
echo 'No stopped containers found.'
return 0
fi
docker rm --volumes --force $(docker ps -a -q)
docker rm --volumes --force $(docker ps -a -f 'status=exited' -q)
}
function docker-remove-untagged-images()
......@@ -24,32 +24,95 @@ function docker-remove-untagged-images()
function docker-remove-all-images()
{
docker rmi --force $(docker images -a | awk '{print $3}')
docker rmi --force $(docker images -aq)
}
# Based on http://www.commandlinefu.com/commands/view/13708/show-memory-usage-of-all-docker-lxc-containers
function docker-memory-usage()
{
TMP_FILE=$(mktemp)
(
echo 0 > ${TMP_FILE}
for line in `docker ps | awk '{print $1}' | grep -v CONTAINER`; do
name=$(docker ps | grep $line | awk '{printf $NF" "}')
mem=$(echo $(( `cat /sys/fs/cgroup/memory/system.slice/docker-$line*/memory.usage_in_bytes` / 1024 / 1024 )))
all=$[$(cat $TMP_FILE) + $mem]
for id in `docker ps -q`; do
name=$(docker ps -qf "id=${id}" --format '{{.Names}}')
mem=$(docker stats --no-stream --format '{{.MemUsage}}' \
"${id}" | cut -d' ' -f1)
calc_mem=$(echo $mem | cut -d'B' -f1 | LC_ALL=C numfmt --from=auto)
all=$[$(cat $TMP_FILE) + $calc_mem]
echo $all > ${TMP_FILE}
printf '%s | %dMB\n' $name $mem
printf '%s | %s\n' $name $mem
done | sort -rh -k3
printf '| -----\nMemory summary | %dMB' $(cat $TMP_FILE)
all=$(cat $TMP_FILE | LC_ALL=C numfmt --to=iec-i --suffix=B)
printf '| -----\nMemory summary | %s' $all
) | column -t -s '|'
rm ${TMP_FILE}
}
function docker-stop-all()
{
for line in `docker ps | awk '{print $1}' | grep -v CONTAINER`; do
docker kill -s 9 ${line}
done
docker ps -a -q | xargs -rn1 docker kill -s9
}
function __docker_prepare_user_container()
{
cat << EOF >${SCRIPT_FILE}
# Set the root password and grant root access to web
echo 'root:root' | chpasswd \
>/dev/null 2>&1
echo "${USER} ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
# Create a new identical user
if type "useradd" >/dev/null; then
groupadd -f -g "$(id -u)" "${USER}" \
>/dev/null 2>&1
useradd -g "$(id -u)" -m -o -u "$(id -u)" "${USER}" \
>/dev/null 2>&1
elif type "adduser" >/dev/null; then
addgroup -g "$(id -u)" "${USER}" \
>/dev/null 2>&1
adduser -h "${HOME}" -u "$(id -u)" -S -G "${USER}" "${USER}" \
>/dev/null 2>&1
fi
# Own the user home
chown "${USER}:${USER}" "${HOME}"
# Copy the profile
cp /tmp/profile "${HOME}/.profile"
chmod 0777 "${HOME}/.profile"
# Save command for later
if [ -n "\${1}" ]; then
echo \$* >/cmd
chmod +x /cmd
fi
# Switch to a user shell
su -s /bin/sh - "${USER}"
EOF
cat << EOF >${PROFILE_FILE}
# Set some shell options
export PS1='\`id -u -n\`@\`uname -n\`:\${PWD} $ '
alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."
alias .....="cd ../../../.."
alias c='clear'
alias ll='ls -lisa'
alias l='ls -lias'
alias mkdir='mkdir -p -v'
alias less='less -R'
alias x='exit'
# Go finally to the mount point
cd /data
# Execute command if available
if [ -f /cmd ]; then
/cmd
exit \$?
fi
EOF
}
function docker-user-container-here()
......@@ -64,33 +127,46 @@ function docker-user-container-here()
exit 1
fi
if [ -z "${PORT}" ]; then
local PORT=$RANDOM
local PORT=$(shuf -i 26000-30000 -n 1)
fi
if [ -z "${CONTAINER_PORT}" ]; then
local CONTAINER_PORT=3000
fi
echo "Bind host port ${PORT} to container port ${CONTAINER_PORT}"
local IMAGE="${1}"
shift
echo "# Bind host port ${PORT} to container port ${CONTAINER_PORT}"
echo
SCRIPT_FILE=$(mktemp --suffix '-docker-here-init')
PROFILE_FILE=$(mktemp --suffix '-docker-here-profile')
docker run --rm -i -v `pwd`:/data -e USER=`whoami` -p 0.0.0.0:${PORT}:${CONTAINER_PORT} \
-u "`id -u`:`id -u`" -t "${1}" \
/bin/bash -c 'echo "export PS1=\"\${debian_chroot:+(\$debian_chroot)}\${USER}@\h:\w\$ \";
alias l=\"ls -lisa\"; alias x=\"exit\"" > /tmp/.bashrc;
cd /data; bash --rcfile /tmp/.bashrc -i'
__docker_prepare_user_container "${SCRIPT_FILE}" "${PROFILE_FILE}"
docker run --rm -it \
-v `pwd`:/data \
-v "${SCRIPT_FILE}":/tmp/init \
-v "${PROFILE_FILE}":/tmp/profile \
-v ~/.ssh:${HOME}/.ssh \
-p 0.0.0.0:${PORT}:${CONTAINER_PORT} \
"${IMAGE}" \
/bin/sh -i -c "sh /tmp/init $*"
rm -f "${SCRIPT_FILE}" "${PROFILE_FILE}"
}
function docker-user-container-run()
function docker-container-ip()
{
if [ -z "${1}" ]; then
echo 'No image was specified.'
echo 'Usage: docker-container-run IMAGE'
echo 'No container id was specified.'
echo 'Usage: docker-container-ip ID'
exit 1
fi
local IMAGE="${1}"
shift
NETWORKS='.NetworkSettings.Networks'
docker run --rm -i -v `pwd`:/data -e USER=`whoami` \
-u "`id -u`:`id -u`" -t "${IMAGE}" \
/bin/bash -c "cd /data; $*"
docker inspect "${1}" \
--format "{{range \$net := ${NETWORKS}}}{{\$net.IPAddress}}{{end}}" \
| head -n1
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment