How to Build Docker Containers on Ubuntu

DockerDockerBeginner
Practice Now

Introduction

This comprehensive Docker tutorial provides developers and IT professionals with essential knowledge about container technology. Learn how to set up Docker on Ubuntu, understand core concepts, and leverage containerization for consistent application deployment across different computing environments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("`Docker`")) -.-> docker/ContainerOperationsGroup(["`Container Operations`"]) docker(("`Docker`")) -.-> docker/ImageOperationsGroup(["`Image Operations`"]) docker(("`Docker`")) -.-> docker/DockerfileGroup(["`Dockerfile`"]) docker/ContainerOperationsGroup -.-> docker/create("`Create Container`") docker/ContainerOperationsGroup -.-> docker/ps("`List Running Containers`") docker/ContainerOperationsGroup -.-> docker/run("`Run a Container`") docker/ContainerOperationsGroup -.-> docker/start("`Start Container`") docker/ContainerOperationsGroup -.-> docker/stop("`Stop Container`") docker/ImageOperationsGroup -.-> docker/pull("`Pull Image from Repository`") docker/ImageOperationsGroup -.-> docker/images("`List Images`") docker/DockerfileGroup -.-> docker/build("`Build Image from Dockerfile`") docker/ContainerOperationsGroup -.-> docker/ls("`List Containers`") subgraph Lab Skills docker/create -.-> lab-392844{{"`How to Build Docker Containers on Ubuntu`"}} docker/ps -.-> lab-392844{{"`How to Build Docker Containers on Ubuntu`"}} docker/run -.-> lab-392844{{"`How to Build Docker Containers on Ubuntu`"}} docker/start -.-> lab-392844{{"`How to Build Docker Containers on Ubuntu`"}} docker/stop -.-> lab-392844{{"`How to Build Docker Containers on Ubuntu`"}} docker/pull -.-> lab-392844{{"`How to Build Docker Containers on Ubuntu`"}} docker/images -.-> lab-392844{{"`How to Build Docker Containers on Ubuntu`"}} docker/build -.-> lab-392844{{"`How to Build Docker Containers on Ubuntu`"}} docker/ls -.-> lab-392844{{"`How to Build Docker Containers on Ubuntu`"}} end

Docker Essentials

Introduction to Docker Technology

Docker is a powerful platform for container technology that revolutionizes software deployment and virtualization. As an open-source tool, Docker enables developers to package, distribute, and run applications consistently across different computing environments.

Core Concepts of Docker

What is Docker?

Docker is a lightweight virtualization technology that allows developers to create, deploy, and run applications using containers. Unlike traditional virtual machines, containers share the host system's kernel, making them more efficient and faster to start.

Container Architecture

graph TD A[Docker Engine] --> B[Container Runtime] A --> C[Docker Images] B --> D[Application Container] C --> D

Key Docker Components

Component Description Purpose
Docker Engine Core runtime environment Manages container lifecycle
Docker Image Read-only template Defines container configuration
Docker Container Runnable instance Executes application

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=amd64 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

Basic Docker Commands

## Check Docker version
docker --version

## Verify Docker installation
docker run hello-world

## List running containers
docker ps

## List all containers
docker ps -a

Advantages of Docker

Docker provides significant benefits for modern software development:

  • Consistent environment across development and production
  • Faster application deployment
  • Improved resource utilization
  • Simplified dependency management
  • Enhanced scalability and portability

Creating Docker Images

Understanding Docker Images

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

Dockerfile Basics

A Dockerfile is a text document containing instructions for building a Docker image. Each instruction creates a new layer in the image.

Dockerfile Structure

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

Common Dockerfile Instructions

Instruction Purpose Example
FROM Specify base image FROM ubuntu:22.04
RUN Execute commands RUN apt-get update
COPY Copy files into image COPY ./app /application
WORKDIR Set working directory WORKDIR /application
CMD Define default command CMD ["python", "app.py"]

Creating a Simple Python Web App Image

Sample Dockerfile

## Use official Python runtime as base image
FROM python:3.9-slim

## Set working directory
WORKDIR /app

## Copy requirements file
COPY requirements.txt .

## Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

## Copy application code
COPY . .

## Expose port
EXPOSE 5000

## Define startup command
CMD ["python", "app.py"]

Building Docker Image

## Build image with tag
docker build -t hello-world-app .

## List local images
docker images

## Run the created image
docker run -p 5000:5000 hello-world-app

Image Management Commands

## Remove specific image
docker rmi hello-world-app

## Remove all unused images
docker image prune

## Pull image from Docker Hub
docker pull ubuntu:22.04

Best Practices

  • Use minimal base images
  • Minimize number of layers
  • Avoid installing unnecessary packages
  • Use multi-stage builds for smaller images
  • Leverage build cache efficiently

Container Management

Container Lifecycle Overview

Docker containers have a defined lifecycle with multiple states, from creation to termination. Understanding these states is crucial for effective container management.

stateDiagram-v2 [*] --> Created Created --> Running Running --> Paused Paused --> Running Running --> Stopped Stopped --> Removed Removed --> [*]

Basic Container Operations

Container Management Commands

Command Function Example
docker create Create a container docker create nginx
docker start Start a container docker start container_id
docker stop Stop a running container docker stop container_id
docker restart Restart a container docker restart container_id
docker rm Remove a container docker rm container_id

Running Containers

## Run container in foreground
docker run nginx

## Run container in background
docker run -d nginx

## Run container with port mapping
docker run -p 8080:80 nginx

## Run container with custom name
docker run --name web-server nginx

Container Inspection and Monitoring

## List running containers
docker ps

## List all containers
docker ps -a

## View container logs
docker logs container_id

## Inspect container details
docker inspect container_id

## Monitor container resource usage
docker stats

Container Networking

## List docker networks
docker network ls

## Create custom network
docker network create mynetwork

## Connect container to network
docker network connect mynetwork container_id

Advanced Container Management

Container Resource Limits

## Limit CPU and memory
docker run -d \
  --cpus="1.5" \
  --memory="512m" \
  nginx

Container Scaling

## Create multiple container instances
docker run -d -p 8001:80 nginx
docker run -d -p 8002:80 nginx
docker run -d -p 8003:80 nginx

Container Deployment Strategies

Persistent Data Management

## Create volume
docker volume create myvolume

## Mount volume to container
docker run -v myvolume:/app/data nginx

Environment Configuration

## Set environment variables
docker run -e DATABASE_URL=localhost nginx

Summary

Docker revolutionizes software development by enabling lightweight, portable containers that simplify application deployment. By mastering Docker's core components, installation process, and basic commands, developers can create more efficient, scalable, and reproducible software environments with minimal overhead and maximum flexibility.

Other Docker Tutorials you may like