Table of contents
- What is Docker?
- Why would you need to use Docker?
- Virtual Machines vs. Containers, How does it Work?
- What is a Container?
- Where do containers live?
- Docker image and Dockerfile
- Docker Architecture
- Installation of Docker
- Docker Commands
- Docker Networking
- Docker Volumes
- Docker-compose
- A microservices-based project using Docker containers
- References
What is Docker?
Docker is a tool that allows us to package and run our applications in isolated environments also known as containers. In simple terms, a developer package the application and its associated dependencies in a single file also known as a Dockerfile and that is used to build the docker image, and by running the docker image we can have a running docker container. This ensures that the application can run on any environment having docker installed on the environment.
Why would you need to use Docker?
Docker containers are portable across machines. Once a container is built we can share that with others and run it anywhere.
Docker containers are isolated from each other and from the host machine. This ensures that one application can not impact another application or the host OS.
Docker images are versioned control, this makes it easy for an application to roll back to its previous version if needed.
Docker containers offer microservices architecture. Each container can run one microservice, making it easy to scale individual services independently.
Docker containers are more resource-efficient than virtual machines since they share the host OS kernel. This allows us to run more containers on the same hardware.
Docker containers start up very quickly since they don't need to boot a full operating system. This results in faster deployments and scaling.
Virtual Machines vs. Containers, How does it Work?
Virtual machines virtualize both the operating system and applications. Each VM runs its own guest operating system. Docker containers only virtualize the application layer. They run on top of the host operating system and share its kernel.
Virtual machines are heavier, typically in the order of gigabytes. They need to boot a full guest operating system. Docker containers are lightweight, typically in the order of kilobytes.
Virtual machines take longer to start up since they need to boot a full guest OS. Docker containers start up almost instantly since they reuse the host OS.
Virtual machines can run any operating system. Docker containers work best with Linux distributions.
What is a Container?
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. Containers provide portability, efficiency, and isolation compared to traditional virtual machines.
Where do containers live?
Docker containers live both in memory and on the disk of the host machine. When we run a container, Docker does the following:
Pulls the image from a disk or downloads it from a registry.
Loads the image filesystem into memory.
Creates a container configuration with settings like networking and environment variables.
Starts the container process by running the default CMD or ENTRYPOINT command.
We can see the containers by running the "docker ps
" command. The location where Docker stores container data depends on the storage driver, but by default, it is mentioned below:
Linux:
/var/lib/docker
macOS:
~/Library/Containers/com.docker.docker/Data/vms/0/
Windows:
C:\ProgramData\docker
Docker image and Dockerfile
A Docker image is a read-only template that contains everything needed to run an application - the code, runtime, system tools, system libraries, and settings. In simple terms, a container is a running instance of a docker image.
A Dockerfile is a script file that contains all the instructions to create a Docker image by automating the steps we would normally have to carry out manually to run an application.
Below is an example of Dockerfile:
FROM ubuntu
CMD ["echo", "Hello Sumit, I am the container!"]
In order to get a docker image from the above docker file we will run the command below:
docker build . -t <image-name>
In order to get a container from the docker image we will run the command below:
docker run --name <container-name> <image-name>
You can find more information related to the docker file here
Docker Architecture
When we interact with the Docker client via command-line interface in order to work with docker, the docker daemon finds the docker image present locally or from the docker registry (Private or Public) and helps us pull/push/run the images in order to work with the docker containers.
Docker's architecture follows a client-server model with several important components:
Docker Engine
Docker Objects
Docker Registry
The Docker engine consists of:
Docker daemon (dockerd) - It is a background process that manages Docker objects like images, containers, networks, and volumes. It listens for requests from the Docker client and REST API.
Docker Client - It is the primary command line interface for Docker. It allows users to interact with the Docker daemon and issue commands.
REST API - The Docker daemon exposes a REST API that can be accessed by the Docker client or other third-party tools. This allows programmatic interaction with Docker.
Docker objects consist of:
Images
Containers
Networks
Volumes
A registry stores Docker images. The public Docker Hub registry is the default, but private registries can also be used.
Installation of Docker
Click on the below environment links to get started with the docker installation.
Install Docker Engine on CentOS
Install Docker Engine on Ubuntu
Install Docker Desktop on Windows
Docker Commands
Please find the docker CLI Cheat Sheet by clicking here
Use the docker --help
command to learn more about all the commands.
Docker Networking
Docker networking allows containers to communicate with each other and the Docker host. Docker uses the Container Network Model (CNM) to manage container networking. CNM uses a network driver and IP address management (IPAM) driver to provide networking for containers.
Docker uses the below network drivers to implement the CNM concepts:
bridge - It is the docker default network driver. It uses a Linux bridge to provide isolation between containers.
host - It uses the host's network directly.
none - Provides full network isolation.
overlay - It is used for clustering. This creates an overlay network across multiple hosts.
macvlan - This assigns a MAC address and IP to the container from the host's subnet.
Docker Volumes
Docker volumes allow us to persist container data outside of the container's filesystem. Volumes can be created and managed in different ways. To create the volume explicitly and apply it while running a container use the below commands:
docker volume create <volume-name>
docker run -d -p <desired-port>:80 -v <volume-name>:/app --name <container-name> nginx
Docker-compose
Docker Compose is a tool used to define and run multi-container Docker applications. With Compose, we define a multi-container application in a docker-compose.yml
file and then use docker-compose
command to start all the application's services from that definition.
Use the below commands to install docker-compose:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
Use the below commands to work with docker-compose
docker-compose up -d
docker-compose down
docker-compose -f <filename> up -d
docker-compose -f <filename> down
A microservices-based project using Docker containers
You can find the source code for this project here
In order to quickly get started with the application download the file dockerhub-image-compose.yml
and run it using docker-compose -f dockerhub-image-compose.yml up -d
Now access the application using "localhost":5000 or "ip-of-machine":5000 and "localhost":5001 or "ip-of-machine":5001
References
Finally, you have come to this point. Great job! If you have learned docker from this article then you can support this by liking, commenting, and sharing with others.