Docker Tutorial For Beginners - How To Containerize Python Applications

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys today i want to show you how you can use docker for your python application i show you all the essential stuff to get started and then we take a look at two different projects and how we can dockerize them the first is a simple script that needs additional dependencies and the second project is a web application with fast api this second example also shows how to dockerize a virtual environment so this example is one of the most common use cases for docker and if you follow me here then you should be able to apply this for all your own projects all right so first of all what is docker in simple words docker is a tool that lets you put your application into a container these containers allow you to package up the application with all of the parts it needs so all the libraries and all other dependencies and then we can deploy it as one package so if another one is using this then he or she doesn't have to worry about installing all the correct dependencies first so we can start using this right away okay so now let's start dockerizing our python applications [Music] first of all we have to install docker of course so for this you can go to the official homepage docker.com and then click on get started and then i recommend to download and install docker desktop so this gives you both the command line interface and also a useful desktop application so choose your operating system and download this and follow the installation guides and once you have installed this you can verify this in your terminal by typing in docker minus v this should tell you the current docker version so yeah here we can see that i already have this on my machine all right so this is working and here i'm using visual studio code as my editor and if you're using the same then i also recommend to install the docker extension so you can go to the command palette type in install extensions and then here search for docker and use the official docker extension from microsoft so i already installed this and this gives you nice features for example for debugging or for auto completion so go ahead and do this and now we can start dockerizing our first python script so for the first example i want to have a single python script main.pi and for this example i'm gonna use the code from another tutorial that i already have here on my channel so this is the imdb movie scraping tutorial where i scrape the imdb charts and then randomly suggest a movie so this uses the requests library and beautiful soup and if you're interested in that then go ahead and check that tutorial out as well so i will put the link in the description so now i simply copy and paste this and here i'm dealing with a user input as well so for now i comment this out but i will show you how we can use this in a second as well so let's comment this out so now we can start stockerizing this and the first thing we need is a docker file so we have to differentiate between three different things the first one is the docker file then we have the docker image and then we have a docker container so a docker file is a blueprint for building images a image is a template for running containers and the container is then the actual running process where we have our package project so the first thing we have to do is to define our docker file and by the way we can use comments in here like this and the first thing we have to do is specify a base image so here we can say from and then use python and then colon and we can specify the version 3.8 so this is pulling this image from docker hub which already has python installed so we have all the things we need to have python in our container and now the next thing we want to do we want to add this main pie file into our container so we do this by saying at and then the source is main dot pi and the destination is just a dot so in the current directory in our container now that we have that we have to install the dependencies so as i said we are using requests and beautiful soup so we have to install them with pip and we can do this by saying run and then simply the command pip install requests and also beautiful soup and i guess the pip package is called beautiful soup 4 so now that we have that and then the last thing we have to do is specify the entry command when we start our container and here we want to say python and then as seconds parameter here we say colon slash and then main dot pi so this is simply running python main.pi in our container terminal so now we have all for our docker file and now we have to create our docker image so we do this with the command docker build minus t for tag and then the name of the image so i call this python imdb and hit enter and i also have to specify the location so here i simply use a dot again and now it's building the image and as you can see this is um executing these steps in this order so the order is important so it's pulling the python image then it's adding main.pi then it's installing the modules and then we are done and then when we start our container then it's running this file and then we start our container by saying docker run and then the name of the image python imdb and then we see that our container is starting and the script is running and we get a random movie suggestion and then our container comes to the end so it's shutting down again so now we can try this again and then we should get another random suggestion all right so this is working and now as i said here i commented out this part where we used the user input so now let me put that in again so now we have the complete script and save this and then we have to build our docker image again so again we run this command and now if we start our container with this docker run command like this then it will crash so yeah so we see we get this eof error and this is because for if we use a user input then we have to use additional arguments for the docker run command so we have to say docker run minus t and minus i so the i stands for interactive mode and the t will give us a pseudo terminal and where we can type in the user input so now if we run it like this then this should work again all right so now this is working so we get a movie suggestion and then it asks us if we want to see another movie if i say yes then this should give me another one and yes and yes so this is working and then if i say no then this should end the script and also ends the container all right so this is working and this is the first example now let's have a look at a more advanced tutorial so for the second example i want to create a web application with the fast api framework but this basically works the same for every other web framework that you use so we start by creating a virtual environment then install the dependencies and then um write our application and then dockerize it so let's do that all right so here i'm in a new empty directory and the first thing i want to do is create a virtual environment so i do this by saying python 3 minus mv and vn this command might be slightly different on windows and now we created it and then we have to activate it by saying dot v and slash bin slash activate and again this command might be slightly different on windows and now we are inside this virtual environment so now we install the dependencies in this case for fast api so we say pip install fast api and we also need a web server so we say pip install uv corn and hit enter so now we have that and now let's create our app so let's create another directory in here and then the file main dot pi in this directory and now let's grab the example code from the website so this one here and paste it in here so this syntax is very similar to the flask syntax so we create an app and then we define our functions where we define the routes so we have one base route and then one route at slash item slash item id so now first of all let's test our app locally so we do this with this command or we can also do it in a second way so let me show you this as well so we can import the uv corn library and then we say if name equals equals main and then we say uvicorn dot run and then we want to run our app so this is i think the same or very similar with flask with flask you can also start it from the terminal or you can start it with flask.run inside here or app.run i guess it's with flask so yeah so let's do it like this and then of course we have to run the script so now we say let's go into the app directory and then let's say python main dot pi and it says that our server is up and running so we can go to localhost 8000 and we see we get the hello world response so this is working and if we go to slash items slash item id then we get the second response that we specified so yeah so this is working so let's shut down this server again and let's um go back to the root directory from this project and let me clear this so now we want to dockerize this so the first thing we want to do is to save all the dependencies in a requirements txt file so for this we say pip freeze and then the greater sign and then requirements.txt so this writes all the dependencies that we just installed into this file so we see we have fast api we have unicorn and some other dependencies so the next thing we want to do is again we want to create our docker file docker file and this should be in the root directory of our project and here again we start by specifying a base image so here again we say python and version 3.8 and now i want to do something new so i want to organize the folders in the container in a slightly better way so i create a working directory with this command and then let's call this directory in this container fast api dash app and this will also automatically go into this directory so now and this is our starting point and now we want to copy the requirement into this um working directory so we simply have to say colon then we have to install the dependencies and we do this again with the run command and this time we say pip install and then minus r require mans dot txt then the next thing we want to do is to copy this whole app folder with the main dot pi file so let's do this by saying copy and we want to copy from dot slash app so this is the folder here on our machine and then we specify the folder name inside the container so let's also call this app so now we have that and now we also have to specify the entry point for our container so again here we use the command python and then we want to run the main.pi file so this is in the dot slash app folder and then slash main dot pi so now this is our docker file and then again we have to build our image so we say docker build minus t and then let's call this python minus fast api and again i forgot the dot here so again with the dot at the end for the current location so now this is running and executing all those steps all right so this worked so now we have a docker container with all those dependencies installed so now we can run this again with the command docker run and the name was python fast api all right so now this is saying that our uvicon server is running at localhost 8000 but now this does not work so if we go to this route again and then hit enter then we see this site cannot be reached so for this we have to do some changes so first of all when we so let's stop the container again so first of all um what we have to do is when we run our container we have to map the port by saying minus p and then we have to map the port from the outsides to the port from the container so like this but before we do this we also have to specify the host and part in our app so we can do this here in this uv corn run command and here we can put in the port argument port equals 8 000 so this is the default anyway but what is also very important is the host so we have to say host equals and then here as a string 0.0.0.0 this is very important so this is the same when we are using this or for example when we're using flask we always have to specify this host address so now that we have that and what we have to do is again we have to build our image so again let's run the docker build command and then let's run our container again with this minus port argument and we map 8000 two eight thousand so now if we run this then it should work so now again let's go to this localhost eight thousand address and hit enter and now we see this is working and we can also go to items route and then use an item id so this is working again and we see here we get the information in this terminal so yeah now our web application is running as a docker container so now for example we can go to the docker dashboard and then here we see that this is the current container so here we can inspect the locks or delete this or shut it down and restart this so yeah the docker desktop application is very helpful and what's also very nice is that we can get a terminal inside this docker container if we click on here so now we have a terminal and then i also i want to to demonstrate this folder structure again so this is actually my default terminal um so let me go back to my other terminal and then i want to show you how we can do this with the command so first of all we can say docker ps this will list all the running containers so we see that right now this container is running and this is the container id so we can grab this command docker exec minus i t and then this docker container id and paste this in here and then we have to say slash bin slash ace h and now we get the shell inside this container by using the command line so now for example if we say ls then we see we are currently in our working directory so inside this directory so if we go one folder up by saying cd colon colon and again say ls then we see all of the folders and so this linux file structure in our container and then here we see this new folder that we created with this command work directory fast api so that's why i did this so now again if we go into this um fast api dash f directory this is our starting point and then again we have this app directory and here we have the main.pi file that we copied with this command so yeah this might be helpful sometimes to get a command line in your terminal and yeah this is all i wanted to show you for now i hope you learned a lot here and if you enjoyed this tutorial then please hit the like button and consider subscribing to the channel and then i hope to see you in the next video bye you
Info
Channel: Python Engineer
Views: 74,111
Rating: 4.9329233 out of 5
Keywords: Python, Docker, Container, dockerize, containerize, docker app, web app, deploy, fastapi, flask, django, python application, virtual environment, linux, Python script, Docker Tutorial, Beginner Tutorial, Getting started
Id: bi0cKgmRuiA
Channel Id: undefined
Length: 22min 17sec (1337 seconds)
Published: Sat Nov 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.