Docker
Docker Architecture
Docker uses a client/server architecture. The following is a breakdown of the core components associated with Docker.
Docker Host
A Docker host is a physical or virtual machine running Linux or another Docker-Engine compatible OS.
Docker needs a Linux kernel on non-Linux operating systems (like Windows and macOS) because Docker containers rely on Linux- specific kernel features to function.
Docker host provides networking and storage capabilities for containers. And the bridge network on the Docker Host, allows containers to communicate with each other and with the Docker Host.
Docker Engine
Docker Engine is the core component of the Docker platform, responsible for building, running, and managing Docker containers.
It’s a client/server application consisting of the Docker daemon, Docker API and Docker client.
Docker Daemon
Docker daemon is a service that manages Docker containers, images, networking and storage volume, by using the commands from the client through the API.
Essentially the Docker daemon serves as the control center for Docker implementation.
Docker Client
The Docker client provides the CLI that accesses the Docker API to communicate with the Docker daemon.
Docker API
Docker API is a REST API that allows Docker clients to interact with the Docker Daemon. It allows to manage Docker containers, images, networks, and other components programmatically.
Docker Objects
Docker objects are components of a Docker deployment that help package and distribute applications. They include images, containers, networks, volumes and more.
Docker Containers
Docker containers are the live, running instances of Docker images. While Docker images are read-only files, containers are live, ephemeral, executable content.
Users can interact with them and adjust their settings and conditions by using Docker commands.
While running the container, Docker takes the image and adds a writable layer on top, allowing the container to modify files and directories.
Containers run in isolated environments, like own filesystem, process space, and network interface. It ensures that the application behaves the same way, regardless of where it is run.
By default, containers are ephemeral. When a container is stopped or removed, all data written to its writable layer is lost unless it has been persisted.
Docker Images
Docker images contain executable application source code and all the tools, libraries and dependencies the application code needs to run as a container.
While running the Docker image, it becomes one instance (or multiple instances) of the container.
Docker images are immutable. It ensures consistency and reliability when the image is deployed across different environments.
Volumes
Volumes are Docker-managed storage objects that are used to persist data generated and used by Docker containers.
Networks
Docker networks are objects that enable communication between Docker containers and between containers and the Docker Host.
Docker Terms and Tools
Docker file
Every Docker container starts with a simple text file containing instructions for how to build the Docker container image.
Docker file automates the process of creating Docker images.
It’s essentially a list of CLI instructions that Docker Engine will run to assemble the image.
Docker build
Docker build is used to create the docker image by packaging the code and dependencies.
Docker Compose
Docker Compose helps to manage multi container applications, where all containers run on the same Docker host.
It allows to configure the application’s services, networks, and volumes in a single file and then manage the entire application stack using simple commands.
Docker Hub
Docker Hub is a cloud-based registry service for Docker images. It provides a centralized location to store, manage, and share Docker images. It hosts a collection of official images maintained by Docker and other organizations.
Docker Desktop is a tool designed to simplify the management of containerized applications. It provides a user-friendly interface for handling Docker containers.
Essential Docker Commands
docker info : Provides detailed information about the Docker installation.
docker pull <image_name> : Pull any image which is present in the official registry of docker, Docker hub.
docker login : Used to authenticate to a Docker registry.
docker push <image_name> : Used to upload a Docker image from local machine to a remote Docker registry.
docker images : Lists all the images.
docker run <image_name> : Run a container from an image.
docker rmi < image_name> : Delete the images.
docker rmi -f < image_name> : Delete the images forcefully.
docker run - - name <container_name> <image_name> : Give name of container It creates a new container from the image specified and starts that container. If the docker image is not present, then the docker run pulls that.
docker ps {options}
docker ps : Shows the list of all the running containers.
docker ps -a : Shows all the containers, stopped or running.
docker ps -l : Shows the latest container.
docker ps -q : Shows only the Id of the containers.
docker stop <container_name> : Stop a container.
docker start <container_name> : Start the stopped container again.
docker restart <container_name> : Restart the container.
docker inspect <container_name> : Helps to debug the container’s errors.
docker logs <container_name> : Displays the logs from a container.
docker logs -f <container_name> : Follow the logs in real-time.
docker rm {options} < container_name>
docker rm <container_name> : Delete a container.
docker rm -f <container_name> : Remove the running container forcefully.
docker rm -v <container_name> : Remove the container and volumes associated with it.
docker volume create <volume_name> : Creates a new volume.
docker volume ls : Lists all Docker volumes.
docker run -d - - name <container_name> -v <volume_name>:/<file_location> <image_name> : Run a container with a volume for persistent storage.
docker exec {options}
It allows to run new commands in a running container.
docker exec -d <container_name> <command> : For running the commands in the background.
docker exec -it <container_name> /bin/bash : Accessing an interactive shell inside a container.
docker commit <container_name> <new_image_name> : After running containers using the current image, updates can be made by interacting with the containers. From those containers, a new image can be created using this command.
docker save -o <output_file_name>.tar <image_name> : Used to save a Docker image into a tarball so that the image can be transferred, archived, or shared without relying on Docker Hub or another registry.
docker load -i <input_file_name>.tar : Used to restore a docker image from the saved tarball.
docker cp {options}
Used to copy files or directories between a Docker container and the local filesystem.
docker cp <container_name>:/path/inside/container/<file_name> /path/on/host/
docker cp /path/on/host/<file_name> <container_name>:/path/inside/container/
Docker Ports
To access a Docker container from the outside world, the host port must be mapped to the container’s port. Port mapping helps achieve this.
docker run -d -p <port_on_host> <port_on_container> < image_name>
docker attach <container_name> : Connects to the main process of a running container, allowing for direct interaction with that process’s input and output.
docker network create <network_name> : Create a new network.
docker network ls : Lists all Docker networks.
docker network connect <network_name> <container_id> : Connect a running container to network.
docker run - - network <network_name> <image_name> : Start the container with a specified network connection.
docker network inspect <network_name> : Inspect the network to view network configurations.
docker network rm <network_name> : Remove network.
Docker system prune : Removes unused data (stopped containers, unused networks, dangling images).
docker container prune : Remove Stopped Containers.
docker image prune : Remove Unused Images.
docker network prune : Remove Unused Networks.
docker volume prune : Remove Unused Volumes.
Docker Build
docker build -t <image_name:tag> . : Builds a Docker image from a Dockerfile.
docker build -t <image_name:tag> -f <path_to_dockerfile> <build_context> : Specify Dockerfile location.
docker build - - no-cache -t <image_name:tag> . : Building Without Cache.
Docker Compose
docker compose up : Starts all services defined in a docker-compose.yml file.
docker compose down : Stops and removes all services defined in a docker-compose.yml file.
References
https://aws.amazon.com/what-is/containerization
https://www.ibm.com/topics/containerization
https://www.ibm.com/topics/docker
https://www.geeksforgeeks.org/containerization-architecture-in-system-design
https://www.docker.com
https://kubernetes.io