Lab - Install Docker and Docker Compose on Ubuntu

Context To serve as the basis when self hosting services at home or in the cloud.

Install Docker CE

Remove any existing Docker installations:

sudo apt remove docker docker-engine docker.io containerd runc

Install the prerequisite packages:

sudo apt install ca-certificates curl gnupg lsb-release

Install using Docker's apt repository:

sudo mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Add the Docker repository to the APT package manager:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Update the APT indices, then install the Docker Engine along with the Docker Compose plugin:

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Once installed, ensure that the Docker daemon is running:

sudo systemctl status docker

If it is not running (active), enable and start the Docker daemon with the following commands:

sudo systemctl enable docker
sudo systemctl start docker

To run docker as a non-root user, add user to the docker group

sudo groupadd docker
sudo usermod -aG docker $USER

Log out and log back in so that your group membership is re-evaluated.

If you're running Linux in a virtual machine, it may be necessary to restart the virtual machine for changes to take effect.

You can also run the following command to activate the changes to groups:

$ newgrp docker

Verify that you can run docker commands without sudo.

 docker run hello-world

Accessing Remote Docker

If the Ubuntu box running the Docker engine is somewhere else in the LAN, e.g. with IP 192.168.0.10, and your local machine has Docker or Docker client installed, you can set the environment variable DOCKER_HOST locally, e.g.,

export DOCKER_HOST=tcp://192.168.0.10:2375/

then you can issue docker commands from your terminal directly. Be sure not to do this over insecure network.

References