DEV Community

Yussy
Yussy

Posted on

Try Python without Python (using VS Code + Docker)

What You Will Learn

In this article, we’ll cover:

  • Setting up Docker Desktop from scratch.
  • Configuring VS Code Dev Containers for a seamless workflow.
  • Running Python in a clean environment to keep your local machine clutter-free.

Introduction

I’ve always been searching for the "perfect development environment":

  • Lightweight (like VS Code)
  • Powerful (like Visual Studio)
  • Clean (no more messing up my OS with various languages)

After asking an AI for advice, I found the perfect "Triple Threat" combo: VS Code + Dev Containers + Docker.

In this post, I’ll show you how to set up this environment for Python development.

Note: I am using Windows (Japanese version), so some parts of the screenshots will be in Japanese.

Steps

Step 1: Install and Set Up Docker Desktop

Visit the official Docker website and download the installer.
Since I'm using Windows 11 Home, I chose "Download for Windows - AMD64".

Run Docker Desktop Installer.exe as an administrator.

Why you should run as administrator
If you don't run the installer with administrator privileges, it will fail with an error.
To avoid this, right-click the file and select Run as administrator.

First, check Add shortcut to desktop to create a shortcut on your desktop, then click OK to start the installation.

Once the Installation succeeded message appears, click Close.

If the installer fails because files already exist

If you encounter an error like this:

It means old Docker files are blocking the installation. To fix this, delete these two folders:

  • C:\Program Files\Docker
  • C:\ProgramData\DockerDesktop

After deleting them, run Docker Desktop Installer.exe once more.


If the installation fails with a "failed to install features" error
If you see an error about "failed to install features (exit code 50)", it means Windows virtualization features are disabled.

How to fix:

  1. Search for Turn Windows features on or off in your Start menu.

  2. Check Windows Subsystem for Linux and Virtual Machine Platform, then click OK.

  3. Restart your PC. (Note: If the process hangs after clicking OK, you can force quit it via Task Manager and then restart.)

  4. Run the Docker Desktop Installer.exe again.

After the installation, launch Docker Desktop.

Click Accept.

Continue without signing in.

Now, wait for a few moments while you see the message Starting the Docker Engine....

What if WSL is outdated?
If the engine fails to start, you may need to update WSL.
Open Windows PowerShell as an administrator and run wsl --update.
Then, click Try Again in Docker Desktop.

Once you see "Engine running," the setup is complete.

Step 2: Install the Dev Containers Extension

Launch VS Code and install the Dev Containers extension.

Step 3: Run Python in a Container

Now, let’s create a clean Python environment. We will generate the necessary configuration files to run Python inside a container.

Prepare your workspace

First, create a new folder named python-test (or any name you like) on your computer. Then, in VS Code, go to File > Open Folder and select that folder.

Add Container Configuration

Click the >< (Open a Remote Window) icon in the bottom-left corner of the VS Code window.

A menu will appear at the top. Select Add Dev Container Configuration Files....

When prompted, select "Add configuration to user data" (or workspace).

Next, choose "Python 3" from the list of definitions.

For the version, select the default (e.g., 3.14-trixie).

You will then see a list of additional features to install. You don't need any for now, so just click OK (and OK again on the next screen).

After a few moments, a folder named .devcontainer will be created, containing a devcontainer.json file. This file tells Docker how to build your environment.

Run Python in Your Container

Finally, let’s launch the container and run some code!

Click the >< icon in the bottom-left corner again and select Reopen in Container from the menu.

What if I get an error? (Check Docker Status)
If you closed your PC for a break, Docker might still be "sleeping." Docker needs to be awake to run your container.

Open Docker Desktop and make sure the status is Engine running. Once Docker is awake, try Reopen in Container again.


Once the container is ready, the bottom-left corner will show Dev Container: Python 3.

Now, create a new file named main.py in the same folder as your .devcontainer folder. (Keep the .devcontainer folder for your config files only.)

Copy and paste the following code into main.py:

print("Hello from Docker!")

import sys
print(f"Python version: {sys.version}")
Enter fullscreen mode Exit fullscreen mode

Click the "Run Python File" (triangle icon) in the top-right corner.

Congratulations! You just executed Python inside a Docker container. Your local Windows environment remains perfectly clean.

Step4: Cleaning Up and Shutting Down

When you are finished with your work, it is a good habit to close the connection and stop Docker.

Click the Dev Container: Python 3 in the bottom-left corner and select Close Remote Connection from the menu. This will safely disconnect VS Code from your Docker container.

To save your PC's resources, you should also quit Docker.
Click the ^ (Show hidden icons) in your Windows Taskbar.
Right-click the Docker whale icon.
Select Quit Docker Desktop.

Top comments (0)