Table of contents
- Docker Volume
- Docker Network
- Task 1: Create a multi-container docker-compose file that will bring UP and DOWN containers in a single shot ( Example - Create application and database container )
- Task 2: Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.
- Thanks for reading!
Docker Volume
Docker allows you to create something called volumes. Volumes are like separate storage areas that can be accessed by containers. They allow you to store data, like a database, outside the container, so it doesn't get deleted when the container is deleted. You can also mount from the same volume and create more containers having the same data.
There are two ways of storing files in docker -
Volumes that are part of the host filesystem and managed by docker.
Bind mounts which can be stored anywhere in the host system.
One volume can be attached to multiple containers and that is how it can also be used as a file-share system between containers.
Volumes also support volume drivers, allowing storing the data on remote hosts or cloud providers, among other possibilities.
On the other hand, Bind-mounts can be stored anywhere in the system and can be modified by non-docker processes as well. The files and directories that we are mounting need not to be existing already in the host machine, they can be created at the run time. And Bind mounts have lesser functionality than volumes.
There are two options -v (--volume) or --mount.
-v
-v syntax combines all the options in one field, while the --mount syntax separates them.
-v consists of 3 fields in general which are separated by a colon (:) -
Name of the volume if it is named volume. If it is an anonymous volume then this field will be omitted.
The path where the volume is mounted in the container.
This is an optional and comma-separated list of options.
Example - docker run -d --name cont -v myvol:/app nginx:latest
--mount
--mount is more verbose.
--mount Consists of multiple key-value pairs, separated by commas and each consisting of a <key>=<value> tuple.
The source of the mount. For named volumes, this is the name of the volume. For anonymous volumes, this field is omitted. It can be specified as source or src.
The destination takes as its value the path where the file or directory is mounted in the container. Can be specified as destination, dst, or target.
The read-only option, if present, causes the bind mount to be mounted into the container as read-only. Can be specified as readonly or ro.
The volume-opt option, which can be specified more than once, takes a key-value pair consisting of the option name and its value.
Example - docker run -it --name cont --mount source=myvol,target=/app nginx:latest
Docker Network
Docker allows you to create virtual spaces called networks, where you can connect multiple containers (small packages that hold all the necessary files for a specific application to run). This way, the containers can communicate with each other and with the host machine (the computer on which the Docker is installed). Docker's default network is bridge and it is created with the name docker0.
There are several types of network drivers -
bridge: The default network driver. If you don’t specify a driver, this is the type of network you are creating. Bridge networks are commonly used when your application runs in a container that needs to communicate with other containers on the same host.
host: Remove network isolation between the container and the Docker host. And shares the host’s network with the container.
overlay: Overlay networks connect multiple Docker daemons and enable Swarm services and containers to communicate across nodes.
none: Completely isolate a container from the host and other containers. none is not available for Swarm services.
Macvlan: Macvlan networks allow you to assign a MAC address to a container, making it appear as a physical device on your network. The Docker daemon routes traffic to containers by their MAC addresses. Macvlan networks are best when you are migrating from a VM setup or need your containers to look like physical hosts on your network, each with a unique MAC address.
ipvlan: IPvlan networks give users total control over IPv4 and IPv6 addressing. The VLAN driver builds on top of that giving operators complete control of layer 2 VLAN tagging and even IPvlan L3 routing for users interested in underlay network integration.
Let's complete these to have a complete understanding of the above topics -
Task 1: Create a multi-container docker-compose file that will bring UP and DOWN containers in a single shot ( Example - Create application and database container )
Here, we will take this docker-compose.yaml file to work on the services.
Use the docker-compose up
command with the -d
flag to start a multi-container application in detached mode.
Notice that an external volume is used by the "web" service. If we directly try to create the services without creating the external volume, you will get an error.
Let's create the external volume and the services--
We will stop them and run again in daemon mode -
Let's check the services created by using docker ps
. Here, we can see that 2 replicas of web service have been created as we had mentioned this in the yaml file.
We can check if the web is working -
Use the docker-compose scale
command to increase or decrease the number of replicas for a specific service. You can also add replicas
in the deployment file for auto-scaling.
We have already tried using the replica in the .yaml file.
Now, let's see how to scale up using docker-compose scale
So, let's use docker-compose up -d --scale SERVICES=number.
I told to make "3" instances of the web service, so docker created only 1 more instance as 2 instances were already present.
Now, we can access our website on port 8005 also.
Now, let's keep a single instance of our web service.
Now, we can only access our website on a single port- 8004
Let's try to access it on port 8005 -
Use the docker-compose ps
command to view the status of all containers, and docker-compose logs
to view the logs of a specific service.
Use the docker-compose down
command to stop and remove all containers, networks, and volumes associated with the application.
All the services were stopped and removed, Now, we don't have any service created.
Task 2: Learn how to use Docker Volumes and Named Volumes to share files and directories between multiple containers.
Let's create a local volume using docker volume create --name <any_name>
Create two or more containers that read and write data to the same volume using the docker run --mount
command.
Let's create a container named "cont1" using the Nginx image which will be pulled from the docker hub.
Let's create another container named "cont2" using the Nginx image.
Verify that the data is the same in all containers by using the docker exec
command to run commands inside each container.
Let's add a file with some content in the cont1 container.
Let's check in the cont2 container -
Here, it is verified that these 2 containers are sharing the same volume as we can see that the file created in the cont1 is also accessible to cont2.
Use the docker volume ls
command to list all volumes and the docker volume rm
command to remove the volume when you're done.
We can all the volumes using docker volume ls
-
Let's delete this volume using docker volume rm <volume_name>
As you can see that the volume can't be deleted if it is in use. Let's stop and remove both the containers-
Now, let's remove the volume -
Here, we can see that we no longer have the volume "new_vol" that we created.
So today, we saw how to use docker volumes and network.
Thanks for reading!
#devops#90DaysOfDevops#TrainWithShubham
Let's connect on Linkedin - linkedin.com/in/namya-khullar-7b5758200