#100DaysOfWeb in Python Transcripts
Chapter: Days 97-100: Docker and Docker Compose
Lecture: Nginx config for our static site

Login or purchase this course to watch this video and the rest of the course contents.
0:00 For us to do interesting things with Nginx, we need to give it interesting configuration files. We could somehow cram that in here,
0:09 but what makes a lot more sense is to just have an Nginx configuration file and then copy it to the server. So, let's go over here and have a new file
0:18 called site.nginx. So we have our Nginx, and I'm just going to paste this in and talk you through it real quick.
0:24 So what we're going to do is we're going to listen on port 80. We're going to listen on movieexploder.com. Now, that doesn't really exist.
0:32 Maybe it does, but it's not going to, it has nothing to do with us. But we can hack our host files so that we can test with this.
0:39 We're going to create a directory where we copy all of our working code to slash app. We don't need to be super-careful about where
0:46 this goes, because this Linux configuration is literally only for running this app. And it's a container and it gets thrown away.
0:54 So, we could have our gzip buffers on, but we can leave them off. So we need to configure a few things. css is just static, js is static,
1:03 and node modules are static. And over here, we're going to say we would like the index/ to actually map over to /view/index.html.
1:15 Now, this is super-confusing. That is a web root, not a file path. This is not like /views in the hard drive. This is / in the virtual,
1:24 sort of relative sense, to where the app root is. Okay, so when you ask for a /, that's going to be treated as an index command,
1:32 which just returns HTML, which is that relative to the root. So debugging this stuff turned out to be pretty tricky, but I think this will be good.
1:41 I've already tested it out. So our goal is to copy that over to the server. So we're going to issue a copy command relative to the directory here.
1:51 And notice we automatically get auto-complete even for our files, I so love it. And then what are we going to copy it to?
1:58 I copy it to etc Nginx sites enabled /site.nginx. Okay, this got created on our server, or not on our server, on our Docker machine
2:11 because we installed Nginx. So we're going to copy that over there. And this is how you configure Nginx
2:16 is what we talked about in the deployment chapter. We're just taking those deployment commands and putting them into Docker commands.
2:22 All right, so that's fine. That'll copy that over. We're also going to need our code to do anything interesting when Nginx runs.
2:29 So we're going to do another copy, and this time we're going to copy the directory. We're going to copy Movie Exploder to /app.
2:35 Okay, so that is pretty cool. And see if we can get this to run. Let's go ahead and make a change here. Now, notice these copying of files probably
2:46 should come after the installs as much as possible, right? These, you want to cache that stuff. That takes a long time.
2:52 If a file changes, like if that file changes, these steps down here have to be re-run. Okay, let's do this. Rebuild it, a bunch of stuff was cached.
3:03 See how quick those copies were? That's great. So now we can say Docker run interactive E five bash. Okay, and we can see what's here.
3:14 Oh, look, there's your application. And we could go and do a request against it. Now, Nginx is not running.
3:22 So what we need to do is we need to issue it a command. And the command that we can give it is nginx -g "daemon off;"
3:31 Previously what we did is we'd registered as a system service, but that's not how Docker containers work. There's one thing that runs.
3:37 And the container's alive while it's running. When it's done, it goes away. So we're going to use this command to run Nginx.
3:43 Before we do that, let's do a test. Yeah, it looks like our configuration file is okay. Now if we do a slash T, and that found it.
3:52 Okay, well, let's go ahead and try to run this, see what happens. Looks like it's working. Is there a way to get out of it?
4:01 Yes. All right, so this is a blocking command, right? We want to run this on our container. It's going to just block.
4:05 If it exits, the container goes away. But I want to be able to make a request really quick, so let's do that.
4:11 Ampersand on the back here and run it in the background. We run Glances, now you can see there's a whole
4:16 ton-load of worker processes for Nginx doing their thing. So that's pretty sweet. So if we go over here, now we see ACP local host. Look at that.
4:27 Well, now you're seeing the Nginx configuration page. Close. If we could change our host file to go to Movie Exploder, we could probably get
4:35 what we're looking for. But let's just do one more thing here. Instead of copying a file, we want to remove a file.
4:43 So we're going to remove the default file so that all traffic, even to just the IP address, goes to our site here. So once again, run this.
4:54 And do our build, and now we can Docker run. What do we need to put here? Let's put 80, see if that'll work. Let's try again.
5:05 We're going to give it our command and give it that ampersand so we can stew other commands. And now into HTP, and that's from our little
5:11 install of HTTPie, local host, fingers crossed. Yes, look at that. There it is, that's our app. So maybe we could do something cool like this.
5:22 Maybe we go to local host here. Uh-uh, what's happening? Remember, this container is super-isolated. Within it, we can hit HTTP local host port 80
5:36 and it gives us this back. But the port is not exposed outside of our container. So effectively our web server, well, it's useless.
5:44 So we need to do that, but we have it more or less working.


Talk Python's Mastodon Michael Kennedy's Mastodon