How to Install and Configure Docker on Ubuntu

DockerDockerBeginner
Practice Now

Introduction

This tutorial provides a comprehensive guide to running a Docker image and managing Docker containers. You will learn the fundamentals of Docker, explore Docker images and their use cases, and discover techniques for pulling, managing, and interacting with Docker containers. Additionally, the tutorial covers advanced container management topics, such as Docker volumes, networks, Compose, and Swarm, to help you effectively deploy and manage your applications using Docker.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker(("`Docker`")) -.-> docker/ImageOperationsGroup(["`Image Operations`"]) docker/ContainerOperationsGroup -.-> docker/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/run("`Run a Container`") docker/ImageOperationsGroup -.-> docker/pull("`Pull Image from Repository`") docker/ImageOperationsGroup -.-> docker/images("`List Images`") docker/ContainerOperationsGroup -.-> docker/ls("`List Containers`") subgraph Lab Skills docker/ps -.-> lab-392019{{"`How to Install and Configure Docker on Ubuntu`"}} docker/run -.-> lab-392019{{"`How to Install and Configure Docker on Ubuntu`"}} docker/pull -.-> lab-392019{{"`How to Install and Configure Docker on Ubuntu`"}} docker/images -.-> lab-392019{{"`How to Install and Configure Docker on Ubuntu`"}} docker/ls -.-> lab-392019{{"`How to Install and Configure Docker on Ubuntu`"}} end

Docker Essentials

What is Docker?

Docker is a powerful containerization technology that revolutionizes application deployment and development. It allows developers to package applications with all their dependencies into standardized units called containers, ensuring consistent performance across different computing environments.

Core Concepts of Docker

Containerization Technology

Containerization enables applications to run in isolated environments, providing several key advantages:

Feature Description
Isolation Each container runs independently
Portability Containers can run on any system supporting Docker
Efficiency Lightweight compared to traditional virtual machines
graph TD A[Application Code] --> B[Docker Container] B --> C[Host Operating System] C --> D[Infrastructure]

Docker Architecture

Docker uses a client-server architecture with key components:

  1. Docker Daemon
  2. Docker Client
  3. Docker Registry
  4. Docker Images
  5. Docker Containers

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=$(dpatch -s)]  $(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

Basic Docker Commands

## Check Docker version
docker --version

## Pull an image
docker pull ubuntu:latest

## List images
docker images

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

Key Benefits of Docker

  • Consistent development environments
  • Faster deployment
  • Resource efficiency
  • Scalability
  • Simplified configuration management

Working with Docker Images

Understanding Docker Images

Docker images are read-only templates used to create containers. They contain the application code, runtime, libraries, and system tools needed to run an application.

graph LR A[Dockerfile] --> B[Docker Image] B --> C[Docker Container]

Image Management Commands

Command Description
docker images List local images
docker pull Download images from registry
docker rmi Remove images
docker tag Tag images for versioning

Creating a Dockerfile

## Base image
FROM ubuntu:22.04

## Metadata
LABEL maintainer="[email protected]"

## Update system
RUN apt-get update && apt-get install -y \
    python3 \
    pip

## Set working directory
WORKDIR /app

## Copy application files
COPY . /app

## Install dependencies
RUN pip install -r requirements.txt

## Expose port
EXPOSE 5000

## Run application
CMD ["python3", "app.py"]

Building Docker Images

## Build image
docker build -t myapp:v1 .

## Build with specific tag
docker build -t myapp:latest .

## Build with build arguments
docker build --build-arg VERSION=1.0 -t myapp:v1 .

Docker Image Layers

graph TD A[Base Image] --> B[System Updates] B --> C[Install Dependencies] C --> D[Copy Application Code] D --> E[Define Startup Command]

Image Storage and Sharing

## Login to Docker Hub
docker login

## Push image to registry
docker push username/myapp:v1

## Pull image from registry
docker pull username/myapp:v1

Best Practices

  • Minimize image size
  • Use multi-stage builds
  • Leverage caching
  • Avoid installing unnecessary packages

Docker in Practice

Container Networking

Docker provides multiple networking modes for container communication:

Network Mode Description
Bridge Default network mode
Host Direct host network access
None No network connectivity
## Create custom network
docker network create mynetwork

## Run container on specific network
docker run --network=mynetwork ubuntu

Docker Compose

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

Container Orchestration Workflow

graph TD A[Development] --> B[Containerization] B --> C[Testing] C --> D[Staging] D --> E[Production Deployment]

Scaling Applications

## Scale services
docker-compose up -d --scale web=3

## Monitor container resources
docker stats

Production Deployment Strategies

## Rolling update
docker service update \
    --image myapp:v2 \
    --update-parallelism 2 \
    --update-delay 10s \
    myservice

Container Monitoring

## View container logs
docker logs mycontainer

## Real-time resource usage
docker top mycontainer

Advanced Networking

## Create overlay network
docker network create \
    -d overlay \
    --subnet 10.0.0.0/24 \
    my_overlay_network

Security Practices

Practice Description
Resource Limits Restrict CPU/Memory
Read-Only Filesystem Prevent modifications
Non-Root Users Reduce container privileges

Summary

By the end of this tutorial, you will have a solid understanding of how to run a Docker image, manage Docker containers, and leverage advanced container management techniques to deploy and manage your applications more efficiently. The knowledge gained from this tutorial will enable you to take advantage of the benefits of Docker, such as consistency, scalability, and portability, in your software development and deployment processes.

Other Docker Tutorials you may like