In my case, I could not use runArgs in the devcontainer.json because we are using dockerComposeFile.
The solution was to configure the per-service platform inside the relevant docker-compose.yml, e.g.:
version: "3"
services:
some-service-only-as-amd64:
platform: linux/amd64
build:
context: .
dockerfile: Dockerfile
ports:
- 5000:5000
volumes:
# ... and so on
Answer from Charl Botha on Stack OverflowSpecify platform for Docker image in Visual Studio Code DevContainers - Stack Overflow
How fix Python import error in VS Code editor when using a Dev Container? - Stack Overflow
Python Devcontainer Setup for VSCode
Docker Devcontainers in VScode is really slow
In my case, I could not use runArgs in the devcontainer.json because we are using dockerComposeFile.
The solution was to configure the per-service platform inside the relevant docker-compose.yml, e.g.:
version: "3"
services:
some-service-only-as-amd64:
platform: linux/amd64
build:
context: .
dockerfile: Dockerfile
ports:
- 5000:5000
volumes:
# ... and so on
For me, I got it to work by adding --platform=linux/amd64 to the build.options property in devcontainer.json. e.g.:
{
// Other properties
"build": {
"context": "..",
"dockerfile": "Dockerfile",
"options": [
"--platform=linux/amd64"
]
},
// Other properties
}
"context": ".."specifies the build context directory."dockerfile": "Dockerfile"points to the Dockerfile used for the build."options": ["--platform=linux/amd64"]adds the--platformoption to target thelinux/amd64architecture.
In the json reference for devcontainer.json it says that you can add Docker image build options in the build.options property.
References:
- devcontainer.json Reference
- Image-specific JSON Reference
- Docker Buildx Build Options
- Docker Buildx Platform Options
I often have this same issue with Python+Devcontainer. What works for me is:
setting this env variable:
export PYTHONPATH=/workspace, you can also try with/workspacesor/yourProjectNamedepending on your setup.restart the Python Language Server with
Ctrl+Shift+P>Python: Restart Language Server.
Note that you do not need to rebuild the container. When you find the correct value for PYTHONPATH, you can add it to your Devcontainer configuration.
Is this issue completely resolved? I believe it has to do something with your directory structure.
Please have a look at recommended directory structure here
.devcontainer/
devcontainer.json
.devcontainer/
Dockerfile
README.rst
LICENSE
setup.py
requirements.txt
foo/
__init__.py
foo/
bar.py
tests/
test_bar.py
docs/
conf.py // optional
docs/
index.rst // optional
If you don't want to disturb your directory structure, you may consider below two entries:
- In the
devcontainer.jsonfile, givecontext:"." - In the
Dockerfile, adding appropriate entry forWORKDIR
I'm starting to look into devcontainers with VSCode and I am looking for a container configuration (Dockerfile/devcontainer.json) that sets up a good Python development environment with some recommended extensions and settings (e.g. paths, venv, fromatting, linting) for beginners (its been some time since I was doing Python development), but that's not over-complicated.
I have the following resources that I have pieced together as a starting point and could use some input and feedback on these (I'm not sure if everything makes sense) and what to improve for a good development experience. Particularly I'm looking for input on the VSCode extensions / settings.
docker-compose.dev.yaml
version: '3.9'
networks: app-dev-network:
driver: bridge
name: app-dev-network
services:
backend:
image: backend-dev-image
container_name: backend-dev-container
build:
context: ./
dockerfile: ./Dockerfile.dev
volumes:
- ..:/workspace:cache
networks:
- app-dev-network
ports:
- target: 5000
published: 5000
command: sleep infinityDockerfile.dev
ARG VARIANT="3.11-bookworm"
FROM mcr.microsoft.com/devcontainers/python:${VARIANT}
RUN apt-get update && \
apt-get upgrade --yes --no-install-recommends && \
apt-get clean
ENV PYTHONUNBUFFERED=1devcontainer.json
{
"name": "Backend",
"dockerComposeFile": [
"./docker-compose.dev.yaml"
],
"service": "backend",
"workspaceFolder": "/workspace",
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
],
"settings": {
"python.pythonPath": "${workspaceFolder}/.venv/bin/python",
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
}
}
},
"postCreateCommand": "pip3 install --user -r requirements.txt"
}