Skip to main content

Installing Docker on Ubuntu

Install

  • Run this:
    ### Everything below can be copy/pasted as a one-liner
    . /etc/os-release && \
    sudo apt update && \
    sudo apt install -y apt-transport-https ca-certificates curl software-properties-common && \
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && \
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $VERSION_CODENAME stable" && \
    sudo apt update && \
    sudo apt install -y docker-ce docker-compose && \
     
    # Verify it's running
    sudo systemctl status docker
     
    # Update (create if need be) the docker configuration to use the CGNAT CIDR range for container networking
    sudo apt install -y moreutils jq && \
    sudo bash -c 'if [ ! -f "/etc/docker/daemon.json" ] ; then echo "{}" > /etc/docker/daemon.json ; fi ; jq '\''."default-address-pools"[0].base = "100.64.0.0/16" | ."default-address-pools"[0].size = 24'\'' /etc/docker/daemon.json | sponge /etc/docker/daemon.json' ; cat /etc/docker/daemon.json
     
    # Restart docker to pull in changes
    sudo systemctl restart docker
     
    # Set up cron task to clean up stale, unused docker images
    sudo bash -c 'cat << EOF > /etc/cron.daily/docker-image-prune.cron.sh
    #!/bin/bash
    ### Prune images older than 7 days
    docker image prune -af  --filter "until=$((7*24))h" 2> /dev/null > /dev/null
    EOF
    '
    sudo chmod u+x /etc/cron.daily/docker-image-prune.cron.sh