Exploring Docker [1] - Getting Started

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] this video is sponsored by dev Mountain if you're interested in learning web development iOS or UX design dev Mountain is a 12-week design and development boot camp intended to get you a full-time position in the industry to learn more visit dev mountain comm or click the link in the description below hey what's going on guys welcome to exploring docker so this is gonna be at least two videos because in this video I want to kind of just introduce docker talk about what it is we're gonna look at different commands to create containers talk about images and docker hub and look at a very very simple docker file stuff like that and then in the next video I actually want to do something practical where we take an ojs in MongoDB application something very simple and create what's called a docker compose file so that we can use multiple containers so we can have one for our MongoDB database and then one for our node application and basically have those work together and then we're gonna bring it on to a digitalocean droplet and run it in containers on that in that droplet okay so the the core aspect of docker the the point of it is to run software services or software programs application environments inside of containers so things like let's say nginx which is a web server or Apache or MySQL or MongoDB or even like WordPress or Drupal you can run these things inside of containers that are separate from your operating system separate from your your whole local machine environment so instead of having to you know build an app on one machine and then having to try to mimic that environment on another machine especially if it's a different operating system you might not even you know you might have to install things differently and and it just becomes a pain and docker completely gets rid of that because you can simply run it in a container and it's going to be the same no matter what machine you run it on whether it's Mac Linux you know Windows doesn't matter the version of the operating system or anything like that and you may think that docker is just like another virtual machine but I want to just show you an image real quick comparing something like VMware hypervisor to docker and show you how how it's different so basically with VMware you have your host machine or your server where you run multiple virtual machines and each virtual machine has its own full-blown operating system have it has a you know each one has its own kernel and then you're running your apps on top of those operating systems with docker however we have our server or host machine we have one operating system it could be Windows Mac or Linux and you just have these tiny containers that run which are basically nothing but processes that run on your machine and they they all share a kernel it's not like they all have their own kernel even though they had they have mini Linux distributions like Debian or Alpine or something like that it's they're much much lighter so there's much less overhead and they're very very easy to port to other machines and very easy to deploy alright so docker is a much better solution than doing it this way and you'll you'll find out if you're looking for jobs it with with large companies like Google and Amazon and all these companies they're moving more towards containerization and at least some part of their infrastructure is going to be using docker and going to be using containers so it is a technology that I would suggest learning even if you're a developer and you're not into DevOps you should at least know the basics and then I'm definitely not an expert I'm looking at it from a developer's point of view and just just to be able to move my applications around and test them from other machines and stuff like that rather than deploying although we will be doing a little bit of deployment with digitalocean in the next video all right so let's go ahead and get started now first thing you want to do is go to docker comm and go to get started and I'm not going to do the actual installation because it's very very self-explanatory I already have docker installed but you would go to download format go download for Windows or use your package manager if you're on Linux and you need to actually create an account with docker comm you can see I'm logged in here and one thing I want to mention is if you're on a system that's older than Mac OS Yosemite 1010 3 or something older than Windows 10 like let's say Windows 8 or Windows 7 I think even Windows Home Edition it counts as well if you have those systems one of those operating systems you need to use the docker toolbox instead of docker for Windows or docker for Mac and it's just a legacy desktop solution but you can do all the same stuff that you can do with with the newer version alright so just go ahead and download it install it like any other software just click Next and once it's all set up you'll have the little whale icon it'll be in the top right on a Mac or bottom right on a Windows machine and you'll probably just have to log in with your docker account and you should be all set alright so once you do that you can open up a terminal or a command line and just go ahead and type in docker and you should see all of the commands ok and these management commands here are basically like sub commands so when you do things with containers you'll do like docker container and then the command so like LS will list all your containers or docker image build or something like that so these are kind of like sub commands and you'll find that out along the way alright so let's clear this up and let's go ahead and run docker version okay we're gonna run a couple commands just to kind of get you familiar with it so the server is the docker engine that runs in the background and then the client is what you're using to communicate with that engine and it shows you the versions and all that stuff docker is actually built on the go programming language so it shows you the version of go stuff like that shows you your the architecture the engine is gonna say Linux and then the client is for me darwin which is mac alright so let's do docker info so this will give you a bunch of information if we go to the top here you'll see i'll show you the number containers your system the number running I actually cleared everything out for this tutorial so I have no images on my machine and I'll talk about images in a few minutes no containers running anything like that all right so let's create our first container so I want to create an engine X container and if you don't know what engine X is it's a it's a very common web server it's similar to Apache so when we install or actually let me just let's just create it first so we'll say docker container remember these management commands so and it has a come this management command has a sub command called run so docker container run I'm gonna add the - IT flag which is basically interactive mode I want this to run in the foreground rather than in the background alright so we'll say docker run IT then we want to add a publish flag so you can either do - - publish but most people do just - P because it's shorter and then you want to map the ports so the first port is the port that you want to use on your local machine so I'm just going to use a T which is actually the default port for nginx which would be the second one okay the second port is what is exposed from the container and with nginx its port 80 with Apache its port 80 for instance MySQL would be 3 306 that's the default port for for MySQL but you can map it to whatever port you want on your system ok I could have this could be anything but you want to make sure this is 80 if you're using nginx and then lastly we want to just put the name of the image which is nginx ok so notice that it says unable to find image nginx latest latest is just the tag that it's using which is the latest version so what it does if it can't find it on your machine locally is it goes to docker hub which is basically kind of like the github of docker images and it pulls it down ok you can see pull complete and it all it does what it does and now it's actually running the container and if I were to now go to my local host on port 80 and 80 is the default I don't have to do like : like that just localhost and I'm running nginx and this is running in a container it's not installed on my machine it's it's nowhere on my machine it's inside the container and since I ran this in interactive mode and the foreground it's gonna show me logs every time I reload you'll see the logs show up okay now to give you a better idea of what images are and where they come from let's go to hub docker comm and if we search for nginx and you can search for anything we're going to search for nginx you'll see there's an official image case so that's what we used because we said container run nginx and that's the name of the official image these other ones are actually third-party images that use the image but then they add stuff on to it so nginx proxy this one down here is nginx with PHP and you can use these images as well but this is the actual base official image and if and you can see the number of stars it has and the number of polls the official image oh wait well and well in in all the cases that I've seen has the most stars and polls so let's go ahead and click on this and this gives us a bunch of information it's kind of like a github page with a documentation and we could pull this image from docker hub with this command here although when we create a container it does it automatically okay pull complete so it actually did this in the background now over here you can see the different tags so different versions and stuff like that and you can actually check out the docker file of this nginx image and notice that it comes from from is just a way of extending another image and it comes from Debian ok Debian is a Linux distribution and stretch slim is the version of Debian that it's using okay so that's basically the environment that nginx is running in and then all this other stuff just kind of installs it sets all the configuration does a whole bunch of crap for us to get nginx up and running alright that's the docker file and we can create our own docker file which we'll do later on now this this docker hub page is going to have any information that you need this gives you an example of how to run it shows you the port that you need to use which is 80 it'll show you like an example of the a docker compose file which we haven't gotten into but we will probably in the next video if there's any environment variables that you need to know like MySQL for example you need to have an environment variable for a root password that will be here as well so you're gonna be when you're working with docker you're going to be coming to this - docker hub quite a bit to find out information just like you would with github in source code alright so hopefully that makes sense guys we're going to go back over here and stop this with control C okay now if we do a docker let me actually clear this out first so if we do a docker container LS I'm sorry what the heck docker container LS this will show us our running containers okay containers that are running now since the LA since the nginx was ran in interactive mode when we did control C it stopped it but we can do docker container LS - a and that shows us all of the containers on our system whether they're running or not okay and you can see here exited 35 seconds ago shows us when it was created shows us the image name it has an ID every container has a specific ID it also has a name and you can add your own names which I'll show you in a few minutes but if you don't it just gives you some strange name like this and I actually read that these names come from there two things it's an adjective and then the name of a famous hacker or scientist that's actually what these what these names are so now what I'm gonna do is I'm going to delete or remove this image so I'm gonna say docker not image container I'm sorry so docker container and RM and then you can put the ID now you don't have to put the whole idea I'll just do the first three three characters which is nine five what is it nine five a and enter and now if I do a docker container LS dash a you can see we have no more containers now remember that when I ran the container it pulled the image down so if you want to look at your images that are on your machine you can say docker images and the nginx image is still going to be there even though we deleted the container the image is still on our system so the next time we run a container for nginx it'll it'll be on our system and it'll happen faster okay but you can delete these as well so if I want to do docker image RM and then the ID six eight to now that should have deleted it so if I do docker images now it's gone so the next time I create a container it'll have to once again pull it down from docker hub all right so hopefully that makes sense but I can also pull it down myself by saying docker pull nginx actually should did I don't know that should be fine yeah nginx latest is a default so now if I do docker docker images okay so now nginx is back okay so it didn't create a container it just pulled the image down onto our system so now let's run nginx in the background instead so we're gonna do docker container and we're gonna use a dash D and this stands for detached okay which is gonna run it in the background you can also do dash dash detach if you want and then we're gonna map app or let's do 8080 on a local machine and of course 80 for the container that's what it exposes and then let's see what else let's add a name as well so we'll do dash dash name and I'll call this my nginx and then the last parameter is gonna be the image name itself which is nginx oops what'd I do wrong yeah I forgot run so docker container run alright so notice that it didn't it didn't clog up our console but if we look at docker container LS you can see nginx is running okay and it gives us all the information the ID shows us the ports and so on another command I want to show you is container PS which you might see with docker container PS which you might see in other tutorials because this was kind of the older way to show your containers you can also add the - a flag to show the ones that are not running but you might see me do that as well just because it's a little shorter than container LS oops I'm sorry not I'm an idiot it's not container PS although that does work but you can just do docker PS that's what I meant to do because this is a lot shorter than doing that I didn't even know you could do docker container PS actually anyways let's go ahead and let's let's create an Apache container so we'll say docker container run and if we look up Apache on docker hub you'll see that the official build is actually called httpd okay so we want to do run let's do dash D for detached for our port let's do 8081 because we have nginx on port 8080 which I didn't even show you I guess I should show you that so 8080 that's nginx so we want 8081 map to port 80 because Apache also uses port 80 and then let's go ahead and name it and we'll call it my Apache and we want HTTP D which is the name of the image which we don't have on our machine so it should pull it down okay so you can see it's downloading the image all right so now that should be running if we do docker PS you'll see both Apache and nginx they're running on separate ports so if we go to 8081 we get our apache landing page which is just this it works page alright so that's how we can run these things in separate environments and I want to give you an example of an environment variable so let's go to docker hub and search for MySQL so I'll go to the official MySQL and if we go down it should show us the environment variables right here so environment variables so you can use MySQL root password and actually define your password you can choose a net to allow empty passwords you can choose to create a random password if you do that you can then do container log and it'll show you the password that was set in the logs but what I would use is just just set the password with MySQL root password so let's go ahead and do that let's say docker container run and we'll do dash D for detached for the ports MySQL runs on 33 3306 so we'll map that to 3306 and let's do it let's give it a name I'll just give it a name of MySQL and for our environmental variable we want to do - - env and then the variable which is going to be MySQL underscore root underscore password and we want to set that to whatever and then the name of the image which is MySQL okay doesn't find the image on our system so it pulls it from docker hub okay and then if we do a docker container LS or docker PS it shows us that we have MySQL running here and if you want to stop these without without removing them you can simply say docker container stop and the name I'll say MySQL and it'll stop and if I do a docker PS we don't see it but if we do a - a that shows all of our containers even if they're not running and that shows the MySQL alright so let's see next thing I want to do oh and if you want to remove an image I'm sorry remove a container that's actually running you can do docker let's actually remove our Apache so we'll say docker container remove and then say my Apache now watch what happens when I do this it's going to tell me that I can't remove a running container it tells us that we have to use force ok if 10 before attempting removal or force remove and the way that we forced remove is just adding a dash F and that will remove it so now if I do a docker PS I'll even leave the a in there you can see that it's completely gone ok let's see so now what I want to do is is talk about something that is probably on your mind and that's how do we actually edit files like like in our container on our server on our nginx server how do we edit files well you do that through something called a volume or bind mount but before we get to that I want to show you that you can actually enter your container ok so you can you can bash into an engine X container so let's do that real quick so let's see what I called it my nginx so I'm going to say docker let's see docker container and we want to run exec and we want to add the interactive flag here I T and my nginx is what I called it and then we just want to say bash at the end because we fashioned to it alright so now you see that we have this route at and then whatever the container ideas and if I do an LS then you'll see all of the all of the folders here alright and as I showed you before nginx is is based from the Debian image so that's basically its operating environment now with nginx the web route is gonna be in user / share / nginx so I'm just gonna CD into this nginx slash HTML and if I do an LS you'll see the index.html that's what we're seeing it's for 8080 right that's what we're seeing right here now how do we change this to whatever our home page now of course you could edit it from within here but that's not really a good idea you want to create a volume to basically map this area to an area on your local filesystem okay so I'm gonna exit out of this container with exit you can see that now I'm back out and notice that I'm in a folder called test on my local system okay there's nothing in here and I'm going to let's see I'm actually gonna remove that container real quick actually I'll show you how to remove all containers and if we go to this gist here and I have the link in the description to this gist it just has it has a lot of the commands that we've been dealing with plus a bunch of other stuff I can't remember if I showed you this at the beginning of the video but I use this as a reference all the time so right here you can remove multiple containers to remove all containers you can use this command right here so I'm going to just grab that and paste that in and if any of these are running you'll get an error unless you add the - F so I'm gonna go ahead and do that and now that deleted all the containers on the system okay so we have no containers so I want to create a new container but I want to add a basically at what's called a bind mount okay so I'm gonna say docker container run let's do detached let's do ports we'll say 80 80 again 280 and then we're gonna do a dash V here okay so this is where we want to put the the area on our local machine that we want and we want to map it to an area in the container which is going to be the web route that I just showed you now there's a shortcut we can use for the current directory that we're in and that's money signed parenthesis with PWD inside so that that basically is telling it we want that the directory that we're in right now and this might not work on Windows as you'll see heat in here so you go down to and this can get a little confusing this is like my own personal reference so it's not really I didn't I didn't make it easy to read so this is what we're doing but I thought I had an example to do it without that but he'll basically have to do like from your C Drive C colon slash and then to wherever your location is I guess I don't have it here I thought I did oh well alright so yeah that might not work on Windows Oh another thing I wanted to mention is when you when you bash into nginx like we just did if you're using git bash on Windows you might have to use a command before docker called right here when PTY so you might have to add that if you're using git bash on Windows PowerShell should be okay but I think that you might have to use that on and get bash anyways let's get back to this so we want this to basically bind to slash USR slash slash nginx slash HTML and then let's give it a name of I'll say nginx - website and then of course we need the actual image which is nginx let's run that alright now if I LS in my test directory there's nothing here but I should be able to overwrite that index.html file and add other files if I want so I'm going to actually open up my visual studio code text editor in this directory that we're currently in okay and I'm going to create a file called index dot HTML and just add some basic HTML boilerplate to it I'll just say hello world a little world and save and then let's go back to Chrome let's go to our nginx server and reload and there we go so we've we've taken the index.html and this is now showing here because of the bind mount that we created from the local file system to inside the container and I could even create another file we'll call it about HTML and just copy everything here paste that in and let's just do about and save and go back to Chrome and now if we go to localhost 8080 slash of both HTML we get a boat okay so we basically have access to the web root ins that's inside of our container through this test directory okay or whatever directory you you set it as so now what I want to do is create a docker file so that we can create an image from from this this application so I'm going to let's see create a new file uppercase D docker file not dot docker file so don't put a dot in front of it and in here this is going to actually be very simple we first want to put a from remember the nginx had from Debian we're actually going to do from nginx okay so we get everything that comes with that nginx image and we want we want to add the tag of latest okay so I want the latest version and then I'm going to add a work directory and we want the work directory to be the web route which is gonna be that user / share / nginx slash HTML alright now I have an index and an about HTML page here I actually want to when we build this image I want this to get added so I'm going to use the copy command I'm going to say copy dot so all - all so copy everything from here to the all right so let's save this and now we want to build our image based on this docker file so let's go ahead and let's go back to our I was going to use the integrated terminal but I'll just go back to this okay and let's say we want to use docker image build so say darker image build and then we want to add the Dashti flag and then name it now I'm actually going to show you guys how to push this to docker hub to your account so if you want to do that you want to put your user name actually - be traversée and then so I'll do slash nginx - website or yeah I guess that's fine web bite or wesbite website and then dot which is going to refer to the docker file that's in this particular folder that we're in alright so let's run that and that should build out the image okay so it looks like everything went okay successfully built and now if we do docker images you'll see b-dry be Travis e / nginx website as an image okay it has an ID and everything so we can now build from that or create a container from that so I'm going to clear this out and let's get rid of our original container that we have if we say docker container LS we have this nginx website we didn't have to create this - in order to to create our docker file and create and build that image I was just using this as an example so let's actually remove this so we'll say docker our our container actually we don't even have to use container it's just good practice we should be able to do our m and then nginx - websites force and it should delete it so now if we look at our containers it's not there all right so now let's build a container based on that be traversée nginx image so we'll say docker container run detach ports let's do a different port let's do a tea will do 8080 to map to port 80 and let's see what we'll do I'm not I'm gonna do a bye now let's just do the the image which is be Travis e / nginx - website okay so if we do a docker PS you can see that that's up and running on 8080 - and if everything went okay then 8080 to should show the hello world page so let's go localhost 8080 - and there we go alright so we now have our image we should also be able to go to about HTML because that is in the container okay when we created our docker file we had we copied those files into the container so the last thing I want to do for this video is push this this image to docker hub okay you can see if I go to my dashboard I don't have any images here currently but I can easily push this image here if I want and then I could grab it from any computer in the world so let's go back to our terminal here and let's do let's see docker push and then we just want to push be traversée slash nginx - website it's gonna push - the docker hub repo and by the way if you gets if you get a message about authentication just do docker login and that will authenticate you so let's go ahead over here and reload and there it is be travesty nginx website okay and you can add descriptions and stuff like that if you want here but and it shows you how to pull it so I could go to any computer that I want any server anything anywhere and just run this okay as long as docker is installed this will pull this website down and I mean this is obviously just a stupid little sample website but imagine if this was a very large application now in the next video we're gonna get a little more advanced I want to talk about docker compose which allows us to do things much quicker and allows us to do a lot more for you if you want to use multiple containers and for instance have a database in one container have your your nodejs in one container whatever it may be you can do that really easily so that's what we're gonna do in the next video guys hopefully like this one if you did please leave it a like and I'll see you in the next one
Info
Channel: Traversy Media
Views: 361,261
Rating: undefined out of 5
Keywords: docker, docker tutorial, dockerfile, docker app, docker install
Id: Kyx2PsuwomE
Channel Id: undefined
Length: 34min 38sec (2078 seconds)
Published: Wed Aug 15 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.