githubEdit

Docker

Docker is an OS‑level virtualization (or containerization) platform, which allows applications to share the host OS kernel instead of running a separate guest OS like in traditional virtualization. This design makes Docker containers lightweight, fast, and portable, while keeping them isolated from one another.

Why use Docker?

  • Portability: Runs anywhere in local machine, cloud, on‑prem servers.

  • Consistency: Same behavior in development, testing, and production.

  • Lightweight: No full OS per app; containers share the host kernel.

  • Scalability: Ideal for microservices and orchestrators like Kubernetes and Docker Swarm.

  • Efficiency: Starts in seconds, uses fewer system resources.

Docker Set Up

We can set up using apt. First, we set up Docker's apt repository.

# Add Docker's official GPG key:
sudo apt update
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF

sudo apt update

Generally, best practices dictate that you should copy and paste commands from the internet but you trust me right ;)

Next, lets install Docker:

We can then check that Docker is running:

We can now add the user to the docker group, so that you can run docker commands without sudo:

ssh back into the server and you should be able to run:

Full documentation of setting up in Ubuntu:


Brief Introduction to Docker

1

How does Docker work?

Docker is a containerization platform, that allows applications to run in containers.

Think of containers like micro virtual machines, where each container lives in its own isolated namespace, where it has its own processes, networking users, etc.

This makes containers very lightweight (and fast) compared to virtual machines.

2

Images & Docker Hub

Docker images are templates to create containers. The image specifies things like what files and applications(binaries) should exist in the container, and how to start the container.

Images are normally stored in registries. The most common registry is dockerhub.

https://hub.docker.com/arrow-up-right

  • python:3.14.3:

    • python is the image it is going to pull from the registry. for docker, it pulls from dockerhub by default.

    • 3.14.3 is the image tag, a label assigned to a particular version of an image.

This builds the above Dockerfile with the name mypython.

3

Containers

Containers are running instances of an image. From the image, it runs the cmd block specified on startup.

circle-exclamation
    • finds the image hello-world, and runs it until it completes

    • Lists all containers, running and stopped.

    • Deletes a given container

    • finds the image ubuntu, attaches your input and terminal and runs in it. when you exit, it deletes the container.

    • -i : Run in an interactive process (you can type and it will be passed into the container)

    • -t : it attaches a terminal

    • --rm : it deletes the container its running automatically when stopped

    • finds the image nginx (a web server), creates a container with the name mynginx and runs it detached, in the background

    • -d : run detached, running in the background instead of attached to your terminal

    • shows you information about the current running containers in your system.

    • stops the running container (doesn't delete it!)

4

Volumes

Volumes are how you persist data outside of the container's lifecycle.

Volumes are normally used for databases, sharing files between host and container, etc.

Type
Description

Volume

Docker managed persistent storage

Bind Mount

Mount your host directory into the container

5

Ports

Containers run in their own isolated networks, so their ports are not accessible by default. We need to specify which ports to publish when we run the container

  • 8080:80 : This follows a host: container structure. You local machine's port 8080 is bound to the containers port 80 , so all traffic between those 2 ports are piped.

triangle-exclamation

6

Environment variables

Similar to environment variables in your host system, we can pass environment variables to containers as well. A lot of applications use environment variables for configuration.

7

Networking

Docker provides builtin networking. Networking is a very complex topic, that we do not have the time or the capabilities to explain...

Containers in the same network can see and talk to each

8

Docker Compose

Docker compose is a tool for running multi-container applications

Multi-container applications? Example:

  • your application, written in python

  • postgres which your application talks to

  • redis which your application uses as a cache

All of these can be bundled and orchestrated together using docker compose!

Another side effect: All the commands above? We don't need to run any of them! WAHOOOO

Useful Docker Commands

docker run

It used for launching the containers from images, with specifying the runtime options and commands

docker pull

Fetch the container images from the container registry like Docker Hub to the local machine

docker ps

Display the running containers along with their important information like container ID, image used and status

docker stats

Show the stats and compute usage of the current running containers

docker stop

Halt running containers gracefully shutting down the processes within them

docker start

Restart the stopped containers, resuming their operations from the previous state.

Additional Readings

Some content here was based off:

Last updated