How to Install and Run Docker Containers on Ubuntu

DockerDockerBeginner
Practice Now

Introduction

This comprehensive Docker tutorial provides developers and system administrators with essential knowledge for implementing container technology. By covering fundamental Docker concepts, installation procedures, and practical examples, learners will gain practical skills in modern software deployment and virtualization techniques.

Docker Essentials

Introduction to Docker Fundamentals

Docker is a powerful container technology that revolutionizes software deployment and application virtualization. It enables developers to package applications with all their dependencies into standardized units called containers.

Core Concepts of Container Technology

graph LR A[Docker Image] --> B[Container] B --> C[Application Deployment] B --> D[Isolation]
Concept Description
Docker Image Lightweight, standalone executable package
Container Runnable instance of an image
Dockerfile Script defining image configuration

Basic Docker Architecture

Containers provide lightweight virtualization by sharing the host system's kernel while maintaining application isolation. This approach differs from traditional virtual machines.

Installation on Ubuntu 22.04

## Update package index
sudo apt update

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

## Add Docker GPG key
curl -fsSL  | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

## Set up repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg]  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

## Install Docker Engine
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

First Docker Container Example

## Pull Ubuntu image
docker pull ubuntu:latest

## Run interactive container
docker run -it ubuntu:latest /bin/bash

## Inside container
apt update
apt install nginx -y
exit

Docker Environment Setup

Preparing Ubuntu 22.04 for Docker Installation

graph LR A[System Update] --> B[Dependencies Installation] B --> C[Docker Repository Setup] C --> D[Docker Engine Installation] D --> E[Docker Configuration]

System Requirements

Requirement Specification
OS Ubuntu 22.04 LTS
Architecture 64-bit
Kernel 5.10+
RAM Minimum 2GB

Comprehensive Docker Installation Script

## Update system packages
sudo apt update && sudo apt upgrade -y

## Remove conflicting packages
sudo apt remove docker docker-engine docker.io containerd runc

## Install required dependencies
sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-release

## Add Docker official GPG key
curl -fsSL  | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

## Set up stable repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg]  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

## Install Docker Engine
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io

## Verify installation
sudo docker --version

Post-Installation Configuration

## Create docker user group
sudo groupadd docker

## Add current user to docker group
sudo usermod -aG docker $USER

## Enable Docker service
sudo systemctl enable docker.service
sudo systemctl start docker.service

## Verify docker functionality
docker run hello-world

Docker Compose Installation

## Download Docker Compose
sudo curl -L " -s)-$(uname -m)" -o /usr/local/bin/docker-compose

## Apply executable permissions
sudo chmod +x /usr/local/bin/docker-compose

## Verify installation
docker-compose --version

Container Management

Container Lifecycle Overview

graph LR A[Image Pull] --> B[Container Creation] B --> C[Container Start] C --> D[Container Running] D --> E[Container Stop] E --> F[Container Removal]

Basic Docker Container Commands

Command Function
docker run Create and start container
docker ps List running containers
docker stop Stop running container
docker rm Remove container
docker exec Execute command in container

Container Creation and Management

## Pull Ubuntu image
docker pull ubuntu:latest

## Run interactive container
docker run -it --name my-ubuntu ubuntu:latest /bin/bash

## List all containers
docker ps -a

## Start stopped container
docker start my-ubuntu

## Stop running container
docker stop my-ubuntu

## Remove container
docker rm my-ubuntu

Advanced Container Operations

## Run container in background
docker run -d --name nginx-server nginx:latest

## Map container port to host
docker run -p 8080:80 -d nginx:latest

## Inspect container details
docker inspect nginx-server

## View container logs
docker logs nginx-server

## Execute command in running container
docker exec -it nginx-server bash

Container Resource Management

## Limit container resources
docker run -d \
  --name limited-container \
  --cpus="1" \
  --memory="512m" \
  nginx:latest

## Monitor container resource usage
docker stats

Container Network Configuration

## Create custom network
docker network create my-network

## Run container in specific network
docker run --network=my-network -d nginx:latest

Summary

Docker represents a revolutionary approach to software deployment, offering lightweight, portable containers that streamline application development and infrastructure management. By mastering Docker's core principles, installation processes, and container management techniques, professionals can significantly enhance their software development and deployment capabilities across diverse computing environments.

Other Docker Tutorials you may like