Docker

Basics

Docker Basics

Docker is a containerization platform that allows developers to package applications and their dependencies into lightweight, portable containers. Containers ensure consistent behavior across different environments, from development to production.

What is Docker?

Docker enables you to run applications in isolated environments called containers. Unlike virtual machines, containers share the host OS kernel, making them more efficient and faster to start.

Key Benefits

  • Consistency: Applications run the same way everywhere
  • Isolation: Each container runs independently
  • Portability: Containers work across different platforms
  • Efficiency: Lower resource overhead than VMs

Core Concepts

Images vs Containers

ComponentDescriptionExample
ImageRead-only template containing application codenginx:latest
ContainerRunning instance of an imageActive web server
DockerfileText file with instructions to build imagesRecipe for creating images

Docker Architecture

graph TB A[Docker Client] --> B[Docker Daemon]; B --> C[Images]; B --> D[Containers]; B --> E[Networks]; B --> F[Volumes]; C --> G[Docker Registry/Hub];

Essential Commands

Image Management

  • docker pull <image> - Download an image
  • docker images - List local images
  • docker build -t <name> . - Build image from Dockerfile

Container Operations

  • docker run <image> - Create and start a new container
  • docker ps - List running containers
  • docker ps -a - List all containers (including stopped)
  • docker stop <container> - Stop a running container
  • docker rm <container> - Remove a container

Common Run Options

  • -d - Run in background (detached mode)
  • -p 8080:80 - Map host port 8080 to container port 80
  • -v /host/path:/container/path - Mount volume
  • --name myapp - Assign custom name

Docker Workflow

sequenceDiagram participant Dev as Developer participant DF as Dockerfile participant Img as Image participant Con as Container Dev->>DF: 1. Write Dockerfile Dev->>Img: 2. docker build Dev->>Con: 3. docker run Con->>Dev: 4. Application running

Dockerfile Basics

A Dockerfile contains step-by-step instructions to build an image:

FROM node:16
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Common Instructions

  • FROM - Base image to start from
  • WORKDIR - Set working directory
  • COPY - Copy files from host to container
  • RUN - Execute commands during build
  • EXPOSE - Document which ports the app uses
  • CMD - Default command when container starts

Best Practices

Security & Efficiency

  • Use official base images when possible
  • Keep images small by using minimal base images
  • Don't run containers as root user
  • Use .dockerignore to exclude unnecessary files

Development Workflow

  1. Write application code
  2. Create Dockerfile
  3. Build image locally
  4. Test container functionality
  5. Push to registry for deployment

Docker fundamentally changes how applications are packaged and deployed, providing consistency and reliability across the entire software delivery pipeline.

https://docs.docker.com/engine/storage/bind-mounts/

Bind Mount: Sharing/Accessing directory that is on host OS.

docker run --mount type=bind,src=<host-path>,dst=<container-path>

Troublehsooting

The "Empty Directory" Docker Gotcha on macOS and Windows

The Symptom:

You try to bind-mount a host directory into a Docker container using a command like

  • docker run --mount type=bind,src=/path/on/host,dst=/path/in/container ...
  • docker run --volume /path/on/host:/path/in/container ...

The command runs successfully without any errors, but when you inspect the destination directory inside the container, it's completely empty, even though the source directory on your host machine has files.

The Cause:

On macOS and Windows, Docker doesn't run natively. It operates inside a lightweight Linux Virtual Machine (VM) for performance and compatibility. For security reasons, this VM has a "wall" around it and cannot access your computer's entire filesystem by default.

When you ask Docker to mount a directory that is outside of its pre-approved sharing list, it doesn't fail. Instead, it silently creates a new, empty directory within the VM's own filesystem and mounts that into your container. The result is an empty folder, and no error message to tell you why.

The Fix: Granting Docker Access

You must explicitly tell Docker which of your host directories it is allowed to access.

  1. Open Docker Desktop Settings: Click the Docker icon in your menu bar/system tray and select Settings (or Preferences).
  2. Navigate to File Sharing: Go to Resources > File Sharing.
  3. Add Your Directory: You'll see a list of paths Docker can already access (e.g., /Users, /tmp). Click the + button and add the parent directory you want to mount from (e.g., /Users/yourname/projects).
  4. Apply and Restart: Click the Apply & Restart button. Docker will restart its VM with the new permissions.

Once restarted, your docker run command will work as expected, and the directory will be correctly mounted with all its contents.


Children
  1. Basics
  2. Bind Mount