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
Component
Description
Example
Image
Read-only template containing application code
nginx:latest
Container
Running instance of an image
Active web server
Dockerfile
Text file with instructions to build images
Recipe 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:
Docker fundamentally changes how applications are packaged and deployed, providing consistency and reliability across the entire software delivery pipeline.
Bind Mount: Sharing/Accessing directory that is on host OS.
docker run --mount type=bind,src=<host-path>,dst=<container-path>
Troublehsooting
Troubleshooting Empty Directory Gotcha
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.
Open Docker Desktop Settings: Click the Docker icon in your menu bar/system tray and select Settings (or Preferences).
Navigate to File Sharing: Go to Resources > File Sharing.
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).
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.