Skip to content

Instantly share code, notes, and snippets.

@uhidontkno
Created February 10, 2024 01:57
Show Gist options
  • Save uhidontkno/452b05b750e3b872dc4019b90867fe5d to your computer and use it in GitHub Desktop.
Save uhidontkno/452b05b750e3b872dc4019b90867fe5d to your computer and use it in GitHub Desktop.
Setup Docker via TCP
#!/bin/bash
VERBOSE=false
while [ "$#" -gt 0 ]; do
case "$1" in
-v|--verbose)
VERBOSE=true
shift
;;
*)
echo "WARN: Unknown option: $1 ... but I'm continuing because crashing over a goofy param is stupid."
;;
esac
done
if [[ $EUID -ne 0 ]]; then
echo "ERR: This script must be run as root."
exit 1
fi
if [ ! -d "/etc/systemd" ]; then
echo "ERR: This system seems to not be managed by systemd"
exit 1
fi
if [ -e "/etc/docker/daemon.json" ]; then
if [ "$VERBOSE" = true ]; then
echo "Removing existing daemon.json"
fi
rm /etc/docker/daemon.json # too lazy to append
fi
if [ "$VERBOSE" = true ]; then
echo "Configuring Docker to run over TCP..."
echo "Adding values to Dockerd's daemon.json file"
fi
# use 127.0.0.1 instead of 0.0.0.0 to be more secure
echo '{"hosts": ["tcp://127.0.0.1:2375", "unix:///var/run/docker.sock"]}' > /etc/docker/daemon.json
if [ "$VERBOSE" = true ]; then
echo "Setting up systemd's override.conf for Docker"
fi
if [ ! -d "/etc/systemd/system/docker.service.d/" ]; then
if [ "$VERBOSE" = true ]; then
echo "Making docker.service.d directory"
fi
mkdir /etc/systemd/system/docker.service.d/
fi
if [ -e "/etc/systemd/system/docker.service.d/override.conf" ]; then
if [ "$VERBOSE" = true ]; then
echo "Removing existing override.conf"
fi
rm /etc/systemd/system/docker.service.d/override.conf
fi
touch /etc/systemd/system/docker.service.d/override.conf
if [ "$VERBOSE" = true ]; then
echo "Writing configuration to override.conf"
fi
echo "[Service]
ExecStart=
ExecStart=/usr/bin/dockerd" > /etc/systemd/system/docker.service.d/override.conf
if [ "$VERBOSE" = true ]; then
echo "Reloading systemd daemon"
fi
systemctl daemon-reload
if [ "$VERBOSE" = true ]; then
echo "Restarting Docker service"
fi
systemctl restart docker.service
echo "Docker should be set up over TCP. Check for any errors here if it doesn't work."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment