Dockerfile Tutorial by Example - ( Part I - Basics )

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone this is the new video in the in the docker tutorial series I'm sorry you guys had to wait so long I was pretty busy I was moving from one assignment to another assignment so I was basically changing jobs and I'm just in the middle of that process so I'm quite busy but I received a lot of lot of positive feedback from you a lot of views or 40,000 views on the on the getting started video and a lot of lies I go 400 already so thank you very much I got a lot of positive comments and a lot of questions and yeah many people asked me to create a video on dr. Phi's and dr. Campos and dr. networking so this is exactly what I'm going to do first of all we're going to start with the dockerfile and today I will explain why one user doctor file I will explain about the work you know the process of building a doctor v when you have to be the room you want to build one for your project and we will look at some samples examples that we will create together plus I will show you the documentation you know how to do the doctor file yes so this is what we're going to do and well let's get started so let's start with the first question why you want to be the doctor file or you know bodies what is the benefit of working with data files and the answer to this is when you start up a project and you use a docker image so you go to go to the doctor store that you see on the on the screen here next to me if you go to the doctor store you will fly find a lot of different images and you can use those images to start up containers and please look at the previous video if you haven't seen my video look at the video because the first one I explained all this kind of stuff how this ecosystem is building up but I don't want to spend much time on that today so specifically for the dockerfile if you go to the doctor so you will find a lot of images that you can use to start up containers and in those containers you would have operational and runtime environment for your project now the thing is that there are images on the doctor store for almost everything really so you have based images for the distribution like a time or Debian you have images for for PHP or Python or nodejs or you know whatever you can think of and you have web servers and so on databases of course and but none of these images is really exactly the thing that you need for your own projects so you will very soon realize then you will need something more so you might want to set up an environment for example if you focus PHP then you need to install a micro framework or a framework extreme or lateral or whatever WordPress for example and in those cases you have to add something to the images that you can download from the doctor story can pull from the dollar store so in this case what you need to do you need to build your own images and that's where the doctor fire comes in because the dockerfile is basically the definition of a docker image so the knocker file itself physically is a text file and we're going to create the text file very soon so I show you data files so doc Rifai the text slide that is called docker file without an extension and then there are certain instructions that you can use inside the docker file and that's the way you build up your your docker images and then once your doctor file is ready then you use the comment darker build and dr. build will be the image from the docker file so that's the process of going from the file to the image this process is called building and you use dr. bierd comment to do that so in order to get started let's have a look at the first let's have a look at a few images and now I'm going to explain a couple of things about docker file so let's for example let's take the PHP I'm going to use in this video I'm going to use PHP nodejs plus a pine Linux as a base image and we will play around with those so if you look at PHP for example this is the official image for PHP and if you look at this you see that there are a lot of different variations and versions and people all this stuff here so this is the PHP version as if you look at it first here and then you know there's one that is based based on out time there is one that includes an Apache web server there's a command-line interface and so on and there are different versions here provided so 7.1 5.6 I think you get the idea and if you look at the end of every at the end of every line here you will find a darker file so I'm using this version right now locally so let's look at the darker file so the darker side of that image of the darker store is available on github most actually most of the images that you can find on that your store have a link that is going with pointing to the darker files on most violent github so if you look at this file for example then you can see that this image is based or out of Debian Jesse so the from statement here the nose that is coming from Jesse and on the next line you will you will see that there are some environment settings there are they running some command line commands here and then they were said more environments start became more directories configuration and so on so the doctor size basically you don't need to understand this right now we will move into that later so the doctor file essentially describes step-by-step you know how to build up a runtime environment within the image and then in the container so the thing is that whatever is included here in this data file that's going to be available in your image so for example if you use the PHP image here then you see that some base little spectators are already installed so if you want to do some some curl then curl will be available in that image but for example if you I don't know let's see if you want to use get for example with PHP then I don't see gift anywhere here so you will probably not be able to get pull in that image because get is not available here so then what you need to do you need to add install get into a custom image so that you can do that for your work and that's when you create a docker file and you use this PHP images and base image and add some stuff to it so to make it more clear and better let's start with an Alpine modification so I keep the playground folder here or my machine begin the U is the terminal of course I guess this is not this is a beginners tutorial of course but I don't want to explain you know what a terminal is I assume you guys know these things and I assume you know some minimum common line stuff like you know like listing gift folder is empty and I'm using the shell so please please bear with me I'm using comments like a lot and stuff ok so I get the playground and what I'm going to do first of all likely my daughter images so these are the docker images that I've already pulled from the darker store ok and you see I have I have a lot of token it's not too much actually because I'm trying to keep the stuff clean I have a pine 3 to 4 and this is what we're gonna use so we're going to create a new version of this pine this is another pine Linux a distribution ok if you look at the size this is less than 5 megabytes and if you look at some of the other images that I have if you see like nude for example is another official image and you will see that there are some images that carry my name and these images were created by me so these are already custom images dedicated to the doctor file and if you look at this one this is like 2.1 gigabytes so some stuff really takes up a lot of storage space so that's why i'm trying to minimize the number of installing online machine because they take a lot of lot of space here so I have one for PHP slim for example this is something that I was supposed for myself another one this is this is what I'm using for my blog here this is the jackal image this is the this is something that I'm using for a PHP fat free micro framework image I have no official WordPress I have the official jackal this is the base image of this one here I have a fish of PHP including a web server I have Mario DB I have an engine X proxy and well that's all now okay so in the playground let's just create a dr5 so this is your first docker file and then let's use an editor to edit that doctor file okay so this is our beautiful doctor fine and what we're going to do we're going to use the time image here so this guy here fine three to four and we're going to add some stuff into that so what I could do it let's play with it a little bit okay so let's start up this this image here in interactive mode and - - RM is very important because once you exit from this container it will be automatically removed I'll show you why because if I do a dr. PS nothing is going if I go dr. PS - a there you will see that these are the images that I used to run but they are not running anymore so these are the ones that I started up and I can restart anytime okay so if I because I started them up in a way that they stay around but if I use - - RM that means once I stopped those containers they won't be staying around here so you know I'm trying to create less mess okay so the darker images again and so let's do like docker run - CI - RM and then what I want to do I just want to very quickly start to find three to four and I want to run the shell okay so what happens I am inside this container and I am routes okay and so let's see what is top hat so let's get get version yeah it doesn't have gates let's go boom this is my veins curve curve is not available Bobby get is available okay so let's do the following so there is no get there is no vein there is no curve so let's create another time image that has gift beam and curve okay so that's what we're going to do without occupy so let's leave this container okay and then if I leave my continue to see it's not another run anymore so that container is gone I didn't create a mess which is good okay so again let's leave the images this value we've got and let's create our first dollar fine every dr5 must start with from from means that we need to pick a base image you can pick an existing image from the doctor store or you can build your data file from scratch as well and yeah you know every every image needs to start somewhere so you can build your fashion scratch but for now in the beginning let's use an existing image and let's start from that so if I say from a sign Peter for that means that I'm using that image that we just looked into in the comment line and I'm going to add some stuff into that so there is another keyword here called maintainer which is not so very very okay it's important but it's not critical but what I usually do I just put my name here and my email address so anybody can find me and send me emails on the maintainer of this stuff and what I'm going to do I'm going to do is run and he's going to be really interesting now because uh the point here is I'm you need to understand how the doctor fight is building up so let's add some some instructions here and then I will explain what's going on so we're just going to use the package manager inside of time and then we're going to add some packages so let's add a vein and then and curl for example okay so this is by the way not best practice this is something that I want you to see so let's do that so if I leave this this is one docufide here so what we need to do right now we are going to be at that image so what we're going to do darker beers - tea means that we give it a tag which means that we're going to give a name to our image and you know every images is named in a way that there's a provider or creator or maintainer and there's a slash forward slash and other forward slash is the name of the image and then you have some tag for the version and so on so what you need to do I give it my name because I'm the creator and let's say that this is going to let's call it soft I got a little smarter and then this is going to be version bonded 0 and we need to provide the location of the folder that we are building so we put a dot which means that we are building the current folder and dr. baize you look at me look at this folder and look for the data file and if you find the dr. Phi is going to bear it so let's see what happens mm-hmm now this is pretty interesting and yeah show you why that's that's wait a bit and until it's built I hope my internet connection is up to speed today and you see that we already see that some steps are created here there are five steps also and one two three four is happening right now and why we are waiting let's go back to doctor fine and see so there's one two three four five interestingly so the five steps that you see in terminal are leaked the five steps that are on this lot on the lines of the doctor side and this is going to be pretty interesting so if you look at this here step one is exactly this line here from that you see there and then what it did it says there's this little arrow symbol and there is an ID so after this executing this line we have created this darker image and I will show you those images in a minute so what's happening is that for the first line doctor created an image and this is sort of an intermediary intermediate image then for the next nine maintainer which is me it created a nice first to keep an intermediate image then if this is another image then it is remove the intermediate container and then it created it run the next line and at the end of the next line another image was created so what's happening is you know we are installing also here and in stonee also here so if you go to doctor images right now we will see that we created something called a time smarter version one okay now if you go darker images - a that will give us more images you see here and what's happening here is that these are the these are intermediate images and if we go back here you visited the IDs the image idea that we can see in terminal are the same as we can see here so the final step step I've created the ce-1 a and so on c1 and so on is the idea of the final image then the step before should be C 49-7 B so C 49-7 B you see this is the end of step four is C 49-7 B and the end of step three should be this 340 0 a 3 4 is 0 a and then this one here should be 0 the something 0 D is something you see so the thing is and the first one this one here it's not listed so every step going backwards produce an image and every image is stored on your system so these lines every line that is highlighted here is a darker image only the final one has the name a proper name and the proper tag but every line that is on tact here is also a darker image and you see the size this size the sides are different because as you add stuff to the images this decides would grow so and this is really this has a couple of implications but let me show you these are 4 steps so the step 5 step for step 3 and step 2 so buddy step one step one says from time blah blah blah and it says if the idea is baa 5d and so on and that should be the base image so if we go and find the sign here baa 5 e blah blah blah you see here so that image from the line it says from Alpine 3 to 4 that is this one here and that's the ID that you see up here at focus make sense and so what's happening here is that for every line in your dr. 5 you get an image and this is what we call the this is basically an image cache ok so if we build this image again and we don't change the dr. 5 then you get much less output and the this thing says that I'm using cash using cash using cash using cash hmm so what does that mean that means that when you when you be the doctor five then every line on the darker files will create a new intermediary image and that we call image cache and this will help you a lot because it will speed up your process when we don't once you are you have completed some really complex line complex step on your dock of five and you are adding more you don't need to restart building the entire image again and again you can imagine if you have like 20 50 or 100 lines of an image maybe like 50 60 it's already pretty pretty pretty big and then you don't need to start from scratch or start from the beginning because you can use the cache so let's play with the cache a little bit I hope I'm not going too fast okay let's do this so short summary and then do the next step so the short summary what we have we have done we created a darker file because we are building a custom image for our project let's say we want to use a fine but we need like lame and curl inside of time so that's why we have created the docker file and we have built our own image so we use the docker file and use dr. bierd to create a new image and then if you go to other images you see that we have actually one image created I think it's a good idea to test this out because I promise to you that we going to handle them inside the time we have seen that the pace of time doesn't have been live so let's say like docker run and then the API sorry and and then a bunch matter [Applause] one-zero and being sure okay and then we go famous and you see we got a baja cool so in our smart enough time we already have been included so if you run the beard again then we see that we are using cash to the beard everything so now let's do the following let's change the doctor five okay so instead of two let's say v1 gate and we don't want girl okay so what happens is that we are rebuilding the stuff and you see that every step that was not changed here remains it's still running from cache it remains remains the same and then the last step has been read run and then it will with docker images you will see that this guy here we have a the Alpine smarter and this is the final one so this is the one that has get inside because this is what we specified here and the one that we built previously is here is this image here we can check the image ID and it has lost this name meaning the tag it's not tagged right now because it was supposed to be the you know supposed to have the same name Medina we put a different content a different image behind that name so the original image that we built previously has lost its tag a lot this name and it's right now just hanging around and this is called a dangling image and it is important for the clean of purposes so I can show you how to filter that out so we say doctor images Tess - theater is is dangling it was true and you see this is the only one dangling image you don't need to learn too much of this magic this is actually this is the the only sort of complicated magic comment that I use the darker this is the way you can find dangling images meaning that something that used to used to use but then you change your mind you created something else and this stuff is just hanging around so you can actually remove them an images by thing Dockers I am I like remove image and then you put this stuff in the brackets like this and then if I hit enter now it's going to be removed after I put dash Q here because that Q will remove this this heading stuff here so I could only have the ID so I'm not going to execute this now because I want to explain something or maybe a V like lyric here so the only thing that I still wanted to show you that this is the one so c1 a this is the image ID that we built before so our previous Alpine smiler was these images so I just want to prove it to you that I'm talking the truth here so this used to be our Alpine smarter custom image but right now there we have a different image because we have built a different image and the previous image is hanging around so let's remove it and nine says we have deleted or daughter images and now you know the damming image has disappeared and lets the doctor image catch a and here you have again you have the cash here included and now if I want to build it again now the entire image is being built from the cache and then why is this important because building up your data files you need always you need think of the order that you're doing stuff so I suggest and actually this is the best practice so if you look at the internet everybody's suggesting the same thing to do the system level changes and additions in the beginning you know do the stuff that you probably won't change after it's working do that in the beginning and then later on put they put the stuff that might change at the end on your data file in this way you can use a lot of lot of cash once you break the cash cycle of the cache line then docker will rebuild all these all the following steps so if I just for example say that I want to run if you can and curl here in the first steps I'm Eddie there's another step step here that means the doctor will rebuild the image from this point also this the cat this part of the cash will be used but everything that comes after that will disappear so let's see how that works you see so I'm adding curl and I'm adding wave and everything completely again it will be rerun from from scratch because this is a different image this is a different state where we are at so we're going to rebuild all the all the following steps after we change the doctor file so that is important that's why you know you should think of the structure of the doctor file the order of the stuff that comes into the doctor fine because it has an impact on the rebuilding performance so it's like a doctor images then you see that I have my new image here and the other one the previous one is Deming and if I say doctor image exact a that's the interesting stop here then I see we have a lot of lots of stuff here or some damning part comes into this display here so if I just yeah let's remove those and let's go back so this is a times smarter night has everything included and you have seen that those those parts of define those part of the image were revealed that came after the chain so so there are a couple of considerations here so first of all the order that I mentioned to you second of all the size you know it does matter how many images you have because every image has takes some space and for a time we're lucky because a time is small but if you look at this one line below here this image it has the DS number right and a one to two five or six steps whatever and every step every immediate every step takes up like four hundred megabytes of my space and I have a 256 gigabytes of solid-state disk so you know you can really take a lot of space here I'm using you know Conda mini Conda I know if it is a machine learning library or like a component huge framework whatever so I'm using mini Conda and it used to be at first the first part was mini but the last one here I added a couple of packages here is 2.1 gigabytes and actually I have two of these here and then I'm using PHP slim look at this like 500 megabytes like 10 times hmm so this is you this is a lot of stuff and that's why you need to make sure that you know that you minimize the number of Statten of optimize the number of staff so I show you something what we could do here the instead of putting every comment here on a new line what we can do is that we can actually shoot okay so we can do we can put the whole thing on the new line on on one line so what i'm doing here basically the and splitting of this statement or actually combining the statement to be one i so this is one line but i've written that multiple line so that means yet right now instead of creating six five or six images we can only create three images so actually this is the base of we gonna create one and then this is already going to be the finer so let's rebuild it and see what happens okay let's remove them in okay there is no lemming and then dr. bit here so you see unexpected and OH five what have I done okay so let's do this again okay so I'm buildings let's wait a minute and so what's happening right now you see already that is like the total number of steps is three is not five anymore and that means that we're going to have what only one interim image and that's going to take much less space plus okay let's get doctor we need you - a so you see okay there may be um the other thing is that we have a way of dangling image so let's remove damning images okay now it's going to be better okay so that's the point here then after we remove the damning images you see that this is a pice matter and now we only have one image that is less than five megabytes in size so so this is one way to optimize okay the downside of this is that if you need to so you could probably add a lot more stuff here and this is this is only the part of the noodle configure on linux and for example for PHP maybe you want to install composer just right after this and then if you want to change something on the on the operating system levels then you're going to you know rebuild the entire image from from this point on and this is actually we can of course do it like this back and yeah but that's only just a matter of convenience yeah that's bigger than whatever and I see is going to be rebuilt again okay okay let's remove that link that's similar images - eh okay so let's do back in I have one stop hanging around here all right so so I think we have done this part there is one more thing that I want to show you you know it's just this really about you know structuring the start and optimizing the stuff so let me show you something else and then we can move on okay so let's do something there's another instruction that you can see you can use here actually okay so let's let me tell you what are the what is the what is the instruction set for here so these are the instructions that you can use inside the doctor file and he started from because as I mentioned before every doctor five must start with a from so every no exception you must visit the mosque something that you must do you must start with the from where you will specify the base image for your doctor fine and then there's a lot of stuff that you can put the most-used instructions here or here copy and add and run of course that we've already used so Ronnie's here you can use like your share commands and you can use this format or you can use it the same format that I use here same that you would put in your shell or in the terminal and the combat let's return to the comment a bit later because it's pretty important this is the dista file common that you will run in your container and then the maintainer I have already explained what it is and the labels you can add labels to your data file you know for description purposes and then you can use dr. inspect to see what other labels here like version you see this is a good example so it's additional information on a version description and so on and is important because and you can specify environment variables here for your data file expose is just the instruction to expose the port so for example if you run a web server inside your data file that you probably want to expose port 80 for HTTP and for 40 443 for HTTPS and add and copy what is the difference you know a lot of lot of times I see these questions what is the what is the difference between copy and add both are used to copy files into the darker image so that means that the image itself it contains it's not a container not the runtime is the image itself that will contain some sorts of files so for example a configuration files or environment science and the main difference between copy and add is that add can do a little more because you can use URLs in add so if you if you add a URL then a doctor will actually retrieve the stuff from the internet for you and and can also use compressed zip compressed file and terrifies you see here so you can add the tar file and then it will decompress and copy that into your image so copy is just the same thing but it doesn't do you or Ellen doesn't compression and yeah you say invert this I can also design things that we also usually use right now and is basically example um not using it the user you can set that you can set the user ID so you can switch the user you know because if you don't want to run as root then you need to specify the user and the verb deal is pretty important you can set the working directory so whatever you you whatever instructions you have after work this they will be executed as in that context of the basil if your working directory will be the directory that you stash it right there okay so I don't wanna talk about the others right now but this is basically the instruction set that we use inside the docker file and leave these things you can basically complete almost anything so this is a pretty cool stuff so let me show you something so what I want to do I want to copy one file into our pine container or stocks or time image you see sometimes sometimes I also mix up the notion of container an image but right now we are painting an image so let's do that so let's assume that we have something like poultry text 5 and then let's say whatever ok so this is one text file and what I want to do I wanna copy concrete HC into my container so let's say tell you the source so insert this I will copy that file into my image ok so let's go and build this initiative in the back images ok right now fine and let's do the bed okay so you see I have copied that file inside here plus I have executed this comment here so I've successfully created it so let's now save the actor images okay we have this damning stuff around and then removing it that image is fantastic so let's go like doctor let's look into this image here okay then let's go there you see we have conflicting click in here oh okay sorry okay so so what happened is that I have copied that compact HD into my darker image so if I start up a container here then I see the stuff happening here and yeah this is what I suspected that something was not working yeah not working because that's my forward that's sorry just this is the typo but you know it won't really change the the idea behind ah okay so now I'm installing yeah you made the wrong stuff yeah sorry about that but I think you understand the point so I fixed this okay so the thing is that so right now I'm not running anything anymore okay so let's go back here so now the image is done better images here okay let me move the damn thing again I used to be tedious sometimes I can tell ya so dr. images no not let me show you something important okay so now we have this monitor image and we copy this fine into here so let's rebuild it just to show you the everything is gone done from cache right and I have already shown you that if I change this darker file now then it will not use the cache but very beard now and I'm showing you something really important that if I change this txt file here bubbala change so I haven't touched dr5 I just start this config to x3 and I do rebuild looks look what happens darker understood that I have changed a file that the building was dependent on and it's rebuilding from step from that step forward ok so that means that changing the doctor file somewhere in some step will trigger rebuild on all the consecutive steps but changing the 5 that is a dependency of the doctor file will trigger rebuild as well so right now I'm rebuilding and if I stay doctor images bang you see I have the dangling image and I have a new image here and that new image contains this change already so we can of course okay let's type it dr. Ron I don't know here you do that I mean take some time but I don't care let's do this let's not be afraid of this I know everybody wants to have quick learning quick information and it's just talking and talking and doing this recording but sorry yeah let's do that let's have a look at it okay so you was the source and complex tree you see and this is there is a change already inside so now we we have learned a lot of lot of views so I think this is the essence of the overbooking the doctor file but I have shown you in the last I don't know how many minutes already like 20 or whatever what I have shown you so far in this tutorial is the is the most important thing that I think you should understand or based on my experience is the most important thing that as you build the dockerfile then for every line or every instruction you will have a new intermediary image and if you don't touch those lines then the cache will be used if you touch those lines then the image will be rebuilt okay and this is important because you need to think accordingly if you want to really deep dive into the instructions that you can you can find the documentation this is pretty easy to understand anyway so your going to you know building the dockerfile is actually building off your environment is the same thing so you have B what you are doing is your building of a Linux development test or production environment so you need to understand your subject you know if we are building for fhe you need to understand how to configure that and what do you what are the steps that are required how to install other stuff all this content you need to understand but the doctors fire does it the sacrifices gives you a way to enter other information that you should actually know so dr. slice is a very simple tool what is important to understand about the data file that it has a cache it creates layers and it really does man your hard to set it up how you set it up and you can you know it's also understand where you change the config file or you will left the config file unchanged so this is something so so far we have closed one big chunk or Bombyx a big chapter in the doctor file that I wanted to explain to you and I think you are ready you can leave this tutorial right now or you can stay with me and I'll show you my the process of building a little bit more complex data files first of all and second of all then we're going to add more and play with some more dr. Phibes as well so we're going to create the PHP slim dr. 5 and after maybe we'll also create another one for nodejs but maybe you can do that for yourself as a homework
Info
Channel: takacsmark
Views: 90,094
Rating: undefined out of 5
Keywords: Dockerfile, Dockerfile tutorial, Dockerfile example, docker, dockerfile best practice, dockerfile commands, coding tutorial, docker explained, docker introduction, docker basics, docker beginner tutorial, What is docker, learn about docker, how does docker work
Id: 6Er8MAvTWlI
Channel Id: undefined
Length: 46min 21sec (2781 seconds)
Published: Mon Apr 24 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.