Skip to content

Instantly share code, notes, and snippets.

@hattwj
Last active August 19, 2016 14:17
Show Gist options
  • Save hattwj/5f95b978bea881db0e2311cb66c0e8f0 to your computer and use it in GitHub Desktop.
Save hattwj/5f95b978bea881db0e2311cb66c0e8f0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
##
# This script installs docker and docker-compose on an ubuntu host
# Additionally it:
# - Adds a user to the docker group (to simplify permissions in development)
# - Configures docker to run with bridged networking by default
##
set -e
if (( EUID != 0 )); then
echo "You must be root to do this." 1>&2
exit 1
fi
if [ -z "$1" ]; then
echo "Must specify user to add to docker group"
echo "example: $0 some_user"
exit 1
fi
DOCKER_USER=$1
echo "Updating apt sources and upgrading packages"
apt-get update -qqy
apt-get upgrade -qqy
apt-get install -qqy \
apt-transport-https \
bridge-utils \
ca-certificates \
dnsmasq \
git \
linux-image-extra-$(uname -r)
echo "Adding docker repository to apt"
apt-key adv \
--keyserver hkp://p80.pool.sks-keyservers.net:80 \
--recv-keys 58118E89F3A912897C070ADBF76221572C52609D
echo "deb https://apt.dockerproject.org/repo ubuntu-`lsb_release -cs` main" | tee -a /etc/apt/sources.list.d/docker.list
echo "Installing docker"
apt-get update -qqy
apt-get purge -qqy lxc-docker
apt-get install -qqy docker-engine
echo "Adding user: $DOCKER_USER to docker group"
usermod -aG docker $DOCKER_USER
echo "Installing docker-compose"
curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod ugo+x /usr/local/bin/docker-compose
echo "Configuring network bridge for docker"
if [ $(pidof /sbin/init) ]; then
echo "Sysvinit detected, writing config to /etc/default/docker"
cat <<EOF > /etc/default/docker
DOCKER_OPTS="--bridge=docker0"
EOF
elif [ $(pidof systemd) ]; then
## ubuntu with systemd
systemctl enable docker
echo "systemd detected, writing config to /etc/systemd/system/docker.service.d/docker.conf"
#https://docs.docker.com/engine/installation/linux/ubuntulinux/#/adjust-memory-and-swap-accounting
mkdir /etc/systemd/system/docker.service.d
cat <<EOF > /etc/systemd/system/docker.service.d/docker.conf
[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon --bridge=docker0 -H fd://
EOF
systemctl daemon-reload
systemctl restart docker
else
echo "No init system detected, exiting."
exit 1
fi
echo "
Installation complete!
User $DOCKER_USER has been added to the docker group. This user will need
to start a new login shell to access docker.
verify installation:
docker images"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment