1. Install Visual Studio Code app from here
2. Start Docker app and wait for it to be completely ready - it will take some time for the first time. Open Terminal app and check that everything is working by running any docker command (there should be no errors or warning raised), for example
$ docker images |
3. Start Visual Studio Code app
4. As you probably realized fresh installation of Visual Studio Code is not configured for Python with Docker by default. But we can make Visual Studio Code prepare this configuration for us.
I would suggest to create two files in some folder on your Mac - one file will have name Dockerfile and another will be some with python code - example.py in our case. You can just copy those 2 files from our other tutorial here:
Dockerfile content:
example.py content:
5. Lets open folder - it is tmp_py in our case, with those 2 files in the Visual Studio Code by selecting menu "Open -> Folder" and also trusting content of this folder in the dialog of Visual Studio Code
6. After it opens - lets tap on Dockerfile file first - you will notice that Visual Studio Code will detect that there is existing editor extension for this type of files and offer you to install it. You can take a look into details - it is "Docker" from Microsoft, let's install it.
7. After installation of extension been finished - select option to "Add Docker Files to Workspace" and select Python:General in context menu for type of docker file.
8. For "app's entry point" select example.py from the drop down in the next Visual Studio Code dialog and answer "No" to "Include optional Docker Compose files" question. Also select option to overwrite Dockerfile in your project after this dialog.
9. This is how automatically generated Dockerfile looks like
# For more information, please refer to https://aka.ms/vscode-docker-python FROM python:3.8-slim # Keeps Python from generating .pyc files in the container ENV PYTHONDONTWRITEBYTECODE=1 # Turns off buffering for easier container logging ENV PYTHONUNBUFFERED=1 # Install pip requirements COPY requirements.txt . RUN python -m pip install -r requirements.txt WORKDIR /app COPY . /app # Creates a non-root user with an explicit UID and adds permission to access the /app folder # For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app USER appuser # During debugging, this entry point will be overridden. # For more information, please refer to https://aka.ms/vscode-docker-python-debug CMD ["python", "example.py"] |
10. Tap on example.py file in the Explorer area and select "Install" option for recommended Visual Studio Code extension as well - Python extension from Microsoft will be installed in this step
11. Lets tap on automatically generated Dockerfile and in "secondary click" drop down menu select option to "Build Image..."
12. After this - let's make small change to our python code as following
13. Select "Run And Debug" section in the left menu bar in Visual Studio Code. On the top of section of "Run and Debug" you can see green triangle for execution of code with your docker image, you can also select red dot next to second line of python code to make execution of code to stop at this point when execution is started - notice how variable value also shown at this point of your python code execution
14. You can refer to more details on how to interact with your code within Visual Studio Code documentation