Modern Python Projects Transcripts
Chapter: Deployment
Lecture: Auto reboot and auto update

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One of the problems that we have with our Docker application right now is that if we rebooted the droplet or if this container stops,
0:10 for some reason, it won't automatically restart our docker container. So let me show you what I mean.
0:17 Let's start docker container in a detached mode, so I still can see my terminal. We can see that our container is running, and if we go to the browser,
0:30 it's here. So, if I reboot this machine right now, Docker won't be able to restart our container, So let's give it a few seconds.
0:48 If we check for running docker containers, we will see that there are no containers and our website is down.
0:56 It keeps loading and then it's going to crash. So this problem can actually be easily solved with providing one additional parameter.
1:13 If we run Docker with the restart parameter and we tell it to restart, unless we explicitly stop this container,
1:21 Docker will automatically restart this container whenever we reboot this Droplet or whenever something goes wrong
1:28 and container goes down. So let's do another test. Let's go to the browser, it's working and let's reboot the machine.
1:44 And if we list the running containers again. You can see that after the reboot, Docker has restarted our container. Cool.
1:58 So with the simple trick, we told Docker to automatically reboot our container if something
2:03 goes wrong. Another thing that we could do is to have some kind of very simple continues delivery mechanism. For example,
2:11 whenever we push a new version of image to Docker Hub, we want to automatically deploy. And this we can do with the watchtower tool.
2:26 So watch tower is basically a container that will monitor other containers, and if there is a new version of image available,
2:33 it will restart them. All we have to do is to take this piece of code and run it inside our droplet. I will, however, make some modifications.
2:56 So first I want to also run in this restart mode. So whenever our droplet goes down, we will also restart the watchtower.
3:14 And I also want to change the default interval in which Watchtower will be pulling for
3:19 new images by default. Watchtower will check every five minutes if there is a new version of an image under Docker Hub,
3:27 which is a good default because right now Docker has some limitations of how many images
3:32 you can pull per hour. But I don't want you to sit here and wait for five minutes to see if our image was updated.
3:39 So, I'm going to change the pull interval to 15 seconds. Okay, so we have the watchtower running. Let's just restart our uptime our website.
4:05 So, let's see if our uptimer is running right now. And as you can see it is, So now we have to go back to our initial code,
4:14 change something and rebuild retag and republish new docker image. So, let's change this online to lower case again.
4:28 And that's it. That's not like that. I was experimenting with something in the background. Let's retag and push. Okay,
4:55 Now we go back to our server and we have to wait for a bit We can use the Docker PS command to see.
5:03 When was the last time one of the containers was restarted. And as you can see, our uptimer website was restarted less than a second
5:12 ago. So yeah, that was the watchtower would detected that there is a new image. It pulled this image and it restarted our container,
5:24 right. So now if we go back to the browser and if we refresh, we should see the new version. Cool. Yeah, so it's working.
5:34 So, that's how you can have a very simple continues delivery mechanism with Watchtower pulling new images. It's far from perfect,
5:42 because when watchtower is updating your container, it's actually stopping it. So if you have a visitor coming to your website during
5:52 the time when the new images being deployed well, your website will be down. But to properly deal with this problem,
5:59 you would need to have two different servers and the load balancer. And when you're updating one of those servers,
6:05 the load balancer will stop sending traffic there. And then you will update the other server Stop load balancer from sending traffic there and
6:13 then you should be fine, which sounds like a lot of work, and it has a lot of work. You can do this with kubernetes, but for our very simple website,
6:23 using the watchtower is perfectly fine. Yeah, that's how you can build and deploy docker images.
6:30 In the next lesson, I will talk about building something more advanced that requires,for example having a database or some kind of other external server.

Talk Python's Mastodon Michael Kennedy's Mastodon