How do I open a VS Code dev container with Podman, and when do I need podman.sock?

December 24, 2024

Suppose you have a Python project. You want to edit the code in VS Code and run the project’s chosen version of Python in the terminal. A development container lets the project install its tools inside a container while VS Code displays the editor on your desktop and connects its terminal and debugger to that environment. The program that creates and starts containers is the container engine. Here we use Podman. For background on images, containers, and running Podman as an ordinary user, see An Introduction to Podman.1

Podman works in my terminal. How does the editor find it?

On the machine where you open the project, run podman info. Once it displays the engine information, move on to the editor. On macOS the engine runs inside a Linux virtual machine. In her Mac setup, Adriana Villela creates the VM with podman machine init and starts it with podman machine start. If you already have a VM, check its status with podman machine list. Native Linux can use the engine directly.2

Install Microsoft’s Dev Containers extension in VS Code. Open Preferences: Open User Settings (JSON) from the command palette and merge the following entry into your settings. Although the setting name mentions Docker, its value can be the Podman executable. Microsoft describes the compatibility level as “Podman version 5+ is mostly compatible with Docker's CLI commands.” CLI means command-line interface, the commands the extension can invoke.3

{
  "dev.containers.dockerPath": "podman"
}

How do I know the project actually opened in the container?

The user setting selects an engine for this machine. The project’s .devcontainer/devcontainer.json describes the development environment to open. Use the project’s existing file if it has one. To try this in an empty directory, create the file below to select a development image with Python. An image provides the files and tools used to create a container.1

{
  "name": "Python 开发环境",
  "image": "mcr.microsoft.com/devcontainers/python:1-3.12-bookworm"
}

Open the project directory in VS Code and select Dev Containers: Reopen in Container from the command palette. The extension pulls the image if needed, starts the container, and attaches the project directory. When you see the container’s name in the lower left, open a fresh integrated terminal and run python --version to check the interpreter. From another terminal on the host, run podman ps to see the running container from the engine’s side.1

The log mentions docker.sock. Which setting should I change?

Open Dev Containers: Show Container Log and find the command that failed. If the extension cannot find the docker or podman executable, check the user setting and executable path. If a tool starts and then fails to connect to /var/run/docker.sock, it is using a different interface: it sends requests to the engine through a local communication endpoint identified by a file path, a Unix socket. One example is a project that describes multiple services with Compose and starts them through a Compose implementation that needs a Docker-compatible API.34

In the latter case, if the engine runs as an ordinary user on native Linux with the systemd service manager, use that user’s terminal to check the following, as described in the Podman manual:4

systemctl --user start podman.socket
test -S "$XDG_RUNTIME_DIR/podman/podman.sock" && echo "Socket exists"
export DOCKER_HOST="unix://$XDG_RUNTIME_DIR/podman/podman.sock"

The first line starts listening on the endpoint, the second checks that the path contains a socket, and the last tells clients that read DOCKER_HOST where to connect. Rerun the client that failed from this terminal. If the editor launches it, quit VS Code completely and run code . from the terminal to reopen the project. A running process does not acquire a new value just because another terminal ran export. This directory belongs to the current Linux user. Podman machine on macOS has its own connection settings.42

If the log has already reached image pulls or package installation, the extension can call the engine. The next question is which process is contacting which site. See Where should I configure a proxy when VS Code Dev Container downloads fail?.

Footnotes

  1. Microsoft. (n.d.). Developing inside a Container. Visual Studio Code 文档,Getting started。 ↩ ↩2 ↩3

  2. Villela, A. (2024, December 6). Running Dev Containers Locally with Podman and VSCode. Women in Technology, Medium,Installing & Running Podman、Podman and VSCode。 ↩ ↩2

  3. Microsoft. (n.d.). Alternate ways to install Docker. Visual Studio Code 文档,Podman,第 1 段。 ↩ ↩2

  4. Podman contributors. (n.d.). podman-system-service. Podman 文档,Examples,rootless 用户的 socket 与 DOCKER_HOST。 ↩ ↩2 ↩3

© 2024 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2024-12-24). How do I open a VS Code dev container with Podman, and when do I need podman.sock?. https://caiborui.com/blog/2024-12-24-zh-vscode-dev-containers-podman-socket-entrans

@misc{borui2024-dbbdc74b,
  author = {borui},
  title = {{How do I open a VS Code dev container with Podman, and when do I need podman.sock?}},
  year = {2024},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2024-12-24-zh-vscode-dev-containers-podman-socket-entrans}
}