Docker Fundamentals on Ubuntu: From Installation to Container Management
Environment Prerequisites
Development and testing performed on Ubuntu 18.04.2 LTS (Bionic Beaver).
Core Architecture
Docker operates on a client-server model utilizing two primary abstractions:
- Images: Read-only templates containing filesystem layers and application dependencies. Think of these as class definitions in object-oriented programming.
- Containers: Runtime instances of images—encapsulated processes with isolated filesystems, network interfaces, and resource constraints.
Installation Procedures
First, purge legacy Docker packages to prevent conflicts:
sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get update
sudo apt-get install docker.io
sudo systemctl enable --now docker
Image Operations
Discover official repositories:
docker search postgres
Retrieve specific versions:
docker pull redis:7-alpine
Inventory local storage:
docker image ls --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
Persistence operations:
# Export to tar archive
docker save -o ~/backups/webapp-v2.tar myregistry/webapp:v2.1
# Import from archive
docker load < ~/backups/webapp-v2.tar
# Create custom image from running container
docker commit -m "Added SSL certificates" -a "DevOps Team" web-server-01 production/nginx-ssl:v1.0
Remove artifacts:
docker rmi --force sha256:abc123...
Container Management
Initializing containers with resource constraints:
docker run -d \
--name api-gateway \
--hostname gateway-node \
-p 8080:80 \
-p 8443:443 \
-v /var/log/nginx:/var/log/nginx \
-e NODE_ENV=production \
--restart unless-stopped \
nginx:stable-alpine
Parameter reference:
-d: Detached mode (daemon)--name: Human-readable identifier-p HOST:CONTAINER: Port forwarding rules-v HOST:CONTAINER: Bind mount volumes-e: Environment variable injection--restart: Auto-start policy
Interactive access:
# Preferred method: spawns new bash process
docker exec -u root -it api-gateway /bin/sh
# Alternative (attaches to PID 1, blocks when multiple clients connect):
docker attach api-gateway
Lifecycle control:
docker start api-gateway
docker restart api-gateway
docker stop -t 30 api-gateway # Grace period before SIGKILL
docker rm -v api-gateway # Remove container and anonymous volumes