Basic Concepts
Container
Containers are lightweight, portable, and self-sufficient units that include everything needed to run an application, such as code, runtime, operating system, libraries, and dependencies.
- Isolation: Containers run in isolation from each other and from the host, ensuring that changes in one container do not affect other containers or the host operating system.
- Efficiency: Since containers share the same host operating system, they are more resource-efficient than traditional virtual machines.
Image
A Docker image is a static file that contains a filesystem and all the resources required to run a container. It serves as a blueprint for a container.
- Layering: Images are built in layers, allowing for the reuse of layers and reducing the amount of data needed for storage and transmission.
- Creation: Images are created from Dockerfiles, which contain a set of instructions on how to build the image.
Dockerfile
A Dockerfile is a text file script that contains a set of instructions to build a Docker image. Each instruction in the Dockerfile adds a new layer to the image.
- Example:
FROM python:3.8-slim
WORKDIR /app
COPY . /app
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
In the example above:
FROMspecifies the base image.WORKDIRsets the working directory inside the container.COPYcopies files from the host to the container.RUNexecutes a command during the image build.CMDspecifies the default command to run when the container starts.
Docker Engine
Docker Engine is the core of the Docker platform, consisting of several key components.
- Components:
- Docker Daemon: A background process that manages containers and images on the host.
- REST API: An interface that allows communication with the daemon using HTTP.
- CLI (Command Line Interface): A command-line tool that enables users to interact with Docker.
Docker Registry
A Docker registry is a service for storing and distributing Docker images. The most well-known public registry is Docker Hub.
- Public vs Private: Docker Hub offers both public and private image repositories. Organizations can also create their own private registries for internal use.
Volumes
Volumes are a mechanism for managing data in containers. They allow persistent storage of data independent of the container's lifecycle.
- Types: There are three main types of volumes:
- Named Volumes: Managed by Docker and can be shared between containers.
- Anonymous Volumes: Created dynamically without a name.
- Host Volumes: Mapped directly to directories on the host.
Networks
Docker offers various types of networks to connect containers to each other and to external networks.
- Types:
- Bridge Network: The default network that allows communication between containers on the same host.
- Host Network: Uses the host's network without network isolation between the container and the host.
- Overlay Network: Enables communication between containers on different hosts, useful in Docker Swarm clusters.