Docker Advanced Topic and deploying flask-application using docker compose concept
The above image was captured by me in USA- Pacific Ocean in 2016. No copyright tag :P Enjoy the read.
Docker Advanced Topics:
Docker Volume
Docker Network
Docker Compose
Docker Swarm
Docker Volume
In Docker, a volume is a mechanism used to persist data generated by containers or share data between containers. It provides a way to store and manage data independently of the container lifecycle.
When you create a volume in Docker, it is managed by Docker itself and can be attached to one or more containers. The data stored in a volume is preserved even if the container using it is removed or replaced. This allows you to separate the concerns of data persistence from the lifecycle of the containers.
Volumes can be created and managed using the Docker command-line interface (CLI) or through Docker Compose files.
What is WORKDIR in Dockerfile & Concept of Volume and why it is important in the event of a Docker container getting deleted?
WORKDIR is similar to pwd in Linux, so we create a working directory where we will place files.
Docker`s virtual environment can be written to the directory, so to create that directory we use the WORKDIR directory name.
Here we will demonstrate by deploying Django-todo app and using docker volume.Step 1: We will create directories for each Docker image and copy the Pwd path.
Step2: volume is created for Django-todo
docker volume create --name django-todo-volume --opt type=none --opt device=/home/ubuntu/volumes/django-todo/ --opt o=bind
The above commands explain as docker volume creation is done with the name Django-todo-volume with option type none, no special type volume and option device is your directory where you will attach the volume and then the option to bind the container and volume.
so you can move data bidirectional from container to volume and vice versa.
Step3: Inspect the volume
docker volume inspect django_todo_volume
Step4: Killing the container here
Step5: Mounting volume now
Understanding the above command--> docker run -d(detached mode) -p(bind ports) --mount source=device volume, the target is folder within the container, so both will bind and the last parameter is the image name
Docker Network
In Docker, a network is a communication bridge that allows containers to communicate with each other, with the host machine, and with external networks.
Docker provides different types of networks to suit various network requirements:
a. Default Networks: Docker automatically creates three default networks when it is installed: bridge, host, and none.
b. User-Defined Networks: Docker allows you to create custom networks for your containers. User-defined networks provide better isolation and control over container communication.
-Bridge Networks: You can create bridge networks using the docker network create
command
-Overlay Networks: Overlay networks are used for multi-host communication. They enable containers running on different Docker hosts to communicate with each other as if they are on the same network.
-Macvlan Networks: Macvlan networks allow containers to be directly attached to a physical network interface on the host machine.
-Custom Network Drivers: Docker also provides the option to develop custom network drivers to meet specific networking requirements.
c. Network Scopes: Docker networks can be scoped to different levels:
Global Scope: Networks with the global scope are available to all Docker daemons running on the host or in a Docker Swarm cluster.
Local Scope: Networks with local scope are only available to the Docker daemon where they are created.
Docker Compose implemented in deploying Flask-app-ecs as an example
Docker Compose is a tool provided by Docker that allows you to define and manage multi-container applications. It simplifies the process of running multiple containers and configuring their interactions.
With Docker Compose, you can define your application's services, networks, and volumes in a YAML file called docker-compose.yml
. This file describes the desired state of your application, including the containers to run, their configurations, and the networks and volumes they should be connected to.
Sudo apt install docker-compose
git clone "url of your github"
vim docker-compose.yaml
docker-compose up -d
docker ps
Please zoom in and the rightmost column show up flask-app-ecs_flask-app_1
Docker Swarm
It is also called a cluster: It is a container orchestration tool. You can do scaling, health monitoring, and Load balancing. But now Kubernetes is used these days.
We will talk about this in my K8 (Kubernetes) blog later.