Docker - robbiehume/CS-Notes GitHub Wiki

Links

Overview

  • Docker is a tool that allows developers, sys-admins etc. to easily deploy their applications in a sandbox (called containers) to run on the host operating system i.e. Linux
  • The key benefit of Docker is that it allows users to package an application with all of its dependencies into a standardized unit for software development
  • Unlike virtual machines, containers do not have high overhead and hence enable more efficient usage of the underlying system and resources

Why Use Docker?

  • Portability – A containerized application runs the same way regardless of where it's deployed (local machine, cloud, server)
  • Isolation – Containers run independently from one another and from the host OS, reducing conflicts between dependencies
  • Lightweight – Unlike virtual machines (VMs), containers share the host OS kernel, making them much faster and more resource-efficient
  • Scalability – Works well with microservices architectures, allowing easy scaling of individual services

Common Use Cases

  • Microservices: Run each service in a separate container
  • Development Environments: Developers can work with the same stack across machines
  • CI/CD Pipelines: Automate testing and deployment
  • Cloud Deployments: Easily run applications in cloud environments

Key Docker Concepts

Concept Description
Container A lightweight, isolated environment for running applications
Image A snapshot of a container; contains the app and all dependencies
Dockerfile A script defining how an image is built (OS, libraries, commands, etc.)
Registry A storage for images (e.g., Docker Hub, AWS ECR, GitHub Container Registry)
Volumes Persistent storage for data shared between containers
Network Allows communication between containers

How Docker Works (High-Level Flow)

  1. Write a Dockerfile
    • Defines the environment for your application
  2. Build an Image
    • docker build -t myapp . → Converts the Dockerfile into an image
  3. Run a Container from the Image
    • docker run -d -p 8080:80 myapp → Starts a container from the image, exposing it on port 8080
  4. Share the Image
    • docker push myapp → Upload to a registry so others can use it
  5. Deploy Anywhere
    • Run the container on any system with Docker installed

Docker vs Virtual Machines (VMs)

Feature Docker (Containers) Virtual Machines
Size Small (MBs) Large (GBs)
Startup Time Seconds Minutes
Performance Near-native speed Slower
Isolation Shares OS Kernel Full OS Isolation
Portability Highly portable Less portable

Images vs Containers

  • An image is a package/template used to create one or more containers
  • Containers are running instances of images that are isolated and have their own environments and set of processes

Containers

  • VMs are great at providing full process isolation for applications: there are very few ways a problem in the host operating system can affect the software running in the guest operating system, and vice-versa
    • But this isolation comes at great cost — the computational overhead spent virtualizing hardware for a guest OS to use is substantial
  • Containers take a different approach: by leveraging the low-level mechanics of the host operating system, containers provide most of the isolation of virtual machines at a fraction of the computing power
  • There are already pre-built containers for common applications

Basic Docker Commands

  • # Check Docker is installed
     docker --version
    # Pull an image from Docker Hub
      # Or it will automatically pull it when you do the run command for the first time
     docker pull nginx
    # List available images
     docker images
    # Creates a new container from an image and starts it in the background 
      # To run in foreground (attached), remove -d
     docker run -d -p 8080:80 nginx
    # List running containers
     docker ps
    # Stop a container
     docker stop <container_id>
    # Starts an existing stopped container
     docker start 
    # Remove a container
     docker rm <container_id>
    # Remove an image
     docker rmi <image_id>
    # View container logs
     docker logs <container_id>

In depth commands

  • Stop all running containers: docker stop $(docker ps -aq)
  • Remove all containers: docker rm $(docker ps -aq)
  • Remove all images: docker rmi $(docker images -aq)

Misc.

  • When providing an ID to a command, you just need the first n characters that make it unique compared to others

Basic run options

  • Option Description
    -d Runs the container in detached mode (background)
    -it Interactive mode with a TTY (for CLI-based apps)
    --rm Removes the container after it stops
    --name <name> Assigns a custom name to the container
    --restart=<policy> Defines the restart policy (always, unless-stopped, on-failure)
⚠️ **GitHub.com Fallback** ⚠️