Skip to main content

Setting up Docker and Docker Compose

A step by step tutorial to setup Docker and Docker Compose on Linux systems

This document describes the steps for setting up docker and docker-compose on a Linux system. It supports Ubuntu and CentOS-like flavours.

Create a file /var/tmp/init-docker.sh with the following contents:

#!/bin/bash

set -e

# Install docker
echo
echo "=========================="
echo "STATUS: docker"
echo "=========================="
# Can do command -v but better to be specific
if [[ -f '/usr/bin/docker' ]]; then
echo "STATUS: docker: installed"
else
echo "STATUS: docker: installing"
set -ex
curl -fsSL https://get.docker.com/ | sh -x
command -v docker > /dev/null
set +ex
sudo groupadd docker
sudo usermod -aG docker "$USER"
echo "STATUS: docker: installed"
newgrp -
fi

# Install docker-compose
echo
echo "=========================="
echo "STATUS: docker-compose"
echo "=========================="
# Can do command -v but better to be specific
if [[ -f '/usr/local/bin/docker-compose' ]]; then
echo "STATUS: docker-compose: installed"
else
echo "STATUS: docker-compose: installing"
set -ex
sudo curl -L \
"https://github.com/docker/compose/releases/download/2.23.3/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
command -v docker-compose > /dev/null
set +ex
echo "STATUS: docker-compose: installed"
fi

Run this file as a non-root user who can have sudo privileges

./init-docker.sh

Enable docker to run at startup

For Ubuntu:

systemctl --user enable docker

For Centos / Amazon Linux

sudo service docker start
sudo chkconfig docker on

Optional hardening steps

  • Allow Docker processes to be long-running and not get terminated on User-shell logout Users cannot set user services to run at boot time by default. The admin must enable this on an individual basis for each user. From the documentation:

    Enable/disable user lingering for one or more users. If enabled for a specific user, a user manager is spawned for the user at boot and kept around after logouts. This allows users who are not logged in to run long-running services. Takes one or more user names or numeric UIDs as argument. If no argument is specified, enables/disables lingering for the user of the session of the caller.

    sudo loginctl enable-linger $(whoami)
  • Prevent Docker daemon crashes to terminate the processes. By default, when the Docker daemon terminates, it shuts down running containers. You can configure the daemon so that containers remain running if the daemon becomes unavailable. This functionality is called live restore. The live restore option helps reduce container downtime due to daemon crashes, planned outages, or upgrades.