How to Run and Debug Python Inside Docker Containers Using VSCode

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
sometimes you may want to experiment with a tool or different version of the tool but you don't want to necessarily have it installed on your system because we know that version collisions are real and if you have multiple projects working in different versions then you may end up spending a lot of time fixing your environment well the solution to that is to use docker and we can use docker for development and i'm going to show you a couple of ways to do that i'm going to show you how to do it from the command line and just connect to a python interpreter or i'm going to also show you how to connect visual studio code and actually be able to debug your python code inside the docker container one solution to that is get a docker python image and interactively connect to it and let me show you how we can do that so to do that we can just go to google and just say python docker and the first link is going to be what we actually need on hub.docker.com we can see that we can just do docker pool python to pull this image but it's not exactly what we need we actually may want different tags for example we want a stripped-down version of python if you don't care about certain tools i'm just going to use the regular python but i'm not going to use docker pull python cap hand i'm actually going to pull it and interactively connect to the container running this image all in one line and to run this image we're going to use the run command so we type docker run then dash dash interactive which basically means hey i want to connect to this container and then we do another dash dash tty which means hey whenever i get connected to this container i also want a terminal back so i could execute certain commands against this container and then we type our image name and if you want you can specify a certain version here if it doesn't find this this image on your local machine it's going to connect to docker hub and pull it from there as we should see in just a second as you can see it says unable to find a bunch of things already exist because i ran some commands preparing for this video and now as you can see we got python version 3.9.4 and we got connected to python interpreter so we can do stuff like a equals one b equals two c equals a plus b and we can print c as you can see we can run python here in the shell but typically it's not the experience we want because we don't get syntax highlighting it's also really hard to split the code between multiple lines and it's just not a great experience so there are a couple of other ways we can approach that and one of them let me exit this first one of them is to define what's called a docker file and docker file is basically just a set of instructions that tells docker how to build an image so here we have just four simple instructions the first line is the base image that we want to base our image off so here we know we're gonna run python so we just get python and we can specify the certain version of python we want to run then we set the work directory in in the container basically it's where our app is gonna run from and then here we we're copying our hello.pi file that's on our local machine to the working directory in the container and lastly we execute a command cmd stands for command and we say we want to execute python hello.pi so if if we were here in the shell it would look like this python hello.pi and this is how it would work but on the container and so there are a couple of steps to this so first obviously we want to define the docker file but secondly we also now need to build it we now need to build an image and then start a container from that image so to do that we say docker build and then we can give it a name so we can easily access it later but we can just say dash t and we'll just call it hello and notice this dot at the end we want to make sure that it's running from this directory if our docker file was in some other directory we would either have to cd into that or we could just uh change the path right here but here we're going to build our hello image from this docker file using the docker build command and it shouldn't really take that long especially since we already should have the python image and as you can see it took maybe just a couple of seconds so now we should have the image and if we go to docker images as you can see hello is in there and our next step after we've built the image is to run this image and basically just say docker run hello and we get our hello from python as expected right here what if we changed it and just ran it though without rebuilding it well we get the same hello from python message because in our docker file when we were defining how our image should be built we had this command to copy hello.pi so basically anytime you make changes in hello.pi we have to go rebuild it and then rerun it it's also not that great of experience so the next thing i'm going to show you is how to connect docker with visual studio code and let you write your code in visual studio code and have the ability to debug it without having to run all of these extra commands and the first thing we need to do in order to connect visual studio code with the docker is to install a remote containers extension so we can just type remote dash containers in our extensions tab click install it will ask us or maybe it will it may ask you to reload you just click reload but it looks like this time with dodge.bullet and here now we can click this green let's see it's greater than and less than signs i don't know what they're called and we should have these options remote containers options appear so here we can see uh we can select reopen in container and it should prompt us to set things up so here we already have the docker file so we can just choose from docker file you can also explore different options but let's just choose from docker file for now and as you can see it reloaded and here you will see opening remote at the bottom left and search and dev container pop-up right here it just tells you what it's doing it's building an image and once it's done really there is not much more to it it may take a minute sometimes longer depending on if you have all the necessary images already on your system or if it needs to pull them and here as you can see we got dev container existing docker file in our terminal as you can see we don't have what we had anymore where it was my username now we have this root user for some reason it connected us as root it actually wasn't the case when i was testing it in the preparation for this video but let's see if this actually works so i'm going to change this script a bit and just say basically do what we did before c equals a plus b and we'll print c and what we want to do is see if debugging works and right now it doesn't work so usually i would be able to just click here and a red dot would appear saying that hey set this breakpoint and whenever the code reaches this line just stop but it's not working right now and i bet it's because we don't have the python extension installed in this container so what we do is basically just search for python and as you can see i was right so we had it in our in our regular vs code environment but it's not in our dev container so we can just click here install on dev container and hopefully it works like on my mac i ran into an issue where it did not work and i had to go download it manually and then clicked here and basically installed from vsax but hopefully it just works for us here and we don't have to go through all that trouble and okay it looks like it got installed we did have reload required pop for a second but it looks like it should just work now and as you can see if i hover over here i i do get those dots that i was talking about just a minute ago also it looks like we have no python interpreter selected well let's select 3.9.4 for now uh obviously 2 or any version of 2 is out of support right now and let's see if it works so here we did set it up as you can see it should be right here if you like missed that pop-up you can just click here on python and select your interpreter here and the next thing we do is start debugging click run start debugging or just press f5 choose python file and hopefully it will work yay it did work and as you can see here it reached this point we can look what a variable is what b variable is we can step over it and see that c is now four and this is really it feels like magic obviously it does take some time to set it up especially if you want to configure it in a certain way but just experiment with it this is a good start you just need to figure out how to write a docker file or if you don't have a docker file visual studio code will actually offer offer you to create one for you and then you can experiment with it that way this is all i had for today if you have any questions if something was confusing or if you feel like something needs to be covered more maybe you want to know more about containers images docker files etc just leave me a comment and i'll try to help you as much as i can thanks for watching and i will see you next time so don't forget to subscribe so you don't miss the notification for the next video
Info
Channel: codevev
Views: 39,862
Rating: undefined out of 5
Keywords: docker, python, docker tutorial, vscode, python tutorial, python environment setup, docker environment setup, docker environment, Python, Docker, docker app, virtual environment, Python script, Docker Tutorial, vscode docker remote, vscode docker, vscode docker development, vscode remote development docker, docker made easy, docker in vs code, docker commands, vs code docker python, vs code python docker, docker vscode, docker vscode extension, vscode remote docker container
Id: cJbvcH0JNGA
Channel Id: undefined
Length: 11min 38sec (698 seconds)
Published: Sun Apr 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.