Skip to main content

Docker Installation and Basic Usage

Now that you've mastered core Linux system administration skills, it's time to explore one of the most transformative technologies in modern software development: Docker. This lesson will guide you through installing Docker and using it to create, manage, and run containers.

Learning Goals:

  • Install Docker on Ubuntu using the official repository
  • Run your first container and understand basic Docker commands
  • Build custom Docker images using Dockerfiles
  • Manage containers and images effectively

Installing Docker on Ubuntu

First, let's install Docker using the official repository method for reliable updates and security patches.

Add Docker repository and install
# Update package index
sudo apt update

# Install prerequisite packages
sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release

# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Add the stable repository
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(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

After installation, verify Docker is running and add your user to the docker group to avoid using sudo for every command:

Verify installation and configure permissions
# Start Docker service
sudo systemctl start docker
sudo systemctl enable docker

# Add your user to docker group
sudo usermod -aG docker $USER

# Verify installation (may need to log out and back in first)
docker --version
tip

After adding your user to the docker group, you'll need to log out and log back in for the group changes to take effect. Alternatively, you can run newgrp docker to apply the changes immediately in your current session.

Your First Docker Container

Let's run a simple container to verify everything is working:

Run hello-world container
docker run hello-world

You should see a welcome message confirming Docker is working correctly. Now let's try something more practical:

Run an interactive Ubuntu container
docker run -it ubuntu:20.04 /bin/bash

This command downloads the Ubuntu 20.04 image (if not already present) and starts an interactive bash session inside the container.

Essential Docker Commands

Here are the fundamental Docker commands you'll use daily:

Basic Docker commands
# List running containers
docker ps

# List all containers (including stopped ones)
docker ps -a

# List downloaded images
docker images

# Stop a running container
docker stop <container_id>

# Remove a container
docker rm <container_id>

# Remove an image
docker rmi <image_name>

# View container logs
docker logs <container_id>

Building Custom Images with Dockerfile

While using pre-built images is convenient, you'll often need to create custom images. This is done using a Dockerfile.

Create a simple web application:

Create project directory and files
mkdir my-webapp && cd my-webapp
Dockerfile
# Use official Python runtime as base image
FROM python:3.9-slim

# Set working directory in container
WORKDIR /app

# Copy requirements and install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY . .

# Expose port 5000
EXPOSE 5000

# Define environment variable
ENV NAME World

# Run the application
CMD ["python", "app.py"]
app.py
from flask import Flask
import os

app = Flask(__name__)

@app.route('/')
def hello():
name = os.environ.get('NAME', 'Docker')
return f'Hello {name}!'

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
requirements.txt
Flask==2.3.3

Now build and run your custom image:

Build and run custom image
# Build the image
docker build -t my-webapp .

# Run the container
docker run -p 4000:5000 my-webapp

# Test in another terminal
curl http://localhost:4000

Managing Containers and Images

Learn to efficiently manage your Docker resources:

Container and image management
# Run container in detached mode
docker run -d -p 4000:5000 --name my-running-app my-webapp

# Execute command in running container
docker exec -it my-running-app /bin/bash

# Inspect container details
docker inspect my-running-app

# View resource usage
docker stats

# Clean up unused containers, networks, and images
docker system prune

# Remove all stopped containers and unused images
docker system prune -a
warning

Be careful with docker system prune -a as it removes all stopped containers and all images not used by running containers. This can be destructive if you have images you want to keep.

Common Pitfalls

  • Forgetting to expose ports: When running web applications, remember to use -p host_port:container_port to map ports
  • Running as root in production: Avoid running containers as root in production; use the USER instruction in your Dockerfile
  • Losing data: Container filesystems are ephemeral; use volumes for persistent data
  • Ignoring image sizes: Large images slow down deployment; use smaller base images and multi-stage builds
  • Not cleaning up: Regularly prune unused images and containers to save disk space

Summary

You've successfully installed Docker and learned essential container operations. You can now run pre-built images, create custom images using Dockerfiles, and manage containers effectively. Docker provides a consistent environment for applications regardless of where they run, making development and deployment more reliable.

Show quiz
  1. What command installs Docker using the official repository method?

    • A) sudo apt install docker
    • B) sudo snap install docker
    • C) sudo apt install docker-ce
    • D) sudo yum install docker
  2. How do you run a container in detached mode?

    • A) docker run --detach
    • B) docker run -d
    • C) docker run --background
    • D) docker run -b
  3. What is the purpose of the EXPOSE instruction in a Dockerfile?

    • A) It automatically opens the port on the host machine
    • B) It documents which ports the container listens on
    • C) It configures the container's firewall
    • D) It publishes the port to a load balancer
  4. Which command removes all stopped containers and unused images?

    • A) docker clean
    • B) docker system cleanup
    • C) docker system prune -a
    • D) docker remove --all
  5. Why should you avoid running containers as root in production?

    • A) Root containers use more memory
    • B) Security risks if container is compromised
    • C) Root containers can't access volumes
    • D) Root containers are slower

Answers:

  1. C) sudo apt install docker-ce
  2. B) docker run -d
  3. B) It documents which ports the container listens on
  4. C) docker system prune -a
  5. B) Security risks if container is compromised