Python Tutorial | Connecting Flask and Nginx with Docker

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Last week I created a post (https://www.reddit.com/r/flask/comments/fykw3x/beginners_guide_to_running_flask_with_gunicorn/) on connecting flask and gunicorn. I got great responses and some helpful feedback on the tutorial. I decided to build upon the previous tutorial and connect flask with nginx through docker and docker-compose. Let me know if a more in-depth video like this is better or if people prefer shorted formats.

πŸ‘οΈŽ︎ 4 πŸ‘€οΈŽ︎ u/pavelow53 πŸ“…οΈŽ︎ Apr 17 2020 πŸ—«︎ replies

In addition to the summary that chjassu recommended, I think it would also be helpful to give a high-level explanation of the purpose that each component serves at the start of the video.... why you'd want to combine Flask, NGINX, and Docker in the first place... you know?

πŸ‘οΈŽ︎ 4 πŸ‘€οΈŽ︎ u/vodak πŸ“…οΈŽ︎ Apr 18 2020 πŸ—«︎ replies

So why nginx ?? Does it hurt not having it? I understand the production server is not efficient + a lot problems ..etc That’s why we use gunicorn But dunno why we need nginx ??

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/r-_-mark πŸ“…οΈŽ︎ May 09 2020 πŸ—«︎ replies
Captions
hey guys today I'm going to do a video on how to connect flasks and corn to an Jenks via docker compose is like a little graphic I mean just like how everything's going to work so basically incoming requests for the client or sir gonna be a web browser like Firefox Chrome are going to sent parallel connections to em jinks or nginx nginx is taking one to communicate via a UNIX socket not to get a corn or you WSGI whatever maybe that's Jess or you have and that's an incident request to flask and ask this you sent back the client after that um if you haven't already you're no one to install a docker and docker compose I've got links in description as well as the github repository for this video it's gonna vary on a operating system basis to get started we're gonna set up just a basic flat server and then we'll go from there so what do I usually do I'm making the folder we're gonna call it app with inside of this folder let's create a module file so just telling the talent Python it's the app folder the module and then our create app file 8if inside of here we're gonna start to flask application itself if you haven't already I suggest looking at the video I posted I think it was last week on how to connect flask and good new corn this is kinda it's gonna build on doesn't always have this have been that that video you don't need kind of configuration just route a basic index and let's have it just return the hello world for this video and we're going to return the app at end right it's not to run this we can do it with unicorn as I in our last video so let's one worker let's do so we're gonna want passing the module name so it's an app file the function or the app is created in every app and in the function name like that okay so now we have a app running at localhost port 8000 let's verify that right yep hello world so let's say we wanted to dock your ice this we need to first create docker file let's do that here and let's use let's have it use the Python 2.7 image you know I like to copy all my my flask apps too far WW so we're gonna do that as well here you don't have to do that we're gonna add all the contents to this far ee w folder so we're gonna have the app modules gonna be in there we're gonna change the working directory to be this container home this is how Dockers gonna communicate if doctor propose and to this working directory from here we're gonna want to install our requirements as doc isn't to basically virtualized the dependencies so flashing unicorn need to be installed within set to thought her container so let's create a requirements had TX he filed sub pip can be used let's give it flask and unicorn so those are the two marshals for using in our application and now we want to create our docker compose files basically what doc compose does is it allows you to have two different doctor machines communicate to each other so we're gonna have one machine for nginx at one machine for our flask application start creating that docker compose file you see there's two different darker symbols here right up top you want to specify to specify the version I have your darker compose um why I think it's the latest one right now and then we want to specify the list of services are going to run or basically two different containers are gonna be using so we're gonna have one which is gonna be our nginx container I'm gonna specified the image at this is gonna pull from this is kind of similar to D the image we have here so this is the Python three boys having to slim image here we're gonna use the nginx about 15 image a name our container so we can view the processes pretty easily I'll show you later once you start it specified the volumes of it just where the files are gonna be located so we're in the copy everything in the root directory of the current app to rww and we're gonna create a configuration file let's call it um jinx dev ah let's call it a default koffice is a I'm jinx names your next configuration file when I go over how to create that later um we also copy that to where genetics keeps their configuration files which is at Etsy genetics it's concise and we wanna call it fault but this is how it should one-up exposed to certain ports we're gonna do port 8080 so this this is just the HTTP protocol port we're also gonna create a network to have the the nginx container and the flash team to communicate to each other let's just call it my network for now we're going to create this a little bit and then the nginx containers and depend on our flask one so it depends on the container name was gonna be flasks I'm so let's create two flasks one now right specify where it could build its stalker well so they can build it within the folder that I'm sorry that docker file is located within the current container folder the name of the docker file is docker file this isn't necessarily needed I just like to be explicit with it this container name gonna be flask and the command to run this container we're gonna ride that unicorn command that we did here right so unicorn let's bind it to port 8080 though because this is what we're gonna use within our nginx configuration we're gonna have to specify what port to communicate from with the flask application should do for workers and we're a p-- pretty apps where it's all located function again on white to 0 there again we will specify the same volume I'm just like where everything is located and the network so here is where it gets a little different than the network we did above went to alias it just to make it easier on us because sometimes you're gonna have multiple different networks you want to have a client site Aceros height network to communicate with names gen-x let's just call it a flask app for now now we're gonna actually specify that we have this new my network at the bottom underneath services within the docker compose so I'm just a video to services you want to specify what networks we have we have a network called my network ok so now let's work on our nginx configuration file so what's in do is we're gonna declare what the upstream server is which is a unicorn application this is how this we communicate to the UNIX socket so it's called flask app see I'm gonna buddy side by side so you can kind of see we're doing here so here we a leotard network tepee flask app we have no upstream flash gap here it's communicating went this docker I'm with this unicorn socket specify the server because doctors can automatically resolved the correct address nobody needs just from the container names our container name of in the document post house flask gonna be at port 8000 house whatever and it could have born command with and they'll specified the I'm jinx Claire does so listen for port 80 server name it's gonna connect to local host now we want to Ford all traffic so every traffic coming to localhost pass it into our flask app I think you all need this semicolon TM sorry it's a lot trying to remember here I'm actually using a copy descent from my other file I've got so it saves time when I have all the code for this in a github repository in the description but yeah now we have our in configuration file we should have everything now to actually be able to run this docker compose files and create two containers and connected to with them so let's score what we did here we created the docker file to authorize to flask application we created the docker compose file to connect the flask container with our engine X container to communicate together let me created our engineers configuration to have the and communicate with the unicorn socket that then runs our application so to run all of this now you should feel a type docker it's going to build your containers and then start it so sorry cake flour create unexpected so we got a little air somewhere oh I think I'm missing a semicolon in my um configuration here there we go that should fix it you did see that the kikuna course started correctly though um what X next never started throwing flash starting engine X and there we go now we've attached both flasks in action X listening on port 8000 it's going to then be translated to the engine X it's gonna be served antigen X which is gonna then serve it back to a local host machine even need to port so let's open that up so local host 8's instill work my guess it didn't work local host first there so um what happened was exactly what this graphic says flash communicate with unicorn unicorn went back to nginx and the client was served to local host you can do a few things now everything's up and running um you can view the two containers I want the PS commands or docker so if I dip docker PS right now oh sorry so we've got our nginx container and our flash container commands that we used to run them when they were created the status system as well as some other things see we specified the names at the mansion accent flat stare okay so we've definitely successfully now connected flask and unicorn to the nginx compose I'm gonna have all the code that was used in this video and a gap repositories any linked in the description below
Info
Channel: Andrew Porter
Views: 8,535
Rating: undefined out of 5
Keywords: python, flask, gunicorn, docker, docker-compose, nginx, tutorial, learn, education, example
Id: Vkqz2hK4fKg
Channel Id: undefined
Length: 13min 4sec (784 seconds)
Published: Fri Apr 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.