How to run Python scripts in a Docker container

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024
  • I showcase how to build and run Docker on windows. I create a ubuntu-based Docker Image and then run it to print "Hello World". To showcase the functionality I jump into the docker container and show some basic Linux functionality.
    Commands:
    Build a docker image:
    docker build -t [tag] [dockerfile position]
    Create and run a Docker container
    docker run [docker image]
    How to get into a docker container
    docker run -it [docker image] /bin/bash
    My channel publishes videos regarding programming, software engineering especially Java and JavaFX. If that is what you are looking for then do not hesitate to join me on this journey!
    Subscribe to my TH-cam channel:
    / @randomcode_0
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 63

  • @SolomonLeo_
    @SolomonLeo_ หลายเดือนก่อน +2

    I'm someone who doesn't know anything about Docker, and this video helped me understand a lot of concepts in 9 minutes, wow

    • @Randomcode_0
      @Randomcode_0  หลายเดือนก่อน +1

      Glad to hear that!

  • @Julian-tf8nj
    @Julian-tf8nj 2 ปีที่แล้ว +6

    Best Docker tutorial of many I've watched, thanks!! (Ideal for people who already have a general sense of Docker's high-level architecture)

    • @Randomcode_0
      @Randomcode_0  2 ปีที่แล้ว

      Glad you liked my video! But yea this is not an introduction to Docker.

  • @srujjhanaadevy3712
    @srujjhanaadevy3712 ปีที่แล้ว

    Very Good tutorial, after watching this I am able to run the selenium pytest in the centos docker container.

    • @Randomcode_0
      @Randomcode_0  ปีที่แล้ว

      Thanks for your comment! I'm glad the tutorial helped you get your Docker working.

  • @kartikeyareddyk9581
    @kartikeyareddyk9581 2 ปีที่แล้ว +1

    You are just awesome in explaining really simple. Please keep doing more....... thank you very much. FEEDBACK: Please remove the blue background in the terminal. Not so clear to see what files are there when you clicked. If possible use white.

    • @Randomcode_0
      @Randomcode_0  2 ปีที่แล้ว +1

      Thank you so much! In this video, I am on windows and I, therefore, use PowerShell with definitely isn't the most optimal for clarity, but it is part of the windows "charm". Your feedback is appreciated and noted :)

  • @muhammadroshanmughees5946
    @muhammadroshanmughees5946 ปีที่แล้ว +2

    great tutorial for someone who knows concept of dockers but never used it

    • @Randomcode_0
      @Randomcode_0  ปีที่แล้ว

      Glad you liked my video :)

  • @edydon
    @edydon 11 หลายเดือนก่อน

    Incredibly helpful. Keep up the great work!

    • @Randomcode_0
      @Randomcode_0  11 หลายเดือนก่อน

      Glad it was helpful!

  • @mkrtichhovhannisyan6244
    @mkrtichhovhannisyan6244 2 ปีที่แล้ว +1

    Great tutorial, helped me to understand the basics.

  • @jentcold
    @jentcold 6 หลายเดือนก่อน +3

    Is the audio lagging or is that only my device?
    The tutorial is pretty nice but I'm having a hard time with the audio. Lol

    • @SmartVanture
      @SmartVanture 6 หลายเดือนก่อน +2

      I was really confused about all the positive comments never mentioning the horrible audio and thought it was on my side. Reloaded, used another device but always the same problem. I really have to stopp it and find another tutorial. For non native speakers it's really not usable. :/

  • @cs_dequeue
    @cs_dequeue 2 ปีที่แล้ว +1

    Thank you so much bro. it is awesome explaining!!

  • @EnglishRain
    @EnglishRain ปีที่แล้ว +1

    Excellent tutorial thanks a lot!

  • @williamzhang7083
    @williamzhang7083 ปีที่แล้ว

    Exactly what I needed

  • @hemantjoon
    @hemantjoon 2 ปีที่แล้ว

    Short and crisp

  • @ing.riccoskombar
    @ing.riccoskombar 11 หลายเดือนก่อน

    thank you! very helpful

    • @Randomcode_0
      @Randomcode_0  11 หลายเดือนก่อน

      Glad it helped!

  • @pluralvistamaar4121
    @pluralvistamaar4121 2 ปีที่แล้ว +1

    Thank you for this well explained video. I have a question: where do I create my python environment, in or outside the container?

    • @Randomcode_0
      @Randomcode_0  2 ปีที่แล้ว

      In this video we create our docker container based on an Ubuntu image, we then install python inside this container to create an ubuntu environment with python installed.

    • @KingErasmos
      @KingErasmos ปีที่แล้ว

      The container is the isolation layer, so assuming you don’t want to run multiple Python apps in the one container (probably don’t) then you don’t need to configure the Python venv environment.

  • @john_hind
    @john_hind 11 หลายเดือนก่อน

    The bit I just do not get is right at the start: 'FROM ubuntu:latest', but we are on Windows and we keep being told that containers are not virtual machines! How did 'ubuntu' get in on the act and what would be the significance if we selected a different Linux distro as the base? Do we need to do anything different if we are running, say, on a Raspberry Pi?

    • @Randomcode_0
      @Randomcode_0  11 หลายเดือนก่อน

      You are correct in stating that a container is not a virtual machine, but in practice, it works in a similar way. The main difference is that you will have a build file (Dockerfile) for a container, which then defines all the expected functionality, therefore not needing any manual interaction with the container. This allows you to have a prebuilt setup for any service, which can then be started, or even more instances can be started if needed.
      The first line in the Dockerfile defines the base image from which we will build, which doesn't necessarily have to be an operating system but can be an OS with some prebuilt functionality. Nevertheless, it still provides a 'virtual environment.' For instance, a container built from a MySQL base image is essentially an image built on top of some other Linux base image with added MySQL functionality. Similarly, a Python base image could be an any Linux distribution with Python pre-installed. This is a powerful feature as we can build upon other base images and start with predefined environments containing prebuilt functionality.
      Hope this explanation helps, otherwise, feel free to ask more questions.

    • @john_hind
      @john_hind 11 หลายเดือนก่อน

      @@Randomcode_0 I thought I'd understood that containerization provides isolation of applications on a common operating system while virtual machines run multiple instances of potentially dissimilar OSs on a common platform. So containerization is an isolation layer between applications on a common OS while virtualization is an isolation and translation layer between operating systems and an underlying platform operating system or hypervisor. But this does not explain how Docker (containerization) can allow a Linux app to run on Windows.

    • @Randomcode_0
      @Randomcode_0  11 หลายเดือนก่อน

      You are correct that the intention is to run isolated applications on a common OS in separate containers. How it actually works is Docker for Windows uses a technology called 'LinuxKit' to run Linux containers on Windows. It doesn't run a full Linux VM but rather a lightweight Linux subsystem. It's important to note that this is not a full Linux VM like you would have in traditional virtualization. It's a highly optimized and minimal Linux environment that only provides what's necessary to run containers. In short, Docker for Windows uses 'LinuxKit' to run Linux containers on a lightweight internal Linux VM, bridging Windows and Linux environments.

    • @john_hind
      @john_hind 11 หลายเดือนก่อน +1

      @@Randomcode_0 Thanks, Random (if I may call you that!). This has clarified a lot for me. Docker containers basically run on Linux (unless otherwise stated), but the Docker system itself installs a virtual machine on Windows to allow Linux containers to run (something like WSL?). All the Linux containers are running on the same virtual machine. But I think for the tutorial it would be clearer if you used the 'Python:3' official base image.

  • @user-ur7nq8nl1v
    @user-ur7nq8nl1v 5 หลายเดือนก่อน

    It’s easy to understand,thank U~😄

  • @dannythedabbler
    @dannythedabbler 2 ปีที่แล้ว

    I don't know why but there are entire sections where I'm only picking up parts of words audio wise. Is there an audio issue with the video?

    • @Randomcode_0
      @Randomcode_0  2 ปีที่แล้ว +1

      Yeah there seems to be some parts where the sound is a bit funky. Thanks for letting me know!

  • @TN-cx4qi
    @TN-cx4qi 11 หลายเดือนก่อน

    I know in this instance installing nano in the container doesn’t really make a difference but do not get in the habit of doing this. If you install something in the container manually make sure you add that to the Dockerfile.

    • @Randomcode_0
      @Randomcode_0  11 หลายเดือนก่อน +1

      You are correct, and I would even add that you should not have to change anything manually inside a container, as the point is to have an instance that can be rebuilt and still have the same functionality.

  • @cueva_mc
    @cueva_mc 2 ปีที่แล้ว

    How do you create the Dockerfile?

    • @Randomcode_0
      @Randomcode_0  2 ปีที่แล้ว

      You create a new file and call it "Dockerfile" with no extension

  • @karantiwari3993
    @karantiwari3993 2 ปีที่แล้ว

    I got the gist of docker in 9 crisp minutes only issue I faced was when we were in the container and we changed our code basically the python file. When we exited and ran the same image again the changes weren't visible, how to run so that changes that were made are visible?

    • @Randomcode_0
      @Randomcode_0  2 ปีที่แล้ว +3

      The concept of docker is that we can create an image which can be run from a base state, we will never be able to keep the changes made inside a container unless the container is sending this information to somewhere else like a database. If we want to make changes we would make them externally and make a new image and a container. Think of it as we would push changes to Github and then have some automatic DevOps retrieve it and run it on some servers, it is not optimal to make changes in something running on live servers.

  • @GelsYT
    @GelsYT 2 ปีที่แล้ว

    AMAZING THANKS

    • @Randomcode_0
      @Randomcode_0  2 ปีที่แล้ว +1

      Glad you like it!

    • @GelsYT
      @GelsYT 2 ปีที่แล้ว

      Definitely! Just wondering what if we're going to update the container? So we'll be changing code of the DOCKERFILE and add more print on the .py file
      How do we update it? Are we going to just do the same thing? THANKS!

  • @wave34a
    @wave34a 2 ปีที่แล้ว +1

    Sound is very choppy.. not sure why?

    • @Randomcode_0
      @Randomcode_0  2 ปีที่แล้ว +2

      Yeah there seems to be some parts where the sound is a bit funky. Thanks for letting me know!

    • @wave34a
      @wave34a 2 ปีที่แล้ว

      @@Randomcode_0 Thanks for the video though! Was very helpful!

  • @pranitpawar2200
    @pranitpawar2200 2 ปีที่แล้ว

    after running build command getting below error:
    failed to compute cache key: "/main.py" not found: not found

    • @Randomcode_0
      @Randomcode_0  2 ปีที่แล้ว

      It seems like the main.py file is not where it is supposed.

  • @re.liable
    @re.liable 2 ปีที่แล้ว

    From this vid I get the sense that a Docker image with Ubuntu as base is kinda like a lightweight Ubuntu install. Makes me think of VMs and WSL/2
    Can you give like an overview of Docker vs WSL vs VMs? I've tried searching but most answers are going over my head... 🤯

    • @Randomcode_0
      @Randomcode_0  2 ปีที่แล้ว +1

      Docker would be used on a server like VMs, the main difference is that docker is more lightweight and we build the setup of docker compared to a VM where we build inside the VM. Docker, therefore, allows us to create the setup of an application that can be run anywhere.
      The concept of containerization (Docker is a specific technology) is often used as a part of microservices with clusters like Kubernetes.
      I have never used WSL myself, but I know of people using it. WSL seems more like a tool for windowd alowing windows user to work in a ubunutu enviorment, which cna make some types of develpoment easier.
      I might make a video discussion the topic time will tell.

    • @re.liable
      @re.liable 2 ปีที่แล้ว

      @@Randomcode_0 I was mainly confused by the terms "Docker WSL2 backend." From what I can tell, there are apparently 2 ways to install Docker in a Windows machine:
      1. Docker Desktop, as a Windows program
      2. Inside WSL2, so kinda like as a Linux program, only (no access to Windows files)
      Not that I have experience with WSL/2 either lol. I might pick up learning Docker at a later time, build apps using it. Its an exciting tech.
      Hoping for your growth! You have a really nice channel here 👍🏼

    • @Randomcode_0
      @Randomcode_0  2 ปีที่แล้ว

      @@re.liable You got it right! As you have stated, we need to have the docker backend running to use. On windows, we install docker desktop. WSL is just a kind of VM running some Linux environment, and on Linux, we would install docker and it can always be run. Therefore, it would be the same on either WSL a computer with Ubuntu, or any Linux server.

  • @TheSubhayan1985
    @TheSubhayan1985 ปีที่แล้ว

    How about libraries? Lets say I have to import pandas in the program. How do I add the dependency?

    • @Randomcode_0
      @Randomcode_0  ปีที่แล้ว

      The docker container is like a minimal and compressed virtual environment, in the dockerfile we would run commands that install whatever we want. In your case we would:
      1. install python with pip
      2. use pip to install pandas or any other library
      This then allows for a docker environment that can be run quickly and has the same state every time. Everything should always be set up in the dockerfile, we never want to change anything after the container has been started.

  • @ajayjoseph8722
    @ajayjoseph8722 ปีที่แล้ว

    The tutorial is pretty good but it will be a lot better if you could show the application first of all then use the docker and share and bring it out of the docker to like a production app if that makes sense. There are a lot of things missing. I am still not sure what the advantage of a docker is. It cannot display GUI, it needs requirement.txt, use relative paths (local file) etc. If you know the files required for an application, why can't you just focus on installing the right packages? Or is it, once you make an app (for ex an exe app) will it contain all the necessary files so you can share it with someone? If you develop an exe file you cant ask a novice to install docker and go through all the steps just to see it working, when you can do all of it on a virtual environment (minus sharing it with other users). I am very much lost and I hope it makes sense.

    • @Randomcode_0
      @Randomcode_0  ปีที่แล้ว +1

      Docker is a lightweight virtual environment that allows us to predefine all the setup required to start a completely new service in seconds. A use case could be follows:
      1. Build a local service, which could be a frontend or backend (mostly used for web).
      2. Dockerize the service, creating a docker image that can be run on any environment, solving the "it worked on my machine" problem.
      3. Push these docker containers to a cloud provider such as Google Cloud or AWS.
      4. Because these containers can be started in seconds, the number of running instances of the same service in the cloud can be scaled up or down as needed.
      Docker and containerization are closely tied to many modern cloud solutions.
      Another use case could be a company with separate frontend and backend departments. The frontend team may want to test their frontend service against a local backend but may not understand the backend concepts. If the backend team has built its service into a docker container, the frontend team can simply start a docker container and know which ports they need for connections. For large companies with many teams working with multiple programming languages, everyone can work together using docker as a common technology.
      I hope these simple examples of use cases make the functionality a bit easier to understand

    • @AKKJ420
      @AKKJ420 ปีที่แล้ว

      ​@@Randomcode_0Brilliant so docker is mainly for web applications. I don't think it's any good for GUI or live server like hotreload for kivy. Silly question, how does docker differ from me creating a bash script which installs a virtual environment and the all other files. Test it and share the bash script with others along with the source code. Would this be easier than setting up docker? I know for a fact that hotreload works on virtual environment but sharing ve is impossible.

    • @Randomcode_0
      @Randomcode_0  ปีที่แล้ว +1

      Docker is not only used for web applications but it is widely used in that domain. It can also be used for GUI applications but may require additional configuration for display forwarding which can sometimes be problematic in my experience.
      The critical difference between Docker and a bash script is that Docker provides containerization technology that packages an application and its dependencies into a portable unit that can run consistently across different environments. This is especially useful when deploying applications to different platforms or collaborating with developers with different development environments. While a bash script can work, it may not be as portable or consistent as using Docker and may require additional configuration for each developer or environment.

    • @AKKJ420
      @AKKJ420 ปีที่แล้ว

      @@Randomcode_0 cheers buddy very kind of you.

  • @ruzanb433
    @ruzanb433 2 ปีที่แล้ว

    This video is great, thanks wonder if you could help me with saving the GNU nano where we edited the code

    • @Randomcode_0
      @Randomcode_0  2 ปีที่แล้ว +1

      When I save in nano I use "CTRL" + "s" to save and then "CTRL" + "x" to exit. Or you can just "CTRl" + "x" and then a prompt will ask if you want to save then just input "y".