docker_deploy.sh
· 3.3 KiB · Bash
Исходник
#!/bin/bash
# detect release and add package sources
[ "$(lsb_release -rs)" = "testing" ] || [ "$(cat /etc/debian_version | awk -F. {'print $1'})" -ne "8" ] && echo exit 1
echo "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" > \
/etc/apt/sources.list.d/$(lsb_release -cs)-docker.list && \
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 0EBFCD88; \
# Install needed dependencies
aptitude install -y apt-transport-https ca-certificates bridge-utils curl; \
aptitude update; \
# Install docker and enable it
aptitude install -y docker-ce && \
systemctl enable docker && \
# Allow to relay from local networks (where our containers are running)
sed -i 's#127.0.0.0/8#127.0.0.0/8 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16#' /etc/postfix/main.cf && \
service postfix reload && \
# https://gist.github.com/steakknife/9094991#file-run_me_001__install_docker_and_fixes-sh-L20-L22 (enable swap resource limiting)
# https://tianon.github.io/post/2017/05/18/docker-setup-redux.html#configure-boot-parameters
[ -x /usr/sbin/update-grub ] && \
sed -i 's/\(GRUB_CMDLINE_LINUX_DEFAULT="quiet\)"/\1 cgroup_enable=memory swapaccount=1 systemd.legacy_systemd_cgroup_controller=yes vsyscall=emulate"/' /etc/default/grub && \
update-grub; \
# Enable live restore (https://docs.docker.com/config/containers/live-restore/)
cat > /etc/docker/daemon.json <<EOF
{
"live-restore": true
}
EOF
# Install docker-compose and bash completion
COMPOSE_VER=$(curl -s -o /dev/null -I -w "%{redirect_url}\n" https://github.com/docker/compose/releases/latest | grep -oP "[0-9]+(\.[0-9]+)+$") && \
curl -o /usr/local/bin/docker-compose -L https://github.com/docker/compose/releases/download/$COMPOSE_VER/docker-compose-$(uname -s)-$(uname -m) && \
[ -d /etc/bash_completion.d/ ] || mkdir -p /etc/bash_completion.d/ && \
curl -L https://raw.githubusercontent.com/docker/compose/${COMPOSE_VER}/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose && \
chmod +x /usr/local/bin/docker-compose && \
# Install ctop
CTOP_VER=$(curl -s -o /dev/null -I -w "%{redirect_url}\n" https://github.com/bcicen/ctop/releases/latest | grep -oP "[0-9]+(\.[0-9]+)+$") && \
curl -o /usr/local/bin/ctop -L https://github.com/bcicen/ctop/releases/download/v$CTOP_VER/ctop-$CTOP_VER-$(uname -s|tr '[:upper:]' '[:lower:]')-$(dpkg --print-architecture) && \
chmod +x /usr/local/bin/ctop && \
# Add some cleanup jobs
cat > /etc/cron.weekly/docker-cleanup <<EOF
#!/bin/bash
# See https://getintodevops.com/blog/keeping-the-whale-happy-how-to-clean-up-after-docker
# DELETE STOPPED CONTAINERS, AND VOLUMES AND NETWORKS THAT ARE NOT USED BY CONTAINERS
# docker system prune -a -f
# DELETE EXITED CONTAINERS
# docker container ps -aqf status=exited && docker container rm $(docker container ps -aqf status=exited)
# DELETE DANGLING AND UNTAGGED IMAGES
# docker images -q -f dangling=true && docker image rm $(docker images -q -f dangling=true)
# DELETE ORPHANED AND DANGLING VOLUMES
DOCKER_VOLUMES=\$(docker volume ls -qf dangling=true)
if [ "\${DOCKER_VOLUMES}" != "" ]; then
docker volume rm \${DOCKER_VOLUMES}
fi
# garbage collection, see https://github.com/spotify/docker-gc/blob/master/README.md
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc:ro -e MINIMUM_IMAGES_TO_SAVE=2 -e GRACE_PERIOD_SECONDS=432000 spotify/docker-gc | grep -v running
EOF
chmod +x /etc/cron.weekly/docker-cleanup
| 1 | #!/bin/bash |
| 2 | # detect release and add package sources |
| 3 | [ "$(lsb_release -rs)" = "testing" ] || [ "$(cat /etc/debian_version | awk -F. {'print $1'})" -ne "8" ] && echo exit 1 |
| 4 | echo "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" > \ |
| 5 | /etc/apt/sources.list.d/$(lsb_release -cs)-docker.list && \ |
| 6 | apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 0EBFCD88; \ |
| 7 | # Install needed dependencies |
| 8 | aptitude install -y apt-transport-https ca-certificates bridge-utils curl; \ |
| 9 | aptitude update; \ |
| 10 | # Install docker and enable it |
| 11 | aptitude install -y docker-ce && \ |
| 12 | systemctl enable docker && \ |
| 13 | # Allow to relay from local networks (where our containers are running) |
| 14 | sed -i 's#127.0.0.0/8#127.0.0.0/8 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16#' /etc/postfix/main.cf && \ |
| 15 | service postfix reload && \ |
| 16 | # https://gist.github.com/steakknife/9094991#file-run_me_001__install_docker_and_fixes-sh-L20-L22 (enable swap resource limiting) |
| 17 | # https://tianon.github.io/post/2017/05/18/docker-setup-redux.html#configure-boot-parameters |
| 18 | [ -x /usr/sbin/update-grub ] && \ |
| 19 | sed -i 's/\(GRUB_CMDLINE_LINUX_DEFAULT="quiet\)"/\1 cgroup_enable=memory swapaccount=1 systemd.legacy_systemd_cgroup_controller=yes vsyscall=emulate"/' /etc/default/grub && \ |
| 20 | update-grub; \ |
| 21 | # Enable live restore (https://docs.docker.com/config/containers/live-restore/) |
| 22 | cat > /etc/docker/daemon.json <<EOF |
| 23 | { |
| 24 | "live-restore": true |
| 25 | } |
| 26 | EOF |
| 27 | # Install docker-compose and bash completion |
| 28 | COMPOSE_VER=$(curl -s -o /dev/null -I -w "%{redirect_url}\n" https://github.com/docker/compose/releases/latest | grep -oP "[0-9]+(\.[0-9]+)+$") && \ |
| 29 | curl -o /usr/local/bin/docker-compose -L https://github.com/docker/compose/releases/download/$COMPOSE_VER/docker-compose-$(uname -s)-$(uname -m) && \ |
| 30 | [ -d /etc/bash_completion.d/ ] || mkdir -p /etc/bash_completion.d/ && \ |
| 31 | curl -L https://raw.githubusercontent.com/docker/compose/${COMPOSE_VER}/contrib/completion/bash/docker-compose > /etc/bash_completion.d/docker-compose && \ |
| 32 | chmod +x /usr/local/bin/docker-compose && \ |
| 33 | # Install ctop |
| 34 | CTOP_VER=$(curl -s -o /dev/null -I -w "%{redirect_url}\n" https://github.com/bcicen/ctop/releases/latest | grep -oP "[0-9]+(\.[0-9]+)+$") && \ |
| 35 | curl -o /usr/local/bin/ctop -L https://github.com/bcicen/ctop/releases/download/v$CTOP_VER/ctop-$CTOP_VER-$(uname -s|tr '[:upper:]' '[:lower:]')-$(dpkg --print-architecture) && \ |
| 36 | chmod +x /usr/local/bin/ctop && \ |
| 37 | # Add some cleanup jobs |
| 38 | cat > /etc/cron.weekly/docker-cleanup <<EOF |
| 39 | #!/bin/bash |
| 40 | # See https://getintodevops.com/blog/keeping-the-whale-happy-how-to-clean-up-after-docker |
| 41 | # DELETE STOPPED CONTAINERS, AND VOLUMES AND NETWORKS THAT ARE NOT USED BY CONTAINERS |
| 42 | # docker system prune -a -f |
| 43 | # DELETE EXITED CONTAINERS |
| 44 | # docker container ps -aqf status=exited && docker container rm $(docker container ps -aqf status=exited) |
| 45 | # DELETE DANGLING AND UNTAGGED IMAGES |
| 46 | # docker images -q -f dangling=true && docker image rm $(docker images -q -f dangling=true) |
| 47 | # DELETE ORPHANED AND DANGLING VOLUMES |
| 48 | DOCKER_VOLUMES=\$(docker volume ls -qf dangling=true) |
| 49 | if [ "\${DOCKER_VOLUMES}" != "" ]; then |
| 50 | docker volume rm \${DOCKER_VOLUMES} |
| 51 | fi |
| 52 | # garbage collection, see https://github.com/spotify/docker-gc/blob/master/README.md |
| 53 | docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc:ro -e MINIMUM_IMAGES_TO_SAVE=2 -e GRACE_PERIOD_SECONDS=432000 spotify/docker-gc | grep -v running |
| 54 | EOF |
| 55 | chmod +x /etc/cron.weekly/docker-cleanup |
| 56 |