Docker Concepts Introduction

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so for those people who are looking all around you and everybody's using docker but you're not really sure what it is or what role it fills well don't worry you're not alone docker is gaining huge traction it's being adopted very widely and very fast so first what is docker docker itself has kind of this larger toolset that contains the tools necessary to build images that contain very software and then of course to launch containers which are instances of those images one important attribute of a docker image is that a docker image is built to do one very specific thing so for instance there's a docker image for Python 3 and all that's in that image is just Python 3 and possibly pip there's also a docker image for say Redis which just contains Redis enough nails there's a docker image for my sequel which just contains my sequel and you you can kind of see the pattern here there's two ways docker images come to be the first way is that you can get docker images directly from the docker repository itself what happens is different vendors like Redis and nodejs they'll publish these images to the docker repository so you can pull them down to your machine and use them in in your environment the second way is you can build your own image and what you do is you take an official image from docker repository and then you can extend that to do other things and we'll briefly touch on later an example of building your own image according to your specifications so images themselves they don't do anything like an image is not it's not running when you want to run an image that's when it becomes a container what containers are is there docker images with some specific configuration applied to those images I'm talking about configuration there's a litany of different configuration options but primarily what we're gonna talk about today is things like exposed ports and Inter container networking and then volumes at this point in the video you might be thinking to yourself well how does this differ from just like a virtual machine that you would run in say VirtualBox or VMware and the answer to that is they are similar to virtual machines but they have a couple very very key differences the first difference is that a container starts and stops very fast and with much less space and much less memory usage because images are often highly stripped-down versions of like a Linux operating system plus the software that's necessary so it keeps out a lot of fluff from the container the second difference is docker containers are not designed to just be turned on and connected to like you're not supposed to start a container and then go interact with that container that's not really what they're for docker containers are designed to start and then once they're started they're designed to run one command at start and as soon as that command finishes running it tears down that container so at this point I think it's helpful to jump into some examples so where I'll do a total of four examples here the first example is we're gonna use docker specifically an image containing Python 3 to run a Python file and we have our file here it's very simple it's just print hello nothing crazy the command that we're interested in right now is gonna be docker run and that's a command that's used to basically start an instance of an image also we call this a container so start a container so docker run is in the format of docker run and then specify options and then specify the image name and then specify the command to run in that container so first by specifying - RM we're saying once the container shuts down to just remove that container this next option here is a very important one it's - a V and it's for volume and the reason we need volumes is because the container does not have access to the actual host machine so when I start a Python 3 container all it has access to is what was present in that image but we need it to run a file that's on the host so to do this we can basically mount host folders on a folder in the container so this basically says is mount the current working directory onto slash source of the container and this next option is going to be the image name it's going to be Python is the image and then 3 is going to be the tag and finally the last part is the actual command to run and this is going to be what command to run in the container so to recap docker is going to start a container it's going to mount the current working directory to slash SRC it's going to start the container with Python 3 and then it's gonna run Python / SRC / hello pie in the container so let's actually do this to see what happens I want to run this command the first thing it's gonna do is it's going to say it can't find the image locally so it's doing right now is it's reaching out to the docker repository to download everything it needs to do keep in mind that only needs to go through that process once because once it has the Python 3 image locally it could just start the container directly from that instantly and you can see here once a guy everything built and it started the container it printed out hello which was the result of that one file we can see now that if we run the same thing again it just prints out hello instantly it doesn't have to rego through everything I just want to show you what would happen real quick if I remove the volume basically now the container cannot see the file it needs to execute so when I go to execute it you can see it says cannot open files / SRC / hello dot PI and that's because it's looking for a file it doesn't exist because we did not mount it this is why volumes are so important in such a key part of making docker containers work for you next one is we're going to start an actual Python interpreter from the docker container so we have our second command here which is basically identical to our first command except we've added this - IT and this allows us to make it interactive meaning that we're gonna do something in the container and then rather than doing Python /sr c / float up high we're simply doing Python and then C normally in a host if you write Python and hit enter you get the Python interpreter so in this case when we run this you'll see that we get a Python interpreter now this is a python interpreter that is being ran in the docker container itself and just like that last command if you wanted to instead of run Python you can also just run bash and what this allows you to do is actually look at what the running container looks like right after it starts so if you run this you can see that we get a new can you prompt here and and remember how I said that it mounted the current working directory to slash SRC well if we CD to slash SRC and we do an LS dash L we can see our hello dot pie here and that remember we're in the container right now root at and this hash is going to be the container ID it's also worth noting if we run something like top inside the container you can see that it's just it's just two things it's just top and then it's just bash that was ran when it started you noticed that you can't see all the other stuff on the computer okay now we're gonna move on an example where say you need Python 3 but you also need something like numpy so of course you can look for a prebuilt can you can look for a pre-built image already maybe you can find one that has Python 3 that has numpy but if not you could just build your own and this is where we introduce this thing called the docker file and the purpose of the docker file is to describe how an image is built and what is contained in it so remember in the first example we use the image called Python colon 3 which is the image Python with the tag 3 and then this is one real true power of docker is that you can extend from an existing image and add your own stuff so what we're doing here is we're saying from Python colon 3 so it says use Python 3 as our base image and then what we can do is we can run different things that will change that image so in this case we're saying run pip 3 installed numpy so what the resulting thing here is we have an image that is python 3 + numpy so to build this image we're introducing a new command here it's gonna be docker build and for an option who are using - T and this sets the tag so we're saying this should be Python underscore numpy and they were supplying a dot which just says build the current directory which we'll just to look for docker file in that directory so if we run this command over in our terminal we can see that it says from python 3 run pip 3 install numpy and you can see it's actually running numpy in that image and the same output that you see here is the same output that you would see if you're on just a normal virtual machine or your own computer running pip install numpy essentially this is doing is it's starting a container of Python 3 it running pip3 installed numpy and then seeking an image of that container and it's getting rid of the container that's a docker will do internally this is also a good time to introduce a couple key commands with docker that lets you kind of inspect what images you have so you can simply do docker image LS you can see that we have we have two images here one is our Python 3 and then we have our Python underscore numpy tag latest so let's cool now it's just like we ran our script before with Python 3 instead we can replace it with Python underscore numpy so let's go ahead and do that so these last three scripts and we don't need to run all of them these are exactly the same as the previous 3 that we had in the original Python example except instead of Python colon 3 we replace it with Python underscore numpy other than that nothing has changed they're all exactly the same so if we want to run that hello dot PI we can copy this over here and this uses the Python underscore numpy image to start a container with and run that script and then our output is hello because we're moving on to our third example now the first two examples were useful to show kind of the workings of images and containers but it's not a great example on what you might use it for in practice so here's one great example of something you might use in practice so imagine you have just a static website and you want to serve it and you want to use an engine next docker container to do so Annie's command should start looking pretty familiar now so we're using docker run here I know what that means - - RM we use that before that we'll just remove the container once it stops and then we're mounting a volume here we're mounting the current working directory - / user / share / nginx / HTML now when the vendor publishes their container they'll publish documentation with it and the case of nginx they will tell you that by default nginx will serve content located at / user / share / nginx slash HTML so to get a really quick container up and running you can just mount the files you want to serve on top of that directory and then here's a new option that we haven't seen yet - P this is for ports and what this says is take the hosts port 8080 and forward it to the containers port 80 remember container is an host they don't clash so you can run something on port 80 in a container and you can run something on port 80 on your host as well and then for the image name we specify the images nginx and the tag is going to be latest which just gives you the latest nginx this is also gonna be the first time that we start a container that's not gonna immediately exit because remember nginx once we start up nginx it needs to stay running and it's gonna stay running until well until you until you stop it from running so let's run this and see what happens there just like last time it can't find nginx latest locally so it needs to go out to docker and download everything from there now once that's running you can go to your browser and you can type in you know in your case you can do 127.0.0.1 colon 8080 and it should open the index dot hello sorry index.html and all this a logging you're seeing in the background the reason that's there is because we have the container running in the foreground so it's logging everything out to the current terminal there is a way to start it in the background so we can clear all this out come back over here and then we'll start it again except we're gonna add this - addy and that's the only difference from the first one to the second one is that we're doing a dash T which says start it as a daemon in the background just want to run this you could see that it just starts and then it just exit exits like that you can you could check to see which containers are running by doing docker container LS now this is not very neat because my text is really large so all of you can see it but the important thing here is that the container ID is here and then the image and then the startup command it says when it was created it says it's status you know up 18 seconds and that says the port's so this is saying the host 8080 should point to the containers 80 and then this is the name of the container it's just a some arbitrary name that docker picks for it if you want to stop a Ryan container you simply do docker stop and then just specify container name and then that will just stop it now there's no containers running next thing could do here is if you wanted to customize the nginx configuration file you could use - uh v2 mount a separate configuration file on top of the one in the container that would be the way to adjust the configuration if you didn't want to add the configuration at docker runtime what you could do is you could create a docker file and then you could put that file into the image either ones fine just depends on your use case the last thing I want to show here is how to log into a running container so if you have a container start in the background so I'm just going to start this back up then my the docker container LS to see it so relaxed Grider so if you want to log it into this container you can do docker exec and you can do - IT for interactive and then at this point you specify the name of the container and then you specify what you want to run in the container so if you want a if you want a shell you could just do bin bash and then when you run it you can see that you get a terminal here at this point you're now operating in the container so you can see the various things in the container you could go to Etsy nginx if you wanted to check out the injects figuration file or if you want to access access logs or whatever you need to do everything you're doing here is in the container and if you do top of course tops not present here actually this is a good example is that not all containers are designed to be used like this so the the people that built the nginx container basically said well we don't need top you know tops not relevant to serving a web page so you know different images vary and they contain different software but they always err on the side of keeping the image as small as possible because that allows it to be started quicker and distribute easier so up until now we've only talked about single docker containers doing a single thing but what happens when you have a certain use case that requires multiple containers like imagine a typical web application any web application is most likely going to have a web server like nginx it's going to have a back-end programming language like say no js' it's going to have a like my secret postgrads and then it's probably going to have some sort of some sort of caching system or place the story sessions such as memcache or Redis so what happens when you have all four of these containers raid Iraq and then how do they communicate between one another so here is yet another powerful feature of docker which is the docker networks so now I will a docker network isolate the two containers from the rest of the network the other thing that will do is it'll set up DNS entries between the two containers so they can talk to each other according to the container name pretty cool right so demonstrate this we're going to create two containers the first container is going to be a my sequel 5.6 container and then the second container is gonna be a note js8 container which we're just going to start a terminal in so we'll start by creating the network which these two container is going to share that's as simple as doing da current Network create and then multiple is gonna be just the name of the network and you could put whatever here you could put any name you wanted so we'll copy that we're right over here you can see it has this hash which the hash the network now we can check it by doing docker and network LS so we can see that our network exists here let's now that we have our network we're going to create our first container now here we have a couple of new options we have - - net this allows us to say make this container use the network that's named multiple and then - - named mol underscore my sequel remember that random generated container name the docker gave it well by using - - name you can have it have a specific name that way you can reference it later and in this environment variable this is just specific to my sequel it's it's not actually a docker command however the - E is going to be to establish an environment variable with a particular value in that container so go ahead and run this and get this thing moving and then of course I can't find it so it has to download it and build it it's not that's done remember we use - D so it started in the background and then that that's ready to go we could check that with docker container LS we can see that it's running here and mol my sequel is going to be its name so next we're going to do is create a node.js container and then gonna run bin Bosch as the first command that way we just open a terminal into that container and again we're going to specify the proper network or my specified - IT that waits interactive and now we're gonna specify a name called molt node so we have mall and mole node so we'll take this copy it in here just like my sequel it has to download everything and build it it's not everything's built and you can see that I have a terminal into the container of the node js8 you know container so I'm gonna do now is I'm just gonna do ping mol underscore my sequel you can see that I'm getting a response and that's because because these two things are used in the same network it's allowing this container the node container to contact the mole my sequel container if I had the my sequel CLI tool inside the node.js container which I don't because no js' is not related to my sequel I would be able to connect to the database from here so in my node.js application I could simply just reference mol underscore my sequel and it makes it really easy to connect to other containers that are part of that same network now it's also worth mentioning that unless you specify otherwise you do automatically have access to anything that's on the internet so you can contact google.com for instance so let's just recap what we've done here so we start by creating a very simple Python container and running a Python file used in that container we then create a second Python container but from an image that also had numpy installed to it and then we executed that same file we then moved on to serving up a single static HTML file using an engine X container and finally we demonstrated the power of docker networks to take two containers and tie them together with the same network and then we logged into one of the containers the node.js container and we were able to ping the my sequel container successfully meaning that it's able to contact my sequel no problem and then from my sequel it could also contact the node.js container if it was so inclined to do so now keep in mind and all this software that we've been using nginx nodejs my sequel and Rita's Python 3 we never actually installed any software to our computer other than docker and that's very powerful because that totally removes the need to control versions you can run Python to 2.6 2.7 Python 3 3.4 3.5 so you can run all these in all separate containers without any conflicts same with node.js if you have a node.js 4.0 6.0 8.0 and 10 point no application you can run four separate containers that have four separate applications mounted into those containers and you can run them without any conflicts and there's docker images for everything if you want to check out more go to hub docker comm and then just search for whatever whether it's my sequel or Redis node anything you could possibly want it's all on there and if it's not on there take the thing that is almost what you want and just use docker file and just make it exactly what you want my final thought before we end the video is that there is an easier way to describe different container layouts and how it should work and different options and it's called docker compose and I'm gonna cover it in a separate video you may find that once you learn docker composed that's all you're gonna want to use and that's that's really all I use but understanding docker run and docker network and docker build are all very important to understanding kind of the underlying technology for docker anyway that's it for now see you in the next video
Info
Channel: Engineer Man
Views: 86,307
Rating: undefined out of 5
Keywords: docker, engineer man
Id: 6aBsjT5HoGY
Channel Id: undefined
Length: 21min 17sec (1277 seconds)
Published: Sun Feb 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.