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