Docker on Ubuntu: A Comprehensive Guide

Setting Up and Using Docker on Ubuntu

Introduction

Docker is an open-source platform that automates the deployment of applications inside lightweight, portable containers. Containers include everything an application needs to run, making it easy to deploy and scale applications on different environments. In this guide, we will walk you through the process of installing and using Docker on Ubuntu.

Step 1: Installing Docker

Update Your System

First, ensure your system is up to date. Open a terminal and run the following commands:

sudo apt update
sudo apt upgrade

Install Prerequisites

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

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

Add Docker’s GPG Key

Add Docker's official GPG key to your system:

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

Add Docker Repository

Set up the Docker repository:

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

Install Docker

Update the package index and install Docker:

sudo apt update
sudo apt install docker-ce

Start and Enable Docker

Start Docker and enable it to start on boot:

sudo systemctl start docker
sudo systemctl enable docker

Step 2: Running Docker Commands

Verify Installation

To verify Docker is installed correctly, run:

sudo docker --version

You should see the Docker version printed on the screen.

Running Your First Container

Let's run a simple Docker container to verify everything is working. We'll use the hello-world image:

sudo docker run hello-world

This command downloads the image (if not already downloaded) and runs it in a container. You should see a message indicating that Docker is working correctly.

Listing Docker Images

To list all Docker images on your system, use:

sudo docker images

Listing Docker Containers

To list all running Docker containers, use:

sudo docker ps

To list all containers, including stopped ones, use:

sudo docker ps -a

Stopping and Removing Containers

To stop a running container, use:

sudo docker stop <container_id>

To remove a container, use:

sudo docker rm <container_id>

Step 3: Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services.

Installing Docker Compose

Download the latest version of Docker Compose:

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

Apply executable permissions to the binary:

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

Verify the installation:

docker-compose --version

Using Docker Compose

Create a docker-compose.yml file for your application:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"

Run the application:

sudo docker-compose up

Docker Compose will pull the Nginx image (if not already present), create a container, and bind it to port 80.

Conclusion

Docker is a powerful tool that simplifies the deployment of applications inside portable containers. By following this guide, you should now have Docker installed on your Ubuntu system and understand the basics of running and managing Docker containers. Docker Compose further enhances Docker's capabilities by allowing you to manage multi-container applications with ease.