Learn Docker basics, how it solves 'works on my machine' issues, and step-by-step usage for developers to simplify app deployment.
Ask about this video. Answers come from its transcript only — with the timestamp, so you can check them.
Generated from the transcript and can be wrong — check the timestamp.
Key Takeaways
- Docker solves environment inconsistency by packaging code with all dependencies.
- Learning Docker is beneficial for career growth due to its widespread industry use.
- Docker images are reusable recipes; containers are running instances of those images.
- Using Dockerfiles and caching optimizes build times and development workflow.
- Docker Compose simplifies managing complex applications with multiple containers.
What the video covers
- Introduction to the common developer problem: code working on one machine but failing elsewhere.
- Explanation of Docker as a tool to package code and dependencies into containers for consistent environments.
- Docker's popularity and relevance in tech jobs, making it a valuable skill to learn.
- Analogy of Docker images as recipes and containers as meals, simplifying code sharing and deployment.
- Step-by-step guide to downloading Docker, verifying installation, and setting it up in an IDE.
- Detailed explanation of Dockerfiles, including base images, working directories, and commands.
- Building Docker images efficiently by caching dependencies to speed up builds.
- Running containers from images and exposing necessary ports for applications.
- Introduction to Docker Compose for managing multi-container applications and volumes.
- Encouragement to explore further resources and newsletters for deeper Docker knowledge.
Chapters
- 00:00Introduction: The Developer's Problem
- 01:00Why Learn Docker? Benefits and Use Cases
- 02:50Docker Basics: Images and Containers Explained
- 03:40Simplifying Code Sharing with Docker
- 04:33Understanding the Working Directory and Dockerfile
- 05:25Writing a Dockerfile for a Node.js Server
- 06:14Building and Running Docker Images
- 07:02Optimizing Builds with Dependency Caching
- 09:25Using Docker Compose and Volumes for Complex Apps
Full Transcript — Download SRT & Markdown
Speaker A
Hi, so you're a developer? Well, you think you're a developer. Nice. Building apps is pretty cool, except when it's not. Let's say you're building a web app. The web app works perfectly.
Speaker A
on my machine this has happened since the beginning of Technology let me show you hey it's not working it works on my machine now this has happened or will happen to every programmer we call this developers law any code that works
Speaker A
Hey, it's not working. What? Yeah, it's not working, but it works on my machine. This has happened since the beginning of technology. Let me show you. Hey, it's not working. It works on my machine.
Speaker A
sponsor of today's video oh no wonder Docker is the number one most used developer tool based off a stack Overflow survey that helps you develop ship and run applications within lightweight containers what okay so Docker is actually pretty simple and something you
Speaker A
Now, this has happened or will happen to every programmer. We call this developer's law: any code that works perfectly on your machine will inevitably fail on everyone else's. I made that up, by the way. But what if I told you there's a way to prevent this? This is Docker.
Speaker A
means learning Docker will make you money yeah that makes sense what makes Docker so good Docker essentially allows us to package up our code imagine Docker like a Lunchable the Lunchable has your trash code dependencies like no. JS or
Speaker A
Docker? I hardly know her. So, what's Docker? Well, it's the sponsor of today's video. Oh, no wonder. Docker is the number one most used developer tool based off a Stack Overflow survey that helps you develop, ship, and run applications within lightweight containers.
Speaker A
where or who uses it it works so basically Docker makes it easier to run your code on any computer because it has everything you need to run the code no need to install anything Docker does it all for you yeah that makes sense now
Speaker A
What? Okay, so Docker is actually pretty simple and something you should know. Sloth, why should I know Docker? You should know Docker because a lot of tech companies use it, because it makes development and deployment easier.
Speaker A
instructions this means a Docker image contains the technology we need run time and any system tools we need to run the specific code now we need something to actually run the code which is where containers come in a container is like
Speaker A
Yeah, that makes sense. Plus, it's a skill that's in a lot of job postings, which means learning Docker will make you money. Yeah, that makes sense. What makes Docker so good? Docker essentially allows us to package up our code. Imagine Docker like a Lunchable. The Lunchable has your trash code, dependencies like Node.js or Python, environment settings, and everything else needed to run your trash code.
Speaker A
image you can create a container and run the code you want proof here's me running a Docker image that contains Doom yeah the entire game of Doom to be fair you can run doom on almost anything but this is what makes Docker amazing it
Speaker A
You can bring that Lunchable anywhere and give it to anyone. At school, at work, you can let your friend have it. I like my cheese drippy, bro. No matter where or who uses it, it works. So basically, Docker makes it easier to run your code on any computer because it has everything you need to run the code. No need to install anything. Docker does it all for you.
Speaker A
it does the rest for you yeah that makes s i simplified this explanation a lot because a lot of you have a shorter attention span than a goldfish so if you're a nerd and you actually want to learn more about Docker I'm going to
Speaker A
Yeah, that makes sense. Now, how does Docker do that? Docker does this with two important concepts that you have to know: images and containers. Everything revolves around these two concepts, so don't forget them. You can think of images as the recipe. It contains all the ingredients and instructions.
Speaker A
mention it's now that you understand why Docker is important and how it works kind of let me show you how to actually use it step one download Docker obviously go to doer. or click the link in the description and download Docker
Speaker A
This means a Docker image contains the technology we need, runtime, and any system tools we need to run the specific code. Now, we need something to actually run the code, which is where containers come in. A container is like the actual meal. It's what gets made from the recipe.
Speaker A
check if you have Docker open up your terminal and type Docker D- version if you see this you have Docker installed if you don't then uh good luck little bro figure it out step three set up Docker on your IDE of choice I'm doing
Speaker A
Now, the cool part about containers: with one image, we can create multiple container instances. What? Now, what does this mean? It means as long as you have the Docker image, you can create a container and run the code you want. Proof: here's me running a Docker image that contains Doom.
Speaker A
a Docker file remember our recipe analogy this is literally where we write our recipe let's write one together here we have a simple node server yeah you heard me node JavaScript first we're going to create the docker file and
Speaker A
Yeah, the entire game of Doom. To be fair, you can run Doom on almost anything, but this is what makes Docker amazing. It allows us to simplify the process of sharing code. You no longer have to do a tedious process like installing all these tools and making these configurations to run some code. All you need to do is run one Docker command, and it does the rest for you.
Speaker A
easy first you get your code then you go into the file or directory download your dependencies and then you run the server with the mpm start command bada bing bada boom easy peasy now that you understand how to run the server all we
Speaker A
Yeah, that makes sense. I simplified this explanation a lot because a lot of you have a shorter attention span than a goldfish. So, if you're a nerd and you actually want to learn more about Docker, I'm going to have links in the description if you want to do your own research, or you can check out my free newsletter called Sloth Bites, where I give you free programming information every week to make you a better programmer. And did I mention it's free?
Speaker A
are if you hover over it you're going to get some nice documentation every Docker file starts with this command now we have to select a base image and a base image is basically just our starting point we can use the officially
Speaker A
Now that you understand why Docker is important and how it works, kind of, let me show you how to actually use it. Step one: download Docker. Obviously, go to docker.com or click the link in the description and download Docker Desktop. It'll download Docker for you, and it comes with a nice little GUI to manage your images and containers. But if you're feeling like a grownup, it also comes with the Docker CLI, so you can do everything in the terminal.
Speaker A
to type worker slapp worker stands for working directory and this is basically us telling Docker hey start at this point this is where our code's going to be the next step is a very important step so you better listen to this step
Speaker A
Step two: check if you have Docker. Open up your terminal and type Docker -v or Docker version. If you see this, you have Docker installed. If you don't, then, uh, good luck, little bro. Figure it out.
Speaker A
so we're going to write these commands this First Command copies our package files and the second command well it should be pretty obvious it installs our dependencies now why are we doing this unlike you and me Docker is actually
Speaker A
Step three: set up Docker on your IDE of choice. I'm doing VS Code because I hate myself. Make sure to download the Docker extension on your IDE because it'll have language support and other goodies that will make your life so much easier.
Speaker A
change use cash that's it so the reason we do this for our dependencies is every time we change our code it's not going to download the dependencies again it's just going to use the cash so it basically skips this step which makes
Speaker A
Step four: the Dockerfile. This is pretty important. Now, what's a Dockerfile? Remember our recipe analogy? This is literally where we write our recipe. Let's write one together. Here we have a simple Node server. Yeah, you heard me, Node JavaScript.
Speaker A
copying everything that also means we're copying our node modules that's not good because this file is big but not as big as your M we need a way to ignore that file we can do that with a Docker ignore
Speaker A
First, we're going to create the Dockerfile, and inside this file, we're going to write the steps and instructions to run the server. Now, in order to write the steps, you need to know the steps, so you need to understand how to run the server. But since we're using JavaScript, it's pretty easy. First, you get your code, then you go into the file or directory, download your dependencies, and then you run the server with the npm start command. Bada bing, bada boom, easy peasy.
Speaker A
do that with the EnV command now since this environment variable is a port we're also going going to need this command this basically tells Docker hey we're going to need this port now it's time for the last step running the
Speaker A
Now that you understand how to run the server, all we have to do is write all that in our Dockerfile. What? So, at the top of your Dockerfile, you're going to write FROM, and if you downloaded that Docker extension I told you to get, like the good boy you are, if you hover over it, you're going to get some nice documentation. Every Dockerfile starts with this command.
Speaker A
building the image while the CMD keyword is what Docker uses to actually start the container if we use the Run keyword for this step the container would never start anyways enough Yap our Docker file is complete step five building the image
Speaker A
Now, we have to select a base image, and a base image is basically just our starting point. We can use the officially supported Node.js base image that comes with everything we need. So, in our Dockerfile, we're going to write FROM node:22.
Speaker A
tag understand so give your Docker image a name you can name it whatever you want the last thing we're going to add is the path to our Docker file which in our case is the current working directory so
Speaker A
All we have to do now is write the instructions to start the server. So, underneath the FROM command, you're going to type WORKDIR. WORKDIR stands for working directory, and this is basically us telling Docker, "Hey, start at this point. This is where our code's going to be." The next step is a very important step, so you better listen to this step.
Speaker A
run command make sure you write the name you gave your image hey what's that Das P this- P is for port forwarding you can do your own research search for that but for now think of it as a bridge between
Speaker A
Usually, in a Node project, we get our code and then download our dependencies. We could do that in Docker, but that's not the best way. Instead, what we'll actually do is we're going to download our dependencies first and then get our code.
Speaker A
and you're going to see that your node server is now running congratulations buddy you know the basics of Docker I'm proud of you step seven debugging even though Docker makes it easier for us to share code it doesn't stop us from
Speaker A
So, we're going to write these commands. This first command copies our package files, and the second command, well, it should be pretty obvious, it installs our dependencies. Now, why are we doing this? Unlike you and me, Docker is actually pretty smart. It has this thing called layer caching. You can basically imagine every step that we write as a layer, and Docker looks at every layer individually.
Speaker A
dot click the container and you can view any logs you can execute any terminal commands and you can see how the container's doing pretty simple now if you think you're too good for a gooey you can also do this in the terminal
Speaker A
So, for layer caching, it goes like this: layer changed, do it again; layer did not change, use cache. That's it. So, the reason we do this for our dependencies is every time we change our code, it's not going to download the dependencies again. It's just going to use the cache, so it basically skips this step, which makes our builds way faster.
Speaker A
Docker that looks inside your Docker image and it generates a detailed report of all the packages that's inside your image and it's looking for any vulnerabilities inside those packages if it detects any vulnerabilities it'll give you suggestions on how to handle
Speaker A
Our next step now is to copy the code into the image. We do that with this command. Wow. Now, this command copies everything, all our files and our folders into the Docker image. But this is a problem since we're copying everything, that also means we're copying our node_modules. That's not good because this file is big, but not as big as your M.
Speaker A
image that's running and once you're there you're going to see this vulnerabilities tab with a button that says start analysis click the button and you're done it's that simple step number nine Docker compose and Docker volumes let's be honest here this note example
Speaker A
We need a way to ignore that file. We can do that with a .dockerignore file, and inside that file, we're going to add the node_modules folder and any other files you don't want in there. On to the next step. In our code, we have this PORT environment variable. We need to add this into our Dockerfile. We can do that with the ENV command.
Speaker A
all of them into one giant container but I don't recommend that a lot of people don't recommend that please don't do that it's pretty stupid now the way people usually do this is they separate each of these services and put them in
Speaker A
Now, since this environment variable is a port, we're also going to need this command. This basically tells Docker, "Hey, we're going to need this port." Now, it's time for the last step: running the server. We do that with this command.
Speaker A
together and manage them this this is a lot of work and a lot of areas could go wrong trying to manage these containers lucky for us a lot of programmers are lazy just like me which is why we have
Speaker A
Whoa, whoa, whoa, whoa! Why'd you do it like that, and why didn't you just use RUN npm start like you did for install? Good question. Act...
Speaker A
we added a database to our node server so the first thing we're going to do is create a composed. yo file and inside that file we're going to create an object called services and inside that service object we're going to be writing
Speaker A
down keys that represent the containers we want to run so in our case our back end and our database since our backend is using a Docker file we created we're going to use the build key and the value
Speaker A
is going to be the location of our Docker file which in our case is the current working directory so it's going to be a period and remember our backend needs port forwarding so we're going to add that in our configuration to and now
Speaker A
moving on to our brand new database container what we're going to put inside of this one is the image key because we're not using a Docker file for this database instead what we're going to do is use a base image of the database we
Speaker A
want to use which for this example I'll go with postgress now now our database also has some environment variables we have to add so we can do that with the environment key and underneath that we just add all the information okay we're
Speaker A
almost done I swear I pinky promise we just need to add one more thing and that's a volume whenever we close our containers we lose all the state and data and our containers right now don't share data between each other so we need
Speaker A
to add a volume what is that all right a volume is basically just a folder on our computer that Docker can access to save any data from our containers and to share that data with other containers and it's pretty easy to create inside
Speaker A
your Docker compos file you're going to copy down this stuff and this line right here says Hey Docker create a volume called postgress data and the other one tells Docker hey we have this volume here this is how you connect to it our
Speaker A
Docker compose file is now complete all we have to do now is run this command in our terminal Docker compose up and Docker will find this configuration file and run all the containers together and if you want to shut down your containers
Speaker A
then run this command Docker compose down and that's all you need to know about Docker compose because I'm too lazy to teach you anything step 10 Docker build Cloud if you noticed building our containers takes a bit of
Speaker A
time even for the simple notes server now imagine how long these containers take for a complex application apparently based on this 2022 survey from incredib build the average developer loses an hour just waiting on their Docker build Docker realized this
Speaker A
problem and recently created something to help reduce build times they created Docker build cloud and the main difference is it builds your containers using the cloud yeah it's it's in the name now when docker's words using the cloud allows your builds to be up to 39
Speaker A
times faster plus if you're working with a team you can share the same cash with them so one person can build the image and now everybody can use that same cash which speeds everything up for everybody so if you have a more complex project or
Speaker A
have a Docker project that takes forever to build you should probably check out Docker build Cloud I'm not going to show you how to do it because I don't have a complex project uh step way okay so I'm not going to lie to
Speaker A
you all I kind of wasted your time you could have just done Docker and N in your terminal and Docker would have created everything for you so uh thanks for letting me waste your time bye
Topics:DockerDocker tutorialcontainersDocker imagesDockerfileapp deploymentdeveloper toolsNode.jsDocker Composeprogramming











