#100DaysOfWeb in Python Transcripts
Chapter: Days 97-100: Docker and Docker Compose
Lecture: Building a Docker image

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now it's time to build our own containers. It was fun to run Bash on Ubuntu on my Mac but it was kind of useless, okay?
0:08 So really to take advantage of these things you pretty much have to do a little bit of customization. Sometimes just a tiny bit.
0:16 You just want to fire up a database server but maybe you need to say here's the volume you're going to use to save your files or something like that.
0:23 Or maybe you need to copy your source code and install a bunch of package dependencies and things like that.
0:29 Remember back here in the deployment section when we deployed our bill tracker and we had this server script here and it had all these steps.
0:38 Well what we're going to do is we're going to go apt-update, then we'll go apt-upgrade. Then we're going to install the build essentials
0:45 and then we're going to install Nginx and we're going to do all those things. We're going to do the same thing over here with our dockerfile.
0:53 So what we're going to do is we're going to have a new file called Dockerfile, like that. We're going to come over here and say
1:01 associate that with a Dockerfile like that. Now PyCharm, because of that plugin I told you about can actually run these things and do all sorts of
1:09 interesting stuff, and in addition to giving us help like you can get a little run thing that will build and run it and update it and so on.
1:17 That's pretty cool and I actually kind of like it. But I'm going to stick to running stuff over here in the terminal, so it's super clear for you
1:23 what's happening. Okay, so the first thing that we need to do when we're building a Dockerfile is we need to go
1:30 and actually say, this image that we're building we're defining a set of steps, layered steps that build; each layer is going to build a new image
1:40 based on the previous one, okay? So we have to start out with from, so we're going to say from Ubuntu...
1:49 latest, or whatever image you found over on DockerHub or you built yourself here and is on your system you can put that here as the foundation.
1:58 And then we're just going to go step by step by step to update things and make it go. So we're going to run some commands on the container.
2:06 We're going to run apt-get update and we're going to run apt-get upgrade. Remember those vulnerabilities?
2:17 By the time we get to line four, this is going to be updated and fixed at the time of the build. Now there's also a couple of little nice utilities
2:25 that I like to have on my systems. Let us log in, let us ask more information about the running processes and so on. So I'm going to add those here.
2:36 We're going to have some more apt installed and do another run apt-install y, so if there's any questions You sure you want to install?
2:44 Yes. We do that and then quite. We're going to install a few things. Out of the box we don't get sudo. We're going to have Fail2ban
2:52 in case people start hacking away on it. We're going to have HTTPie because I like it much better than Curl.
3:00 We don't have Curl either, so you'd have to install that. The last one, Glances. We're going to hold off on Glances for just a minute.
3:08 So let's go ahead and run this and let's see first if we go over here again. I'm going to try to docker run, remember we had this?
3:18 Let's try to do HTTP. Command not found, of course because it comes from this install. So let's go and build instead let's go and build this one.
3:29 So I'll do a little save. And let's change into the front end. We have our Dockerfile. We'll just say docker build.
3:39 There's probably better choices here but this says Build this Directory. It goes and finds the Dockerfile and then off it goes.
3:47 So, notice it didn't go and get Ubuntu again. We already had it, and now it's running an update and it's finding the issues. Oh, we need a -y here.
4:00 So what you're going to find this is kind of an iterative process. You make your way through Oh, we've made it to line three
4:05 something went wrong with line three we try again. Reset, run it again. So we're running apt-update. Now we're running apt-upgrade.
4:20 And these are going to be persisted into a new image. That worked, so we're installing. We're installing Fail2ban right now.
4:31 I'm not sure actually if Fail2ban is going to run in the background, so maybe it's not use useful but certainly HTTPie is going to be helpful
4:38 and Glance is if we want to install that. So when we log in, if we would try to debug it it's a little easier.
4:43 So notice it's built this container right here. So now if we say docker images sorry, not a container, an image called E8 something.
4:52 So we have two, but notice each step there was some... Here we did a little step up here. We built an intermediate container and so on here.
5:03 So if we go over here and we do a -a you can see some of these intermediate containers no one ever asked for, but they were built along the way.
5:11 So now, if we try to build this again it should go super fast. Look, all these steps are already done. Well good, it's cached.
5:19 We can go ahead and run it, so let's go and try to run; you don't need the whole container id you can just say E8 as long as it's distinctive
5:26 so we could say Docker run in interactive mode that bash. Okay, it's running. The name here is going to be the container ID.
5:39 And we can ask things like, what's running which is Bash, but we could do HTTP.com. Hey look, it works!
5:47 Why does this system all of the sudden have HTTP installed? HTTPie? Because one of the layers was created in the Ubuntu system
5:55 based on the layers above, that has this installed. So we don't have to do that again. So here's how we build one of these images.
6:03 And what we're going to need to do is install some more stuff. How are we going to get our movie exploder code over there?
6:11 How are we going to get Nginx installed, and so on.


Talk Python's Mastodon Michael Kennedy's Mastodon