0:00 hi so you're a developer well you think
0:01 you're a developer nice building apps is
0:03 pretty cool except when it's not let's
0:04 say you're building a web app the web
0:06 app works perfectly hey it's not working
0:08 what yeah it's not working but it works
0:09 on my machine this has happened since
0:11 the beginning of Technology let me show
0:14 you hey it's not working it works on my
0:17 machine now this has happened or will
0:19 happen to every programmer we call this
0:21 developers law any code that works
0:23 perfectly on your machine will
0:24 inevitably fail on everyone else's I
0:26 made that up by the way but what if I
0:27 told you there's a way to prevent this
0:29 this is Docker Docker I hardly know
0:33 her so what's Docker well it's the
0:36 sponsor of today's video oh no wonder
0:38 Docker is the number one most used
0:41 developer tool based off a stack
0:42 Overflow survey that helps you develop
0:44 ship and run applications within
0:45 lightweight
0:46 containers what okay so Docker is
0:49 actually pretty simple and something you
0:50 should know sloth why should I know
0:52 Docker you should know Docker because a
0:54 lot of tech companies use it because it
0:55 makes development and deployment easier
0:57 yeah that makes sense plus it's a skill
0:58 that's in a lot of job posting which
1:00 means learning Docker will make you
1:01 money yeah that makes sense what makes
1:03 Docker so good Docker essentially allows
1:05 us to package up our code imagine Docker
1:07 like a Lunchable the Lunchable has your
1:09 trash code dependencies like no. JS or
1:11 python environment settings and
1:12 everything else needed to run your trash
1:14 code you can bring that Lunchable
1:16 anywhere and give it to anyone at school
1:18 at work you can let your friend have it
1:19 I like my cheese drippy bro no matter
1:21 where or who uses it it works so
1:23 basically Docker makes it easier to run
1:25 your code on any computer because it has
1:27 everything you need to run the code no
1:29 need to install anything Docker does it
1:31 all for you yeah that makes sense now
1:32 how does Docker do that Docker doeses
1:33 this with two important Concepts that
1:35 you have to know images and containers
1:37 everything revolves around these two
1:39 concepts so don't forget them you can
1:41 think of images as the recipe it
1:43 contains all the ingredients and
1:44 instructions this means a Docker image
1:46 contains the technology we need run time
1:48 and any system tools we need to run the
1:50 specific code now we need something to
1:52 actually run the code which is where
1:54 containers come in a container is like
1:55 the actual meal it's what gets made from
1:57 the recipe now the cool part about
1:59 containers with one image we can create
2:01 multiple container
2:03 instances what now what does this mean
2:05 it means as long as you have the docker
2:06 image you can create a container and run
2:08 the code you want proof here's me
2:10 running a Docker image that contains
2:12 Doom yeah the entire game of Doom to be
2:14 fair you can run doom on almost anything
2:16 but this is what makes Docker amazing it
2:18 allows us to simplify the process of
2:20 sharing code you no longer have to do a
2:21 tedious process like installing all
2:23 these tools and making these
2:24 configurations to run some code all you
2:26 need to do is run one Docker command and
2:28 it does the rest for you yeah that makes
2:30 s i simplified this explanation a lot
2:32 because a lot of you have a shorter
2:33 attention span than a goldfish so if
2:35 you're a nerd and you actually want to
2:36 learn more about Docker I'm going to
2:37 have links in the description if you
2:39 want to do your own research or you can
2:41 check out my free newsletter called
2:44 sloth bites where I give you free
2:46 programming information every week to
2:48 make you a better programmer and did I
2:50 mention it's now that you understand why
2:52 Docker is important and how it works
2:54 kind of let me show you how to actually
2:56 use it step one download Docker
2:58 obviously go to doer. or click the link
3:00 in the description and download Docker
3:01 desktop it'll download Docker for you
3:03 and it comes with a nice little gooey to
3:05 manage your images and containers but if
3:07 you're feeling like a grownup it also
3:08 comes with the docker CLI so you can do
3:10 everything in the terminal step two
3:11 check if you have Docker open up your
3:13 terminal and type Docker D- version if
3:16 you see this you have Docker installed
3:18 if you don't then uh good luck little
3:19 bro figure it out step three set up
3:21 Docker on your IDE of choice I'm doing
3:23 vs code because I hate myself make sure
3:25 to download the docker extension on your
3:26 IDE because it'll have language support
3:28 and other goodies that will make your
3:29 life so much easier step four the docker
3:31 file this is pretty important now what's
3:33 a Docker file remember our recipe
3:34 analogy this is literally where we write
3:37 our recipe let's write one together here
3:38 we have a simple node server yeah you
3:40 heard me node JavaScript first we're
3:42 going to create the docker file and
3:43 inside this file we're going to write
3:45 the steps and instructions to run the
3:46 server now in order to write the steps
3:48 you need to know the steps so you need
3:49 to understand how to run the server but
3:51 since we're using JavaScript it's pretty
3:53 easy first you get your code then you go
3:54 into the file or directory download your
3:56 dependencies and then you run the server
3:57 with the mpm start command bada bing
3:59 bada boom easy peasy now that you
4:01 understand how to run the server all we
4:02 have to do is write all that in our
4:04 Docker
4:04 file what so at the top of your Docker
4:07 file you're going to write from and if
4:08 you downloaded that Docker extension I
4:10 told you to get like the good boy you
4:11 are if you hover over it you're going to
4:13 get some nice documentation every Docker
4:15 file starts with this command now we
4:17 have to select a base image and a base
4:18 image is basically just our starting
4:20 point we can use the officially
4:22 supported node.js base image that comes
4:24 with everything we need so in our Docker
4:25 file we're going to write from node
4:27 colon 22 all we have to do now is write
4:30 the instructions to start the server so
4:31 underneath the fir command you're going
4:33 to type worker slapp worker stands for
4:35 working directory and this is basically
4:37 us telling Docker hey start at this
4:39 point this is where our code's going to
4:40 be the next step is a very important
4:41 step so you better listen to this step
4:43 usually in a node project we get our
4:45 code and then download our dependency we
4:47 could do that in Docker but that's not
4:49 the best way instead what we'll actually
4:50 do is we're going to download our
4:51 dependencies first and then get our code
4:53 so we're going to write these commands
4:55 this First Command copies our package
4:56 files and the second command well it
4:58 should be pretty obvious it installs our
5:00 dependencies now why are we doing this
5:01 unlike you and me Docker is actually
5:03 pretty smart it has this thing called
5:05 layer caching you can basically imagine
5:07 every step that we write as a layer and
5:09 Docker looks at every layer individually
5:11 so for layer caching it goes like this
5:13 layer changed do it again layer did not
5:15 change use cash that's it so the reason
5:16 we do this for our dependencies is every
5:18 time we change our code it's not going
5:20 to download the dependencies again it's
5:21 just going to use the cash so it
5:23 basically skips this step which makes
5:24 our builds way faster our next step now
5:26 is to copy the code into the image we do
5:28 that with this command wow now this
5:29 command copies everything all our files
5:32 and our folders into the docker image
5:33 but this is a problem since we're
5:35 copying everything that also means we're
5:36 copying our node modules that's not good
5:39 because this file is big but not as big
5:41 as your M we need a way to ignore that
5:43 file we can do that with a Docker ignore
5:45 file and inside that file we're going to
5:47 add the node modules folder and any
5:48 other files you don't want in there on
5:50 to the next step in our code we have
5:52 this port environment variable we need
5:53 to add this into our darker file we can
5:55 do that with the EnV command now since
5:57 this environment variable is a port
5:59 we're also going going to need this
6:00 command this basically tells Docker hey
6:01 we're going to need this port now it's
6:03 time for the last step running the
6:04 server we do that with this command whoa
6:06 whoa whoa whoa why'd you do it like that
6:07 and why didn't you just use Run mpm
6:09 start like you did for install good
6:11 question actually the reason is because
6:12 the Run keyword happens when we're
6:14 building the image while the CMD keyword
6:16 is what Docker uses to actually start
6:18 the container if we use the Run keyword
6:19 for this step the container would never
6:21 start anyways enough Yap our Docker file
6:24 is complete step five building the image
6:25 we're going to have to be grown-ups here
6:27 and use our terminal to build your image
6:29 we use Docker build command so inside
6:31 your terminal you're going to type
6:32 Docker build- t t stands for tag and it
6:35 basically means give your image a name
6:37 tag understand so give your Docker image
6:39 a name you can name it whatever you want
6:40 the last thing we're going to add is the
6:42 path to our Docker file which in our
6:43 case is the current working directory so
6:45 all we have to do is type A period run
6:47 this command and you're going to see
6:48 Docker running all the steps we wrote in
6:49 our Docker file step six running the
6:51 container we can do that with the docker
6:53 run command make sure you write the name
6:55 you gave your image hey what's that Das
6:56 P this- P is for port forwarding you can
6:58 do your own research search for that but
7:00 for now think of it as a bridge between
7:01 our computer and our container the
7:03 number on the left is the port for our
7:04 computer and the number on the right is
7:06 the port for the container you don't add
7:07 this part it's not going to work go
7:09 ahead try it I dare you run the command
7:11 and you're going to see that your node
7:12 server is now running congratulations
7:14 buddy you know the basics of Docker I'm
7:16 proud of you step seven debugging even
7:18 though Docker makes it easier for us to
7:20 share code it doesn't stop us from
7:22 building our containers wrong or from
7:23 writing trash code luckily debugging and
7:25 checking any logs is pretty easy with
7:27 Docker desktop so open up Docker desktop
7:29 top and look at the container that's
7:30 running it should have a little green
7:32 dot click the container and you can view
7:33 any logs you can execute any terminal
7:35 commands and you can see how the
7:36 container's doing pretty simple now if
7:38 you think you're too good for a gooey
7:40 you can also do this in the terminal
7:41 with the docker exact command step eight
7:43 Docker Scout now if you notice when you
7:44 ran the docker run command Docker gave
7:46 you a recommendation to run this command
7:48 Docker Scout quick view now what's
7:50 Docker Scout it's a tool created by
7:51 Docker that looks inside your Docker
7:53 image and it generates a detailed report
7:55 of all the packages that's inside your
7:56 image and it's looking for any
7:58 vulnerabilities inside those packages if
7:59 it detects any vulnerabilities it'll
8:01 give you suggestions on how to handle
8:03 them using Docker Scout is pretty simple
8:04 you can do it with the terminal or with
8:06 Docker desktop I personally recommend
8:08 Docker desktop it's really easy let me
8:10 show you how to use it all you have to
8:11 do is go to your images click on the
8:12 image that's running and once you're
8:14 there you're going to see this
8:15 vulnerabilities tab with a button that
8:16 says start analysis click the button and
8:18 you're done it's that simple step number
8:20 nine Docker compose and Docker volumes
8:23 let's be honest here this note example
8:25 was really simple nowadays babies in the
8:27 womb can do this example what if we
8:28 wanted to add a database what if we
8:30 wanted to add a front end how do we add
8:32 that with Docker now you could combine
8:33 all of them into one giant container but
8:36 I don't recommend that a lot of people
8:37 don't recommend that please don't do
8:39 that it's pretty stupid now the way
8:41 people usually do this is they separate
8:43 each of these services and put them in
8:45 their own containers and this is called
8:47 a multicontainer application what an
8:49 obvious name right but this creates a
8:51 brand new problem now that you have
8:53 multiple containers you have to run
8:54 those containers separately and you have
8:56 to find a way to connect them all
8:58 together and manage them this this is a
8:59 lot of work and a lot of areas could go
9:01 wrong trying to manage these containers
9:03 lucky for us a lot of programmers are
9:04 lazy just like me which is why we have
9:06 this tool called Docker compos Docker
9:08 compose is pretty easy to understand it
9:10 basically tells all your containers how
9:12 to work together to create the full
9:14 application let me show you a quick
9:15 example on how to use it let's pretend
9:17 we added a database to our node server
9:19 so the first thing we're going to do is
9:20 create a composed. yo file and inside
9:22 that file we're going to create an
9:24 object called services and inside that
9:26 service object we're going to be writing
9:27 down keys that represent the containers
9:29 we want to run so in our case our back
9:31 end and our database since our backend
9:33 is using a Docker file we created we're
9:34 going to use the build key and the value
9:36 is going to be the location of our
9:37 Docker file which in our case is the
9:39 current working directory so it's going
9:40 to be a period and remember our backend
9:42 needs port forwarding so we're going to
9:44 add that in our configuration to and now
9:46 moving on to our brand new database
9:47 container what we're going to put inside
9:49 of this one is the image key because
9:50 we're not using a Docker file for this
9:52 database instead what we're going to do
9:54 is use a base image of the database we
9:56 want to use which for this example I'll
9:58 go with postgress now now our database
10:00 also has some environment variables we
10:01 have to add so we can do that with the
10:03 environment key and underneath that we
10:05 just add all the information okay we're
10:06 almost done I swear I pinky promise we
10:08 just need to add one more thing and
10:09 that's a volume whenever we close our
10:11 containers we lose all the state and
10:13 data and our containers right now don't
10:15 share data between each other so we need
10:16 to add a volume what is that all right a
10:19 volume is basically just a folder on our
10:21 computer that Docker can access to save
10:23 any data from our containers and to
10:24 share that data with other containers
10:26 and it's pretty easy to create inside
10:28 your Docker compos file you're going to
10:29 copy down this stuff and this line right
10:31 here says Hey Docker create a volume
10:33 called postgress data and the other one
10:34 tells Docker hey we have this volume
10:36 here this is how you connect to it our
10:38 Docker compose file is now complete all
10:40 we have to do now is run this command in
10:41 our terminal Docker compose up and
10:43 Docker will find this configuration file
10:45 and run all the containers together and
10:47 if you want to shut down your containers
10:48 then run this command Docker compose
10:50 down and that's all you need to know
10:51 about Docker compose because I'm too
10:53 lazy to teach you anything step 10
10:54 Docker build Cloud if you noticed
10:56 building our containers takes a bit of
10:57 time even for the simple notes server
10:59 now imagine how long these containers
11:01 take for a complex application
11:02 apparently based on this 2022 survey
11:04 from incredib build the average
11:06 developer loses an hour just waiting on
11:08 their Docker build Docker realized this
11:10 problem and recently created something
11:11 to help reduce build times they created
11:14 Docker build cloud and the main
11:15 difference is it builds your containers
11:17 using the cloud yeah it's it's in the
11:18 name now when docker's words using the
11:20 cloud allows your builds to be up to 39
11:23 times faster plus if you're working with
11:25 a team you can share the same cash with
11:27 them so one person can build the image
11:29 and now everybody can use that same cash
11:31 which speeds everything up for everybody
11:32 so if you have a more complex project or
11:34 have a Docker project that takes forever
11:36 to build you should probably check out
11:38 Docker build Cloud I'm not going to show
11:39 you how to do it because I don't have a
11:41 complex project uh
11:42 step way okay so I'm not going to lie to
11:44 you all I kind of wasted your time you
11:46 could have just done Docker and N in
11:47 your terminal and Docker would have
11:49 created everything for you so uh thanks
11:51 for letting me waste your time bye