Docker & Docker-Compose

Docker & Docker-Compose

Nearly all of my guides will be utilizing Docker and Docker-Compose so I wanted to make sure there was a quick guide just for these installations to refer to. All of this is well documented on Docker's Website as well but feel free to use this guide too. In this guide, we will be installing Docker on Ubuntu Server 20.04 LTS x64.  I won't dive into installing Ubuntu Server in this guide as Ubuntu's website contains all of this information already, but I would recommend enabling ssh services during your Ubuntu install so you're ready to start working on the server right away.

Install Docker:

sudo apt-get update
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \ 
gnupg \
lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg -- dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo \ 
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update; sudo apt install docker-ce docker-ce-cli containerd.io -y

Verify Docker is running:

sudo docker run hello-world

If successful, you should see something like this:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest:sha256:0fe98d7debd9049c50b597ef1f85b7c1e8cc81f59c8d623fcb2250e8bec85b38
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
  To generate this message, Docker took the following steps:
      1. The Docker client contacted the Docker daemon.
      2. The Docker daemon pulled the "hello-world" image from the Docker Hub.(amd64)
      3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
      4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
  To try something more ambitious, you can run an Ubuntu container with:
    $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/

Now that Docker is installed, we'll install and configure docker-compose to execute as root:

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

The above will pull the latest docker-compose installation files for us. Then we'll make the binary executable and link it to our Docker installation binaries:

sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

Optionally, we'll add the docker user to run as root so we don't need to start each command with 'sudo' and authenticate every time we run docker commands:

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker
docker run hello-world

Great! We now have a working installation of Docker on Ubuntu. If you've run into any snags during this, refer to Docker's Installation Guide for further troubleshooting. I primarily use Docker-Compose in nearly all my guides here so you'll have to re-hash my guides if you prefer not to use it.