Skip to content

Instantly share code, notes, and snippets.

@samuelkarp
Created April 25, 2016 21:50
Show Gist options
  • Save samuelkarp/055b78f600ec54eabc5faf7c53801abf to your computer and use it in GitHub Desktop.
Save samuelkarp/055b78f600ec54eabc5faf7c53801abf to your computer and use it in GitHub Desktop.
docker/libnetwork default network affixed namespace
#!/bin/bash
# Retry a command until it succeeds or we've hit 10 attempts without it
# returning 0.
function retry_command
{
local tries=0
local rc=1
local max_tries=10
local cmd="${@}"
while [ "${tries}" -lt "${max_tries}" ]; do
/usr/bin/env bash <<< "${cmd}"
rc="${?}"
if [ "${rc}" -eq 0 ] ; then
return 0
else
((tries++))
sleep 10 || return 2
fi
done
return ${rc}
}
sudo /sbin/service docker start
retry_command "sudo docker info"
if [ "$?" -ne 0 ]; then
echo >&2 "Unable to start docker"
exit 1
fi
sudo docker pull registry:2
# Run a loop 100 times.
# Each iteration of the loop will start
# ten containers in parallel, then stop them in parallel.
# After the containers have stopped, check that all of
# the docker daemon's threads are in the same namespace.
# This is essentially the same repro as
# https://github.com/docker/libnetwork/issues/1113
# but with many more tries, since the bug is non-deterministic
i=1
errors=0
while [ "${i}" -le "100" ]; do
sudo /sbin/service docker restart
retry_command "sudo docker info"
if [ "$?" -ne 0 ]; then
echo >&2 "Unable to start docker"
exit 1
fi
cd /proc/$(pidof docker)/task
if [ "$?" -ne 0 ]; then
echo >&2 "/proc/$(pidof docker)/task doesn't exist or isn't a directory."
exit 1
fi
seq 10 | xargs -I{} -P10 sudo docker run -d registry:2
if [ "$?" -ne 0 ]; then
echo >&2 "Failed to start all containers"
exit 1
fi
sudo docker ps -q | xargs sudo docker stop
if [ "$?" -ne 0 ]; then
echo >&2 "Failed to stop all container"
exit 1
fi
namespacecount=$(sudo ls -l */ns/net \
| awk '{print $NF}' \
| sort -u \
| wc -l)
if [ "${namespacecount}" -ne "1" ]; then
echo "Found docker daemon threads in ${namespacecount} different namespaces"
let errors++
fi
let i++
done
if [ "${errors}" -ne "0" ]; then
echo >&2 "FAIL: Daemons threads found using ${namespacecount} different namespaces"
exit 1
fi
echo "All daemon threads used the same namespace"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment