Docker Advanced - Volume, Compose & Swarm:

1. What is Docker Volume?

Volume is simply a directory inside our container.

  • First, we have to declare this directory as volume and then share volume.

  • Even if we stop the container, still we can access volume.

  • You can share one volume across any number of containers.

  • You can map Volume in two ways:

    • container ------ container

    • Host ------ Container

2. Benefits of Volume:

  1. Decoupling Containers from storage.

  2. Share volume among different containers

  3. Attach Volume to Containers.

  4. On deleting Container, Volume doesn't delete.

3. Creating Volume:

  1. git clone https://github.com/LondheShubham153/django-todo-cicd.git

  2. cd django-todo-cicd

  3. docker build . -t django-todo-cicd

  4. docker images

  5. Now create a directory named Volume and copy the PWD.

  6. Now Create volume:

    docker volume create --name django-todo-cicd-volume --opt type=none --opt device=/home/ubuntu/projects/django-todo-cicd/volume --opt o=bind

  7. To inspect Volume/check:

    docker volume inspect django-todo-cicd-volume

  8. Now attach the volume and run it in a Container.

    docker run -d -p 8000:8000 --mount source=django-todo-cicd

    volume,target=/app django-todo-cicd:latest

    Now if you do ls, you will get to see all the files under volume

  9. docker exec -it a2d40a9bd bash

  10. Now create a file in the apps folder, do ls and exit, then do ls, you will find the same file under the volume folder.

4. What is Docker Compose?

Docker Compose is a tool that was developed to help define and share multi-container applications. With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

5. Create a Compose file:

TASK -1

  • Create a Multi container docker-compose file that will bring UP and bring DOWN containers in a single shot (create an application and a database container)
  1. sudo apt-get install docker-compose ------> install docker-compose

  2. vim docker-compose.yaml -----> Create a compose file

  3. docker-compose up ------> it will start composing the compose file

  4. docker-compose up -d ----> it will start the multi-container application in a detached mode.

  5. docker ps

  6. docker-compose down -----> It will stop/remove the Containers, networks and volumes associated with the application

5. What is Docker-Swarm?

Docker Swarm is an orchestration tool provided with Docker. With Docker Swarm, you can manage, scale, and maintain containerized applications.

It has been configured to join together in a cluster. The activities of the cluster are controlled by a swarm manager, and machines that have joined the cluster are referred to as nodes.

Clustering with Docker Swarm - Creating and managing a Swarm | Easy Guide -  CD Cloud Logix

Creating Docker-Swarm

  1. First, let's create 4 instances in AWS and name them docker-swarm-master and the rest docker-swarm-workers.

    1. First, connect to the Docker master instance and update the system & install docker.io

      sudo apt-get update && sudo apt-get install docker.io

    2. Similarly using the above command update and install docker in worker instances.

    3. sudo usermod -aG docker $USER

      sudo reboot

    4. Now go to the master instance and type docker swarm init

      it will initialize it as master

    5. Now go to the master instance and go to security group >> edit inbound rules >> add the port which you will get in the master branch when you initialize docker swarm.

      Similarly, add this inbound rule to the workers as well.

    6. Now copy the docker swarm join token and paste it to your worker nodes so that it will join the swarm master as a worker

    7. To check docker swarm nodes

      docker node ls

    8. To start a service

      docker service create --name mysql-service --replicas 5 --publish 3306:3306 -e MYSQL_ROOT_PASSWORD=test@123 mysql:5.7

    9. Now you can do docker ps in master as well as in worker nodes, and you will find a container that is created has been distributed between master and nodes.

      In master 1 container running

      In worker nodes 2-2 each are running.

    10. To scale up your replicas:

      docker service scale <service-name>=10(Replicas)

    11. Now if you want to remove the service

      docker service rm <service name>