Docker Container Tutorial #10 Handling Volumes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to another how to code well YouTube tutorial my name is Peter Fisher today is the tenth video or tenth tutorial in the docker containers tutorial series if you followed along then please let me know it'd be lovely to hear from you and find out what you think of the series so far today we're going to focus on volumes doctor volumes as well as how to create a bind mount host folders on your host machine to a docker container now this can trip a couple of people have certainly trips me up from time to time and that's because if you if you do the configuration slightly differently slightly wrong by perhaps mistake then it can go off and do something slightly differently something that you weren't quite expecting so what I'm going to do is go quite slowly and I'm going to discuss each each point in a in a bit of detail so do bear with me okay as always what we're going to do is a docker machine active just to list the active machine that we're running and that's the how to code well that's great I'm also going to do a docker images this should bring up one image the Ubuntu image it's the latest one I haven't done anything with it and if I did a docker PS minus a we can see that we have no containers here that's fine let's clear that down now I'm going to talk about some arguments that I haven't talked about thus far so we're going to do docker run bias might help to list those out so the arguments that we're going to use are the - fee and also them the - - volumes from so let's focus on this one first so the - fee or - - volume it's a bind mount of a volume so you're buying mounting from your host machine - a container and we can supply a lot of these multiple amounts of these things because you've got the comma there and so forth so let's just clear this down and and do that let's first of all what we need to do is is actually create a directory on this host machine for us to to mount so let's do an LS or four current directory and as you can see it's completely empty so let's do a mock though and let's create a directory called data CD into that data directory and let's just touch a file we're going to call this test one dot txt I'm just import tests actually like so let's do an LS so we've created a file on this host host machine and if I was to LS again we can see that that's the folder that we've just created fine okay let's clear that down I'm just going to bring that up for reference now what we need to do is a docker run - I T because we want to do it interactively and we're going to give it a name of test1 so that's the name of the container we're also going to do a minus V flag because that's the volume flag that what I was just talking about now this flag comes with two aspects to it the first on the left hand side here is the source this is the source path this is the path to the source that you want to mount in the container and then on the right hand side of the colon is the destination so this is the way you want mounting it to so of the path in the container that you want to mount to now I must stress that to do a bind mount you need to do you need to use an absolute path if you do a relative path or if you just have a path for term doesn't have any you know if you if you don't actually specify the path and you just give it say a name and it's going to do something completely totally different I'll get onto that down the road in this tutorial let's just focus on this bow mount for now so let's put in the source and I'm going to do home directory and it is the docker vid like so and it's the data directory that we've just created and the destination of the in the container I want to do slash data so I'm put it in the root of the container in /data as always we're going to use the Ubuntu image and I'm going to dive into the bash shell so before I do this I just explained so docker run - i.t we're going to run this container interactively we're going to give the container name of test1 and we're going to bind mount this host directory which is my home directory then dock of it then the data directory into a new directory called slash data at slash data so in the root of the of the container there's going to be a folder called data and in that data folder there will be that test file that I've just created we're going to use the Ubuntu image like normal and we're going to dive in using the bash shell so let's just press Enter and get in there so let's do an LS we're in the root of this container already and we can see that we have this data directory created for us if I was to do an LS of that we can see that we have this test text file that we created on the host machine also if I was to exit you so I'm just going to come out the container and let's LS again so we have this data directory we're on the host machine let's do CD in tube data and let's do a bin of test and we're just going to put some bits and pieces in here so let's just do a test example like so I'm going to exit you and right quit map so we've actually put some contents in this file now clear this now because I've exited the container I'm gonna have to start it again and I think I call it let's just do a docker PS - a my memory yes it's test1 so docker start temps test1 start that up and we're going to just dive into here and see that that the contents were changed let's do it docker Exe see - - IT to the test one container and let's jump in into the bash shell and if I was to do a LS we can see that we have this data directory again CD into that and then LS so we can see the file if I was to cat that file so test dot txt we can see that we've actually got the contents so the contents were changed on the host and it's being reflected in this container so we found bind mounted this directory the data directory into this container and it means that we can manipulate the data or the files in this directory from the host which is fantastic for things like web development where you're creating like a you've got a container of a web server perhaps or Apache or nginx or whatever and you're developing a website but you're developing it on the host machine and you're using an IDE like sublime text or NetBeans or whatever to create the the website create files and so forth but the the actual development environment the actual environment of where the website is is in a series of containers that you've got on your your development machine and and so any changes that you do to the files of the of the website get reflected in the container which is really useful I do that all the time let's just come out of here let's do an exit of that and let's clear down the screen one thing I would like to just show is is is how we can debug mount points and so forth so let's do I'm going to do a docker - - help so what we're going to use is a command called what's it called it is sorry here we are inspect so we're going to do an inspect of a container so let's do a clear this down and do a docker PS - a let's do a docker inspect of test one okay so this returns the Jason big JSON payload of information about the container and the one I was looking for is this mount bit here so we can actually see that the destination here and we've got our W which is a read and write and the propagation and where it is on the file system okay let's let's just clear that down let's do a another example let's do a docker PS - a docker RM we're going to remove this we're going to remove file sorry we're going to remove a container so test one we're also going to remove the what we've created on the host machine so RM minus RV data so that's going to remove that folder and the file that we've created inside now the next thing I'm going to demonstrate is creating volume so these are slightly different from buying mount mounts and the configuration is subtly different so let's let's do that lets do a docker PS just to make sure that nothing is there that's great clear that down so we're going to do a docker run again - I T because we want to do it interactively I'm going to give it a name of test - and this time within a - V and we're going to create a volume docker volume so we're not doing a bind mount we're doing a dock up folium so we're going to call this data and the destination is slash data again and we're going to do that Ubuntu image and we're going to dive in as the bash shell so we're doing it we're creating a content we're calling it name - we're doing interactively we're going to land in the bash shell off of the bun - image please note though that I haven't actually given a absolute path to a folder such as - what I want to bind mount so we're not actually doing a bind mount here we're just creating involve a volume a dock a volume called data explain what one of those are in a minute and we're going to mount that into the data folder here so let's do that and again if we were to do an LS we can see that we have the data folder here but if obviously if we did an LS of data it's completely empty because there is nothing in it so we've actually created a blank mount point and we've given it a name of data in this container so this is really useful for sharing key or keeping data persisting persisting data when you have lots and lots of containers and you can have lots of containers that share this data volume and what it's done is if I was to exit this is if I was to do another command that I'll show is docker volume LS so it actually created a docker volume called data so this didn't actually happen beforehand because we were buying mounting this is only happening because we didn't supply an absolute path to this to this source we created this so we've created a docker volume and we've called it data now there are other commands that we can do against the docker volume such as if I did the docket volume - - help we can see that we can create a volume we can inspect we can list and we can remove volumes now this is the reason why this is this is so good is because we were actually decoupling the container from the storage so the container from the volume we can share this volume amongst different can Tanner's as well which I'll get on to later on in this tutorial so we can actually create a container attach this volume to this container put some bits and pieces into it delete the container that's not actually going to delete the volume create a new container and attach this volume to the new one so let's clear this down let's do a docker PS because I can't remember whether it's yeah it's exited so let's do a docker start whoops of test two like so and also let's do a docker e.x ec- IT a test two and we're going in as bash and let's do a CD into the data directory this is the volume and also let's do a touch of test1 dot txt so we're creating a file LS in here let's do an exit off of that and a clear of the screen do a docker PS minus a let's do a docker RM minus F we're going to remove this container like so docker PS - Amy can see it's gone however the docker volume is still intact docker volume LS we can see that we still have this data directory here and we can we can create a completely different container and we can attach it to this data volume and still use the the files that we've created inhale just do an example if we did it clear and a docker run we're going to give this a name of sorry a - I T give this a name of test3 now I think it is and then we're going to do a minus V data and it's going to be slash data and back into the bun to image and we're going to go in as a bash shell so we're going to do whoops that's a that doesn't look right this - I T because we want to do it interactively run - IT interactive container give it a name of test3 the volume that we are using is the data volume now because the data volume exists we obviously don't recreate it so we're just going to attach that existing volume we're going to again at use the destination of slash data using the Ubuntu image and going in as the bash shell let's brush press enter here like so created a completely new container and if I was to do an LS we can see again we have the data directory CD into data and LS and we can see the file so that's an example of creating containers that are pretty decoupled from the from the volumes and we can remove the containers it's not going to remove the data that we've attached the volumes that we've attached and we can spin up another container and attach that volume to the new container let's exit this let's do a clear and then a docker whoops docker PS - a and then a docker RM minus F of test3 whoops test3 not test2 there we go and docker volume LS let's remove this volume our docket volume our M of data now I'm going to show you another volume command now I'm going to do the docker run - - help we're going to do this volumes from command so this volumes from command will allow us to mount the volumes from a specified container to a new container okay so this is really useful when you've got a container that has say a lot of log files and you want to create a container that just handles those log files perhaps passes those log files out in raluca piece of software like go access or has some sort of you know way of auditing and managing log files perhaps it's a firewall and it's it's it's scanning those log files for intrusions and stuff like that let's just clear this down I'll give you an example clear that docker PS minus a we have no containers here docker volumes or volume minus LS or LS we have no volumes so let's do a docker run - I T - - name in this instance we're going to call it master and we're going to do - V we're going to create a volume called backup and we're going to woops that's wrong there we go again a mount map - slash backup we're also going to create another volume we're going to do - V again and we're going to do logs and we're going to mount that to the logs file folder like so again use the Ubuntu image and we're going to go in as bash like that okay so docker run doing it interactively giving it the name of master we've got two volumes that we're mounting the first one is the backup second one is logs going into slash backup and slash logs these volumes they don't have the absolute path so these aren't buying mounts these are going to be created as docker volumes and we're going to give this the name of master let's dive in do an LS and we can see that we have this backup directory here we also have the logs directory again these are totally empty so LS backup and LS what if I call it logs let's do a touch let's just create a file in the backup directory and we're just going to call this B K p1 tar and a touch of temp 1 log whoops in sorry in logs temp 1 dot log like so and let's do just just a show that they're actually in there let's do an LS of logs and an LS of backup there we go so we've created a master container and this master container has produced a log file and a backup file let's exit this container and let's clear the screen to a docket volume again just to list these volumes so we've created these volumes as we created the container the master container let's use the volumes from command let's create a container and mount all of these volumes in one so by doing so we do a docker run - I T to do interactively - - name we're going to call this slave 1 and the command is - - volumes - from we're going to give the container name this is the name of the container that we want to attach the volumes from so this is master and again we go in yes the Ubuntu image and bash shell okay so docker run we're doing interactively giving it a name of slave 1 so that's the name of the container volumes - from we give the master container this is the container name that we just created we're going to mount all the volumes from this master container going in again as the Ubuntu image and the bash shell because it's interactive let's do that so we've created a completely new container here we pulled it slave 1 and if I was to do an LS we can see that we have the backup directory and we also have the logged directory - if I was to do an LS of backup we can see that that is the backup file that we created from the master container as well as a of logs and that is the temporary log file as well so let's exit this and let's do a docker PS - a these are the two containers that we've created the master and slave so you can imagine that like this slave container does things to the from the that does things to the the files and folders that were mounted from the master perhaps it's on a cron or something or perhaps it's doing some thing to logging and perhaps it's putting the backup somewhere or whatever doing some sort of rotation type thing or or you know anything really let's do a clear let's do a volume because there is another command that I would just like to show you and that is the inspect command of the volume let's do a curve volume LS and do a docker in SPECT sorry no no no it's docker volume inspect dock of Alim inspect and we're just going to do the backup volume for now let's just press Enter so this is just going to return some low-level JSON information about the the docker volume so we can see the driver and the mount point now the driver can be you can you can assign different drivers to different volumes if they're if you have different drivers that manage different volumes and so forth so you can have different drivers that that handle different ways of sharing between machines perhaps local driver just means that it's local to this docker machine that's the mount point and that's the name and before I showed you the the docker inspect which just gives a bigger list I'll do that again actually let's do a clear of this and do a docker in spec of master so that's the all the JSON information of feet master container and there is here we are to mount points here logs and backup they both have read and write access so you can supply what level of access as well that you can do when you create the containers the drivers and this nations and so forth okay I've rabbeted on for quite a bit today in this tutorial so I shall leave you there with this this is an example of docker volumes and mount points or by mounting the docker files sorry about bind mounting local files from a local from your local machine it's very useful and it's one of the top reasons why I like docker so much and because it's so versatile so if you've got any comments or questions then do leave them in the comment section below if you've liked this video then show your appreciation give it a thumbs up share it around to anyone who you might think might find this useful as always my name is Peter Fisher follow me on twitter my twitter handle is PFW d thanks again and I shall see you next time cheers bye
Info
Channel: Peter Fisher
Views: 72,690
Rating: 4.940609 out of 5
Keywords: docker container, docker, docker tutorial, docker env, environment variables, docker container tutorial, coding, code, docker webserver, docker machine, docker ubuntu, docker images, learn docker, docker pull, docker hub, docker (software), container, docker volumes, persisting data in docker, docker data, docker volume, docker storage, containers, swarm, compose, docker swarm
Id: pOGVngLsaX4
Channel Id: undefined
Length: 25min 18sec (1518 seconds)
Published: Tue Feb 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.