#100DaysOfWeb in Python Transcripts
Chapter: Days 97-100: Docker and Docker Compose
Lecture: Building the services with docker-compose

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It's time to build our dockder-compose set of services that are going to control the base server the front-end server
0:07 and our services Python web application. So let's go over here and create a new file. I'm going to call it just docker-compose.yml. Here we are.
0:19 Now, this has a certain format that you have to follow. You can just look it up on the Docker website but, it's pretty straightforward.
0:26 So the first thing we have to do is specify the version. And, by the way, why is it straightforward? Because we're using the Docker plugin for PyCharm.
0:33 Watch this. I want to have the version. Oh, that's how I put it. And then the value's going to be a string and the value's going to be 3.
0:42 Now, the way this works it's kind of like a dictionary in Python. If you have the key, you have the value.
0:47 And then if you have sub-dictionaries, you indent. And, if you're going to have a list of items like this would be A and B that's how YAML works.
0:58 I'll show you a website where you can learn more about it later. But for now, let's just keep going. So we're going to have our version
1:03 and we're going to have services. Okay notice the help. Notice that it shows the dictionary thing right here.
1:09 So, we know that this is going to be a set of key values not a list or something like that. So, what are the values? Let's have the base_server
1:17 remember we're going to build the base_server this is where we just make up the name. And then we're going to put some values in there.
1:25 And then we're also going to have the front end put some values in there and then we're going to have the services.
1:33 Kind of unfortunate that I named it this and that. But, that's what it's called. So there's the web services.
1:39 So over here, how do we describe how we want it to build it? Well, if you type B, you can say there's a build.
1:46 Okay, and what are we going to do for build? We're just going to specify the server. Over here it's going to be this folder.base_server. That's it.
1:55 Specify the directory it finds the Dockerfile. Off it goes. And then, we also want to give it a name. If you recall, over here
2:04 we say this is based on base server latest. Well how does a thing over here know as we did on the command line that we want to specify that name?
2:12 So what we're going to say is, image. It's going to be that like so. So that'll specify what the image is supposed to be.
2:19 And for this one, we're not going to run it or anything. Technically it'll run, but it'll just exit immediately. We just need it to be built previously
2:27 before we get to the front end one. So for the front end one, we're going to do a build and the build is going to be frontend like that.
2:35 Let's do the same for services while we're on the build stuff and it'll be dot slash services like so. Now, we want to make sure that
2:48 when the front end starts that the services also start and maybe even start before the front end starts. We can say, this depends upon
2:55 notice the little square brackets means this could be a list of things and it already puts in the list indicator for YAML.
3:04 And what's it going to depend upon? Notice the autocomplete. Oh my gosh, that is awesome. So those are the other services defined in this YAML file.
3:12 So it depends on services. Now remember, this one also had to expose a port. So what we're going to do, trying to say, port
3:22 and what we're going to do is go from AD in our machine to AD on the container like so. So we just say this.
3:30 And that's it. This is all we got to do to define the frontend one and we have to make sure that the base server is here as well.
3:36 So let's say this also depends on base_server, right? In order for a build to happen. Next we have our build here. Let's do our depends on base_server.
3:47 And we're going to need our ports. And it's going to be 7007 mapped to 7007. Now, this is close, and I believe the way we set up
3:58 these containers, let's double check. This one has an entry point. And this one has an entry point. Let's go and be a little bit more explicit.
4:08 We can specify custom or override the entry points right here. So what we can do is we can come down here
4:14 and say the command for service one is just that. Run Python from our original environment against our app.py.
4:26 And we go in and also specify the command here which is this nginx thing We could run it without, and let it run however it's going to run
4:37 but I kind of like to have it all, you know mapped out right here, what's happening in our docker-compose. Well that wasn't so hard, was it.
4:46 You know all the cool benefits we talked about in the concept video? Let's see if we've got them. Going to find out.
4:52 If we install our Docker desktop here, we should also have a Docker compose, and it looks like we do. Now if we run this in the directory that has
5:02 the docker-compose.yml file, first let's say DockerPS Nothing running. And then we could say Docker compose, and we just say
5:13 bring the system up, it's going to build it it's going to run the containers, and all that. And we could also say down to shut it down later.
5:20 So we say this, it's doing all the building for all the pieces In a little while, so it decided it was going to make
5:28 new instances of the service. Now look at that. It's created the demo base server, the demo service one
5:35 and the front end service one, and now there's nothing for the base server to do, so it woke up and said "Ah! Nothing to do here. Go away."
5:41 But the other two are running. So if we say docker ps you can see that those two are running. If we say docker-compose ps
5:53 Oh, got to be in the right directory. Sorry about that. It looks at the Docker compose file, docker-compose ps
6:05 It looks, and it says "Okay, these things are up and running, this thing exited and that's okay, we don't care." But these two are running.
6:11 Let's just open up our browser and see what we get. If we go to local host, what should we see? We should see our movie exploder app, and it
6:21 should be talking back to the other local server. And beautiful. It is! Look at that. So let's go to comedy, and look at drama
6:30 and if we look at the network behavior down here and just look at XHR, for the Ajax stuff. So we click here, you can see it's going back
6:38 to local host, and if we go back here, you can see it's actually processing the requests as we do them.
6:45 So, we don't really need this to see that that's happening. Go over here, top movies by genre as crime get the top ten, search for Cats
6:53 Notice you can see the search for Cats right there. So this is running entirely in our set of Docker containers.
6:58 In our two services managed by Docker Compose. Well, what did we have to do? We had to define the YAML file, and say docker-compose up
7:08 Let's shut it down real quick. Docker compose down, this has to happen where the Docker compose file is. You see it stopping them. Gently. Gently now.
7:25 In the left terminal, you can see the two services exited. Perfect! It's all done. We do PS again, nothing is running. Of course, we check here
7:35 It's all gone. Doesn't work. Now if we just say compose up again, it should actually be a lot faster than before.
7:42 Look, it already knew that all the builds were successful it had all the images it's needing, so it just goes boom! Start right up and get going.
7:49 And guess what? We're back online, and we've got our biographies all that we want. It's pretty cool. And that's all there is to it, Docker Compose.
7:57 There's a lot more you can do. You can do scaling and things like that, like having three web front ends, and two service back ends.
8:04 The challenge has noting to do with Docker Compose but that is a little tricky, because we have to somehow
8:10 do the load balancing ourself, like set up an nginx that speaks to those various pieces, and it can be done, but it's beyond the scope of
8:18 this short version of our course here. Anyway, there's a lot you can do with Docker compose and it's kind of a gateway to the bigger Kubernetes story
8:26 as well, if you want to go down that path.


Talk Python's Mastodon Michael Kennedy's Mastodon