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
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
Essential Commands
Image Management
docker pull <image>
- Download an imagedocker images
- List local imagesdocker build -t <name> .
- Build image from Dockerfile
Container Operations
docker run <image>
- Create and start a new containerdocker ps
- List running containersdocker ps -a
- List all containers (including stopped)docker stop <container>
- Stop a running containerdocker 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
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 fromWORKDIR
- Set working directoryCOPY
- Copy files from host to containerRUN
- Execute commands during buildEXPOSE
- Document which ports the app usesCMD
- 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
- Write application code
- Create Dockerfile
- Build image locally
- Test container functionality
- 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.
- 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.
Children