The Only Docker Tutorial You Need To Get Started
AI Summary
Docker solves the classic 'it works on my machine' problem by packaging code with all its dependencies into lightweight containers, making it run consistently anywhere. This tutorial covers Docker basics, including images and containers, and provides a step-by-step guide to Dockerizing a Node.js app.
Code that works on your machine will inevitably fail on others' machines.
Docker is the number one most used developer tool that helps develop, ship, and run applications within lightweight containers.
Docker is like a Lunchable: it packages code, dependencies, and environment settings so it works anywhere.
Images are the recipe (contains ingredients/instructions), containers are the actual meal (runs the code). Multiple containers can be created from one image.
Demonstrates running a Docker image containing the entire game of Doom.
Step 1: Download Docker Desktop. Step 2: Check installation with 'docker --version'. Step 3: Install Docker extension in IDE.
A Dockerfile contains the recipe for building an image. Example: FROM node:22, WORKDIR /app, COPY package*.json ., RUN npm install, COPY ., EXPOSE 3000, CMD ["npm", "start"].
Docker caches layers; if a layer hasn't changed, it reuses the cache, speeding up builds. Dependencies are copied and installed before code to leverage caching.
Use .dockerignore to exclude files like node_modules from the image.
Build image: 'docker build -t my-image .'. Run container: 'docker run -p 3000:3000 my-image'. Port forwarding maps host port to container port.
Use Docker Desktop to view logs, execute commands, and monitor container health. Alternatively, use 'docker exec' in terminal.
A tool that scans images for vulnerabilities and provides recommendations. Access via Docker Desktop's Vulnerabilities tab.
Separate services (e.g., backend, database) into own containers. Use Docker Compose to manage them together.
Create compose.yaml with services (backend using build, database using image). Add environment variables and volumes for data persistence.
Cloud-based builds that can be up to 39x faster, with shared cache for teams.
Docker simplifies development and deployment by packaging applications into portable containers. Mastering Docker is a valuable skill for any developer.
Clickbait Check
90% Legit"The title promises a complete beginner's guide, and the video delivers exactly that with clear steps and examples."
Mentioned in this Video
Tutorial Checklist
Study Flashcards (12)
What is the 'Developer Law'?
easy
Click to reveal answer
What is the 'Developer Law'?
Any code that works perfectly on your machine will inevitably fail on everyone else's.
00:13
What is Docker?
easy
Click to reveal answer
What is Docker?
Docker is a tool that helps develop, ship, and run applications within lightweight containers.
00:38
What is the difference between a Docker image and a container?
medium
Click to reveal answer
What is the difference between a Docker image and a container?
An image is the recipe (contains instructions and dependencies), while a container is the actual running instance created from the image.
01:41
What command checks if Docker is installed?
easy
Click to reveal answer
What command checks if Docker is installed?
docker --version
03:07
What is the purpose of the WORKDIR command in a Dockerfile?
medium
Click to reveal answer
What is the purpose of the WORKDIR command in a Dockerfile?
It sets the working directory inside the container where subsequent commands will run.
04:29
Why should dependencies be installed before copying code in a Dockerfile?
hard
Click to reveal answer
Why should dependencies be installed before copying code in a Dockerfile?
To leverage layer caching: if code changes but dependencies don't, Docker reuses the cached dependency layer, speeding up builds.
04:54
What is the difference between RUN and CMD in a Dockerfile?
hard
Click to reveal answer
What is the difference between RUN and CMD in a Dockerfile?
RUN executes commands during image build, while CMD specifies the command to run when the container starts.
06:03
What does the -p flag do in 'docker run -p 3000:3000 my-image'?
medium
Click to reveal answer
What does the -p flag do in 'docker run -p 3000:3000 my-image'?
It maps port 3000 on the host to port 3000 in the container (port forwarding).
06:54
What is Docker Scout?
medium
Click to reveal answer
What is Docker Scout?
A tool that scans Docker images for vulnerabilities and provides recommendations.
07:38
What is Docker Compose used for?
medium
Click to reveal answer
What is Docker Compose used for?
It manages multi-container applications by defining how containers work together in a compose.yaml file.
09:03
What is a Docker volume?
medium
Click to reveal answer
What is a Docker volume?
A folder on the host machine that Docker can access to persist data and share data between containers.
10:13
What command starts all services defined in a Docker Compose file?
easy
Click to reveal answer
What command starts all services defined in a Docker Compose file?
docker compose up
10:38
🔥 Best Moments
Developer Law invented
The creator humorously claims to have invented the 'Developer Law' on the spot.
00:26Running Doom in Docker
Demonstrates Docker's power by running the entire game of Doom inside a container.
02:09Time wasted confession
The creator admits the whole tutorial could be replaced by a single 'docker init' command.
11:39Full Transcript
Download .txt[00:00] Nice, but 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. 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.
[00:13] 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 developer law. Any code that works perfectly on your machine will inevitably fail on everyone else's.
[00:26] I made that up by the way. What if I told you there's a way to prevent this? This is Docker. Docker? I hardly know her. So what's Docker? Well, it's the sponsor of today's video. Oh, no wonder.
[00:38] Docker is the number one most used developer tool based on the Docker model. That helps you develop, ship, and run applications within lightweight containers. What? Okay, so Docker is actually pretty simple and something you should know.
[00:51] 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. Yeah, that makes sense. Plus, it's a skill that's in a lot of job posting, which means learning Docker will make you money. Yeah, that makes sense.
[01:03] What makes Docker so good? Docker essentially allows us to package up our code. Imagine Docker like a Lunchable. The Lunchable has a trash code, dependencies like Node.js or Python, environment settings, and everything else needed to run your trash code.
[01:15] 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. 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.
[01:29] No need to install anything. Docker does it all for you. 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.
[01:41] You can think of images as the recipe. It contains all the ingredients and instructions. 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.
[01:55] A container is like the actual meal. It's what gets made from the recipe. Now the cool part about containers is 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.
[02:09] 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 allows us to simplify the process of sharing code.
[02:21] 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. Yeah, that makes sense. I simplified this explanation a lot, because a lot of you have a shorter attention span than a goldfish.
[02:35] 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.
[02:50] And did I mention it? Now that you understand why Docker is important and how it works kinda let me show you how to actually use it Step 1 Download Docker Obviously go to docker or click the link in the description and download Docker Desktop It download Docker for you and it comes with a nice little GUI to manage your images and containers
[03:07] But if you're feeling like a grown-up, it also comes with the Docker CLI, so you can do everything in the terminal. Step 2. Check if you have Docker. Open up your terminal and type docker-version. If you see this, you have Docker installed. If you don't, then, uh, good luck little bro. Figure it out.
[03:21] Step 3. Set up Docker on your IDE of choice. I'm doing VF 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'll make your life so much easier. Step 4. The Dockerfile. This is pretty important.
[03:33] 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. Java server. 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.
[03:47] Now, in order to write the steps, you need to know the steps. So you need to understand how to run this server. But since we're using Java Server, it's pretty easy. First you get your code, then you go into the file with directory, download your dependencies, and then you run the server with the npm sub command. Bada ding, bada boom, easy piece.
[04:00] Now that you understand how to run the server, all we have to do is write all that in our Docker file. What? So at the top of your Docker file, you're going to write from. And if you downloaded that Docker extension I told you to get, let's pick this boy you are. If you hover over it, you're gonna get some nice documentation.
[04:14] Every Docker file starts with this command. Now we have to select the 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 Docker file, we're going to write from Node colon 22.
[04:29] 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 slash app. Workdir stands for working directory. And this is basically us telling Docker, hey, start at this point. This is where our code is going to be. The next step is a very important step.
[04:42] So you better listen to this step. Usually in a Node project, we get our code and then download our dependency. 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.
[04:54] 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 installed our dependencies. Now, why are we doing this? Unlike you and me, Docker is actually pretty smart. It has this thing called layer caching.
[05:06] You can basically imagine every step that we write as a layer. And Docker looks at every layer individually. So for layer caching, it goes like this. Layer change, 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 gonna download the dependencies again.
[05:22] It's just gonna use the cache. So it basically skips this step, which makes our build way faster. 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.
[05:34] 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 mod- We need a way to ignore that file. We can do that with a Docker Ignore file.
[05:46] And inside that file, we're going to add the node module 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 Docker file We can do that with the EMD command Now since this environment variable is a port we also gonna need this command This basically tells Docker hey we gonna need this port
[06:03] Now it's time for the last step, running the server. We do that with this command. Whoa, whoa, whoa, whoa, why'd you do it like that? And why didn't you just use runnpm start like you did for install? Good question, actually. The reason is because the run keyword happens when we're building the image,
[06:15] while the cnd 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 5. Build in the image. We're going to have to be grown-ups here in Unoterminal.
[06:28] To build your image, we use the Docker build command. So inside your terminal, you're going to type Docker build dash T. T stands for tag, and it basically means give your image a name tag. Understand? So give your Docker image a name. You can name it whatever you want.
[06:41] The last thing we're going to add is the path to our Docker file, which in our case is the Tramp working directory. So all we have to do is type a period. Run this command and you're gonna see Docker running all the steps we wrote in our Docker file. Step 6. Running the container. We can do that with the Docker run command.
[06:54] Make sure you write the name you gave your image. Hey, what's that dash P? This dash P is for port forwarding. You can do your own research for that, but for now, think of it as a bridge between our computer and our container. The number on the left is the port for our computer, and the number on the right is the port for the container.
[07:07] If you don't add this part, it's not gonna work. Go ahead, try it. I dare you. Run the command and you're gonna see that your Node server is now running. Congratulations, buddy. You know the basics of Docker. I'm proud of you. Step 7, debugging. Even though Docker makes it easier for us to share code, it doesn't stop us from building our containers wrong or from writing trash code.
[07:25] Luckily, debugging and checking any logs is pretty easy with Docker Desktop. So, open up Docker Desktop and look at the container that's running. It should have a little green 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.
[07:38] Now, if you think you're too good for a GUI, you can also do this in the terminal with the Docker exec command. Step A, Docker Scout. Now if you notice, when you ran the Docker run command, Docker gave you a recommendation to run this command, Docker Scout QuickView. Now what's Docker Scout? It's a tool created by 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 them. Using Docker Scout is pretty simple. You can do it with a terminal or with Docker Desktop. I personally recommend Docker Desktop. It's really easy. Let me show you how to use it. All you have to do is go to your images.
[08:12] Click on the image that's running, and once you're there, you're gonna see this Vulnerabilities tab with a button that says Start Analysis. Click the button, and you're done. It's that simple. Step number 9. Docker Compose and Docker Volumes. Let's be honest here. This node example was really simple.
[08:26] Nowadays, babies in the womb can do this example. What if we wanted to add a database? What if we wanted to add a frontend? How do we add that with Docker? Now, you could combine all of them into one giant container, but I don't recommend that. A lot of people don't recommend that.
[08:39] 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 their own containers. And this is called a multi-container application. What an obvious name right But this creates a brand new problem Now that you have multiple containers you have to run those containers separately and you have to find a way to connect them all together and manage them This is a lot of work and a lot of areas could go wrong trying to manage these containers
[09:03] Lucky for us, a lot of programmers are lazy, just like me, which is why we have this tool called Docker Compose. Docker Compose is pretty easy to understand. It basically tells all your containers how to work together to create the full application. Let me show you a quick example on how to use it.
[09:16] Let's pretend we added a database to our Node Server. So the first thing we're going to do is create a compose.yaml 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 down keys that represent the containers we want to run.
[09:30] So in our case, our backend and our database. Since our backend is using a Dockerfile we created, we're going to use the build key. And the value is going to be the location of our Dockerfile, which in our case is the current working directory. So it's going to be a period. And remember, our backend needs port forwarding.
[09:44] So we're going to add that in our consideration too. And now 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 want to use, which for this example, I'll go with Postgres.
[09:59] 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 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.
[10:13] And our containers right now don't share data between each other. So we need to add a volume. What is that? Oh, 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.
[10:26] And it's pretty easy to create. Inside your Docker Compose file, you're going to copy down this stuff. And this line right here says, hey, Docker, create a volume called Postgres Data. And the other one tells Docker, hey, we have this volume here. This is how you connect to it.
[10:38] Our 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, then run this command. Docker Compose Down.
[10:51] And that's all you need to know about Docker Compose, because I'm too lazy to teach you anything at Step 10. Docker Build Cloud. If you notice, building a container takes a bit of time, even for this simple node server. Now imagine how long these containers take for a complex application.
[11:03] Apparently, based on this 2022 survey from IncrediBuild, the average developer loses an hour just waiting on their Docker Build. Docker realized this problem and recently created something to help reduce build times. They created Docker Build Cloud.
[11:15] And the main difference is it builds your containers using the cloud. Yeah, it's in the name. Now, in Docker words, using the cloud allows your builds to be up to 39 times fast. Plus, if you're working with a team, you can share the same cache with them.
[11:27] So one person can build the image, and now everybody can use that same cache, which speeds everything up for everybody. So if you have a more complex project or have a Docker project that takes forever to build, you should probably check out Docker Build Cloud.
[11:39] I'm not going to show you how to do it because I don't have a complex project. Uh, step in your way. Okay, so I'm not gonna lie to you all. I kind of wasted your time. You could have just done Dr. Init in your terminal, and Dr. would have created everything for you. So, uh, thanks for letting me waste your time.
[11:52] Bye.