Summarify.net

The intro to Docker I wish I had when I started

Ud7Npgi6x8E — Published on YouTube channel typecraft on July 15, 2024, 12:00 PM

Watch Video

Summary

This summary is generated by AI and may contain inaccuracies.

- For the longest time, Docker was a tool that I used sparingly throughout my whole career. In this video, we're going to cover virtualization and containerization and what the difference is between the two. This video is a gentle introduction to Docker. - Speaker A tells Docker an important thing to learn in the web development career because most web developers use containers and containerization technologies every single day of their career. But what is a container. - In order to talk about containers, we should talk about virtualization first. Then we talk about how virtualization works in a typical sense and how it differs from containerization. Finally, we see containerization through Docker in work. - Docker is the management layer that will manage the lifecycles of all of our containers that we want to create. Docker gives us a helpful command that we can run to give us our first taste of what Docker will do for us. - We understand the relationship between a dockerfile and a Docker image and how a docker image is used to then run a docker container. Then we try out a real world example to get a feel for how the Docker CLI works. - We have built an image that has all the programs installed in it, and it runs the script that prints out ascii text to the screen. Now we want to modify the script and create a new image with a different tag.

Video Description

Part TWO (docker-compose): https://youtu.be/HGKfE-cn9y4
X: https://twitter.com/typecraft_dev
Learn is 50% off! https://learn.typecraft.dev

Sign up for Docker Hub: https://dockr.ly/3Y30xG5
Download Docker Desktop: https://dockr.ly/46cgv2H

Docker is an amazing piece of technology. And I'm sad to say, I took it for granted for many years. This is the video I wish I had when I started learning Docker. We'll be talking about virtualization, containerization, and the difference between the two (because this stuff was messed up in my head for a long time). Before you know it, we'll cover Dockerfiles, images, and building containers.

So join me in a gentle introduction to docker. Let's get into it.

chapters:
0:00 - intro
1:30 - Let's talk about virtualization
3:12 - Let's talk about containerization
4:50 - Let's install Docker!
7:00 - What the hell is a Dockerfile? An anatomy
9:27 - How Dockerfile, images, and containers relate
11:20 - Let's create our first image and container
15:29 - Tagging a new version of our image

Transcription

This video transcription is generated by AI and may contain inaccuracies.

For the longest time, Docker was a tool that I used sparingly throughout my whole career. I mean, I'm a ruby on rails dev. I don't have to know Docker. I can just run all my services locally on my machine, right? I can even remember throughout my career, if anyone ever mentioned that we needed to use Docker to run something, I would always say Docker. Barely even know her. And as hilarious as that joke is, I was missing out on a core foundational piece of technology that I should have known about all along. In this video, we're going to cover virtualization and containerization and what the difference is between the two things, because for me, this was always messed up in my head. We're also going to cover Docker files, images, Docker containers, and how they all fit into the grand scheme of Docker. This video is going to be a gentle introduction to Docker, and we're just going to be scratching the surface. So stick around. This is going to be a fun one. So why is Docker an important thing to learn in the first place? Well, you see, you probably use containers and containerization technologies every single day of your career. If you're a web developer, even if you don't realize it, containers are a way to build reproducible, lightweight environments for processes to run. And we use them everywhere in continuous integration and continuous deployment pipelines like on GitHub, actions that you probably use all the time. And we also use it whenever you're deploying to a server. If you deploy something to the cloud, chances are you're interacting with container technology somewhere along the way. So I think it's a pivotal thing that you need to learn in your web development career. But wait, what the hell is a container anyways? In order to talk about containers, we should talk about virtualization. Now, bear with me for a minute, because these two things are very closely related, and I think it's important to understand the distinctions between the two technologies. So let's talk about virtualization. So let's draw up how virtualization works in a typical sense, and we can talk through it. Now, typically when it comes to virtualization, you start off with a host machine. This could be your host. Now, the host could be anything. It could be your local PC. It could be a server up in the cloud, server in a data center somewhere. Whatever it is, it's a piece of hardware. Now in this piece of hardware, you have different things that control how this hardware works. You have things like your cpu, you also have things like memory, and you have your hard drive. We'll call this IO. Now, when it comes to virtualization, what happens is we take little pieces of each of these pieces of hardware and separate them out into a separate machine. This is a virtual machine. And then we take these pieces of hardware, and in this virtual machine we actually run a full entire operating system. Now this technique is commonly used in the cloud. If you're deploying something to AWS or an EC two instance, typically what you're doing is you're spinning up a new virtual machine that you can then deploy your code onto. Now, virtual machines have a special type of program that can run and manage the lifecycle of these machines. This program is called a hypervisor, and the hypervisor is in charge of virtual machines. It manages the lifecycle. It starts them up, it stops them, it creates them, it deletes them. It provisions resources for them. That is what the hypervisor does. Now, a common hypervisor that you would be aware of is vmware or virtualbox. These are the programs that control the virtual machines. Now, virtualization is similar, but it differs from containerization, which is the thing that Docker is kind of based around. So let's talk about containerization. In a container setup, what you would do is you would have a host PC, much like the virtualization setup that we set up. Now, let's say on this host PC we want to run a set of processes, but we want these processes to run in isolation. We don't want them to touch anything else. Now we can achieve that using some techniques, right? Let's say we want these processes to run on this machine. Processes. What we do is we can use some techniques like the ch root command, which will create a new root for a process. So it can only live inside that route and it can't touch anything outside of that like any of the other users directories or things like that that are already on the system. We could also use a kernel feature like the r limit feature, which will limit, limit the amount of resources these processes take up. These techniques, amongst other things, will encompass what is containerization. Now, with containerization, you could do all this manually yourself, but it's really difficult and pretty tricky. So there are programs that help manage the lifecycle of your containers. This is where Docker comes into play. Docker is a program that manages the lifecycles of containers, edit them, run them, and interact with them. So to sum up, containerization is the ability to create a lightweight environment where processes can run on a host operating system. They share all the same things in that operating system, but they cannot touch anything outside of their little bounded box. Okay, so we've talked enough about virtualization and containerization. Let's see containerization through Docker in work. Let's get our hands dirty. So now let's install Docker again. This is this portion of our graph right here. This is the management layer that will manage the lifecycles of all of our containers that we want to create. That's what Docker does for us. Now to install Docker. There are examples of how to install it on the Docker website. For me. I use Arch Linux by the way. So I am going to use Pac man to install Docker and it's as simple as that. Docker is now installed. So how do we know that Docker is even running correctly on our system? Well, Docker gives us a helpful command that we can run to sort of give us our first taste of what Docker will do for us. We can do Docker space, run space. Hello hyphen world. Let's see what happens. We're going to break down this command a little bit, but let's just see what happens now. Okay, a lot of stuff just happened. Let's go through this line by line and let's see what Docker is telling us that it did. Now to start off with, Docker was unable to find the image hello world latest. Now this is the name of our image and this is the tag of our image. Now in Docker speak, an image is basically the thing we run our containers from. I'll explain it again in a second. And latest is the tag of that image. By default, Docker tags its images with the latest tag so darker was unable to find the image hello world latest locally. So it pulls it from a repository. Docker will pull images that are already known from Docker Hub. We can actually check out Docker hub by going to hub dot docker.com. and this is where you can see all of the images that Docker already has pre built in this platform here. Docker hubs. This is where you can explore. If I wanted to look for like let's say a postgres image, here it is right here. I can use the postgres image from Docker hub. I don't have to build one myself. So Docker hub is very helpful and that's what it does here pulls the hello world image from Docker hub. And as you can see here it says status downloaded newer image for hello World latest. Awesome. And then it says hello from Docker this is the actual image running a container. I'll explain this in 1 second here. This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps. The client contacted the Docker Damon. The Docker Damon pulled the hello world image from Docker Hub. That's what we were just talking about. And then the daemon created a new container for us from the image. Okay. And now the Docker daemon streamed that output to the Docker client, which sent it to your terminal. Okay, let's unpack this a little bit. You might be asking yourself, what the hell is an image? And how does docker know how to build these things and run containers from images? What is all this stuff? Now before we talk about how to build images and how to then run containers from the images, we have to talk about something called a dockerfile. This is an example docker file, and it's pretty contrived. But essentially what you would have with any project, anything you want to build an image out of is you would have a directory structure that looks something like this. In this contrived example, we have a directory that contains a dockerfile, and within the dockerfile we have a coffee recipe folder. Let's just imagine that this is a coffee recipe application of some sort. In this coffee recipe folder we have two scripts, prepare beans and brew coffee. Okay, so now let's talk about what this docker file is doing. You can see on this very first line we have from Ubuntu latest. Now you might remember this terminology from not too long ago. That means that we want to use the Ubuntu image at the latest tag. This is the terminology for an image and the tag for that image, Ubuntu latest okay, great. The next line says we want to run apt get update and apt get install some contrived package. What is this? This line tells Docker that we want to run something on the image that was added above. So we're running apt get update and apt get install on our ubuntu latest image. So this runs command on the image. Next we want to do is copy the coffee recipe directory from our local file system into this image. So what we want to do here is basically just copy things from our local directory. Again, you can remember our directory structure looks like this. We have the coffee recipe directory, and under this we have a couple of scripts. So we want to copy the coffee recipe directory into this image. Next we want to run in our image the script prepare beans, sh so this will run that script. Now remember, since we copied the directory into our image, we will have this available to us because prepared beans is right here. Oh look, I misspelled it. How fun. And then next we have this line that says command. Now command is the default command that this container is going to run. This can be overwritten in the CLI, but by default we're going to run the brew coffee command. This is the default command for our docker image. So when the docker container runs, it's going to run this default command. So now let's zoom out a little bit here. What we are going to do is we are going to take in this contrived example, this dockerfile. And from this dockerfile we are going to build a docker image. Now this image, we could name anything, but let's just call it coffee, right? This image by default will create, will be called coffee. At the latest tag you can give an image whatever tag you want. But again by default Docker gives the tag of latest to every image. Now this image is almost like the file system for the container to run, and it's immutable. You can only build one image, you don't change your images. What you do if you want to change anything is you change your dockerfile and then build a new version of your image for containers to run. Now from our image, what we want to do is we want to call Docker Run and that will then run our container and our container. As we said before, we'll call the run command that we specified in our darker file, which was brew coffee sh. And again, to actually build the image we want to run Docker build. And there are certain flags we can pass to build. Like I said, to change the tag of the image from the default of latest, you can also name the image whatever you want. But this is generally the process for Docker. We want to create a docker file in some repository or in our directory. We want to then use this dockerfile as the instructions to build a new image for Docker. Then we can run this image and docker will spin up a new container on our system that is not able to touch anything else within our system and it will run whatever code we want it to run using the command flag. So that in a nutshell is how docker sets up dockerfiles, images and containers. Okay, so now we have all of our core concepts in place. We understand the relationship between a dockerfile, how a Docker file is the instructions to build a Docker image and then how a docker image is used to then run a docker container all underneath the umbrella of the Docker ClI. So let's get our hands dirty and actually try out a real world example. This is going to be a simple example. It's a contrived example yet again, but we're actually going to run a real container on our system to get a feel for how the Docker CLI works. Now let's just say I have a directory called Docker example, and in this directory I have two files, a docker file and a print message sh file. Let's take a look at them. Let's start first with our print message sh. We can see that is a bash script and then it has a variable that has a list of phrases. These phrases are then randomly selected and we print them out to the terminal using a program called Figlet. Now what if I don't have Figlet installed on my local machine? Well, that's okay. That's why we have this docker file. So in our dockerfile what we want to do, as we've seen before, we want to use Ubuntu latest. That means we're using the latest version of the Ubuntu image from docker, probably from Docker hub. Then on that image we want to run apt get update and apt get install figlet and wget. We're going to use wget in the next line where we run wget and we wget some fonts that we want to then install on our system. Then we want to copy our local print message script into the container for printmessage sh. This is what we're actually going to run. Then we want to chmod plus x printmessage sh. That just makes this script executable inside of the container. And our final command is to just run printmessage sh. It's a pretty simple docker file, but this will give us an understanding of how to use these things in real time. So now that we're within this directory that has a docker file, we can call from within the directory Docker build. Now in Docker build we could tag that by default. It will tag with the latest release and we can call this ASCII. And we want to make sure we build everything in this current directory. This will build our very first image. Let's see what this command does. Okay, this was a lot of stuff, but let's just go through it really quickly and see what we did here we can see that it sent the build context to the Docker daemon. And then we did the steps that were in the dockerfile. We can see the Dockerfile working for us. Step one out of six from Ubuntu latest. Now it pulls from library Ubuntu. This is probably pulling from Docker Hub. We then run apt get update and apt get install these couple of programs onto our Ubuntu image. Very good. We can see that that is running and that's what all of this output is. And then in our next step, step three, we want to run wgethouse for these files. Now these files were then taken and saved into this image. Very good. And now in step four out of six, we copy our local print message script into the image. Then we run Chmod plus X which will make it executable. And at the very end we want to make sure our default command is just by running this print message script. Cool. Okay, so now we have our image built. How do we run the container based on this image? Well, what we can do is we can check what images we have currently built on our system. With the Docker images command, we hit enter and we can see that amongst a couple others like Ubuntu and hello world. We have our ascii image tagged at the latest tag with an image id created about a minute ago. And that's what we have right here. We built our image. I'm going to make my font smaller here because we're printing out ascii text and it's going to be pretty large on the screen. You'll see what I mean in a second. But basically what we want to do is now that we have our image built, we can run Docker run and then type the name of the image which is ASCII. We could also optionally add the tag, which would be latest. And I'll just add it right here. If we do this and enter them this command, we can see that it actually runs the container and it runs the script that prints out ascii art to our screen. Isn't that awesome? So basically what we've done here is we have now a docker file that installs things into an image. We have an image that has all these programs on it like Figlet, Wget and everything else. And then it has a script inside of that image as well. Now the container runs and the containers default command is to then print this stuff out to the terminal. And that's what it is we just did that awesome. Now we can keep running this command, we can keep running this docker container, and it will just randomly select another thing. Apparently it was the same for a bunch of those, but it will select random sayings and then print them out in an ASCII text to us. Very cool. But now let's say we want to modify this script and we want to print out different things. What do we do now to update this image? Well, images are immutable. What you're going to have to do is edit your docker file and then create a new image, probably with a different tag. Let's get into that. So we see we have our dockerfile here and we have our print message. Sh. Let's just say we want to change some of these phrases. This is fun. I love docker. Cool. Okay, so now we've changed the script that actually prints the messages out. What we're going to want to do is build a new image from this docker file because this new image is going to contain different things. Okay, so let's do docker build dash t. And what we want to do is we want to name this ascii with a colon. The colon is going to denote the tag, and I'm just going to call it different. We can call whatever we want and we want to build it from our local directory. Now let's hit enter so we can see here we get very similar output to what we had before. We probably get less output because we already have the Ubuntu image installed. And we already have these programs installed in the Ubuntu image that Docker is using to build this image. But we can see down below, we've successfully built this new image, which has a new id. And there's a new tag called ASCII different. Very cool. All right, so now let's check docker images to see what this looks like. Okay, great. We can see that we have ASCII's latest build, which isn't currently the latest because we tagged it with something different. But that's okay for now. And we have our new ASCII different tag. So this tag is a snapshot of our docker file that was built into an image. So now let's run our new image in a container we can call Docker run ASCII. And we want to add the tag of different. Let's see what happens. You can see it pushes out the different text that we did. Now we have different sayings that we're putting out there, like I love you or I love Docker. Let's make this a little bit smaller so we can see it a little bit better. But I love docker. This is fun. I love you. These are all the different messages that we put in our print message sh. Now, the cool thing is we still have the previous version of this image. Like I said, they're immutable. You don't change the images, just create new ones. So we can also say docker, run ASCII latest. And that was the previous one that we built, which has all of these Star wars things in there. Let's make this smaller so we can run both of our containers based on the images that we've built. The images are immutable. You don't delete them, you just create new versions of them. Very cool. So what have we learned here? Well, I think we've learned a lot. We've learned about the difference between virtualization and containerization, and we've also learned about Docker as a whole and how docker files, images, and containers all relate to one another. But this is just the surface of the surface that we're scratching here. If you want to learn more about Docker, like Docker compose, mounting, docker volumes, or even doing things like port mapping in Docker, then leave a comment down below of what you would like to learn next. And hey, thanks, nerds.