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