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:- 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.- Checks whether the image exists locally.
- Downloads the image if necessary.
- Creates a container.
- Runs the container.
4. Docker Image
A Docker image is a packaged template containing everything required to run an application. Example:5. Docker Container
A container is a running instance of a Docker image.6. Dockerfile
A Dockerfile is a text file containing instructions for building a Docker image. Example:7. Important Dockerfile Instructions
FROM
Defines the base image.
FROM instruction.
WORKDIR
Sets the working directory inside the container.
COPY
Copies files from the local machine into the container.
RUN
Executes commands while building the image.
RUN executes during:
CMD
Defines the default command when the container starts.
CMD executes when:
EXPOSE
Documents the port used by the application.
EXPOSE does not automatically publish the port to the host.
Port publishing requires:
ENV
Sets environment variables.
ARG
Defines build-time variables.
8. Dockerfile Example
A basic Python application:app.py
Dockerfile
9. Building an Image Locally
The basic command is:10. Running a Docker Image
Run an image:11. Docker Layers
One of the most important Docker concepts is layers. Each Dockerfile instruction generally creates a layer. Example:12. Docker Layer Caching
Docker caches previously built layers. Suppose this Dockerfile exists:13. Why Dockerfile Order Matters
Consider this Dockerfile:14. .dockerignore
A .dockerignore file prevents unnecessary files from being copied into the Docker build context.
Example:
.dockerignore:
.dockerignore:
- Smaller images
- Faster builds
- Prevent accidental copying of secrets
- Better caching
15. Docker Image Tags
Images can have tags. Example:16. Container Lifecycle
A container has different states.17. Useful Container Commands
List running containers:18. Port Mapping
Containers have their own network environment. Suppose a web application runs on:19. Volumes
Containers are designed to be temporary. Data inside a container may disappear when the container is removed. Docker volumes provide persistent storage.20. Bind Mounts
A bind mount connects a local directory to a container directory. Example:21. Docker Networking
Docker containers can communicate through networks. Create a network:22. Docker Compose
Docker Compose is used to manage multi-container applications. Example architecture:compose.yaml:
23. Docker Registry
A registry stores Docker images. The common workflow:24. Multi-Stage Builds
Multi-stage builds help reduce final image size. Example:- 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
Use smaller base 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:.env:
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.
29. Docker Build Context
The build context is the set of files Docker can access during a build. Example:. means:
.dockerignore is important.
30. Docker Caching Best Practices
Consider dependencies:31. Basic Docker Workflow
A typical development workflow:32. Common Docker Commands Cheat Sheet
Images
Containers
Logs and Debugging
Cleanup
Remove stopped containers: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.