How to Create and Run Docker Containers on Ubuntu

KubernetesKubernetesBeginner
Practice Now

Introduction

This comprehensive tutorial explores container technology fundamentals, focusing on Docker as the leading containerization platform. Designed for developers and system administrators, the guide provides practical insights into creating, managing, and deploying containers across different computing environments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ConfigurationandVersioningGroup(["`Configuration and Versioning`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ClusterInformationGroup(["`Cluster Information`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicsGroup(["`Basics`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/CoreConceptsGroup(["`Core Concepts`"]) kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/ConfigurationandVersioningGroup -.-> kubernetes/version("`Version`") kubernetes/ClusterInformationGroup -.-> kubernetes/cluster_info("`Cluster Info`") kubernetes/BasicsGroup -.-> kubernetes/dashboard("`Dashboard`") kubernetes/CoreConceptsGroup -.-> kubernetes/architecture("`Architecture`") subgraph Lab Skills kubernetes/get -.-> lab-392932{{"`How to Create and Run Docker Containers on Ubuntu`"}} kubernetes/version -.-> lab-392932{{"`How to Create and Run Docker Containers on Ubuntu`"}} kubernetes/cluster_info -.-> lab-392932{{"`How to Create and Run Docker Containers on Ubuntu`"}} kubernetes/dashboard -.-> lab-392932{{"`How to Create and Run Docker Containers on Ubuntu`"}} kubernetes/architecture -.-> lab-392932{{"`How to Create and Run Docker Containers on Ubuntu`"}} end

Container Fundamentals

What is Containerization?

Containerization is a lightweight virtualization technology that packages applications and their dependencies into isolated, portable units called containers. Unlike traditional virtual machines, containers share the host system's kernel, making them more efficient and faster to deploy.

Key Concepts of Container Technology

Concept Description
Container Lightweight, standalone executable package
Image Read-only template for creating containers
Runtime Software responsible for running containers

Docker: The Leading Container Platform

Docker is the most popular containerization technology, enabling developers to create, deploy, and run applications consistently across different environments.

graph TD A[Application Code] --> B[Dockerfile] B --> C[Docker Image] C --> D[Container Runtime] D --> E[Running Container]

Practical Docker Example on Ubuntu 22.04

First, install Docker on Ubuntu:

## Update package index
sudo apt-get update

## Install dependencies
sudo apt-get install ca-certificates curl gnupg

## Add Docker's official GPG key
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL  | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

## Set up repository
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg]  \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

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

Creating and Running a Simple Container

## Pull an Ubuntu image
docker pull ubuntu:latest

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

## Inside the container, you can run commands
root@container:/## cat /etc/os-release
root@container:/## exit

Microservices and Containerization

Containerization enables microservices architecture by providing:

  • Isolation between services
  • Easy scalability
  • Consistent deployment across environments
  • Efficient resource utilization

Kubernetes Essentials

Understanding Kubernetes Architecture

Kubernetes is an open-source container orchestration platform designed to automate deployment, scaling, and management of containerized applications.

graph TD A[Kubernetes Cluster] --> B[Control Plane] A --> C[Worker Nodes] B --> D[API Server] B --> E[Scheduler] B --> F[Controller Manager] C --> G[Kubelet] C --> H[Container Runtime]

Core Kubernetes Components

Component Function
Pod Smallest deployable unit
Node Physical or virtual machine
Cluster Collection of nodes
Deployment Manages application replica sets
Service Network abstraction for pods

Installing Kubernetes on Ubuntu 22.04

## Update package index
sudo apt-get update

## Install required packages
sudo apt-get install -y apt-transport-https curl

## Add Kubernetes GPG key
curl -s  | sudo apt-key add -

## Add Kubernetes repository
sudo bash -c 'echo "deb  kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list'

## Install Kubernetes components
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

Basic Kubernetes Deployment Example

## Initialize Kubernetes cluster
sudo kubeadm init --pod-network-cidr=10.244.0.0/16

## Configure kubectl for current user
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

## Deploy a simple nginx deployment
kubectl create deployment nginx-demo --image=nginx

## Expose the deployment as a service
kubectl expose deployment nginx-demo --port=80 --type=NodePort

Container Scaling and Management

Kubernetes provides powerful mechanisms for:

  • Automatic container scaling
  • Self-healing of failed containers
  • Rolling updates and rollbacks
  • Load balancing across containers
  • Resource allocation and management

OpenShift Insights

What is OpenShift?

OpenShift is an enterprise Kubernetes platform developed by Red Hat, providing advanced container management and deployment capabilities for complex enterprise environments.

graph TD A[OpenShift Platform] --> B[Kubernetes Core] A --> C[Enterprise Features] B --> D[Container Orchestration] C --> E[Security] C --> F[Monitoring] C --> G[Developer Tools]

OpenShift Key Components

Component Description
OCP OpenShift Container Platform
Routes External service exposure
BuildConfigs Source-to-image builds
DeploymentConfigs Advanced deployment strategies

Installing OpenShift Client Tools on Ubuntu 22.04

## Download OpenShift CLI
wget 

## Extract CLI tools
tar xzf openshift-client-linux.tar.gz

## Move binaries to system path
sudo mv oc kubectl /usr/local/bin/

## Verify installation
oc version
kubectl version

Connecting to OpenShift Cluster

## Login to OpenShift cluster
oc login  -u username -p password

## List available projects
oc projects

## Create a new project
oc new-project myapp

## Deploy an application
oc new-app nodejs:14~

Enterprise Container Management

OpenShift extends Kubernetes by providing:

  • Enhanced security policies
  • Integrated container registry
  • Advanced networking capabilities
  • Comprehensive monitoring and logging
  • Seamless hybrid cloud integration

Summary

By mastering container fundamentals, developers can leverage Docker's powerful ecosystem to build, package, and distribute applications more efficiently. The tutorial demonstrates how containerization simplifies software deployment, enhances portability, and provides consistent runtime environments across diverse infrastructure platforms.

Other Kubernetes Tutorials you may like