dockervisual-studio-codepodman

How to attach Visual Studio Code terminal to a podman container?


I have installed the podman CLI on the Ubuntu 22.04 operating system with the following commands.

sudo apt-get update
sudo apt-get -y install podman

For testing purposes, I pulled the python:slim docker image and then ran a new container from that image.

podman pull python:slim
podman run -dt python:slim

Now I would like to attach the Visual Studio Code terminal to the recently created container. Therefore, I installed the docker extension from VS Code. However, I got the following warning.

Failed to connect. Is Docker installed?


Solution

  • Downgrade Docker Extension

    To fix the problem, I had to downgrade the Docker extension to 1.22.2 version because there is a known breaking change at the 1.23.0 docker extension version. Downgrade the Docker extension: right-click on the Docker extension > select the Install Another Version... option > select the 1.22.2 version.

    code --install-extension ms-azuretools.vscode-docker@1.22.2
    

    Alternatively, you can also use the terminal to downgrade the extension.

    Configure Settings

    The next step was to configure the docker.host and the docker.dockerPath variables under the settings. Before that, we need to create a docker daemon equivalent, since Podman is daemonless. It is achieved by enabling the podman.socket unit.

    systemctl enable --now --user podman.socket
    

    It exposes a Unix socket that can be used as the docker host. Next, update the docker settings.json with the following values:

    {
        "docker.host": "unix:///run/user/1000/podman/podman.sock",
        "docker.dockerPath": "/usr/bin/podman"
    }
    

    Alternatively, you can search for the Docker: Docker Path and the Docker: Host section in the Settings and update their value.

    On Linux, you can identify the Podman socket location by:

    podman info --format '{{.Host.RemoteSocket.Path}}'
    

    And the Podman CLI path using this command:

    which podman
    

    Attach Terminal

    Now, you can attach the terminal to the container. First, start the container then right-lick on it and select the Attach Shell option.

    attach_shell_to_docker_image

    You can quit from it by typing the exit command, then press any key to close the terminal.