- 1 day ago
Category
📚
LearningTranscript
00:01next thing that we are going to do is learn about docker volume or docker volumes
00:06volumes are persistent data stores for containers
00:10basically we have seen in mongodb node.js application
00:13example in the example we have seen in our node.js application
00:15if we have a running container, if we have a running container
00:18if we have a mongodb database in the container, we need to add
00:22something else to the database, if we stop that container
00:25or if we stop that container or delete that container
00:28then all the data will be lost
00:31in this way, the normal docker containers are not
00:34in their data persistence, it means that the container
00:37will stop or delete, so our data
00:41is permanently deleted, it is not a way to save it
00:44so to solve this problem, we have something called volumes
00:48in docker, volumes concept basically
00:50let us suppose, this is our host machine
00:54now we have a container that we have created
00:58so this is let us suppose
01:00our container A, now container A
01:03we have some data that we want to store
01:05let us suppose, this is our data
01:07which we want to store
01:08so what do we do in our host machine
01:11we have some data that we want to store
01:13we create a data store
01:14which we want to call it
01:17basically this is a file or a directory
01:20which we have reserved
01:22and we have these two
01:23we have mapping
01:25so whatever changes we want to do in container
01:27this data
01:28we will replicate
01:30and the same data
01:33we have local host machine
01:34so if we want to stop this container
01:37or delete
01:38then our data
01:40will always persist
01:42so basically for docker volumes to work
01:44we have a memory
01:45or a small part of our host system
01:47which we reserve
01:48and then we mount it
01:51we attach it
01:52onto the docker container
01:54and then all of the data
01:56is now available here
01:58and it is permanent
01:59it is persistent
02:00unless and until
02:02our host machine
02:03something happens
02:03and one more interesting thing
02:05about volumes
02:06is that if we have
02:07another container
02:08let us suppose
02:09created
02:09our container
02:10so if we want to
02:12make a single volume
02:14with multiple containers
02:15with multiple containers
02:19we can share it
02:20in fact
02:21if we stop this container
02:23and we will restart it again
02:24then we can fetch it again
02:26from this volume
02:26from this volume
02:27then we will see
02:28our database
02:29like in the mongodb
02:30example
02:31then we will not recreate it again
02:32once again
02:33we will have to get there
02:34already existing data
02:35we will get there
02:36with this
02:37with this
02:38if we will delete our containers
02:40then this data
02:41will persist on our host machine
02:43so this entire concept
02:44leads to the existence
02:45of persistent data
02:47now
02:47docker volumes
02:48for the demo
02:48we will consider a very simple example
02:49for example
02:51we are going to run
02:52a ubuntu container
02:53so we can run the container
02:54we can do
02:55docker run
02:56we will do hyphen it
02:57and ubuntu
02:59before running this
03:00we are going to add
03:01one more extra option
03:03with it
03:03and that option
03:04basically
03:05for the volume option
03:07we can simply
03:08write volume
03:09or we can write
03:10hyphen v
03:10and hyphen v
03:11we can define
03:12our volume directory
03:13so for example
03:15to consider
03:15basically
03:16what we are trying to run
03:18we are trying to run
03:19this container
03:20which we will run
03:22ubuntu
03:23this ubuntu container
03:24we want to create a folder
03:26let us call it
03:27test
03:28slash data
03:29so we will create a folder
04:15in the host system
04:16which is basically this path
04:18which is basically this path
04:19that we are creating the volume
04:19that we are creating
04:20which path we are creating
04:21within the host
04:21so here we will enter
04:23desktop
04:24slash data
04:25in fact here
04:26we will enter the relative path
04:27we will enter the absolute path
04:34we will enter the whole path
04:35inside that path
04:35we can do the entire path
04:36into that path
04:36and then
04:36we will enter the entire path
04:37and then
04:38then we will enter
04:41the entire path
04:42which is going to be
04:42slash
04:43test
04:44slash
04:45data
04:45and this way we will create our new volume which is then mounted onto the container
04:50so to do this, first of all, we have already defined "-v", we are going to define the volume
04:56so first of all, let's give host system the path, we will start with users and system name
05:02we are going to go to desktop and in desktop we will create a folder in the container
05:07we want to create a test and with that we want to create another data folder
05:14"-it", because we want to run our container in interactive mode, then we can see our files and folders
05:20so let us now run our container
05:25so first of all, since the image was not locally available, we will pull that image
05:30and finally, we are now inside our container, we are in the container root and this is the container id
05:36now we can see our files and folders here, let us cd into test and here we have this data
05:44folder
05:44let us also cd into the data folder, now in this data folder we will change some changes
06:06let us suppose a new file index.html, so as we have created index.html, we will enter our data
06:12folder
06:12so we have this index.html, we will notice this, here in the terminal which we have created
06:18that was created inside the container, so this is our folder, this exists on our own local host machine
06:25outside of container and outside of docker onto our desktop, we want to create another file
06:30create, let us create server.js and now we have two files available and in the desktop
06:36we are simply going to exit this container, now we are simply going to exit this container
06:45now this container exists, but now this container will stop
06:47if we do docker ps, now we have no running container
06:51not in containers, it is not in containers, it is in containers, but now it is stopped
06:54but if we look at our data, then this data will persist
06:59or again, if we start our container with the container id
07:05then our container is now start
07:07let us do docker exec-it
07:12container id
07:14with
07:15with
07:15slash bin slash bash
07:17so now we are inside the container
07:19we will do ls again
07:20let us cd
07:22into test
07:23into data
07:24so ls
07:25we will do ls again
07:26then we will have those files again
07:27access
07:28if we have not attached volume
07:30then in this case
07:32this file is created permanently
07:33from container
07:34in fact, if we exit
07:36and we do docker stop
07:39the container
07:40and then we are going to remove the container permanently
07:43this container
07:45we have permanently removed
07:46this container
07:47we have permanently removed
07:47from containers
07:47but the data is still
07:51persist
07:51so this is how docker volumes work
07:54now let us explore more options related to docker volumes
08:02we want
08:03with the help of docker commands
08:04we can create our own custom volumes
08:06custom volumes create
08:08first of all
08:10let us explore
08:11docker desktop
08:11how do we see volumes
08:13in the volumes
08:14we can explore all of the docker volumes
08:16to list all of the available volumes
08:19we can write
08:19docker
08:20volume
08:20ls
08:21so we will have all of the volumes
08:23up to create a new docker volume
08:26we have this command called create
08:27create
08:28after writing
08:28we can give our volume a specific name
08:31and with the same name
08:32Docker will automatically create that volume
08:35create
08:36so let us try this
08:38docker
08:39volume
08:39create
08:40and i am going to call it
08:42my volume
08:43we will enter
08:44and if we go to docker desktop
08:46we have my volume
08:47in fact
08:48we can do docker volume ls
08:50and we will be able to see our volume
08:53this same volume
08:54if we want to delete this same volume
08:55if we want to delete this same volume
08:55then we can simply do
08:56docker volume rm
08:58and after that
08:59our volume
09:00name
09:00so
09:01our volume
09:02will delete
09:03and it won't be visible
09:05in docker desktop
09:05now
09:06this is the way
09:07now
09:07one question here
09:08is
09:10that
09:11when we try to create a new volume
09:13like this
09:13docker
09:14automatically
09:17create that volume
09:18and the answer to that is
09:20if we are working on a windows machine
09:22then by default docker
09:23we can create our volumes
09:25if we are working on a mac or linux machine
09:28then in this directory
09:29our volumes
09:30create
09:30now
09:31this is the process
09:32simple
09:33just to create volumes
09:35but just to create volumes
09:36not enough to create volumes
09:38we need to mount them
09:39onto the containers
09:40that we are working on a value
09:41if we are running containers
09:42we need to create a volume
09:44so we can do this
09:45in which way
09:45we can attach the volume
09:48so we can do it
09:48in docker
09:49we have two important commands
09:50one command is our
09:52hyphen hyphen volume command
09:54and the second command is
09:56hyphen hyphen mount
09:57this is a short way
09:58that we already have used it
10:01in short
10:02we can use volume command
10:03and mount command
10:04we can use both
10:05the same functions
10:07and that is the exact output
10:09So let us see examples of how this works. Basically, we can attach our three types of volume with our
10:16container, which is our first way to attach Named Volumes.
10:21Named Volumes means that these volumes are already created and we have assigned a name.
10:28So to create Named Volumes, we create some of these volumes, and then if we refer to these volumes, we
10:35can refer to them using this name.
10:37So for that, the command is docker run, and instead of the host path, which we have seen in the
10:44demo,
10:44we give our volume name and then we give the container directory. For example, if we have created my volume,
10:51and this is the container directory in which we want to store data in this way,
10:56we can separate these options with this colon when we are trying to run a container.
11:00Now, Named Volumes under, docker will search for us that if the name of the volume already exists,
11:06then it will attach it with the same name. If not, docker creates a new volume with the same name
11:12for us.
11:13By default, if the volume doesn't exist before, docker will give us a new volume.
11:18Second way is using anonymous volumes.
11:22There is no name in anonymous volumes. In this case, what do we do?
11:26We simply give our mount path.
11:28This means, in which directory we want to store data in the container.
11:32We give it only. As such, we are not concerned, where docker will store our data,
11:36where docker will create volume, what name will give it to the volume.
11:38So in this way, we can use anonymous volumes. And third way, which we have already seen,
11:43where we give our host directory path, which we have done on the docker desktop in Ubuntu case.
11:49And we give container directory path. We call it bind mount.
11:55So in this process, basically, we are binding the host directory path with the container directory path.
12:01And we are then mounting this volume. So in conclusion, we have three different ways of creating volumes
12:06and attaching them to a running container.
12:09Now, generally, when we talk about production environments,
12:12generally, if we go into an organization, if we use Docker volumes,
12:15then there is the best way that we go forward with named volumes.
12:19So when we deal with the production environment,
12:22we prefer to create the names of volumes.
12:33And we can also do the same things with the mount option.
12:36Mount option, we can go here to the docker volumes page.
12:40And here, we can look at the syntax for the mount as well as the volume flag.
12:44For mount, we simply define these options.
12:47For volume, we write the name of volume.
12:49Then we give the mount path.
12:50If we do the same thing with the mount,
12:52then we can define the type of volume.
12:54In the source, we give the name of volume.
12:56And in destination, we add the mount path.
13:01So in this way, we can add the same thing with volume flag or mount flag.
13:05Generally, if we are frequently working with volumes,
13:07then we can create many volumes in Docker.
13:10Which doesn't attach with any container.
13:12For example, if this is our host machine,
13:15and let us suppose we have data or volume that we have created,
13:18and then we can delete our container.
13:21After that, we have another new container.
13:23We have created it.
13:24And with this, we attached one more volume.
13:27We have attached it.
13:28And then, we can delete this container again.
13:30And then, we can delete it.
13:31So in this way, we can do that.
13:33There are many volumes available in our system,
13:36which are not in use.
13:38No container is not using it.
13:39And we don't need the data inside.
13:40And we don't need the data inside.
13:42In such cases, if we want to delete these volumes,
13:46and we don't have any container attached,
13:48and we don't need the data inside,
13:50there is a command that we can run,
13:52which is called docker volume prune.
13:55Prune means that all the unused volumes
13:58will delete from our system
14:00so that the extra overhead for those resources
14:03will not be added.
14:04So the prune command, we can use it.
14:06We can never delete it.
14:07But this prune command,
14:09we have to use it.
14:10We have to ensure that
14:11whatever data inside these volumes
14:12is available,
14:14the unattached volumes
14:15will not be in our future.
14:17After ensuring that,
14:18we can simply run this command on the system.
14:22in order to Lukin
14:23to upload this to the squad,
14:23They are also being found
14:23where we can password partners
14:23to sew all the different directions.
14:23or sociais scriptures
14:24can determine what to do.
14:24And yet,
14:24if we're indulging.
Comments