Table of Contents:
- Introduction to Docker
-
Docker Architecture
- Docker Daemon
- Docker Client
- Docker Images
- Docker Containers
- Docker Registries
-
Docker Compose
- What is Docker Compose?
- Docker Compose Architecture
- Docker Compose Commands
- Daily Docker Commands
-
Dockerfile Breakdown
- Key Dockerfile Commands & Parameters
- Conclusion
1. Introduction to Docker
Docker is an open platform for developing, shipping, and running applications. It enables you to separate your applications from your infrastructure to ensure faster delivery and more efficient scaling. Docker packages software into containers, which are standardized executable components that combine application source code with the operating system libraries and dependencies required to run that code in any environment.
2. Docker Architecture
2.1 Docker Daemon
-
Docker Daemon (
dockerd
) runs on the host machine, managing Docker objects such as images, containers, networks, and volumes. - The daemon listens to Docker API requests and manages Docker objects.
2.2 Docker Client
-
Docker Client (
docker
) is how most users interact with Docker. It sends commands to the Docker Daemon through CLI or Docker API. - A single client can interact with multiple daemons.
2.3 Docker Images
- Docker Image is a read-only template with instructions for creating a Docker container. It contains the application code, system libraries, and dependencies.
- Layers: Each command in a Dockerfile creates a new layer in the image, stored in a reusable format.
2.4 Docker Containers
- Docker Container is a runnable instance of a Docker image. It runs as a separate process on your host machine.
- Containers are lightweight and portable, encapsulating the code and its dependencies.
2.5 Docker Registries
- Docker Registry is a repository where Docker images are stored. Docker Hub is the default public registry.
- You can create private registries for internal use.
3. Docker Compose
3.1 What is Docker Compose?
Docker Compose is a tool for defining and running multi-container Docker applications. With Docker Compose, you can configure all of your application's services (databases, caches, backend, frontend) in a single YAML file (docker-compose.yml
).
3.2 Docker Compose Architecture
-
Services: Each service in
docker-compose.yml
defines a container. - Networks: Used to define how different services (containers) communicate with each other.
- Volumes: Handle data persistence by mounting storage to containers.
3.3 Docker Compose Commands
-
Start services:
docker-compose up
-
Stop services:
docker-compose down
-
Build services:
docker-compose build
-
View running services:
docker-compose ps
-
Scale services:
docker-compose scale <service>=<number_of_instances>
# Example docker-compose.yml file
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
4. Daily Docker Commands
4.1 Managing Images
-
List all images:
docker images
-
Pull an image from a registry:
docker pull <image_name>
-
Remove an image:
docker rmi <image_name>
4.2 Managing Containers
-
List all containers:
docker ps
ordocker ps -a
(for all, including stopped) -
Start a container:
docker start <container_id>
-
Stop a container:
docker stop <container_id>
-
Run a new container:
docker run -d -p <host_port>:<container_port> <image_name>
-
View logs of a container:
docker logs <container_id>
-
Remove a container:
docker rm <container_id>
4.3 Managing Volumes
-
List volumes:
docker volume ls
-
Create a volume:
docker volume create <volume_name>
-
Remove a volume:
docker volume rm <volume_name>
4.4 Managing Networks
-
List networks:
docker network ls
-
Create a network:
docker network create <network_name>
-
Remove a network:
docker network rm <network_name>
5. Dockerfile Breakdown
A Dockerfile is a script containing a series of instructions on how to build a Docker image.
# Example Dockerfile
# 1. Specify a base image
FROM python:3.9-slim
# 2. Set environment variables (optional)
ENV PYTHONUNBUFFERED=1
# 3. Copy the application code into the container
COPY . /app
# 4. Set the working directory
WORKDIR /app
# 5. Install dependencies
RUN pip install -r requirements.txt
# 6. Expose a port (optional)
EXPOSE 5000
# 7. Define the command to run the application
CMD ["python", "app.py"]
Key Dockerfile Commands & Parameters:
-
FROM: Defines the base image for subsequent instructions. Each Dockerfile must start with a
FROM
instruction.
FROM ubuntu:latest
- RUN: Executes a command in the container during image build (e.g., installing packages).
RUN apt-get update && apt-get install -y nginx
- COPY: Copies files from the host filesystem into the container.
COPY ./src /app/src
- WORKDIR: Sets the working directory inside the container for subsequent instructions.
WORKDIR /app
- EXPOSE: Informs Docker that the container will listen on the specified network port at runtime.
EXPOSE 8080
- CMD: Specifies the default command to run within the container. If a user runs the container without specifying a command, this will be executed.
CMD ["python", "app.py"]
-
ENTRYPOINT: Similar to
CMD
, but it defines a command that always runs, even when arguments are passed to the container.
ENTRYPOINT ["python"]
CMD ["app.py"]
- ENV: Sets environment variables inside the container.
ENV MYSQL_ROOT_PASSWORD=secret
6. Conclusion
Docker provides a streamlined environment for managing and deploying applications in a consistent and scalable way. By understanding Docker architecture, mastering daily Docker commands, utilizing Docker Compose for multi-container applications, and crafting efficient Dockerfiles, you can streamline development, simplify deployments, and ensure your applications run smoothly across different environments.