Preparing the Operating System

Docker runs most reliably on a Linux operating system. This guide is intended for Ubuntu 20.04 LTS.

Installing Docker

Removing Existing Packages

If you have a previous version of Docker installed on your system, remove it:

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

Installing Prerequisites

Install the packages required for Docker installation:

1
2
3
4
5
6
7
sudo apt-get update
sudo apt-get install 
    apt-transport-https 
    ca-certificates 
    curl 
    gnupg 
    lsb-release

Adding Docker’s Official GPG Key

Add the GPG key to use Docker’s package repository:

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

Adding Docker Repository

Register Docker’s package repository with your system:

1
2
3
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

Installing Docker Engine

Install Docker Engine and its dependencies:

1
2
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io

Configuring Docker

Setting User Permissions

Configure your user to run Docker commands without sudo:

1
2
sudo usermod -aG docker $USER
newgrp docker

Starting the Docker Service

Start the Docker service and configure it to start automatically on system boot:

1
2
sudo systemctl start docker
sudo systemctl enable docker

Verifying Installation

Verify that Docker is installed correctly:

1
2
docker --version
docker run hello-world

Running Your First Container

Running an Nginx Web Server

Run an Nginx web server container:

1
docker run -d -p 80:80 --name webserver nginx

Here’s what this command does:

  1. -d: Runs the container in the background
  2. -p 80:80: Maps port 80 on the host to port 80 in the container
  3. –name webserver: Specifies a name for the container
  4. nginx: The name of the image to use

Checking Container Status

Check the list of running containers:

1
docker ps

Viewing Container Logs

View the logs of a container:

1
docker logs webserver

Entering a Container

Enter a running container:

1
docker exec -it webserver bash

Basic Docker Commands

Image Management

1
2
3
4
5
6
7
8
# List images
docker images

# Pull an image
docker pull ubuntu:20.04

# Remove an image
docker rmi nginx

Container Management

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Stop a container
docker stop webserver

# Start a container
docker start webserver

# Restart a container
docker restart webserver

# Remove a container
docker rm webserver

Docker Networking

Creating a Network

Create a network for containers to communicate with each other:

1
docker network create mynetwork

Connecting a Container to a Network

Connect a container to the created network:

1
docker run -d --name db --network mynetwork mysql

Docker Volumes

Creating a Volume

Create a volume for persistent data storage:

1
docker volume create mydata

Mounting a Volume

Mount the volume to a container:

1
2
3
4
docker run -d 
  --name db 
  -v mydata:/var/lib/mysql 
  mysql

Troubleshooting

  1. If the Docker daemon fails to start:
1
2
sudo systemctl status docker
sudo journalctl -u docker
  1. If you encounter permission issues:
1
sudo chown $USER:$USER /var/run/docker.sock
  1. If you run out of disk space:
1
docker system prune -a

Docker is a great way to get started with container technology. With just the basic installation and configuration, you can experience the benefits of containers. Later, you can progress to more advanced container environments with Docker Compose, Docker Swarm, and Kubernetes.