How to Deploy Docker Containers on Ubuntu

DockerDockerBeginner
Practice Now

Introduction

This comprehensive Docker tutorial provides developers and system administrators with a foundational guide to understanding and implementing container technologies. By exploring Docker's core concepts, architecture, and practical implementation, learners will gain critical skills in modern software deployment and infrastructure management.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker(("`Docker`")) -.-> docker/ImageOperationsGroup(["`Image Operations`"]) docker(("`Docker`")) -.-> docker/SystemManagementGroup(["`System Management`"]) docker/ContainerOperationsGroup -.-> docker/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/inspect("`Inspect Container`") docker/ImageOperationsGroup -.-> docker/images("`List Images`") docker/SystemManagementGroup -.-> docker/info("`Display System-Wide Information`") docker/SystemManagementGroup -.-> docker/version("`Show Docker Version`") docker/ContainerOperationsGroup -.-> docker/ls("`List Containers`") subgraph Lab Skills docker/ps -.-> lab-393102{{"`How to Deploy Docker Containers on Ubuntu`"}} docker/inspect -.-> lab-393102{{"`How to Deploy Docker Containers on Ubuntu`"}} docker/images -.-> lab-393102{{"`How to Deploy Docker Containers on Ubuntu`"}} docker/info -.-> lab-393102{{"`How to Deploy Docker Containers on Ubuntu`"}} docker/version -.-> lab-393102{{"`How to Deploy Docker Containers on Ubuntu`"}} docker/ls -.-> lab-393102{{"`How to Deploy Docker Containers on Ubuntu`"}} end

Docker Basics

What is Docker?

Docker is a powerful containerization technology that enables developers to package, distribute, and run applications consistently across different computing environments. As a fundamental tool in modern software development, Docker simplifies application deployment and enhances system efficiency.

Core Concepts of Containerization

Containerization allows applications to be isolated and run independently with their own dependencies. Unlike traditional virtual machines, containers share the host system's kernel, making them lightweight and fast.

graph TD A[Application Code] --> B[Docker Container] B --> C[Shared Host Kernel] B --> D[Isolated Environment]

Docker Architecture

Component Description
Docker Daemon Background service managing containers
Docker Client Command-line interface for interacting with Docker
Docker Images Read-only templates for creating containers
Docker Containers Runnable instances of Docker images

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's 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 docker-ce docker-ce-cli containerd.io

First Docker Container Example

## Pull official Ubuntu image
docker pull ubuntu:latest

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

## Inside container, verify environment
cat /etc/os-release

This example demonstrates how to download an Ubuntu image and run an interactive container, showcasing Docker's simplicity in creating isolated environments.

Container Management

Container Lifecycle Operations

Docker provides comprehensive commands for managing container lifecycle, enabling developers to create, start, stop, and remove containers efficiently.

graph LR A[Create Container] --> B[Start Container] B --> C[Running Container] C --> D[Stop Container] D --> E[Remove Container]

Essential Docker Container Commands

Command Function Example
docker create Create a new container docker create ubuntu:latest
docker start Start a stopped container docker start container_id
docker stop Stop a running container docker stop container_id
docker rm Remove a container docker rm container_id
docker ps List running containers docker ps
docker ps -a List all containers docker ps -a

Container Management Example

## Create a new container
docker create --name web_server nginx:latest

## Start the container
docker start web_server

## List running containers
docker ps

## Stop the container
docker stop web_server

## Remove the container
docker rm web_server

Container Resource Management

## Run container with resource limits
docker run -d \
    --name limited_container \
    --memory=512m \
    --cpus=1.5 \
    nginx:latest

This example demonstrates setting memory and CPU constraints for a container, ensuring controlled resource utilization.

Advanced Container Inspection

## Detailed container information
docker inspect web_server

## Container logs
docker logs web_server

## Real-time container statistics
docker stats web_server

Advanced Container Techniques

Container Network Configuration

Docker provides flexible networking options for complex application architectures.

graph LR A[Host Network] --> B[Bridge Network] B --> C[Custom Network] C --> D[Overlay Network]

Network Types

Network Type Description Use Case
Bridge Default network Isolated container communication
Host Direct host network High-performance scenarios
Overlay Multi-host networking Distributed systems

Custom Network Creation

## Create custom network
docker network create --driver bridge my_custom_network

## Run container in custom network
docker run -d \
    --name web_app \
    --network my_custom_network \
    nginx:latest

Volume Management

## Create persistent volume
docker volume create app_data

## Mount volume to container
docker run -d \
    --name database \
    -v app_data:/var/lib/database \
    postgres:latest

Container Orchestration with Docker Compose

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
  database:
    image: postgres:latest
    environment:
      POSTGRES_PASSWORD: secret

Performance Monitoring

## Real-time container metrics
docker stats

## Container resource usage
docker top container_name

## Performance profiling
docker run \
    --rm \
    -it \
    --privileged \
    ubuntu:latest \
    perf top

Summary

Docker represents a revolutionary approach to application packaging and deployment, offering lightweight, portable, and efficient containerization solutions. By mastering Docker's fundamental techniques, developers can streamline their workflow, ensure consistent environments, and enhance system scalability across diverse computing platforms.

Other Docker Tutorials you may like