Workshop: Getting Started with Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody welcome to today's webinar getting started with docker looks like we have uh some more people joining on but i'm going to go ahead and get started because we're going to go pretty quickly um should be about an hour we might run over a little bit but um yeah again thanks for joining my name is peter mckee i'm the head of developer relations here at docker and uh today i'm gonna be going over getting started with docker um we already had a question what what prerequisites do you need you have to know anything no we will start at the very beginning and go from there so if you would like let me start sharing my screen no didn't want that okay hopefully everybody can see my screen so if you would like to follow along or go or go through the tutorial later on your own two ways you can find it is you can go to github docker get started getting dash started sorry and basically run this little bit of code here you need to have docker installed and that will start the tutorial this is exactly what we're going to do today another way to get started is if you go into your dashboard on docker desktop and you will see actually the quickest way is actually from the menu go to the quick start guide that'll drop you into our quick starting guide which will walk you through about three or four steps and then you can start into the main tutorial um so those are two ways to get to the getting started so what i'm going to do is i just navigated here to github getting started page let me scroll down and i'm going to copy in this docker command so we're going to run a docker run command don't worry about what this command does right now we actually walk through it and i'm going to jump over into my terminal i'm going to run that command let me bump this up so we can see it okay there we go and so what that did is it started up a container and now i'm going to go into my browser and go to localhost and there we go there's our tutorial it's as simple as that and so what this beginning of this bit it kind of explains exactly what we just did so we ran a command called docker run and then we passed it in some flags the first flag that we passed in is a dash d and that'll run your container in detached mode so basically on in the background dash p is to expose ports so we're exposing and mapping port 80 on my system on the host system to port 80 inside the container and we're going to get to networking in a little bit but so this allows the container usually containers are run in isolation and that includes the network and when we publish the dash p stands for publish that'll publish the port it'll publish port 80 and map it into port 80. so that allows network traffic to get through the firewall and into your container and then the name of the image that i start is called docker getting started okay so after we start up let's go ahead and take a look at our running container so with docker desktop it has a dashboard so let me open back up the dashboard so here's the docker desktop dashboard and i'm in containers and apps and we can see here that i'm running a container it tells me that it's running and it's listening on port 80. it assigns a random name when you don't give your container a name so that's where this name comes from and then it tells you what image and then here's some different commands you can do this open it in a browser uh this will give you a cli into that container this will stop the container you can restart it and then you can also delete it or remove it okay so what is a container so you and i'm not going to read line for line here but basically what a container is it's a process like a normal operating system process that runs on your system but it's been isolated from the rest of the machine so isolated from other resources on the machine such as cpu memory network other files that are on the system you can isolate that process to not be able to even see those files or system uh properties right it doesn't even know that those are there so this is a really really good video by liz she's a docker uh captain uh if you get a chance download the download this image and run it and then link into this video and watch this video it goes a little bit a little bit deep underneath the covers on how exactly um containers work but it's very informative and the nice thing you kind of get out of there is how docker containers are made up of a file system in a process so what's an image so if you're a developer out there you can kind of think of as images in containers is object oriented programming so with object-oriented programming we have classes and we have objects a class is a template for an object so when you write code you say new class and that creates a new class and returns an object to you so an object is an instance of a class same in the in the container world so an image is a template of a container so you can think of an image as a class almost so it's a template it defines what um what files are in there what processes are going to run what ports are going to be exposed all those type of things and then when you run an image you run an image inside of a container and a container is a running image that's been isolated all right so let's jump ahead let's go next and we're going to take a look at an application we're going to build so this is a an mvp a little to do app uh it's written in node don't worry about that you don't need to really know a lot of javascript or node or any at all in this uh tutorial um we're not going to be we're not going to be writing a bunch of code javascript code so so let's go ahead and download our app and then we're going to open it up into visual studio so i downloaded that let's go um um i'm gonna go into my sandbox i'm gonna make a directory to uh getting started and i'm going to copy that file i just downloaded from downloads and i think of this app to here and i think i have unzip there we go so let's go into app and then i'm going to open up it in my code editor let me bring this over so you can so everybody can see it and let me make it nice and big oh that's a little too big okay there we go so um so a couple things here you can see the uh package.json and two subdirectories source and the spec so in let's just go ahead and take a look at it so in our source is where we have our actual code running um javascript code um i won't i won't run down through this but this is basically setting up routes and then in here's how you handle the routes and then we have some some testing going on sqlite some testing to set some data up okay so that's our application so we're going to build our image and the way you do that is you use a docker file so docker files are basically uh is a text file that has a set of commands in it that the docker engine will run down one by one reading in these commands and will produce an image for you so let's go ahead and create a file let me close these up okay and we're going to create a new file and let's do it like that there we go and we're going to call it a dockerfile and then we're going to paste these commands into it i'm going to put some white space because i like to see some white space in here right so um let's go back sorry so let me walk down through these commands pretty briefly so from command tells no tells docker that we're going to use the base image called node and it has a tag 12-alpine so an image is made up of a name and a tag and a name is the image sometimes you'll have a repository at the beginning so that's my repository on hub and then you have the name of the image which is node and then you have the tag so since node is an official package the uh repository is assumed to be docker and the tag is node version 12 and it's using alpine as its base image alpine's a slim [Music] linux distribution it's very uh popular when you're building images and right here we're going to create a working directory and what a working directory um it's a it's a way that you can um write relative paths later on in in subsequent commands in your dockerfile so here we set up hey we want to create a working directory called app and every one of the commands that are executed below it should assume that that's the directory working in so down here in line five we copy everything in the current folder on my local machine in to the current working directory inside of the image so that's what the the periods are for it says uh in the local when in my current directory and the copy has uh the format of copy from to so from my local directory into the local directory in the image in that local directory in the image is the app because we set our working directory and then we're going to run yarn install this is if you're not familiar with node and yarn this is just basically the package manager and so it does a it doesn't install it'll look in your package json and will install all of these dependencies and then we give it a command the command tells docker what command you actually want to run when your image is started so uh when your image is started and run inside of a container so it's going to run node and it's going to pass to the source index.js and um what that is is the is the mains you know the main file that will start to get executed and set everything up for us so that's a pretty uh quick run through of a docker file and so to build our image we're going to use this docker build command so let me copy this and then i'll talk about it as it's running let me clear this so let's do this first i'm going to show the help so you can have a ton of different flags that you can and options you can pass into it the main options are give it the path on where where your docker file is located you can also build from a url and then you can give it a context where's the context you want to build what what um working directory should docker look at should use for when you do things like the dot okay and then you can come down here and you can do all kinds of different things don't stress about these right now 90 plus percent of the time you will basically you'll just run a docker oh uh docker build sorry for fast switching okay so we're gonna do a docker build we're gonna give it a dash t which says tag which is basically how you name your image and then we're gonna we're gonna call it getting started and then we're going to give it a dot for the current director we're in and that's our context that we want docker to use to build so let's run that let's jump back into here so basically just walk down through what i just this this text here basically walks down um describes on what i just what i just talked about what's in um what's actually happening when you do a build with the dockerfile okay so as that's building let that finish let me jump over so let me explain what's going on here so we loaded in the docker ignore dot docker nor this tells docker it's just like a um a get ignore or any other kind of ignore file it tells docker hey don't don't worry about these files or folders and those type of things and then it's going to pull down the node 12 it's going to unpack everything extracts everything and then it loads in the context it sends the context to the docker engine to build your application and then it starts walking down the different commands that you have in your docker file and then at the end it says here we're writing image and then it's going to say it's naming it and that's when it does the tagging and it says uh this image is now getting started so let's clear that so our build has been run okay now we can start an image very simply by doing a docker run and that is this command right here so let me put it into the terminal and we'll kind of talk about it so docker run tells a docker to what image to use to start a container and we can pass in different flags also to the run command so we're going to passionate dash d i'm going to break these out so we passionate dash d to run it as a damond as you saw when we actually started the tutorial right um and then we did attach a dash p to tell it what port to listen on and then we're giving it uh we're mapping the ports 3000 on my local machine to 3000 inside of that machine and then it says use the getting started image okay let's go ahead and run it so now it's running after a few seconds we can open up our our local browser so i'm going to just i'm going to do that in a new window here so i'm going to go local host 3 000 and there it is so now we can add items remove items we can check items those type of things okay sorry so there we are there it's running now let's go um take a look at back into the docker desktop and now we can see oh we have another image running which is called bold pane we can see the status it's running it's on port 3000 and the name of the image is getting started and then we have the same options we can do here okay so let's make a this is talking about a small feature request comes in and um so let's go ahead and make the the feature change and then we'll rebuild our image and then we'll rerun it and we'll see the changes in inside of our container so on in source static js app.js file update line 56 so uh static.js static js and it was app correct yep okay and let's go to line 56. oh yuck let's get rid of these okay so we basically why don't you like that i don't like that nasty red line okay well we'll see what happens so now that we made the change let's go ahead and build the image again um it is back on the last screen okay let's clear this and i'm going to go up a couple so that is the same exact command we're going to run that again this is going to build our build our uh image again it's going to do the same exact steps it reads in the the docker file loads looks at the docker nor ignores all those files grabs the contents of our context sends it to the docker engine we already have the node image downloaded and then it's going to start walking through the steps working out copies run yarn and then it'll it'll now create our image and then tags it okay um so let's run let's stop our our image for a second and then we'll restart oh it's actually going to say it shows right here so let's copy that so i'm going to run docker run dash d dash p d is for a daemon to run it in the background dash p again is to expose the port 3000 mapping in the 3000 and we get an error right that's because we already have a container running on port 3000 so it's not able external connectivity on the endpoint so let's clear that so what we have to do here is let's grab the id so let's clear this so we can do docker ps so we can see a list of images that are running on our machine and actually let me when i bumped up the resolution it makes my window too big so uh docker ps so there's a list of our images this first uh image is the one we just started a couple minutes ago about four minutes ago and the other one we started 17 minutes ago this is the docker getting started this is the actual tutorial that's running and then this image is the image that we just tried to build and run but you can see right here that we have port 3000 already mapped in the 3000 and so port 3000 is being used already on our local machine so we're not able to map another port to it so we have to stop this um stop this image and then remove it so bold pane is the name of it so we want to do oops docker stop and now if we do a docker ps we don't see it running and then once what this is saying right here is the container is still there it is just stopped so if we re tried to restart we started to try to start start it again should be started up right so now we're going to have docker ps we have our two images but to see the full list so docker um docker containers are in a couple different states so they can be started they could be stopped um they can be uh created uh those type of things but the main the main two statuses you have of a container is either started and stopped right and so now we have um bolt pane here we want to we want to remove this image because we can see it exited about 31 seconds ago so we're going to do a docker remove and we're going to well that's not the name of it and then she'll just type it in so bold pain um so that tells docker okay remove this image get rid of it i don't i don't need it anymore i'm sorry remove this container pardon me um remove this container i don't need it anymore i have another one running or i want to start another one right so now if we do a docker ps dash a we can see that it's gone right okay um and then also you can you can see these back in the docker desktop you can see your containers and apps running so we still have our tutorial running we have eric thomas that's been created and then we have one that's starting so we can get rid of this let's delete that and remove it okay and then if we do a docker ps dash a again a dash a is for all shows you everything now you can see that eric thomas thompson excuse me has been removed and is no longer there but we still have our other containers running we have one on 3000 and one port 80. if we go look at our application let's refresh that uh-oh um let me get the name of it i don't like this wraps it's hard to get the name oh seems to be running fine let's stop it docker uh stop quirky wales let's remove it okay now let's run it again okay here it is again running on port 3000 that is really weird let's look at the logs of course this has got to happen to me right in the middle of the tutorial well okay and we should only okay we only have that running okay let's rebuild it again okay um docker run let me clear the screen sorry docker ps dash a so there it is again there we go not sure what happened there okay so gets get milk ah get soda get snacks okay cool let's um let's check that one off let's delete this one okay seems to be working fine so let's go back to our tutorial um this is so let's do that now so you can also use the docker ui the desktop dashboard to delete an image like we did before but let's go ahead and do it with this one so you can see that how do i can tell which one is which right so docker forward slash getting started is my tutorial just the getting started images the image that we created and so let's delete it and there we go and let's confirm on the command line it's not running ps dash a and we can see it's been totally removed okay we've already restarted our app oh let's we didn't put the change in there let's put the change back in there oh i know what the change is ah it's not gonna let me do it i did this last time oh these colors in here are horrible we want to add this line let me put it below so you can see the change okay so it's complaining because if we did this sorry for all you react folks we're probably screaming on the on the um on the webinar here okay so this was the original text so we want to change the the text that's in there so you uh have no to-do items yet add one above right so let's remove this we'll save that and let's build our image dash team getting started we'll give it a local context so it's going to run builds our image again let me look at the questions while that's running doesn't let you make the questions bigger of course not there we go yes the workshop's being recorded um you need to install docker desktop on your laptop or um you know your local workstation to have docker installed to follow along ah you got it ivan you were you were telling me how to how to fix there yep it needs to be wrapped in a div okay awesome let's carry on okay so we built our image and now let's run it again uh so we just built it and now let's run it again let me copy that docker ps so we can see it running running on 3000 and now you can see we have our updated text in the image in in our new container and this is what we just did docker ps will list out your containers docker stop stops a container and then once it stops you can remove it and here's where we just restarted our application okay so now let's take a look at sharing out our um image with the rest of the world so i'm going to go to hub.docker.com and i'm going to sign in if you don't have a docker id you can create one ah i thought i could remember a password okay well let's fix that everybody look away just kidding let me grab my password one minute okay there we go okay so we're going to click on the new repository button we're going to name it getting started and then we're going to make sure it's public i'm going to create a new repository it's going to be underneath my account i'm going to name it getting started i'm also going to keep it public and then we're going to do a create okay there we go and you can see here that this tells us the command on how to get our image into hub so docker push p mckee getting started so back on the command line we can run we can run that docker push command and that will push our image uh into into how let's do that i think i need to do a docker log out okay and so now we get an error saying uh this doesn't what are you talking about p mckee sorry so image does not exist locally with the tag pima key getting started and so if we run the docker ls command that'll give us a list of images that are on our machine so let's do that now docker ls uh oh i'm sorry docker images let me clear that so this tells me what images i have on my machine and you can see i have the getting started it's the latest and it was just created about four minutes ago but we don't see any with p mckee forward slash getting started right and so what we're going to do is we're going to use the docker tag command which takes one image and tags it creates a link or a tag to that image with a different name and so right here is the docker tag command and of course i got to put my username in there now this this can be anything but since we're going to push it up into hub the uh you need to tag it with your repository name so when you do a docker push docker knows what repository this image lives in right so now if we do a docker uh images now we can see oh there's my image right there okay and so now after we tag it we can now push this into hub with the docker push so docker push p mckee getting started uh-oh we've been denied right and that's because i need to do a docker login user name okay now let's do a docker push and there we go and it's pushing the image up into hub okay awesome okay so now our image has been pushed up to the hub we can go test it out and pull that image back down to another machine that's running docker so we'll do that we're going to use play with docker to do that so play with docker is a lab that we have it's this one so i'm going to log in log in with your docker id okay and then click start and that will start up play with docker and then we can do create a new instance give that a second to create it and there we go we are we are in um we are in a virtual machine that's running in the cloud that has a docker instance running on it so if we do docker version we can see we have 1903 we have our client and our servers running and so let's go ahead and run the command that we did earlier but this time we're going to our docker run dash d p 3000 to 3000 but this time we're going to put our repository name our hub docker id in there to tell docker on this other machine where to go get that image all right before i do that let me show you so let's run a docker images on this vm this instance we're looking at right now and you can see we don't have any images so if i just did a docker run and um i just called it getting started dash getting dash started docker would look locally and not find an image that's named that it cannot figure out what the repository is where to look on hub because getting dash start is not an official image so it's going to tell us i don't know where that's at so that is why we need to add in the repository name let that run for a second okay there it is running and what's really cool about play with docker is after you did that it knows that you've exposed the port and so we can click on this link and that'll open our app that's running up here in the cloud on this instance and gives us a link into it and so we can interact with our application okay cool pardon me okay so now what we're going to do is one second pardon me i had to have to sneeze there okay so right now we're not persisting data as you saw before once i hit um either stop the image and restart it our to-do's have gone away and every time i hit refresh they go away so what happens is uh the container file system that runs with docker it's a layered file system so each layer every time you create update or remove a file a new layer is is created when we create our docker file and then at runtime docker creates a new file at the very top of the stack of the file of the layers and that's where all your files are written to or read from and once the image is stopped that layer that was there at runtime goes away it's not persisted right so we can see that in action so so we'll start a uh ubuntu container and we'll create a file data.txt and which will put numbers uh random numbers in there from one to ten thousand okay so let me explain this command right now so docker run same same docker run that we saw before we know what the dash d is that's going to run in the background this is the name of the image ubuntu and again we don't have a repository in front of this because ubuntu is an official image and then this right here is the command we're going to pass into uh ubuntu we're going to tell it start bash i forget what the dash c is and then we're going to run this little script which basically loops from 1 to 10 000 and then writes that out into a text file and then it's going to tail that text file so we can see each of those numbers being written to the file okay that talks about the command and um so now we're going to take a look at the data that's being put into the file right so we're going to exec or execute into the container and the way we can do that here is we're going to use uh the desktop to do that let's come over to desktop and we can see all of our containers running and there's my bluetooth so i'm going to click on the little cli button i'm going to get my other bash okay um so let's cat out in the root of that file system data.txt so you can see that file is right here so let's cat that out so go cats and we're going to go data dot text it's at 63 18 oh no it didn't like that okay so it went uh to 63 18. so let's do the same thing from the command line so what yep let's terminate that terminate that what this cli command does is it's gonna it behind the scenes it's gonna do the exi it does the exact same thing we're gonna do here a minute on the command line on the terminal so we're going to um so we're going to docker exec going to give an it um we'll give it the container name and then we're going to cat out the file just like we did in the in the um from the docker cli when we clicked on the cli up here so let's actually get the docker container id which is that so we're going to docker exec it container name and then catwackdata.txt and so what this does it says ex execute a process inside of that container give me an interactive terminal this is the container id that i want to you to run the command in and then it's going to run cat and then data.txt since we ran it with an interactive terminal it's going to grab the output and dump it into the screen okay and now we're going to run a second container and so we're going to run a second ubutu container we're going to run it with an interactive terminal and then we're going to pass an ls which is list and we're going to tell it list the root directory and there we go and you can see that we don't have the data.txt file in this in boot right and what this is demonstrating is that we're writing to a file in our other image in our other container that was built from the ubutu image but those files aren't being saved into the image they're going to go away okay so let's go ahead and remove both of those so we'll do a docker ps docker remove dash f so remove with the dash f forces the image to stop and then be removed if you try and remove an image without first stopping it you'll get an error so we're going to remove it but we're going to tell it to force so go ahead and stop it first and this is friendly rosalind uh ps and there we go it has been stopped okay so now we're going to talk about containers and volumes so volumes are the preferred way to persist data and there's two types of volumes we have volumes which are manned which are named volumes which are managed by docker and then we have uh bind mounts and by mounts basically bind your local file system into the container with a volume docker creates a volume out a storage area outside of your container and then maps that storage area into your container and so anything written to that that area will be stored in the volume outside of your container and therefore you can start and stop containers attaching them to this volume and your data stays will always get written to the volume and always persist same with the bind mount you can map let's say your local working directory or your temp directory into a container and anything written to that temp directory inside a container will get persisted to your local file system in that temp directory so by default the to do app stores data in a sql lite database located here etsy to do's to do dot db um so with the the database is a single file our container writes to the db.db file and so we're going to create a volume to store this to do dot db right and so let's go ahead and create a volume it's very simple to create a volume let's clear that again so i'm just doing a docker volume create and then we give it the name of the volume we want to create and there we go it's been created and now we could stop to do our to-do app in the dashboard or using the docker rm-f and then when we start it back up we're going to map in the volume into etsy so let's find our image is this one right here so we're going to go not image container sorry docker remove dash f and we're going to give it the container id docker ps it's gone okay now we're going to start it back up but this time we're going to map in the volume into the container so when our data is being written into the sql database that will get stored and we were able to then stop our container and start a new container attaching to that same volume that we written to earlier and our data should persist so there we go we have our container running it is this first one right here so now that's up and running let's go ahead and go add some stuff into our to do very important we can delete this let's leave that okay here you can see all right so let's add some stuff get milk uh get car washed cook dinner and finish workshop okay cool so now we're going to remove the to do app right so we'll run a ps to get the id and then we'll run the dash f command like we're doing been doing previously okay docker ps and here it is right there the zero three five d4 or if we wanted to we could actually use the wonderful bose uh remove dash f and catch the w okay you can see it's gone and then we're going to start a new container as we did above and then we'll see if our data has been persisted okay let's refresh oh we have data let's do this docker stop let's get a list here it's that one so let's stop it okay we don't have any data it's been stopped now we're going to start it up okay let's refresh and our data is back awesome so now we're able to persist data instead of losing our data every time our container stops or starts or is removed okay so docker volume ls we can take a look at what volumes we have on our system by running docker volume here let's do the help com if we can spell correctly so this gives us all the options we can do with the volume uh command so we can create which we did earlier we can inspect we can list them and then we can prove prune them so that will remove all uh unused images and then we can remove them so let's take a look at the list so we're going to do docker uh volume um make sure to not it's not volumes it's volume uh noun singular and let's run an ls so you can see here's my to-do volume this is another volume that i was using for a different workshop here's our to-do right and now we can inspect that and see what's um all involved with this volume so to do oh there we go db and there's some different properties in here spit out as json so it tells us the create date what drivers being used you can use different you could put plugins for different drivers you can add labels to your volumes and containers and those type of things and then this tells us the mount point now this mount point is not when you run docker desktop since since docker runs all of your containers as a process a native os process that means you use the underlying opposite operating system to create containers and i'm running on a mac machine which is not a linux operating system it's a bsd based uh os and so the underlying constructs of the operating system are not the same as linux so what docker desktop does is it creates a small tiny little vm and it runs the engine inside of that vm and then it wires that engine up to the cli outside of the vm that's running in your terminal and so when we run images we're running native linux images inside of an image vm on my mac same exact thing happens on windows of course the implementations are different on the two os's but the the design and architecture is the same so this mount point is actually inside the vm where docker engine is running so i can't cd into lib docker tells me no such file no no such file exists it's inside of the docker um vm that's running the engine so i just wanted to call that out in case you do this and you try and cd change directories into this to see your data it's not there okay and then it names it and there's some other options and then tells you the scope okay let's take a look at bind mounts so i talked a little bit real quickly about bind mounts earlier so like i mentioned so name volumes is the preferred way that docker would like you to manage volumes um that way if i create a docker file or run a file and i use volumes it'll be run the same whether it's on your system or on my system it's not dependent on the directory structure of your local system with bind mounts you basically bind the local data structure a file system structure into the container so if on my machine i was located uh i had my data located in um in the root on forward slash data directory and on your system you didn't have it in right in the root root but you had it in your home directory so if i sent you an image and i told you oh hey bind uh bind do a bind uh mount and bind the root data folder into the container your data folder is going to be different than mine and let's say you pull down code from get and it's not living in data the data directory you're not going to see those inside the container and there's a little bit of confusion so that's why we recommend using volumes if you use avon it's always going to be in the same place docker manages that but my bind mounts are very very uh helpful especially for local development i use buy mounts a lot when i want to do local development so starting in dev mode container okay to run a container in supported development workflow we're going to mount our source code and this is kind of what i was talking about this is how i do local development a lot we'll install all dependencies including the dev dependencies we'll start nodemon and watch the file system okay so let's stop all of our images docker ps a docker ps uh remove that not ps remove dash f and it looks like 974 974 oh by the way you with the container ids you only have to pass in enough of the beginning of the container i id that makes it unique um and docker knows okay this container starts with 974. i don't have any other containers starting with 974 i know what you're talking about and while we're at it i'm going to remove that ubuntu okay let me clear and then do a docker ps and there we go we only have our tutorial running okay so we're going to run the follow command it's very hard to see it here so let me copy this out and let me paste it so again we're going to do our nice docker run that we're we're familiar with the dash d and dash p so dash d for daymond so running in the background detached is i believe what the d actually stands for um and then uh dash p which is uh publish a port and we're gonna publish 3000 on our host in the 3000 in the container like we've been doing um and then we're going to set uh a working working directory is dash forward slash app and then we're going to do a mount a bind mount and we do that with the dash v for volume and then we tell it pwd which is print working directory [Music] and this little dollar sign says execute this command and then grab the output and replace it with the output so what this will do is run pwd which shows me pwd gives me my current working directory or print working directory so user's peter when i run uh pwd here i'll get sandbox getting started g started app and it'll map that just like we did here port on your host into the container i'm going to do on a host locally my working directory and i'm going to map that into app and then this is the name of the container we have and then this is the command we're going to run inside of that container so it can't find no 12 alpine it's going to pull it down locally and um did it run the whole thing do a docker ps let me clear that so you all can see that this is my image here oh yep there we go i wanted to make sure it was running uh yarn let's actually let's do this the zoo docker um remove dash f oh no zero zero okay now it's gone docker ps it's gone so we're gonna run we're gonna do that docker run again but this time i'm going to take out the background so we can see it actually running and resolving it's doing the yarn install and then it's going to do the run div and you might be asking oh well wait a minute how's it going to get the code where does it see the package you didn't copy it into the image and that's what the bind mount does uh that'll bind my working directory pwd into app and you can see this is this is uh the ad the folder that's inside of my working directory and so all of this will be acts available to the container so when it runs it looks and goes when we ran the yarn install it says oh there's my package json so it gets all dependencies and then when it runs the yarn run dev it looks in the package json looks at your scripts and sees we have dev and then it's going to run the nodemon source.index real quick for those non-node developers this package json is uh your dependency kind of management but it also has ability to add scripts to it with labels and then a command so you have label pretify and then you have the command that will actually be run so here with dev when we say yarn run dev it looks inside of here says okay underneath my scripts i see a dev oh it wants you to you want me to run node nodemon source forward slash index.js and so what will actually be run is that nodemon and you can see that right here so it's going to start known actually right here a little more specifically which in turn runs node the source in the index and now we're listening on port 3000 and there we go you'll notice that we don't have our data anymore that's because we didn't we didn't mount in uh we didn't bind to our named volume we used a bind mount just to our source code and not into the sqlite database okay okay we watched it um we watched it using the ourselves running it without in the background okay now let's make a change to the app so let's make sure okay it's still running so we're gonna make another change to the app i really gotta change these colors here that's horrible so one line 109. me do it below so um instead of printing out adding so when we click on the button it should this submitting flag changes the true uh and it should say adding when it's done adding the submitting button will be false and it says add item all right so we're changing add item to add so this this should be the default let's get rid of that save it now you notice we didn't change we didn't start our uh we didn't remove our container rebuild and then restart uh the container we just updated some code so let's go look at the application so you can see it says add item let's update it oh now it says add let's do that should you say it goes really fast because we're running local locally but you can see that changed and i was able to change it without starting and stopping the container rebuilding all those type of things why because we have mapped in our source code into the image and then we went then we told the image go ahead and run this in dev mode and watch my files on my file system and when they change recompile everything and restart the app and all inside the container without stopping the container or restarting it okay so let's stop it and let's rebuild our container i'm gonna i'm gonna stop it on here yes remove you and we're going to remove you there we go so i'm going to rebuild it okay well it's running we can move on um basically saying that that uh buy mounts are very common for development setups like we just did super common i use it all the time it's really easy to just just bind mount in your source code configuration files those type of things run your application with inside of a container and that way i can have the specific node version that i need to run that application and then in another window another terminal i can run another image that's of a different version of node and i can map in my source code and develop on that version of node right and i don't have to install node on my machine i just run it inside a container i map in my source code and i'm able to run it right inside the container just like i would normally on my terminal okay so now we're gonna is this done yep we've rebuilt let's clear docker images this is the getting started i just built 53 seconds ago okay all right so at this point we've been running everything um inside of one container we're running my sequel as a as a embedded system and writing to disk and all those type of things so we're going to take a look running multiple containers and running mysql as its own container so to do that we got to talk a little bit about networking okay so when again when a container is run it's isolated and that includes the network so it's put on to its own network it's not able to see any any other containers outside of it unless it is on the same network as that container so what we're going to do is we need to create a network that we can share between our two applications so let's do that now so we're going to create a network creating a network is very similar to creating um volumes so we just do a docker and let's do the help here too but um so here's a command you can um run so we can create we can inspect and we can list like we do with volumes we can also connect or disconnect from a network and then we can remove them prune we also have prune that remove all unused networks nice handy way to clean up things that aren't being used anymore so let's create our network create and what did they call it to do app to do app okay docker network ls and you can see here's to-do app okay now let's start a mysql container and connect it to that network okay and then we're going to use environment variables here to pass in the password and the database so let me read this command explain it real quick so again docker run very familiar with that dash d familiar with that now we have something new we have the network so we're telling it attach this to do app network that we just set up we're going to tell it inside that network i want you to be able to reference this container by the name mysql it's called a network alias and then we're going to map a volume to do mysql data into var lib mysql we're going to set some environment variables the password and the database and then the name of the image it can't find mysql locally so it knows that uh since it didn't have a repository in front of it it knows it'll look at the official images and says oh okay i know what a fit what image that is so it pulls that down and let's do a docker ps and we can see our mysql is running right there okay so we're gonna exec into here so if you remember what exact does that executes a process inside of a container so docker exec we're going to give it an it an interactive terminal and then we're going to have to put in our um container id here make sure i have a space yep and then we're going to tell run mysql that's the command line tool to interact with the sql database sql engine and we give it a bat a dash p um and what was the password secret very secret secret password okay now we got a mysql uh command line tool that's connected into the database and then we're gonna run show databases and there we go we can see to do's down here at the bottom and hooray we've done it okay so now we know we have our sql mysql running inside of a container we're able to connect into it we're able to see the database things are looking good and i think you yep there exit out of that okay so now um we're going to take a look at some networking and how to talk back and forth between these these are some dns tools networking tools we're going to use okay so we're going to uh we're going to run docker run and we're going to attach to the to-do app and we're going to use net shoot so let me clear this so it's going to pull down the net shoot image and get a drink of water here oh two folks if anybody needs to drop it's totally fine we are recording this and we'll send out the replay afterwards if you miss it we're also going to do this again next month so don't worry too bad if you got to run totally understand thank you for coming sorry we're running late this usually takes a little bit longer um than an hour so but we're almost done so okay so now we are inside of the container and let's use the dig command uh which is a dns tool so to look up the ip of mysql okay so we got an answer we got a header um mysql and i believe the ip is right here okay um yeah so a is for the a record for the mysql and results to no there it is i'm sorry 172 1902 okay you can see that using because i use the network alias we get mysql returned to be able to look it up from from other containers or hosts running on that network okay so the to do app supports some environment variables that we can fill out so we can connect to the database so mysql host is the host where the sql server is running mysql underscore user is the name of the user underscore password is the password and db is the name of db we want to use okay so right here is a quick warning and it's highly discouraged to do environment variables with secrets you can you can read down through this but a really good blog post to read is this one here why you shouldn't use environment variables definitely read down through this it explains what's happening why you don't want to do it all those good things i don't want to dive too deep into it uh in in this tutorial okay now let's connect now let's start up our pardon me our dev terminal so i'm going to copy that i'll explain what all this is when we run here in a second okay i'm going to remove this net shoot docker remove okay that's clear okay so again docker run uh dp so as uh detached in exposed support set up the working directory map in our our bind mount our local working directory in the app like we've done before join the network and now here's where we set up our environment variables so we're going to tell it the host is my sql the user is root the password is secret and the database is to use and then we're going to run our node alpine image the official node image i should say and then we're going to run a shell command we're going to say yarn install and then yarn run dev and then let's take a look at the logs i'm going to tail them and let's see yep that is it there okay and there we go it's all ready to go uh everything's been resolved um the reason being let me clear this here ls okay uh so you can see the node modules right here when we were running when we ran the dev environment before because i mapped in uh pwd which is my uh print working directory so this is my current working directory it mapped that into the container so last time when we ran the mpm install or the yarn install forgive me it wrote everything to the node modules but since that was mapped to my local file system those those uh those installs were persisted so when we started the um a new container with the same image and mounted mounted my local working directory again and then ran the yarn installed this yarn install looked and said oh there's your there's your node modules everything is installed already i don't need to run again oh that's right we already ran it but let's pull up so you can see here resolving packages successfully already up to date and now we're listening on waiting for mysql it's been connected and we're connecting with the host and now we're listening on port 3000 let's get again now get coffee uh get chicken that's good enough okay cool so let's say that one's done all right so this just explaining what i just explained looking at the host um now let's go back and let's execute into our another terminal here let me bump this up i could do that yes nice okay uh whoops i'm gonna need the docker ps i'm gonna name the id so docker exec dash i t um and what we're going to run mysql uh sql dash p dot to do's yep uh oh did i run oh no i'm sorry i was trying to connect to the wrong image or container we need to connect to the mysql container so if you see here dbc 0 dbc 0 is the node and so what this said is oh when i tried to run when i tried to execute my sql it failed i don't know what file that is because it's not in our node container it is in our sql container so docker exec dash i t the correct container id and my sql dash p ah see i probably just oh i spelled it right okay cool and then let's run this little select command to look at what to-do's that we just saved and there we go let me see get milk get coffee get chicken so our two containers were able to talk each other right this is just showing that you can now we can now see them all let's get rid of that guy but you can see all of our images running you can see our sql server running or not sql server pardon me my sql running and then our know about node application is running okay so what we did what we just did there is we started each one of those containers individually we had to uh start up our mysql container we had to remember all the command line options we had to remember what ports what what environment variables were named all those type of things and we had to type that all out on the command line of course i copied and pasted it out of the tutorial but in the real world you would be typing out all these long docker run commands right and you might say well let me let me put those into a script bash script or something like that and you could do that but docker has a solution it's called docker compose and what compose does is just that it composes different containers and runs allows you to run those containers at the same time connecting to networks and volumes and passing in environment variables everything we did on the command line but we had to type it out for both our node container and our mysql container we can put all that configuration into a compose file and then just run the composer all right so we don't need to install uh compose we already have it um compose is installed with uh docker desktop scan um docker dot compose version there we go where version uh one two seven okay okay so this is just reminding you of what the big command that we ran and so we're gonna we're gonna take this big command and we're gonna convert that into a compose file and i skipped that so let me go into our app let's create a new file okay cool okay first thing we need to do i think it's rewritten down here let me just i can just hit the copy button actually let me do this this is going to be way easier if i just go down i get the final one there we go paste that in there okay cool so at the very top we tell uh docker compose what version of the compose spec we're using so we're going to use 3.7 and then we have this top level label called services and then underneath the service we list out uh our services and a service is basically a running container and so our first service is named app this is arbitrary this could be um you know it could be our app it could be front end um you know back end whatever whatever you want to call it um but it tells it's the name of the service and then we tell it what image to run so we're going to use again the node 12 alpine what command to run and to pass into that image just like we did on the command line and then we tell it expose port 3000 and map that into port 3000 inside the container we're going to set up our working directory directory just like we did on the command line and then we're going to map our volumes here in a compose file instead of doing a pwd like we did on the command line you can just give it the relative path to the to your local file system so we're going to tell use the current directory i'm in and then we're going to pass in some environment variables just like we did on the command line so we pass in the host the user password and the db so let's save that so that's our this is for our app our node application now let's take a look at for the mysql again this is showing everything we typed out on the command line and had to put these nice little back ticks so we can do multiple line uh command lines and i'm gonna okay so i'm gonna scroll down pardon the okay i'm just gonna grab the mysql from here with the volume and let me explain what all this does okay let's go to the bottom here okay so here we're going to set up our second service called mysql which is going to hold our mysql database again we tell it what image to use mysql57 just like we did on the command line and then we're going to map our volumes in we are using to do mysql data volume right and you can see that down here at the bottom we have a top level primary level label called volumes and underneath that we list out our volume and what this translates to basically is um docker volume create to do my sql data and so this will this command is basically what these two uh labels represent okay and then we're going to pass in our our environment variable so tell what what uh password we need to use for root and what database we want to use okay so now we can run our application stack is what it's called in compose and um so let's make sure nothing else is running so docker ps docker remove dash f we're going to remove node and we're going to remove my sql and as you can see here you can list out multiple uh containers and they'll both be stopped so stopped and removed so there we go we still just have our tutorial running okay i'm going to go back into here okay um so the command we're going to give is called up so we're going to say docker compose up and what up does is it tell it tells compose this read in the compose file look at my services and start them right put bring them up and then we're going to pass in a dash d which is the same as a docker run it's going to run it detached okay so we created a network app default the app comes from i can't highlight in the command line the app comes from compose looks at the folder or directory that you're running in and it'll use the name of that directory as a common prefix to everything it creates and so it's going to create a network and so it says app underscore default it creates a volume remember this is what we named it in our compose file but so we don't have clashes and those type of things it'll prefix it with the name of the directory you can also set this when you run docker compose with a dash dash project so oops it's either at it might just be at the composed level here it is yep project name so we can we can do we could do um when we ran it we could have gave it a project name or a dash p and told it don't don't use the director folder i'm in as the project name uh use this name instead that i'm going to give you so in case you have um interesting directory names that aren't very descriptive okay so we can also take a look at the logs using docker compose so we'll use docker dash f to see our logs and here we can see all the logs being dumped out by both of our containers and you'll notice here that we have my sequel and then down below we have app1 right which is app is coming from in our service is called app and my sql underscore one is coming from there also so you can run multiple instances of these containers and here we could define run two containers and not just one the default is one very rarely running local will you run more than just one but it'll give it a counter of what which one you're actually looking at so we can see our node is um is up and running and listening on port 3000 so let's go and open up our app and see it running let's refresh okay uh i'm gonna the these these start getting smaller and smaller as we go yes uh cheese there we go there they all are all right oops i always alt tab instead of the other tab oh let's and then let's go look at this is very interesting so let's go look at the docker dashboard it's a if you noticed before i was saying containers and apps but we only saw containers running but here with compose it'll also show your compose applications running in the desktop in the in the dashboard so you can see we have our app and then we have this little arrow that we can expand it and then see what containers make up this app and we can see we have our mysql and then we have our app one and the same things we can do here we could connect into exec into oh no i hit the wrong one you can open in the browser you oh the reason that is there um and not above that is because we're exposing a port and uh docker desktop smart enough just like play with docker sees you expose the port says oh okay cool let me get a little shortcut to click on this button so you can open that url in a browser okay awesome and so it's just so what we talked about here so to bring everything down we're going to run a docker compose down let me stop the logs and let's clear this up here so i'm going to do docker compose down okay so it stopped the containers then it removed them and it also removed our network but what's interesting do we do a volume ls we'll see we still have the app to do mysql data volume is still there it didn't remove it but if we did a docker vol network you can see that our app underscore to do um network has been removed actually app underscore default excuse me it's no longer there it's been removed but the volume will stick around the reason being if you save data into that volume you want that to persist while you could do docker compose down docker compose up docker compose down we want to keep that data around as long as we want um and and let's bring it back up just so you can see that there we go and let's do that so it doesn't look like magic and there we go we still have our items it's running in that my sequel that we started all with just compose just firing everything up all together let's stop that okay so we are about at time this next this next kind of section is kind of optional so i'm not going to go through it too much but it talks about scanning images and those type of things um i really recommend that you go to um github let me drop it into the chat here i highly recommend uh i know i went very fast and i was explaining a lot of things and i didn't read through the text i didn't want to bore you with just what you know listening to me read the tutorial to you um but i highly recommend go go to this github repo and run this docker run command right here you don't the only reason i say go to this repos because that's where the command is you don't have to remember the command it's easy to remember docker forward slash getting started so go there run this command this will pull this image down this tutorial image down from hub and it'll start it on port thousand and then you just open up your browser and you can follow along uh i would go to the go through it all and then go through the last module there at the end which is basically extra credit it gets a little deep into scanning and images best practices i recommend that people try to build their own images for some smaller applications they're working on and running those connecting to database and those type of things before you really worry too much about best practices and getting the smallest images possible and all those type of things um you know walk crawl run so uh or crawl walk run so you know in the crawling phase when you're first starting out and in the walking phase right start slow understand the concepts first get things working and running and then once you understand those now dive down another lot uh deeper level and start understanding why you're doing the things that we're doing and how you should be doing it right best practices okay well that's all i have for today uh i really appreciate everybody coming around and actually if you would like i'm gonna look at the questions real quick um i know we're past time but um i still want to take a look at the questions see if there's anything i can answer real quick um are there plans to release docker desktop on linux um there's a bunch of requests for linux docker desktop on linux and if you would like to see docker desktop on linux the best thing to do is to go to our roadmap we have a public world map it's on github so if you get a github forward slash docker forward slash roadmap and i'll drop that in the in the chat so go to our public roadmap that is our public roadmap we do use it we submit our own issues there the project managers are there um our ceo even takes a look at it from time to time our vp of product is in there so we do use that we do have an item in there for desktop on linux please go upvote it add some comments you know get involved in the conversation um let's see yes it's being recorded i get a lot of those um yes you can download it from that github go to the github repo docker forward slash getting dash started and you can follow along right there thank you thank you all for uh helping me debug as i'm as i'm going as i'm going along here uh folks uh put in the questions that's um yeah you have to you can't have the error we were getting with the html it has to be wrapped in a div why does everybody install new packages right within the running container um ali yeah it's definitely not something you would do outside of a development environment 100 so if you're in production if you're in you know some other server where your app is running and you've made a change right do not ssh into that vm run a docker exec you know do a get pool and update your code inside that live container ally's right that's not the way you want to do it uh you want to build you want to build another image you want to push that into your ci cd pipeline and that should update your image in whatever orchestrator you're using in production um if you are only using one docker engine let's say running on an ec2 image right still do the same thing build your image push it to hub then you can either use the docker engine to point into your ec2 instance or you can ssh into that instance and just stop stop that container and re and start a new container with a new image right that's the way to do it um but running locally and developing totally fine um you're not saving them into the container you're seeing them local onto your file system it's just one way to develop inside of containers again at the base of it at the very base technology of containers is a process it's the exact same process when you're on your terminal and you run yarn run dev you run it locally in your terminal that process has started uh it's the same exact process that started that has started inside of the container the main difference is the process inside that container uh docker uses namespaces and c groups and some other things to secure that process and not allow it to break outside of it that's the only difference right completely different than vms vms are not even close to what containers and images are okay does docker desktop exist for ub not sure what ub is there ubudtu maybe desktop does not only desktop is only available for windows and mac um linux you can download uh docker from our site and you can get pre you know binaries and those type of things or you can download the source and compile it um yeah uh hatham uh how can docker run windows containers on linux it can't unless they're run inside of a vm with the vm running a linux os and technically you're not running a windows container on linux you're running a windows container um on windows oh i'm sorry my example i messed that up so you have a linux host os to run windows containers on a linux machine you cannot it's impossible and vice versa you can't run linux containers on side of a windows machine the reason being they share the same os and so windows container is looking for windows os so it's going to use the uh the windows e exe kernel and it's not there so you have to put a windows inside of a vm so you have the os and then you can run your windows containers inside of that vm all right again it kind of goes down understanding at the very base level it's still using the operating system to start a process um oh there it is the yes so does doctor desktop exist for ubuntu um no not docker desktop but docker does a hundred percent um can you run windows desktop app on docker uh i think joanne i think you might be asking can you run a gui a gui app like a windows application in docker yes you can you just need to wire up uh the ports for x term and all those type of things um give you just need to give that process access to um the ui of the operating system so it can so it can paint into the ui but yep you can 100 do it there is uh there's a couple good docker docker contacts i believe we actually have one this year so just google dockercon 2020. it's in our youtube channel so actually go to our youtube channel which is um youtube forward slash docker run and search for running windows gui apps in in docker so it is https colon i think it's youtube.com forward slash docker run with two hours is it secure if we deploy on hub meaning if i use an app um app with real data uh yeah absolutely docker hub is secure you can create uh private repos in the example we created a public repo um and you have to be the owner of that repo to push to it so not everybody can push to your public repo but everybody can pull from your public repo if you want to have it completely uh private you can mark that uh that repo is private uh yes michael the the it's recorded is it possible to make docker build automatic something like hot reload after updating the app dario forgive me if i'm pronouncing that wrong but um not right now but uh yeah that's a very very highly requested feature that we get asked to a lot we're uh the pm team the product management team is is talking to a lot of developers on how they work what they do and that's a that's a that question is asked a lot and um i don't think it's in the works yet but we're really looking into what's the best way to do that right um exactly because right now you have to stop your container rebuild the image restart your container and go with build kit that uh that loop is a lot faster but it's it's a pain it's a pain for sure yeah so we're looking into um especially deving with that dev like cycle we call it the inner loop here at docker so the developer inner loop of coding debugging um and coding running and debugging right and so look uh again if you want to go out to the roadmap the links in the chat go there um either find that feature that's out there or add it if it's not um and then tweet it out so everybody else sees it and can go comment on it um okay i think that's it yes you uh navid we will uh definitely recording i believe it'll be sent out uh after we finish not immediately probably tonight if not tonight tomorrow all right well thank you so much i really appreciate everybody uh oh maxine next course docker swarm uh docker swarm's interesting i love docker swarm love docker swarm that's what i ran internally i ran all of our internal knowledge base our external knowledge base our enterprise portal our training our training platforms all ran on swarm and it ran great but now at docker we're not so much focused on swarm swarm went over to marantis who bought our enterprise software and i'm not 100 if they're still actively working on it you're welcome everybody thanks for joining i really appreciate it connect with me on twitter um at p mckee p-m-c-k-e-e or connect with me on linkedin as another place you'll find me those are probably the two places i'm at um oh i'm also in the docker community slack uh if you're not in the slack please join there's a bunch of great people in there a lot of our captains are in there answer questions and feel free to reach out to me again i always have a caveat it's very hard to help troubleshoot specific issues in slack or or in twitter but i will do my best um awesome i hope everybody enjoyed it and thanks for sticking around for so long i really really appreciate it have a great uh rest of your week um you
Info
Channel: Docker
Views: 8,852
Rating: undefined out of 5
Keywords: docker, containers
Id: wn7s7podZWI
Channel Id: undefined
Length: 104min 45sec (6285 seconds)
Published: Wed Nov 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.