Introduction to Docker

by Gautham Pai

Installing Docker on Ubuntu

Docker is a powerful platform for developing, shipping, and running applications inside containers. This guide will walk you through the steps to install Docker on an Ubuntu system, verify the installation, set up permissions, and run your first container.

Prerequisites

Before installing Docker, ensure that your system is updated. Run the following commands to update your package list:

sudo apt-get update
sudo apt-get -y install ca-certificates curl

Step 1: Add Docker's Official GPG Key

The GPG key ensures that the software you're about to install is authentic and hasn't been tampered with. Here's how you can add Docker's official GPG key:

sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
  • sudo install -m 0755 -d /etc/apt/keyrings: Creates a directory for storing the keyrings with specific permissions.
  • sudo curl -fsSL ... -o /etc/apt/keyrings/docker.asc: Downloads the GPG key and saves it in the specified directory.
  • sudo chmod a+r /etc/apt/keyrings/docker.asc: Ensures that the key file has the correct permissions to be read by all users.

Step 2: Add the Docker Repository to APT Sources

To install Docker, we need to add its repository to our system's APT sources:

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
  • deb [arch=$(dpkg --print-architecture) ...: Adds Docker's repository specific to your system's architecture.
  • sudo tee /etc/apt/sources.list.d/docker.list: Creates a new source list file for Docker.
  • sudo apt-get update: Updates the package list to include Docker's repository.

Now that the repository is added, install Docker and its associated packages:

sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  • docker-ce: The Docker Engine, which runs your containers.
  • docker-ce-cli: The command-line interface for managing Docker.
  • containerd.io: A container runtime used by Docker.
  • docker-buildx-plugin: A plugin for building multi-platform Docker images.
  • docker-compose-plugin: A plugin for defining and running multi-container Docker applications.

Verifying that Docker is installed

After installation, it's important to verify that Docker is installed correctly.

Docker CLI

To check if the Docker CLI is installed, run:

docker --version

You should see output displaying the installed Docker version.

Docker Service

To verify that the Docker service is running:

sudo systemctl status docker

Look for the status "active (running)." If it's running, Docker is successfully installed. Press q to exit the status display.

Containerd Service

Containerd is a critical component of Docker. Check its status using:

sudo systemctl status containerd

Again, ensure that the status is "active (running)." You can further verify by checking the process list:

ps aux | grep containerd

Setting Up Permissions

By default, Docker requires superuser (root) privileges to run. However, you can configure Docker to run as a non-root user.

Checking Docker Socket Permissions

First, run a Docker command as a regular user:

docker ps

If you get a "Permission denied" error, it means your user does not have the necessary permissions. Check the permissions on the Docker socket:

ls -l /var/run/docker.sock

Adding Your User to the Docker Group

Add your current user to the Docker group:

sudo usermod -aG docker $USER
newgrp docker

After doing this, you should be able to run Docker commands without sudo. Verify your groups:

groups

Ensure that "docker" is listed.

Running Your First Docker Container

Now that Docker is installed and configured, let's run your first container.

Pulling an Image from Docker Hub

Docker Hub is a public repository of Docker images. Let's pull the official Nginx image:

docker pull nginx
docker images

This downloads the Nginx image to your local machine.

Creating and Running a Container

Let's create a container from the Nginx image and run it:

docker run -d --name webserver1 -p 80:80 nginx
  • -d: Runs the container in detached mode (in the background).
  • --name webserver1: Assigns a name to the container.
  • -p 80:80: Maps port 80 of the host to port 80 of the container.

Verify that the container is running:

docker ps

Accessing the Nginx Web Server

Find the IP address of your VM and access the Nginx server from your browser:

ip addr show

Look for the eth0 interface to get the public IP. Open your browser and navigate to http://<public-vm-ip>. You should see the Nginx welcome page.

Managing Docker Containers

Docker provides several commands to manage containers.

Stopping a Container

To stop the Nginx container:

docker stop webserver1

Verify that the container has stopped:

docker ps
docker ps -a

Removing a Container

To remove the stopped container:

docker rm webserver1

You can also remove all stopped containers at once:

docker rm -f $(docker ps -aq)

Test Your Knowledge

No quiz available

Tags