Creating a Clean Python Development Environment using Docker and Visual Studio Code

Python

Python is a high-level, dynamically-typed programming language that has taken the software development industry by storm. It’s known for its simplicity, readability, and vast library ecosystem. Python has become the language of choice for many in web development, data science, artificial intelligence, scientific computing, and more. Its versatile nature makes it ideal for both beginners and experienced developers.

Docker

Docker is a revolutionary tool that allows developers to create, deploy, and run applications in containers. Containers can be thought of as lightweight, stand-alone packages that contain everything needed to run an application, including the code, runtime, libraries, and system tools. Docker ensures that an application runs consistently across different environments, eliminating the infamous “it works on my machine” problem. It simplifies the process of setting up, distributing, and scaling applications, making it an invaluable tool for modern development.

Visual Studio Code

Visual Studio Code (VS Code) is a powerful, open-source code editor developed by Microsoft. It provides a lightweight yet feature-rich environment that supports a multitude of programming languages, including Python. With a vast ecosystem of extensions, integrated Git support, debugging capabilities, and an intuitive interface, VS Code has quickly become the editor of choice for many developers around the world.

Why Combine Python, Docker, and Visual Studio Code?

You might be wondering why one would want to combine Python, Docker, and Visual Studio Code. The answer lies in the fusion of simplicity, consistency, and efficiency. By using Docker, you can ensure that your Python application runs the same way, irrespective of where it’s deployed. This means no more headaches about dependency issues or system incompatibilities. On the other hand, VS Code provides a seamless development experience, with features that play nicely with both Python and Docker. Combining these three tools gives you a streamlined, consistent, and efficient development workflow.

Steps to Set Up Your Dev Environment:

  1. Install Prerequisites:
    • Install Docker and ensure it’s running.
    • Download and install Visual Studio Code.
    • Install the ‘Python’ and ‘Docker’ extensions from the Visual Studio Code marketplace.
  2. Setup Docker:
    • Create a new directory for your project.
    • Inside this directory, create a file named Dockerfile.
    • In the Dockerfile, start with the following content:

    • Create a requirements.txt file in the same directory, listing any Python libraries your project depends on,following content:

      numpy
      pandas

      or you can specify the library version:

      tensorflow==2.3.1
      uvicorn==0.12.2
      fastapi==0.63.0

  3. Build the Docker Container Image:
    • In VS Code, open the folder containing your Dockerfile and other project files.
    • Use the Docker extension to build your Docker image by right-clicking the Dockerfile and selecting ‘Build Image’ or run the command
      docker build -t mypythonenv .



    • Run the container and mount your working directory or folder where you have your Python code into the container
      docker run -it --rm -v C:\Users\Sarmad\Projects\MyPythonProject:/usr/src/app mypythonenv



  4. Attach the running Docker Container
    • Attach the running Python container into Visual Studio Code to run and debug your Python Code, click on the Docker icon, then right-click on the running container (in our example called “mypythonenv”) then attach it to Visual Studio Code


    • We have now Visual Studio Code accessing the Python environment running inside the Docker container, the container has access to your Python code files that were mounted in the docker run command line


  5. Run the Python code
    • To run our “hello-world.py” code, click on the Run and Debug icon, then the blue “Run and Debog” button, select Python File.


    • The Python Code will be running inside your container


  6. Clean Up & Share:
    • Once done with development, you can push your Docker image to a registry (like Docker Hub) or your own private registry for sharing or deployment.

By following these steps, you’ll have a Python development environment that’s clean, consistent, and easy to use.

Happy coding!