如何在VS Code开发容器中指定安装额外软件包?
解决Dev Container中安装额外Python/系统包的方法
1. 安装额外Python包(如jinja2-cli)
方式一:直接用postCreateCommand执行pip安装
postCreateCommand会在所有配置的features安装完成后执行,刚好适配你的需求。修改devcontainer.json添加该字段即可:
{ "name": "Debian", "image": "mcr.microsoft.com/devcontainers/base:bullseye", "features": { "ghcr.io/devcontainers/features/docker-from-docker:1": {}, "ghcr.io/devcontainers/features/github-cli:1": {}, "ghcr.io/devcontainers/features/go:1": {}, "ghcr.io/devcontainers/features/python:1": {}, "ghcr.io/guiyomh/features/golangci-lint:0": {} }, "postCreateCommand": "pip3 install jinja2-cli" }
需要安装多个包时,用空格分隔:
"postCreateCommand": "pip3 install jinja2-cli requests beautifulsoup4"
方式二:通过requirements.txt批量安装
如果有大量包需要安装,推荐用清单文件管理:
- 在项目根目录创建
requirements.txt,写入依赖:
jinja2-cli requests beautifulsoup4
- 修改
devcontainer.json的postCreateCommand:
"postCreateCommand": "pip3 install -r requirements.txt"
2. 安装系统相关包
方式一:用postCreateCommand执行apt命令
直接通过apt安装系统包,和Python包安装命令可以用分号串联:
"postCreateCommand": "sudo apt-get update && sudo apt-get install -y curl git-lfs; pip3 install jinja2-cli"
方式二:通过common-utils feature安装
利用dev containers官方的common-utils feature统一管理系统包,它会自动处理apt更新:
{ "name": "Debian", "image": "mcr.microsoft.com/devcontainers/base:bullseye", "features": { "ghcr.io/devcontainers/features/docker-from-docker:1": {}, "ghcr.io/devcontainers/features/github-cli:1": {}, "ghcr.io/devcontainers/features/go:1": {}, "ghcr.io/devcontainers/features/python:1": {}, "ghcr.io/guiyomh/features/golangci-lint:0": {}, "ghcr.io/devcontainers/features/common-utils:2": { "packages": "curl,git-lfs" } }, "postCreateCommand": "pip3 install jinja2-cli" }
内容的提问来源于stack exchange,提问作者BhanuKiran
相关产品推荐
相关产品推荐

