Chapter 1: How to Install and Use Docker on Ubuntu 22.04

Docker is a platform that enables developers to automate the deployment of applications inside lightweight, portable containers. Here's how to install and use Docker on Ubuntu Server 22.04:

Installation steps

Install the necessary packages that allow apt to use repositories over HTTPS:

sudo apt update && sudo apt install apt-transport-https \
ca-certificates curl software-properties-common

Add Docker’s official GPG key to verify the authenticity of the packages:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Add Docker’s official repository to your system’s software repository list:

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

Update the package index again to include Docker's packages:

sudo apt update

Install Docker Engine and related components:

sudo apt install docker-ce docker-ce-cli containerd.io

Check the status of Docker to ensure it's running:

sudo systemctl status docker

You should see output indicating that Docker is active and running.

Step 2: Enable Non-Root User Access (Optional)

To allow your user to run Docker commands without sudo, add your user to the Docker group:

sudo usermod -aG docker $USER

Apply the group membership changes:

newgrp docker

Now, you can run Docker commands without sudo.

Step 3: Test Docker Installation

Run a test container to verify that Docker is working correctly:

docker run hello-world

If Docker is installed correctly, you should see a message saying “Hello from Docker!”


Chapter 2: How to Install and Use Docker Compose on Ubuntu 22.04

Docker Compose is a tool that allows you to define and manage multi-container Docker applications. Here's how to install and use Docker Compose on Ubuntu 22.04:

Step 1: Install Docker Compose Plugin

Docker Compose is now included as a plugin in Docker Engine. To install it:

sudo apt install docker-compose-plugin

Step 2: Verify Docker Compose Installation

Check the version of Docker Compose to ensure it's installed correctly:

docker compose version

You should see output indicating the version of Docker Compose installed.

Step 3: Create a Docker Compose Project

To define and run multi-container applications:

  1. Create a project directory:
mkdir ~/myapp
cd ~/myapp
  1. Create a docker-compose.yml file:
vim docker-compose.yml

Add the following content to define a simple web application with Nginx:

snippet.yaml
version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  1. Start the application:
docker compose up -d

The -d flag runs the containers in detached mode.

  1. Verify the running containers:
   docker ps

You should see the Nginx container listed.

  1. Access the application:

Open a web browser and navigate to http://localhost:8080. You should see the Nginx welcome page.

Step 4: Manage Docker Compose Applications

Docker Compose provides several commands to manage your applications:

docker compose down

This stops and removes all containers defined in the docker-compose.yml file.

docker compose logs

This displays the logs of all containers. You can specify a service to view logs for a specific container:

docker compose logs web
  docker compose up -d --scale web=3

This scales the web service to 3 instances.

docker compose ps

This lists the status of all services defined in the docker-compose.yml file.