Docker VSCode Python Tutorial // Run your App in a Container

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
if you ever wanted to create your own python application inside the docker container here's what you need to do i will show you how to set up visual studio code which extensions you're going to need how the debugging works so that you can easily develop and deploy your own application inside a container [Music] hey everyone this is christian and i create tutorials and content for it professionals here on this channel i also do a lot of live streaming where i sometimes do coding code was challenges or work on my home lab projects so if you want to learn programming together with me or you want to learn more about linux devops processes in general then don't forget to hit the like button and of course subscribe to the channel in this tutorial i will show you how to develop python applications inside the docker container with visual studio code and i'm doing this on my windows 10 machine because we're usually using linux container images because they are very lightweight and easy to deploy python applications and they are very scalable so they can run on container orchestration tools very well like kubernetes for example or any cloud service providers we are going to need the windows subsystem for linux to do this so if you're not running windows 10 and you're using macintosh or linux distribution you can still follow this tutorial you can just skip the part with the windows subsystem for linux and as always you don't need to remember any commands in this tutorial just go to my blog and read the full write up of this tutorial and then you can just copy and paste all the commands and the templates okay guys so because we can't run linux containers natively on windows 10 we need to go and install the windows subsystem for linux first which is a small virtual machine that runs a full linux operating system inside windows 10 so you can just use it to develop run and test your docker containers locally which makes development pretty easy so just go to the official documentation the windows subsystem for linux installation guide for windows 10 and follow all these instructions it's not very difficult what you basically just need to do is you need to enable two features in windows 10 the first one is the windows subsystem for linux then you enable the virtual machine platform click ok and restart your computer then you go again to this instruction guide and you go to this step 4 download the linux kernel update package here download it follow the instructions and then it's also recommended to set wsl 2 as your default version because the version one is an emulator and not really a virtual machine so you will gain a lot of performance improvements using this version too and then you just open the microsoft store search for linux and pick a linux distribution that you're preferring and just go and install this you can use debian suits the linux whatever you want but i would recommend you to use ubuntu 20.04 lts because it's a very stable linux distribution that runs very well on windows subsystem for linux and once you've installed this i would recommend you to use the windows terminal application to just run this wsl machine so windows terminal is a very nice application to run a linux terminal inside windows 10 you can also use that to manage other shells and terminals so it's a very nice application it has a lot of different features i've done a separate video on how to set up your windows subsystem for linux terminal with zsh and some themes and templates just awesome so i'm linking you this tutorial in the description below check it out if you want to set up this with zsh so it looks so nice like this terminal looks okay good after you have done this and you have set up your windows subsystem for linux environment you should also install docker desktop so just go to the official guide on docs.docker.com and search for install docker desktop on windows 10. it is not really required to run linux containers on windows subsystem for linux but it is highly recommended because it manages these linux containers pretty easily and you can just go to the docker desktop open this application on windows 10 and then you can see all the docker containers that are running you can automatically open them in visual studio code start stop them delete the images do some cleanups and whatever so it's very easy and it's very cool it's a useful application so i would definitely use that and then if you open visual studio code you also should install the right extensions in visual studio code so there are really just two ones you're going to need on windows 10 and one that you're going to need on macintosh and linux and the most important one that is docker so just go to the extensions tab and search for docker and select the first one here so the docker extension makes it easy to create manage and debug containerized applications that you are developing on your machine so no matter if you're using this on windows with windows subsystem for linux on macintosh or in linux it's working very well it comes with a lot of great and useful features i'm going to use in this tutorial so this is really required to follow this okay and then you also if you're running windows you should use remote wsl extension which will allow you to open any folder in the windows subsystem for linux and it allows you to remotely connect to your windows subsystem for linux and use visual studio code just like you would use it on a linux operating system so once you've installed those two extensions in visual studio code you can now connect to wsl2 in visual studio code and how to do that you can either click on this icon here on the bottom left status bar click on this and it will automatically reopen visual studio code with one difference you can see in the status bar it is now connected to wsl ubuntu 20.04 which is my default windows subsystem for looks but there's also another way to open or do this and this is also pretty nice so if you're running windows terminal and you want to create a new project folder to develop your application for example let's create a new folder that's called vs code example you can also open remote visual studio code with a code command and then just select a folder you want to open so when you hit enter you can see it will automatically open visual studio code and you can see it's also automatically connected to your wsl machine remotely which is pretty nice and we are now in the current folder we are connected we can now start developing our python application and because i don't have a really good example let's create a very simple application that will just add two numbers and print out the result right so let's create a new file i want to create a new python file so let's start programming and i want to quickly save this as app.python so when you see this prompt here now python interpreter is selected you should also select a python interpreter it's not really required for docker development but if you have installed python in different versions on this machine for example you should select the python interpreter that you want to use so in my case this egg here and if you refresh the folder you can also see there's a new folder created with a settings.json and here the setting for the python interpreter is defined so you could also change this later if you want to do that but for now we start developing our python application so let's just define two variables here and let's add a is equal five and b is equal three and c is equal a plus b so we will just add those two numbers and let's just print out this i'm gonna use this syntax to print out the variables here because i think that's a pretty nice way to do it i also want to print out another statement why i will show you later but for now let's just test and run this application so as we would usually just test and run code on your windows subsystem for you so we can open a new terminal here and then we can just enter python3 app.pi and it will automatically run but you could also use the debugger so for example if you add a breakpoint here and you click on run debugging you can see that it selects you to debug a python file and it automatically hits the breakpoint and then you can inspect the variables here you can step into step over whatever and use debugger to write your application it's a pretty great way to develop complex python applications using the breakpoint feature which is really nice but of course we want to also develop this in a darker container and the docker extension in visual studio code has a pretty comfortable function to generate all the necessary files automatically for us but don't worry i will explain all the things what the extension does to you and of course you can also go and create all these files manually and customize it but to start with a very nice template where you don't need to remember every single file you're going to need that is very very nice to do so when you are using the docker extension just press the f1 key and then you can search for add docker files to workspace now click on this and now you need to select the application platform so usually we want to use python if you want to develop other applications or you want to use python flask for web services or django or whatever you can also select this but in our case we want to use a python general template and now we need to select our app's entry point so this is the main python file our main program that the docker container should execute once it is started right and we of course want to select app.pipe which is just the only file that we're going to need here and we could also optional create docker compose files please leave me a comment if you're interested in doing this and yeah i'm considering doing another tutorial on this as well but in our case we just want to select no and you can see it now creates a bunch of different files here let me just close all this stuff here and let's go over this step by step right so let's go to the project folder let's refresh it and you can see there are now a bunch of different files created here so first we have created two new files the launch.json and the task.json we will come to this later of course and then we have the docker ignore file we have our app.pi we have the docker file and a requirements.txt so the dockerfile is the main file that contains all the instructions how to generate and build the docker image we will go over this in a second the docker ignore is containing all file extensions or types or names or regular expressions that we don't need inside our container image so basically what you define here will be ignored by the docker build command and we also have the requirements.txt which is currently empty but in case you need any libraries packages for extensions that you usually are installing with a python package manager pip you can specify in this requirements.txt file here and it will automatically install all the necessary libraries and packages that you need for your application and this is a very very powerful part of docker containers because you can run this on any environment on any linux operating system it doesn't really matter what is installed on the host operating system the docker container and the requirements make sure that you always have the necessary libraries that you need for your application in the desired version as your application is expecting it and this is a very powerful part of docker i'm not using it in this tutorial but if you need any flask application or you're needing any requests python packages to do web requests or whatever you can put it in here and it will automatically be installed so now let's go over the dockerfile which is a core file and let's go over these lines step by step so the first line will define our base image because we want to build our application on top of the base linux distribution image so in this example the python 3.8 slim buster is a small and a lightweight debian image with python 3.8 installed so that has everything we're going to need to write basic python applications if you're interested in learning more about building docker images or specific version whatever put a comment because i don't really know what to grade for you guys so please let me know the next line is ignoring all these pi c files inside our container so we don't really need that inside a container it's just annoying so we just will prevent python from generating these and this is also terms of buffering for easier container logging so this will just make container logging a bit easier to analyze and then we are installing all the pip requirements so this is just what i've said before it will copy the requirements.txt and the dot stands for our current working directory and then it will run the python command with a pip package manager and install everything that is defined inside our requirements.txt file then it will set the working directory to slash app and copy all files in the entire project folder inside our slash apps directory so when you later run this container and you connect a shell to this container and do an ls command inside that slash app directory you see that all the entire project folder inside here will be copied to this location then we also want to add a new user disable the password and give some limited privileges to this user so this is highly recommended because of security reasons because usually when you run a container it will execute everything with root user privileges and this can be a security concern if for whatever reason your python application has some security vulnerabilities and it is abused by any hackers or malware it could potentially lead to privilege escalation and very bad things that can infect even the host operating system so it is always required or it's always recommended to isolate your application from the host operating system by adding a non-privileged user here so i'm not seeing this in all docker templates and that can be a real problem so make sure you're always using this if you want to learn more about it then just go to the link and read about it and then our last command will just execute the python command with our app.pi so this will just run our application and once our application exit it will automatically also exit the container but now we just want to build our container image and run it and because visual studio code has prepared everything for us we can now just go and execute the docker build dot to build this image or simply just click on the docker file with a right click and say build image so this will automatically build this image and once this is done it usually shouldn't take long because this application is very very simple right we can now exit the terminal and if we execute a docker image list you can see that it just currently built another image that is called vs code example just like our project name is without the dash it's built seven seconds ago it has a size 116 megabytes and now we can just run our container so let's execute the container with a docker run vs code example we can just now use our image that will locally start and you can see it just printed out five plus three is eight program exited and if we execute the docker ps we shouldn't see our container because it is not running anymore but if we execute the docker ps all you can see that this container has just run 12 seconds ago and it is still stored in our system okay so this is pretty nice this is basically everything you need to do to build your python application inside your docker container it's not really that complicated to be honest but i think the most concerning part for me always was how do i do debugging inside a container because the container is an isolated environment how can i connect the debugger to this and this is very very beautiful the docker extension makes it pretty easy for us to debug this application inside the container with no effort at all so in the past i needed to install and use debugging libraries and extension in python but you don't need to do this anymore the extension is smart enough to rewrite our entry point inside the docker file here you can see it in a comment so you simply just can't go to run and start debugging and that it will automatically debug it let's also add some breakpoints and i can show you how easily this works so click on start debugging and it will automatically build our container and connect a debugger to it and then if we execute it you can see it immediately hits this breakpoint here so when we go to the debug section here you can see that we are currently at this point here we can also inspect the variables and see that c has a variable of 8. if we continue you can see it steps to the next breakpoint you see the output on the terminal here the variables would change if there is anything to be executed and if we hit continue again the program exited and the debugger stops it works pretty well and this is very very cool it makes it very easy but how is this possible right so you can see the docker extension has also created two new files here and this is first the launch file and the tasks file so when we look into these files you can see that there are some json configurations that change or they hook at specific points of these actions here what you execute the star debugging action for example is this hook here and it do a pre-launch task you can see the docker run debug so if we want to know what this task is doing we need to go into the tasks.json file and see ah there is a tax that has this label docker run debug and it depends on the docker build tasks so first of all it will execute this docker build task which will basically just build the container set attack to vs code example latest so if you want to customize your image tag if you want to customize a specific version whatever you want to do you can also customize it here it also has a docker file in the context and then it automatically runs this container you can see it runs this file and it starts is with a debugger and of course you can now just go and push this container image to your private registry or to the public docker hub registry where other people can just pull down the image and use your application very very easy no matter where they deploy it if they deploy it in a kubernetes or container orchestration tool whatever it doesn't really matter the docker image will always run with the defined requirements and libraries you specified inside your dockerfile okay guys so i hope this tutorial helped you to deploy your python application inside the docker container with visual studio code very easily and you could now just develop your python application you can do some amazing things and deploy it on any container orchestration tool on any cloud provider with docker containers very very easily by the way if you're interested in other python projects or how to push this to a private container registry and how to use ci cd pipelines and devops processes to deploy your applications automatically then again don't forget to hit the like button and subscribe because i'm planning to do more tutorials on this so please leave me in the comments what you want to see and what your current projects are what you're working on and if you're struggling somewhere then again just join our discord community we have a very friendly and respectful community of many talented it professionals i'm very proud of you guys so check it out it's always a lot of fun to chat with you guys okay so thanks everybody for watching enjoy the rest of your day take care of yourself and i see you soon bye bye
Info
Channel: Christian Lempa
Views: 67,331
Rating: undefined out of 5
Keywords: docker vscode python, docker vscode tutorial, vscode python docker tutorial, wsl docker vscode, wsl2 docker vscode, docker build, docker container, docker made easy, docker tutorial, docker tutorial for beginners, vscode debug docker container, vscode docker, vscode docker development, vscode docker extension, vscode docker python debug, vscode remote development docker, debug python in docker container vscode
Id: jtBVppyfDbE
Channel Id: undefined
Length: 19min 13sec (1153 seconds)
Published: Mon May 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.