How to Run and Debug Python Inside Docker Containers Using VSCode

  Рет қаралды 55,993

codevev

codevev

Күн бұрын

You don't need to have Python installed on your system in order to run it. Replace Python with almost any other language and keep your computer free from all the things you need to have installed in order to start developing software!
Version collisions are in the past. Create a docker image for each of your projects that you're working on and you will not have to spend hours or days fixing a local environment because you upgraded a package that broke everything.
In this video, I will show you a couple of ways to run Python in a Docker container, and in the end, we will set up VSCode (Visual Studio Code), so you could run and debug your code inside a container without leaving VSCode!
Timeline:
0:00 Why do you want to run code inside a container
0:45 How to run Python in Docker using an interactive terminal
3:00 Create a Docker file to create an image and run your Python code using docker commands
6:10 How to connect Visual Studio Code to a Docker container to run Python code
#SoftwareEngineering #Python #Docker
-------------------------------------------------------------------------------------
📰 Sign up for my newsletter
Become a better dev in just 5 minutes (or so) a week: codevev.com
🖥️ Free Courses to Learn Programming
Harvard CS 50: pll.harvard.edu/course/cs50-i...
FreeCodeCamp: www.freecodecamp.org
Udacity: www.udacity.com
🛒 Products I recommend
codevev.com/#products
Connect with me
Website: codevev.com
KZfaq: / @codevev
#codevev
Connect with me
Website: evgenyurubkov.com
KZfaq: / @codevev
#codevev

Пікірлер: 66
@codevev
@codevev 3 ай бұрын
If you want to learn how to connect from VSCode to a remote server via SSH, watch kzfaq.info/get/bejne/qs-kldqHuLKnp6c.html next
@mahmoudelzouka5689
@mahmoudelzouka5689 3 жыл бұрын
Thanks, Evgeny for the demo! this is what I was looking for.
@codevev
@codevev 3 жыл бұрын
You're very welcome! Glad it helped!
@TechWazza
@TechWazza 2 жыл бұрын
Thanks for the this Evgeny...this was very simple, short and great help.
@codevev
@codevev 2 жыл бұрын
You're welcome! Glad it helped!
@bluesdog88
@bluesdog88 3 ай бұрын
Thanks for the run through, just what I was looking for!
@codevev
@codevev 3 ай бұрын
Glad I could help!
@farmingbyfaith4210
@farmingbyfaith4210 2 жыл бұрын
Awesome Video. Im busy with a project to use Python Data Science tools inside docker, but the tools then control metatrader inside wine. So I can have many algorithms running at once. I like the fact that I can debug line by line the Python code as it controls metatrader
@codevev
@codevev 2 жыл бұрын
Sounds complicated :)
@jarorkwong8042
@jarorkwong8042 2 жыл бұрын
Thank you bro, nice and easy!
@codevev
@codevev 2 жыл бұрын
Great! Glad it helped!
@irongranca
@irongranca 2 жыл бұрын
Thanks from canary islands :)
@codevev
@codevev 2 жыл бұрын
Awesome, you're welcome!
@ahmedshawky9845
@ahmedshawky9845 Жыл бұрын
Thanks mate, really appreciate it
@codevev
@codevev Жыл бұрын
Anytime!
@IFinging
@IFinging 2 жыл бұрын
Fantastic video, Thank you
@codevev
@codevev 2 жыл бұрын
Glad it helped!
@sg22r
@sg22r 9 ай бұрын
Pure GOLD!
@codevev
@codevev 9 ай бұрын
Thanks!
@chennahoom
@chennahoom Жыл бұрын
Hey! Awesome video! Just a quick question, when I reach the part after I click "Reopen in Container" ... I'm not getting the option to open "From Dockerfile" (7:22 in your video) , I get a list of configuration template. Do you know why maybe?
@codevev
@codevev Жыл бұрын
Thanks! It looks like the option to open from dockerfile is only available when there's a Dockerfile that already exists in your workspace
@mohasinsultan
@mohasinsultan 2 жыл бұрын
Thanks
@danieltala9586
@danieltala9586 2 жыл бұрын
Thanks! much better than debugpy :)
@codevev
@codevev 2 жыл бұрын
I don't even know what debugpy is 😃
@alchemication
@alchemication Жыл бұрын
This is nice, thank you, I just wonder if it could somehow work with K8s, so when your container dies, VS Code still works (as container would just simply restart) ... Also would be great to see version control integrated into this.
@codevev
@codevev Жыл бұрын
Thanks! I haven't done much with K8s but let me know if you decide to try it out
@ammadkhan4687
@ammadkhan4687 2 ай бұрын
Nice explanation. Thanks . Do you some some video to map network drive in docker container?
@codevev
@codevev 2 ай бұрын
Thanks! I don't have a video about mapping network drive to a container yet. I haven't used Docker much lately. Good luck!
@basicmachines
@basicmachines 4 ай бұрын
Thanks. I couldn't find the 'Remote - Containers' extension in VS Code but I found something called 'Dev Containers' by Microsoft which sounds like the same thing. Has the name changed maybe?
@codevev
@codevev 4 ай бұрын
Just checked, Dev Containers from Microsoft extension does look correct!
@codevev
@codevev 4 ай бұрын
Correction. Dev Containers will probably work on its own but it's included in the Remote Development extension, which is a pack that not only lets you connect to Docker containers from VSCode but also ssh into other hosts
@christiangunther650
@christiangunther650 2 жыл бұрын
Hej Thx Evgeny! But could you explain the next steps on e.g. launch.json and how to configure properly? I have my problems there ... Cheers
@codevev
@codevev 2 жыл бұрын
Christian, do you run into any specific problems? Mind sharing an error message or what is going on exactly?
@davidvc4560
@davidvc4560 Жыл бұрын
Thanks! Could you clarify if the VSCode actually created the container itself or connected to an existing container? @7:16. It looks like VSCode created a container to me, because it didn't ask for container name or id.
@codevev
@codevev Жыл бұрын
It should have created a new container on its own
@davidvc4560
@davidvc4560 Жыл бұрын
@@codevev thank you!
@devashishnigam5971
@devashishnigam5971 2 жыл бұрын
How do we handle venv and requirements.txt. It would be great if you can cover that
@codevev
@codevev 2 жыл бұрын
That's really the beauty of running Python inside the Docker container! You actually don't need venv because a container is an isolated environment. Hence, no special treatment of requirements.txt is needed but of course it depends on what exactly you're doing
@f0l1v31r4
@f0l1v31r4 10 ай бұрын
And does it work for framesworks like fastAPI, Flask and Django?
@codevev
@codevev 10 ай бұрын
Yes
@emanueol
@emanueol Жыл бұрын
uou manually installed python extension in the container, i wonder if its possible docker file to also do that ?
@codevev
@codevev Жыл бұрын
Great question! You would do that in the devcontainer.json file rather than the Dockerfile
@codevev
@codevev Жыл бұрын
Check this link out: code.visualstudio.com/docs/devcontainers/containers#_adding-an-extension-to-devcontainerjson
@emanueol
@emanueol Жыл бұрын
@@codevev thanks 👍
@3MandMatt
@3MandMatt 2 ай бұрын
would you add a video for docker compose?
@codevev
@codevev 2 ай бұрын
Anything in particular about it?
@pranitpawar2200
@pranitpawar2200 Жыл бұрын
after running build command getting below error: failed to compute cache key: "/main.py" not found: not found
@codevev
@codevev Жыл бұрын
You may have a typo in your Dockerfile. Sounds like it's not finding the path to the main.py file.
@sergeyasdf2677
@sergeyasdf2677 Жыл бұрын
Hi there! How could I remote debugging on docker container?
@codevev
@codevev Жыл бұрын
Did you watch the video?
@UpToTheSkyAgain
@UpToTheSkyAgain 7 ай бұрын
I appreciate your explanations. Very clear and the step by step procedure is very nice. However I don't manage to do the last part. I get and error reopening the project in the container : [7105 ms] Start: Check Docker is running [7105 ms] Start: Run in Host: docker version --format {{.Server.APIVersion}} [7177 ms] Server API version: 1.41 [7178 ms] Start: Run in Host: docker volume ls -q [7225 ms] Host server: Error: spawn docker ENOENT at ChildProcess._handle.onexit (node:internal/child_process:283:19) at onErrorNT (node:internal/child_process:476:16) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) [7270 ms] Host server: (node:11049) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1) (Use `node --trace-warnings ...` to show where the warning was created) [7274 ms] Error: spawn docker ENOENT at ChildProcess._handle.onexit (node:internal/child_process:283:19) at onErrorNT (node:internal/child_process:476:16) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) The only difference with you is that I did the first part already in a remote access, in SSH. However "reopen in container" is supposed to work. Any idea, welcome. :-)
@codevev
@codevev 7 ай бұрын
I think when you reopen in container, it just looks for Docker running on your local host. So if you hadn't installed it there, it can't find the running instance of Docker and is throwing an error. I'm actually not sure if what you're trying to do is supported, might be a good thing for me to try figuring out. But at my new job we aren't currently using docker containers and I do all of my development on an ec2 instance via remote ssh through VSCode😂
@damircicic
@damircicic 2 ай бұрын
Hi when I click on the icon in the lower left corner and then click on any option in the list I get the message that current user does not have permission to run docker (I wanted to attach to the running container). Does anyone know how to solve this problem?
@codevev
@codevev 2 ай бұрын
Are you on windows? Try opening VSCode as administrator
@damircicic
@damircicic 2 ай бұрын
@@codevev I am on Ubuntu. Sounds promising. Thanks! I'll give it a try.
@codevev
@codevev 2 ай бұрын
I see. Probably opening VSCode with 'sudo' could work. But maybe it's something else. I don't think I ever did it on Linux, just Windows or Mac
@nedkapinill
@nedkapinill 2 ай бұрын
@@damircicic ​ I had same problem. I can open container with rootless in VS code. You can google "Run the Docker daemon as a non-root user (Rootless mode)" to fix permission problem.
@brunaguterres1817
@brunaguterres1817 Жыл бұрын
Unfortunatly there is no Remote - Containers extension on my VS code :(
@codevev
@codevev Жыл бұрын
Bruna, you will need to install it first. You can follow this link: marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack
@brunaguterres1817
@brunaguterres1817 Жыл бұрын
@@codevev Thank you so much. Nice tutorial. One of the most useful in KZfaq lately.
@codevev
@codevev Жыл бұрын
Thanks for the kind words :)
@MentoriadeSoftware
@MentoriadeSoftware Жыл бұрын
Hi codeEV, Thanks for your video, I've done a test like you showed and everything works fine with the remote, but when I want to run my .py file I get an error: Se produjo una excepción: ModuleNotFoundError No module named 'matplotlib' File "/workspaces/python/consulta.py", line 3, in import matplotlib.pyplot as plt This is my Dockerfile FROM python:3.7.13 WORKDIR /home/tips/python RUN pip install surprise RUN pip install d2l==0.17.5 RUN pip install -U mxnet-cu101==1.9.1 RUN pip install seaborn RUN pip install gluon COPY . . CMD [ "python", "./consulta.py" ] This is the consulta.py file import matplotlib.pyplot as plt #
@codevev
@codevev Жыл бұрын
Hi! If I'm reading it correctly, the pip command to install matplotlib is missing from your dockerfile
@julioalvia3619
@julioalvia3619 Жыл бұрын
Thanks
@codevev
@codevev Жыл бұрын
You're welcome!
My Favorite Way To Handle Dev Environments | VS Code Devcontainers
30:05
Docker VSCode Python Tutorial // Run your App in a Container
19:13
Christian Lempa
Рет қаралды 89 М.
Monster dropped gummy bear 👻🤣 #shorts
00:45
Yoeslan
Рет қаралды 13 МЛН
Зомби Апокалипсис  часть 1 🤯#shorts
00:29
INNA SERG
Рет қаралды 6 МЛН
ПЕЙ МОЛОКО КАК ФОКУСНИК
00:37
Masomka
Рет қаралды 8 МЛН
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 350 М.
Debugging Python in Docker using VSCode
11:15
That DevOps Guy
Рет қаралды 37 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН
Python 101: Learn the 5 Must-Know Concepts
20:00
Tech With Tim
Рет қаралды 1 МЛН
Using docker in unusual ways
12:58
Dreams of Code
Рет қаралды 379 М.
you need to learn Docker RIGHT NOW!! // Docker Containers 101
23:19
NetworkChuck
Рет қаралды 2,6 МЛН
How to containerize Python applications with Docker
26:08
Docker
Рет қаралды 77 М.
Debugging a Dockerized Django app with VSCode
23:22
London App Developer
Рет қаралды 34 М.