Docker Fundamentals
What is Docker?
Docker is an open-source platform that enables developers to build, deploy, and run applications in containers. Containers are lightweight, standalone, and executable software packages that include everything needed to run an application, including the code, runtime, system tools, and libraries. Docker provides a consistent and reliable way to package and distribute applications, making it easier to develop, test, and deploy software.
Docker Architecture
Docker uses a client-server architecture, where the Docker client communicates with the Docker daemon, which is responsible for building, running, and managing Docker containers. The Docker daemon runs on the host machine, while the Docker client can run on the same machine or a remote machine.
graph LR
A[Docker Client] -- API --> B[Docker Daemon]
B -- Executes Commands --> C[Docker Images]
B -- Manages --> D[Docker Containers]
Docker Images
Docker images are the building blocks of containers. They are read-only templates that contain the application code, runtime, system tools, and libraries needed to run the application. Docker images are created using a Dockerfile, which is a text file that contains instructions for building the image.
## Dockerfile
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y nginx
COPY index.html /usr/share/nginx/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Docker Containers
Docker containers are instances of Docker images. They are lightweight, portable, and self-contained environments that run applications. Containers are isolated from the host system and from each other, ensuring consistent and reliable application behavior.
## Run a container
docker run -d -p 80:80 my-nginx-app
Docker Networking
Docker provides built-in networking capabilities that allow containers to communicate with each other and with the host system. Docker supports several networking drivers, including bridge, host, and overlay networks.
## Create a bridge network
docker network create my-network
## Run a container on the network
docker run -d --network my-network my-app
Docker Volumes
Docker volumes are used to persist data generated by a container. Volumes can be used to store application data, configuration files, and other persistent information. Volumes can be mounted to the host filesystem or to other containers.
## Create a volume
docker volume create my-data
## Run a container with a volume
docker run -d -v my-data:/app my-app