An Introduction To Docker For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] in this video i'll be giving you an introduction to docker and talking specifically about what docker is the difference between docker and virtual machines the docker architecture terminology related to docker like containers images and registries and then showing you how to run existing docker images and how to create your own the first part of this video will be fast paced and i'll simply explain the fundamental docker concepts then i'll head over to the computer and walk you through the setup and commands however before we get started i do need to thank lenode for sponsoring this video lenode offers affordable servers that are powerful fully configurable and easy to use and set up with plans starting at just five dollars per month and capped monthly prices that means no surprise fees or hidden overages lenode offers no nonsense hosting at a price you can afford the node has full api integrations with tools like terraform and kubernetes and is personally what i've been using for over two years to host dozens of development projects bots websites domains and much more later in this video i'll actually show you how easy it is to set up a lenod using their one-click install options and we'll mess around with docker in linux you can get started with lenod today for free and get a hundred dollar sixty day credit by signing up at lenode.com tech with tim click the link in the description and now let's get into it so let's start by talking about docker docker is an open platform for developing shipping and running applications it helps you separate your infrastructure from your application and lets you ship deploy and run applications very quickly docker provides something called containers which are isolated environments that contain all of the dependencies for a particular project containers are similar to virtual machines but are much more lightweight and flexible and can usually be started in a matter of seconds as opposed to minutes with virtual machines when you use a docker container you no longer need to rely on the setup of the host machine any dependencies that your container needs will be installed automatically in an isolated environment for you so docker is very popular for ci and cd and is helpful when you want to let anyone run your application quickly regardless of their host operating system and their pre-existing setup so now that we have a basic understanding of what docker is let me discuss the difference between docker and virtual machines because they are similar in some ways and sometimes they do accomplish the same tasks so virtual machines are simply instances of an entire operating system that's running on the hardware of a single machine so you may have say four virtual machines running on one physical machine that you have now these are isolated environments that are running an entire operating system so virtual machines are managed by something called a hypervisor and they utilize a lot of memory cpu and other system resources now this is because they're running an entire operating system and that os requires ram disk space cpu etc now docker is different because it uses something called containers that on a low level are simply processes running in an isolated environment and sharing the host operating system so rather than having a bunch of virtual machines with their own entire operating system you have a bunch of docker containers that are relying on the same host operating system but still are able to run different applications and have their own isolated environment now compared to virtual machines docker containers are much smaller in size they take up less system resources and can be started very quickly not to mention multiple docker containers can run on a single host operating system now to explain docker even further let me discuss the architecture of docker so docker uses a client server architecture that runs over a rest api now the client communicates with something called a daemon which is simply a persistent background process that manages all of your containers and other information related to docker when you use the docker commands you're simply sending requests to the docker daemon to start stop build or delete a container this makes it really easy for us as docker users to manage containers without understanding the lower level features of docker we simply use the command line tool and we're able to do everything we need now i am going to start showing you how to set up docker and some common commands but first is let me define a few pieces of terminology starting with images so an image is essentially a blueprint for building docker containers you can create your own docker image or you can use someone else's now images are often based on other images so you could take someone's image and modify it to work for your particular project for example you may take an ubuntu image and then you may add some lines in there so it installs an apache web server now if you are going to be making your own image then you put that in something called a docker file docker files are what contain the different docker images next we have something called a registry now a registry is simply a place where images are stored the default registry is called docker hub and this is just a website that has a bunch of pre-made images you can think of it kind of like github where github is just a place where you store code docker registries are a place where you store images now finally we have containers containers are runnable instances of images and are built from those images they contain everything you need to run an application in an isolated environment like your tools dependencies software and more so to quickly summarize here we have something called images now images are the blueprint for building containers they're essentially the instructions on what needs to be done to set up a container properly these images are stored in something called a registry and then you have containers which are the runnable instances of the different images so if you actually want to run something on your machine using docker then you need a container and that container requires an image to be built from all right so that's pretty much it for the debrief on docker i hope this gave you a little bit of information and a brief introduction to docker now we're going to head over to the computer and see how to set up a docker container and use the basic commands all right so we are now on the computer and in front of me i have the download page for docker i will leave this in the description but depending on the operating system you're on you're going to want to download the appropriate version now going through the installation steps can take a few minutes but it is pretty straightforward so i'm not going to run you through that setup because there's not really any value in me walking through the installer what i do want to do though is use lenode to actually get a remote virtual private server this is going to be running linux and i'll show you all of the docker commands on linux because that's mostly where you're going to be doing them all right so i'm here on the node and i've signed into my account and once you sign in it's going to bring you to the dashboard that looks like this you guys won't have any servers running if this is a new account but for me i actually use the node so i have a bunch of stuff here now i also have some volumes i have some domains i have a kubernetes cluster so you can do all kinds of stuff on lenode and it's been really useful as i've been using it for the past few years and really easy to set everything up anyways if you don't already have a lenote account and you want to follow along with linux or on linux then click the link in the description you'll get a hundred dollars in free credit on leno now you can of course follow along on windows and mac if you would like however i'm going to be doing this on linux and just spinning up a linux server so to do this i'm going to click on create lenode and what i'm going to do is go to marketplace now the node has a bunch of one-click install options here which are really nice because it performs all the installation for you so you don't have to do that when you first boot up the server so i'm going to click on docker which is down here i'm going to scroll down and just go through a few of these configuration options so one thing you can do is make it so the docker command runs right as this lenode is booted up i'm actually going to do that and i'll explain what this command does later on so i'm going to say docker run hyphen d hyphen p i'm going to say 80 colon 80 and then i'm going to use the image of docker slash getting hyphen start now whenever you are running docker commands on a linux server typically you want to have a limited user that's not the root user that does this so we could make this ourself but i'm actually just going to use the one click install here to do this for us so it says the limited sudo user to be created for this linear so i'm going to put in my name which is tim put in whatever username you would like and then i'm going to put in the password of course you don't want to share this with anyone okay other than that the only thing that we need to configure here is the image and the region so the image is going to be the operating system image which in this case is debian this doesn't have anything to do with docker so don't worry about that and then for the region just use the one that's closest to you i'm going to go with toronto ontario now there's a bunch of different options here for the lenode servers you have dedicated cpu shared cpu high memory gpu i'm just going to go with the most basic one which is the nanoed one gigabyte which has a shared cpu this will be totally fine for this tutorial other than that you can change the name if you would like and you can enter the root password in fact i think we need to do that so i'm going to enter the root password right here and then i can click on create lino now this usually takes a minute or two to spin up once it is spun up i will show you how we can ssh into it we're going to be using something called get bash but if you're on mac or linux then you can actually just use the default terminal all right so my node is now running and we can actually leave this page but what i want to do is just look for the ip address of this server first so this is the ip address but we also have the ssh access so i'm just going to copy the ssh root at and then the ip address of this server we're going to use that in a second now to ssh into this server we're going to have to use a specific tool if we're on windows and that tool is git bash so i'll leave a link to this in the description but just download this this is actually just downloading git itself and then it will come with git bash if you choose to install that so please choose to install that because you need git bash then we can use git bash which is kind of a unix-like terminal on windows now if you're on mac or linux you can open up your default terminal and all of the commands that i type in in a second are going to work in that terminal all right so let me load git bash here from the windows search bar it's going to bring up a terminal window i'll zoom in and all we have to do here is type in the command and then this is going to be let me paste this in here ssh and then root at and then the ip address now i'm actually going to change from root to be the user that we want which is limited which in this case is tim so again if you're on mac or linux you're going to be in your default terminal if you're on windows download git bash you can open that from the windows search bar here and then just type in ssh and then your username so it's going to be different for you guys obviously mine is tim now if you did not have a limited user setup you're going to have to ssh as the root user and then create the limited user now i assume you guys would have already set up that limited user anyways i'm going to do this it's going to ask me if i want to continue connecting i'm going to say yes and then it's going to ask for my password so let me type in the password here and hit enter okay so now i'm logged into the server pretty easy i'm just going to clear the screen and we can start looking at some docker commands so remember when i actually was in the node one click install i put a command there for it to run as soon as this linode was fully booted up so that command was just running a getting started image from docker but we made it so that we were actually going to expose whatever application was running on port 80 which is kind of the default http port so what i'm going to do is go back to the node i'm going to copy this ip address and i'm just going to put it here in google chrome and hit enter now when i do that it's going to bring me to this getting started page which is actually a container that's being ran on our lenode right now so i'll show you how we access that in a second but notice this is the public ip address of our lenode and then it just automatically brought us to slash tutorial again that's because i put in that docker command so it's running the container and then we expose the port 80 so now i'm able to actually view this if you want an explanation of what the command is doing you can read this right here hyphen d running the container in detached mode and then we're mapping port 80 of the host to port 80 in the container which allows us to actually view this page on the web okay so let's go back here to git bash and i'll start showing you some docker commands so the first command we want to look at is ps now ps is simply going to list out all of the running containers so i'm going to type sudo docker and then this is going to be ps now the first time we type sudo we need to type in our password so i'm going to go ahead and do that the reason we need to type in sudo is because this is not the root user but we are in the sudo group because that's automatically what lenode would have done for us okay so here we are we can see that we have a container id an image we have ports command and created in fact let me make this a little bit larger so it's easier to see so this is just showing us all of the running containers this is the id of the container i'm going to show you how we can stop the container so i'm going to copy this here so let's copy it and i'm just going to type sudo docker stop and then the id of the container that i want to stop which is this so let's go ahead and stop the container we get this output and now if i go back to this page and i refresh you're going to notice that it's hanging it's not refreshing because now the container that was running this website is no longer running so i do understand that some of this is pretty vague i'm going to talk about this more in depth but so far what we've done is we've spun up a lenod so a remote linux server we've ssh into it we've used our limited user which was created by lenode for us if you didn't create the limited user you would have to make one yourself and then add it to the sudo group we then had the command run automatically when we booted the node which actually created this container with the getting started image and then we saw that it was running by typing docker where is the command here sudo docker ps this is listing all of the running containers for us then we stopped the container by doing sudo docker stop and then the id of the container now what i can do is actually run the container again or start it again and the way i would do that is simply do docker so sudo docker start and then the id of the container which is going to be this so now if i start the container it's going to take a second but if i go back to the web page here and i refresh we see that we get the getting started page awesome all right so now that we have looked at how to do that i'm going to show you a few more docker commands then we're going to pull an application that has a docker file with it we're going to build the image from that docker file we're then going to use that image to actually run a container and i'll walk through the steps and how that works then after that i'll show you how we make our own docker file and our own image as well as contain so we just started this up but i'm going to stop it again and i want you to notice here that when i run the command sudo docker ps as soon as this successfully stops okay so that successfully stops so let's run the command sudo docker ps here that we don't get any containers now the reason we're not getting any containers is because this only shows us all of the containers that are running so what i want to do to look at all of the containers that i have even the ones that are stopped is add this hyphen a flag here which is going to show me all of those so now when i do this you can see that i get this container right here and it was created five minutes ago status exited 20 seconds ago it's no longer running okay hopefully that makes sense if you want to view all the containers even the ones that are not running you do hyphen a now i actually want to delete this container because this was just for an example so to do that i can use the command sudo docker rm and then i'm going to type the id of the container which is oops that was not the id i'm going to have to find it which it looks like is right here okay so let's paste that in and hit enter okay so now i've actually deleted this container so if i do sudo docker ps hyphen a we can see we have no containers because we've removed it using the rm command nice so now that we know how to remove start stop and list all of the containers let's look at how we can actually get into a custom example download something that has a docker file build the image and then run that image alright so for this example here what i need to do is download something from github it's just going to be a repository but that repository has a zip folder in it so i need to be able to unzip that so to unzip that i need to install a tool and this is sudo apt install and then unzip okay so let's install that first and then once that is installed what we can do is use the curl command to actually get the repository that we want now all of the commands that i'm typing in here are going to be available from the link in the description there's a lenode guide that i'm following that has all of these so if you just want to copy and paste them just go in the description and you'll see it says lenode guide and you can see all the commands there anyways this is the command that i want so i'm going to say curl hyphen l o and then https colon slash github.com docker samples slash node bulletin board slash archive slash master dot zip now this is just a node.js project so i'm just going to download the github repository and then i'm going to set up the docker container and show you how easy this is to do with docker anyways i'm going to do this i've now downloaded everything so if i type ls we can see that i have master.zip in my home folder now i need to unzip that so i'm going to type unzip and then master.zip cannot find or open master.zip will be helpful if i spelt master correctly so let's spell it correctly okay looks like it's all good so let's type ls and now we have the node bulletin board master so what i'm going to do is cdn here so cd node bulletin board master let's type in ls and then we have the bulletin board app which i need to cd into as well so let's cd into the bulletin import app great so now what i can actually do is i can build the image that is inside of here so if i type ls we can see that we have a docker file and if we want to have a look at the docker file we can do that by typing nano it's just a text editor built into linux and then docker file so here we can see the definition of our image because again that's what the docker file is so all of our docker files are going to start from some type of image so in this case the image that we're using is node colon current slim now this is just an image that's available from the registry and remember the default registry is the docker hub now you can actually find a ton of different docker images from docker hub so feel free to look there if you're looking for a specific one anyways we have the working directory which is equal to this we're copying package.json we're running npm install we're exposing this port and then we're running this command and copying a few other things we don't really need to understand everything in this file the point is this is kind of the instructions and what's going on when we actually run the container from the image right okay so let's get out of that and now let's see how we can actually build this image so since i'm inside of a folder that has the docker file all i need to do is type the following and let's actually just clear the screen it's just a little bit easier to see so the command that i'm going to run here is sudo docker run and this is because i'm running an image so this is the keyword that i use then i'm going to pass the options now the first option that i want to pass is hyphen p now hyphen p stands for port or for publish whatever you want to call it and this allows us to actually link a port from our local host or from our local machine sorry to the container so yes the container is running on our local operating system but it's in an isolated environment and its ports are going to be different from the ports of this machine the host machine right so what i need to do is link one of the ports from the host machine to one of the ports of the container and the way i do that is i type in the port on my host machine which is going to be 8000 and i connect that to port 8080 which is where the node server is going to be running in my container so i type sudo docker run hyphen p 8000 colon 8080. again the first one is the host and the second one is the uh container so this means if i go to the public ip address slash 8000 or colon8000 sorry it will bring me to the container at port 8080. okay now after this i have one other option which is hyphen d now hyphen d stands for detached and this is going to make it so that we detach this container from this terminal process now if we don't do this what's going to happen is we're going to see all of the output directly in our terminal and if we close the terminal it's actually going to stop the container so most of the times you don't want that you want to detach the container so we type hyphen d now after we do that we can add a name for this to make it easier to reference so i'm going to type bb and then we need to put the name of the image that we want to run which in this case is bulletin board right because that's the tag that we added to our image when we made it okay so sudo docker run hyphen p 8000 colon 8080 hyphen d which is detached and then the name bb and then the image we're running is bulletin board all right so let's go ahead and run this and see if it works okay so that is good we got some output here right and we didn't get any errors meaning the container should actually be running all right so now this is running i'm just going to go to google chrome and i'm going to go to the ip address of my server which is this right here by the way i did that by just hitting the windows key and then v and i'm going to put in port 8000 because this is the port that we linked to port 8080 for our container so let's hit enter here and see what we get and notice we actually get an application right this was the node app which i guess is just some type of bulletin board okay so i could delete something okay let's see if that works i didn't make this application this is just a test application i just wanted to show you how that works all right so there we go we have now run this container so let's see this container let's go sudo docker and then we can list them with ps this container is indeed running and if i want to stop this container then i will do stop and i can just type in bb because that's the name that we gave great so now i have stopped this container and the last part of this tutorial is showing you how to make your own image so let's see how to do that we actually have to write a docker file to do so all right so for this example here where we create a docker file i'm just going to set up a super basic python program that requires that we install a python module so i can run you through how we create a docker file that can run some python code so the first thing i'm going to do is make a directory so i'm just going to say mkdir and then not python let's go with test like this i'm going to make a file i'm going to say sudo nano main dot pi and inside of here i'm going to import a module this module is request it's not built into python so we need to install this and then i'm just going to print test like this okay or this is working now i'm not actually going to use the request module i just want to import it so we can make sure that we actually installed it properly we don't get an error if we didn't install it okay so let's click this let's get out of that sorry which is control x and now what i want to do is create a requirements.tx file which is just going to list all of the modules that we need to install so i'm going to say sudo nano and then requirements.txt let's make sure we spelt that correctly looks like we did and inside of here i'm just going to type in requests i don't even need to give it a version okay so let's click out of that i now have my main dot pi and my requirements.txt however i realized i forgot to put them inside of the test directory so let's just move those over i'm going to say mv and then main.pi and i think i can just do dot slash test okay and then we want to move requirements.txt so let's move that into test and now let's cd into the test directory and ls and we have our two python files great now what i want to do is create my docker file so i am going to say sudo nano and then docker file like that make sure you have a capital d and you have no file extension you cannot have txt or anything like that it just needs to be called docker file okay so now i am inside of nano again built in text editor that we've been using and i'm going to start writing the docker file now the first thing that we need for all of our docker files is an existing image that we're going to be working from we're kind of inheriting from it and then adding some stuff to it so to find images what you can do is go to docker hub so let me just open this up and let's go into it okay so i'm on docker hub here i'm just going to type in python and we're going to look for a python image so here we have python it is an official image that's great and then we can look at all of the different tags here within this so we have 3.9 3.10 i'm just going to use 3.9 slim buster i'm not really going to explain the rationale behind that choice i'd recommend you just stick with this one for now but there is you know all kinds of tutorials online on how to pick what image you want for your docker file in this case we're just doing 3.9 slimbuster because i know this one will work okay so i'm going to go here now to my docker file and the first line that i need is a from and i'm going to put python which is the image name and then the tag which is going to be 3.9 slim buster so this is saying we're going to start working from this file right here from this image then i can add all the other commands that i want now whenever i want files to be a part of my container or a part of my image i need to copy them so there is currently a main.pi and a requirements.txt file in the directory where this docker file is so if i need those as a part of my container i need to copy them in so i'm going to type copy and then i'm going to type the path so i'm going to type [Music] requirements.txt so this is the path on my local machine and then after this i put the path i want to copy this to which is just going to be the same thing so requirements.txt this is the path inside of the container this is the path on the local or host machine let's make sure i spelt that right i think i did okay hopefully that makes a bit of sense but we're copying from the local machine or the host machine to the container and if i wanted this to go in a different directory then i would put a different path right okay now other than that i want to copy my main.pi file so i'm going to say copy main.pi and then dot slash main.pi and i'm pretty sure you can also just type a dot here rather than the full path we'll go with the full path for now okay now after that what i want to do is run a command now the command that i want to run is a pip command that's going to install the requirements that i have in requirements.txt so i'm going to type run and this is going to be pip install hyphen r and then requirements looks like i can't autocomplete inside of here dot txt okay now i might have to do pip 3 here i'm going to go with that for now because this might have python 2 installed uh but yeah this is the command that we want to run okay so pip3 install hyphen requirements.txt now after this we also want to run the main python file so to do that i need to type the following command so this command is cmd and then i'm going to pass as a list python3 and then main dot pi and we'll just do a dot slash to make sure this works okay so let me explain this to understand this a little bit confusing so again we're saying from and then the image that we want to inherit from this is pretty straightforward we're copying files from the host machine to the container and then this right here is a little bit different than cmd so what run is going to do is run this command one time when the container sorry is first created okay now this cmd command is different this is going to be run when the container is started so we're not going to run this when we are initializing or creating the container we are just going to run it when we start the container so hopefully that makes sense but that's the difference between run and cmd this happens inside of the container it's going to happen once when we first create it and then cmd will happen every time we start the container so if we stop it and restart we're going to re-run this command okay hopefully that is all good now i'm going to save this because we should have a valid docker file if we don't the mistake is going to be that i have python3 and pip3 instead of just pip and python okay so now what we want to do is we want to build an image from this docker file now we already know how to do this but we're just going to say docker so actually we need sudo first sudo docker build hyphen t and then i'm just going to do a python hyphen test for this one i'm going to put a dot because it's in the current directory so the path is right here okay so we're going to build this image let's make sure everything works successfully looks like it is good and once this is done i will be right back alright so this has finished successfully and we can see that i actually did run pip3 install requirements.txt and those installed the request module that we had inside of that file alright so now i'm going to go ahead and run this so i'm just going to type sudo docker run and then this is going to be python hyphen test i don't need the other options like hyphen p and hyphen d because i'm not publishing this right i don't have any ports i'm connecting and i'm not running this in detached mode because i just want to see the result so let's hit enter and we got this is working nice so the container is indeed working awesome all right so with that said i think i'm going to end the video here now in this video i did show you how to create a docker file how to build docker images how to run containers and hopefully at the beginning i gave you a decent explanation about what docker is and how it works if you guys would like to see more advanced docker tutorials on this channel in the future please leave a comment down below make sure you like the video and subscribe to the channel and one last thank you to lenode for sponsoring this video with that said i will wrap it up here hope you guys enjoyed and i will see you in another youtube video [Music]
Info
Channel: Tech With Tim
Views: 53,110
Rating: undefined out of 5
Keywords: tech with tim, introduction to docker for beginners, introduction to docker, docker for beginners, what is docker, docker vs virtual machines, docker architecture, docker setup, how to setup docker, linode, linode setup, how to setup linode, how to create a custom docker image, docker containers, what is a docker container, how are docker containers used, how to run docker on linux, basic docker commands, build a docker image, coding with docker, programming with docker
Id: ZiwvZenOzto
Channel Id: undefined
Length: 29min 8sec (1748 seconds)
Published: Wed Feb 02 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.