Skip to content

Instantly share code, notes, and snippets.

@gdm85
Created September 9, 2014 09:59
Show Gist options
  • Save gdm85/62d376fcf85979dead1c to your computer and use it in GitHub Desktop.
Save gdm85/62d376fcf85979dead1c to your computer and use it in GitHub Desktop.
testcase for port-in-use Docker recognition issue
#!/bin/bash
## @author gdm85
## testcase for inconsistent Docker port-in-use recognition
#
TMPD=$(mktemp -d) || exit $?
function cleanup() {
local CONTAINER="$1"
docker kill "$CONTAINER" >/dev/null && docker rm "$CONTAINER" >/dev/null
}
IMG=port-testcase-7158
docker rmi "$IMG" 2>/dev/null
cd $TMPD && \
cat <<EOF > $TMPD/Dockerfile || exit $?
FROM ubuntu:latest
CMD sleep 600000
EXPOSE 23
EOF
cd $TMPD && \
docker build --tag="$IMG" . > /dev/null && \
cd && rm -rf $TMPF && \
docker run -d -p 23:23 --name=test1 $IMG || exit $?
## here the evil happens
IP=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' test1) && \
iptables -D FORWARD -d $IP/32 ! -i docker0 -o docker0 -p tcp -m tcp --dport 23 -j ACCEPT && \
cleanup test1
## finally, docker ports state is corrupted and can only be restored by restarting the daemon
docker run -d -p 23:23 --name=test1 $IMG
RV=$?
cleanup test1 || exit $?
if [ $RV -eq 0 ]; then
echo "$IMG: all OK"
else
echo "$IMG: docker port in use state is corrupted" 1>&2
exit $RV
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment