You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Docker与Podman共用同一VSCode Dev Container环境

实现Podman与Docker共用同一Dev Container环境

我在学校项目里用Dev Container效果不错,自己用Podman操作,但同学用Docker,想让我们俩能共用同一个容器环境,该怎么弄?

原配置文件

devcontainer.json(原版本,需手动注释Podman代码适配Docker)

{
    "name": "poetry3-poetry-pyenv",
    "build": {
        "dockerfile": "Dockerfile"
    },

    // 👇 Features to add to the Dev Container. More info: https://containers.dev/implementors/features.
    // "features": {},

    // 👇 Use 'forwardPorts' to make a list of ports inside the container available locally.
    "forwardPorts": [8080],

    // 👇 Use 'postCreateCommand' to run commands after the container is created.
    "postCreateCommand": "bash ./.devcontainer/post-install.sh",

    // 👇 Configure tool-specific properties.
    "customizations": {
        "vscode": {
            "extensions":[
            "ms-python.python",
            "njpwerner.autodocstring",
            "tamasfe.even-better-toml",
            "ms-azuretools.vscode-docker",
            "bradlc.vscode-tailwindcss"]

        }
    },
    "mounts": [
        // Re-use local Git configuration
        "source=${localEnv:HOME}/.gitconfig,target=/home/vscode/.gitconfig,type=bind,consistency=cached"
    ],

    "remoteUser": "root",
    "containerUser": "node",
    
    //Podman. Comment out when using Docker
    "workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind,Z",
    "workspaceFolder": "/workspace",

    "runArgs": ["--userns=keep-id"]
}

Dockerfile

# FROM mcr.microsoft.com/devcontainers/base:${templateOption:imageVariant}
FROM node:18-bullseye-slim

ARG DEBIAN_FRONTEND=noninteractive
ARG USER=node

RUN DEBIAN_FRONTEND=noninteractive \
    && apt-get update \ 
    && apt-get install -y build-essential --no-install-recommends make \
        ca-certificates \
        git \
        libssl-dev \
        zlib1g-dev \
        libbz2-dev \
        libreadline-dev \
        libsqlite3-dev \
        wget \
        curl \
        llvm \
        libncurses5-dev \
        xz-utils \
        tk-dev \
        libxml2-dev \
        libxmlsec1-dev \
        libffi-dev \
        liblzma-dev
# Shap/llvm-lite dependency
RUN DEBIAN_FRONTEND=noninteractive \
    && apt-get install -y llvm-dev
# Git LFS
RUN DEBIAN_FRONTEND=noninteractive \
    && apt-get install -y git-lfs
# Python and poetry installation
USER $USER
ARG HOME="/home/$USER"
# ARG PYTHON_VERSION=${templateOption:pythonVersion}
ARG PYTHON_VERSION=3.11

ENV PYENV_ROOT="${HOME}/.pyenv"
ENV PATH="${PYENV_ROOT}/shims:${PYENV_ROOT}/bin:${HOME}/.local/bin:$PATH"

RUN echo "Starting pyenv download" \
    && curl https://pyenv.run | bash \
    && echo "Installing Python" \
    && pyenv install ${PYTHON_VERSION} \
    && echo "Setting python version as global" \
    && pyenv global ${PYTHON_VERSION} \
    && echo "downloading Poetry" \
    && curl -sSL https://install.python-poetry.org | python3 - \
    && poetry config virtualenvs.in-project true

RUN git lfs install

解决方案:修改devcontainer.json实现双环境自动适配

不需要手动注释代码,通过条件化配置让文件自动识别Podman和Docker,步骤如下:

1. 修改后的devcontainer.json

{
    "name": "poetry3-poetry-pyenv",
    "build": {
        "dockerfile": "Dockerfile"
    },
    "forwardPorts": [8080],
    "postCreateCommand": "bash ./.devcontainer/post-install.sh",
    "customizations": {
        "vscode": {
            "extensions":[
                "ms-python.python",
                "njpwerner.autodocstring",
                "tamasfe.even-better-toml",
                "ms-azuretools.vscode-docker",
                "bradlc.vscode-tailwindcss"
            ]
        }
    },
    "mounts": [
        // Re-use local Git configuration,修正目标用户路径
        "source=${localEnv:HOME}/.gitconfig,target=/home/node/.gitconfig,type=bind,consistency=cached"
    ],
    "remoteUser": "node",
    "containerUser": "node",
    // 条件适配Podman的SELinux挂载标签与Docker默认挂载
    "workspaceMount": "${localEnv:CONTAINER_RUNTIME}==podman ? source=${localWorkspaceFolder},target=/workspace,type=bind,Z : source=${localWorkspaceFolder},target=/workspace,type=bind",
    "workspaceFolder": "/workspace",
    // 仅Podman添加--userns=keep-id参数,Docker忽略
    "runArgs": "${localEnv:CONTAINER_RUNTIME}==podman ? [\"--userns=keep-id\"] : []"
}

2. 关键修改说明

  • 用户路径修正:原mounts中Git配置的目标路径是/home/vscode/.gitconfig,但Dockerfile使用node用户,改为/home/node/.gitconfig避免权限不匹配。
  • 条件化工作区挂载:利用Dev Container的环境变量插值语法,根据CONTAINER_RUNTIME的值判断:Podman需要Z标签处理SELinux上下文,Docker无需此标签。
  • 条件化运行参数:--userns=keep-id是Podman特有的参数,用于让容器内用户与宿主机用户UID/GID一致,避免文件权限问题;Docker下自动跳过该参数。

3. 使用方式

  • Podman用户:启动Dev Container前,在终端设置环境变量:
    export CONTAINER_RUNTIME=podman
    
    或者在VS Code的Dev Container配置中添加这个环境变量。
  • Docker用户:无需额外设置,直接启动即可,配置会自动适配Docker环境。

4. Dockerfile无需修改

Dockerfile负责构建容器镜像,与容器运行时(Podman/Docker)无关,两种工具都能正常执行镜像构建流程。


内容的提问来源于stack exchange,提问作者TeaDrinkingProgrammer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 03:26:20