Modern Python Projects Transcripts
Chapter: Deployment
Lecture: Building a Docker image

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Before we can build our Docker image, make sure that we actually have Docker Desktop running.
0:06 And if we do around Docker build -t uptimer-website. command And what this command does is that it will build a container and given the name
0:18 uptimer website, and it will use the Docker file from the current directory. That's why we use this dot at the end. Now we have to wait for a bit,
0:28 depending on the speed of your Internet connection. It might take longer to pull the Docker image from the Docker repo,
0:33 but do not or later we will be done. Since I was playing with Docker a bit before recording this part,
0:40 you can see that I'm using the cached layers in Step 2, 3,4,5 and then since I
0:47 made some modifications the last step so the copying is actually being executed. But since copying files doesn't take that much time,
0:55 it's actually going to go very fast. So now that our images built, we can run it with the Docker run command.
1:08 This command will run our uptimer website image and it will map port 80 from the container to port 80 on our computer.
1:17 If you forget to add this -p 80:80 parameter, your Docker container will be running. Your application will be running in the Docker container,
1:26 and it will be accessible on Port 80. It's just the container won't be exposing port 80 to you, so you won't be able
1:34 to access that website from your computer. You would have to somehow log into the container, and then you would be able to see it.
1:42 Once you run it, you can see a few things, first We are using the IP on Port 80 from inside of the container. Due to the nature of containers,
1:52 it's usually a good idea to use ports instead of Next. As you can see, we're putting three workers,
2:03 which is more than needed for our local host example. But I just wanted to show you how we can use more workers to handle more
2:10 traffic in the future. So now we should be able to go to the browser and access Port 80 on our local host.
2:28 Perfect. So that's the up timer running in the Docker. If I stop it, you can see that it's no longer accessible.
2:38 And just to show you what happens if we forget this parameter,
2:42 let's remove that. Everything looks the same because this is the output from the container But if we try to access this website in the browser,
2:51 it's not accessible. Okay, so we have a Docker file, and in the next lesson, we will try to deploy it somewhere. But before we wrap up this lesson,
3:01 I just want to show you what happens if you don't separate. The requirements.txt from the rest of your code. So if we go back to the Docker file,
3:11 I mentioned that we first copy the requirements .txt we installed them and then we copy the rest of the code.
3:19 Now, if we go and change one of the files, usually template is the best idea. And after each change, we have to rebuild our Docker image.
3:32 So let's around the build command again. And as you can see, we are using caching all the way until the copy step. If we moved things around,
3:43 for example, if we just copy all the files here and then we run pip install and then we run gunicorn. Let's let's also remove this thing.
3:58 Everything will work fine. We will be able to build our Docker file. It's just each time you change one of the files,
4:05 you will be rerunning the pip install Step over and over again. Let me show you what I mean. If we go back here and we rebuild Docker,
4:16 you can see everything is cache until the copy step. But now we are running pip install. So as you can see,
4:23 it's already 20 seconds and the previous and previously it took less than 4 seconds. Okay, I will stop this and revert my changes.
4:41 So using caching correctly using non root user and things like that are some of the things that you have to remember when used Docker.
4:50 So, if you want to learn more, I suggest that you read the Docker documentation. They also have the section with like,
4:56 best practices where you can learn the basics of how to write good Docker images.
5:01 But for now, let's move on And let's publish our Docker image in the Docker hub.

Talk Python's Mastodon Michael Kennedy's Mastodon