Skip to main content

Overview

Docker is a platform for building, packaging, and running applications inside isolated environments called containers. Docker helps solve the common problem:
“The application works on my machine but not on another machine.”
A container packages the application along with its dependencies and configuration.

1. Why Docker?

Without Docker:
With Docker:
Benefits:
  • Consistent environments
  • Easy application deployment
  • Dependency isolation
  • Portability
  • Faster setup
  • Reproducible builds
  • Easy scaling
  • Better development and deployment workflows

2. Important Docker Concepts

The main Docker concepts are:

3. Docker Architecture

Docker follows a client-server architecture.
Example command:
The Docker client sends the command to the Docker Engine. The engine:
  1. Checks whether the image exists locally.
  2. Downloads the image if necessary.
  3. Creates a container.
  4. Runs the container.

4. Docker Image

A Docker image is a packaged template containing everything required to run an application. Example:
Images are immutable. A Docker image can create multiple containers.
Example:

5. Docker Container

A container is a running instance of a Docker image.
Example:
Useful commands:
Shows running containers.
Shows all containers, including stopped containers.

6. Dockerfile

A Dockerfile is a text file containing instructions for building a Docker image. Example:
The Dockerfile tells Docker:

7. Important Dockerfile Instructions

FROM

Defines the base image.
Example:
Other examples:
Every Dockerfile usually starts with a FROM instruction.

WORKDIR

Sets the working directory inside the container.
After this:
Commands run relative to this directory.

COPY

Copies files from the local machine into the container.
Conceptually:
A more optimized approach:

RUN

Executes commands while building the image.
Other examples:
RUN executes during:

CMD

Defines the default command when the container starts.
CMD executes when:

EXPOSE

Documents the port used by the application.
For example:
Important: EXPOSE does not automatically publish the port to the host. Port publishing requires:

ENV

Sets environment variables.
Example usage:

ARG

Defines build-time variables.
Example:
Difference:

8. Dockerfile Example

A basic Python application:

app.py

Dockerfile

Build:
Run:
Output:

9. Building an Image Locally

The basic command is:
Example:
Breaking it down:
The workflow:
Check images:

10. Running a Docker Image

Run an image:
Run with a custom container name:
Run in detached mode:
Detached mode means the container runs in the background.

11. Docker Layers

One of the most important Docker concepts is layers. Each Dockerfile instruction generally creates a layer. Example:
Conceptually:
Docker combines these layers into an image.

12. Docker Layer Caching

Docker caches previously built layers. Suppose this Dockerfile exists:
During the first build:
During the next build, Docker checks whether instructions and files have changed.
This makes builds faster.

13. Why Dockerfile Order Matters

Consider this Dockerfile:
If any application file changes:
This is inefficient. A better version:
Now:
This is an important Docker optimization technique.

14. .dockerignore

A .dockerignore file prevents unnecessary files from being copied into the Docker build context. Example:
Without .dockerignore:
With .dockerignore:
Benefits:
  • Smaller images
  • Faster builds
  • Prevent accidental copying of secrets
  • Better caching

15. Docker Image Tags

Images can have tags. Example:
Format:
Examples:
If no tag is specified:
is generally used by default.

16. Container Lifecycle

A container has different states.
Commands: Create and start:
Stop:
Start again:
Restart:
Remove:
Remove a running container:

17. Useful Container Commands

List running containers:
List all containers:
View logs:
Follow logs:
Open a shell:
For smaller images:
Inspect container details:

18. Port Mapping

Containers have their own network environment. Suppose a web application runs on:
To access it from the host:
Format:
Example:
Another example:

19. Volumes

Containers are designed to be temporary. Data inside a container may disappear when the container is removed. Docker volumes provide persistent storage.
Create a volume:
Use it:
Example:

20. Bind Mounts

A bind mount connects a local directory to a container directory. Example:
Conceptually:
This is commonly used during development.

21. Docker Networking

Docker containers can communicate through networks. Create a network:
Run containers:
Architecture:
Containers in the same network can communicate using container names. Example:

22. Docker Compose

Docker Compose is used to manage multi-container applications. Example architecture:
Instead of running multiple commands manually, Docker Compose defines services in one configuration file. Example compose.yaml:
Start all services:
Run in background:
Stop:

23. Docker Registry

A registry stores Docker images. The common workflow:
Examples of registries include Docker Hub and private container registries. Basic commands:

24. Multi-Stage Builds

Multi-stage builds help reduce final image size. Example:
Architecture:
Benefits:
  • Smaller production images
  • Fewer unnecessary dependencies
  • Improved security
  • Faster deployment

25. Image Size Optimization

Large images can cause:
  • Slow builds
  • Slow downloads
  • More storage usage
  • Larger attack surface
Best practices:

Use smaller base images

instead of unnecessarily large images.

Use .dockerignore

Avoid copying:

Remove unnecessary dependencies

Install only required packages.

Use multi-stage builds

Keep build dependencies out of production images.

26. Docker Environment Variables

Environment variables help configure applications. Example:
Inside Python:
Using an environment file:
Example .env:
Important: secrets should not be permanently copied into Docker images.

27. Docker Image vs Container

Example:

28. RUN vs CMD vs ENTRYPOINT

These instructions are commonly confused.

RUN

Executes during image build.

CMD

Defines the default command when a container starts.

ENTRYPOINT

Defines the main executable for the container.
Result:
Comparison:

29. Docker Build Context

The build context is the set of files Docker can access during a build. Example:
The . means:
Docker sends files from the build context to the Docker Engine. This is why .dockerignore is important.

30. Docker Caching Best Practices

Consider dependencies:
Then application code:
Recommended order:
This maximizes Docker layer caching.

31. Basic Docker Workflow

A typical development workflow:
Commands:

32. Common Docker Commands Cheat Sheet

Images


Containers


Logs and Debugging


Cleanup

Remove stopped containers:
Remove unused images:
Remove unused resources:
Use caution:
This can remove unused images and other Docker resources.

33. Docker Security Basics

Important practices:
  • Use trusted base images.
  • Keep base images updated.
  • Do not store secrets inside Dockerfiles.
  • Use .dockerignore.
  • Run containers with minimal permissions.
  • Avoid running applications as root when possible.
  • Keep images small.
  • Scan images for vulnerabilities.
Example of creating a non-root user:

34. Health Checks

A health check allows Docker to determine whether an application is healthy. Example:
Conceptually:
This is useful for production applications.

35. Important Docker Best Practices


36. Complete Docker Learning Roadmap

Summary

The fundamental Docker workflow is:
The most important concepts to master first are: