Docker Volume and Docker Swarm

As we know Docker containers are ephemeral,i.e all the data will be wiped when the container shuts down. So we use volumes to make Docker data persistent.

Volumes

Volumes are like virtual hard drives managed by Docker. Docker handles storing them on disk (usually in /var/lib/docker/volumes/).

You can declare a directory as a volume only while creating a container as it can't be created from the existing container.Volumes can be shared with any number of containers.

The volume is not included when we update an image.

Volume can be mapped in two ways :

  • Container <- -> Container

  • Host <- -> Container

Creating a volume

docker run -d --name <container_name> -v <source>:<target> <image_name>

Creating volume from Dockerfile

FROM ubuntu
VOLUME ["/myvolume"]

Once you create a container with this docker file , do docker volume ls you will find a volume named myvolume .

Attaching existing volume to another container

To attach the volume myvolume to new container use :

docker run -it --name <container_name> --privileged=true --volumes-from <container_name> <os_name> /bin/bash

Now you are inside the container,do ls and you can see your volume.

To make a folder as volume go inside the folder and run the command

docker volume create --name < volume_name > --opt type=none --opt device=< path_of the volume > --opt o=bind

docker run -d --mount source=<volume_name>,target=/app <image_name>

if we make any changes inside the volume it will reflect inside the container and vice-versa.

Docker-Swarm

It is a service that allows users to create and manage a cluster of Docker Nodes and schedule containers. We have two types of nodes in Docker Swarm :

  • Manager node: Maintains cluster management tasks.It knows the status of all the worker nodes in a cluster.

  • Worker Node: Receive and execute tasks from a manager node