Can you write a detailed, step-by-step tutorial that guides people through the process of setting up their VS Code development environment to develop in a dev container on a remote Linux host? The remote Linux host is using podman, and all dev containers should be set up to use rootless podman. Dev containers must use alpine Linux. The dockerfile and devcontainer.json file must be set up to be shared among developers so that they can mount their own working directories without running into permission issues. The tutorial should include examples, and the audience is a group of mid-level professional developers.
Assistant
Developing in a Dev Container on a Remote Linux Host using VS Code and Podman
Audience: Mid-level professional developers
Objective: Set up a VS Code development environment to develop inside a dev container on a remote Linux host using rootless Podman with Alpine Linux. Share Dockerfile and devcontainer.json among developers, ensuring smooth mounting of working directories without permission issues.
Table of Contents
Introduction
Prerequisites
Step 1: Setting Up the Remote Linux Host
Step 2: Configuring SSH Access
Step 3: Installing VS Code and Extensions
Step 4: Configuring VS Code for Remote Development
Step 5: Preparing the Dev Container Configuration
Step 6: Configuring Podman for VS Code Remote Containers
Step 7: Starting the Dev Container
Step 8: Mounting Working Directories Without Permission Issues
Step 9: Testing the Setup
Conclusion
Appendix
Introduction
Developing within containers ensures consistency across development environments and streamlines the build and deployment process. This tutorial guides you through setting up Visual Studio Code (VS Code) to develop inside a dev container running on a remote Linux host using rootless Podman (an alternative to Docker) and Alpine Linux. Sharing configuration files among team members can sometimes lead to permission issues, especially when mounting directories. We'll address these concerns to create a smooth collaborative environment.
Prerequisites
Before you begin, ensure you have the following:
Local Machine:
VS Code installed.
Internet access to download extensions.
SSH client installed (typically pre-installed on Linux and macOS).
Remote Linux Host:
A user account with sudo privileges (if rootless Podman isn't installed yet).
SSH access configured.
Knowledge:
Familiarity with command-line operations.
Basic understanding of containers and Podman.
Understanding of SSH and remote connections.
Step 1: Setting Up the Remote Linux Host
We'll start by ensuring the remote host is ready.
1.1 Update the System
Log in to your remote host via SSH:
bash
ssh username@remote-host-ip
Update the package lists and upgrade installed packages:
bash
sudo apt update && sudo apt upgrade -y
Note: Replace apt with your package manager if not using Ubuntu/Debian.
Remote - SSH: Allows you to open folders on a remote machine using SSH.
Dev Containers: Enables running VS Code within a container.
To install extensions:
Open VS Code.
Go to the Extensions view by clicking on the Extensions icon or pressing Ctrl+Shift+X (Cmd+Shift+X on macOS).
Search for Remote - SSH and Dev Containers and install them.
Step 4: Configuring VS Code for Remote Development
4.1 Add SSH Host in VS Code
Open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P).
Type Remote-SSH: Add New SSH Host... and select it.
Enter the SSH connection string:
plaintext
username@remote-host-ip
Choose the SSH configuration file to update (usually the default is fine).
4.2 Connect to the SSH Host
Open the Command Palette and type Remote-SSH: Connect to Host....
Select the host you just added.
A new VS Code window will open connected to the remote host.
Step 5: Preparing the Dev Container Configuration
To ensure that all developers share the same environment, we'll create a shared Dockerfile and devcontainer.json.
5.1 Create a Project Directory
On the remote host, create a directory to hold your project files.
bash
mkdir -p ~/projects/my-shared-project
cd ~/projects/my-shared-project
5.2 Create a Dockerfile
Create a Dockerfile using Alpine Linux as the base.
dockerfile
# Use the latest Alpine Linux image
FROM alpine:latest
# Install necessary packages (example: bash and curl)
RUN apk update && apk add --no-cache bash curl
# Set the working directory
WORKDIR /workspace
# Set a non-root user with a fixed UID and GID
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID
RUN addgroup -g $USER_GID $USERNAME \
&& adduser -u $USER_UID -G $USERNAME -h /home/$USERNAME -D $USERNAME \
&& chown -R $USERNAME:$USERNAME /workspace
USER $USERNAME
# Expose port (if necessary)
EXPOSE 8080
CMD [ "sleep", "infinity" ]
5.3 Create a devcontainer.json File
Create a .devcontainer directory and add devcontainer.json:
bash
mkdir .devcontainer
cd .devcontainer
Create devcontainer.json with the following content:
args: Passes the UID and GID from the local environment to avoid permission issues.
remoteUser: Specifies the user inside the container.
mounts: Binds the local workspace folder to /workspace inside the container.
Note: The ${localEnv:UID} and ${localEnv:GID} variables may not be available by default on some systems. We'll address this in the next steps.
Step 6: Configuring Podman for VS Code Remote Containers
By default, VS Code's Dev Containers extension uses Docker. We'll configure it to use Podman.
6.1 Install Podman on the Remote Host (Already Done)
Ensure Podman is installed on the remote host (covered in Step 1).
6.2 Install Podman on Local Machine (If Using Linux)
If you're running VS Code on a Linux machine, install Podman locally to provide compatibility with the Dev Containers extension.
Alternatively, you can configure the extension to use Podman on the remote host via SSH. Since we're developing on a remote host, this may not be necessary.
6.3 Configure VS Code to Use Podman instead of Docker
Create or modify the settings.json file in VS Code to point to Podman.
Open Settings (Ctrl+, / Cmd+,).
Search for docker and find the Docker > Execution: Command setting.
Set it to podman.
Alternatively, in JSON format:
json
{
"docker.executablePath": "podman"
}
Note: This setting applies to your local VS Code environment. Since we're connecting to a remote host where Podman is installed, we need to ensure the Dev Containers extension uses Podman remotely.
6.4 Configure the Remote Host's Docker Socket for Podman
On the remote host, create a symbolic link from the Docker socket to the Podman socket.
Alternatively, set the DOCKER_HOST environment variable.
Step 7: Starting the Dev Container
7.1 Open the Project in VS Code
In the remote SSH window:
Open the project folder (~/projects/my-shared-project) via File > Open Folder.
7.2 Reopen Folder in Container
After the project folder is open, you should see a prompt: "Folder contains a Dev Container configuration file. Reopen folder to develop in a container."
Click Reopen in Container.
If the prompt doesn't appear:
Open the Command Palette (Ctrl+Shift+P / Cmd+Shift+P).
Type Remote-Containers: Reopen in Container and select it.
7.3 Wait for the Container to Build and Start
VS Code will build the container using the Dockerfile and start it. The first build may take some time.
Step 8: Mounting Working Directories Without Permission Issues
Permission issues often arise due to mismatched user IDs between the host and the container.
8.1 Ensure Consistent UID and GID
In the devcontainer.json file, we're passing the UID and GID using build arguments:
Alternatively, for Windows and macOS users, you can set the UID and GID manually.
8.3 Using Environment Variables
Developers can set environment variables in their local machine to pass the correct UID and GID.
On Linux/macOS:
Add to your shell profile (~/.bashrc or ~/.zshrc):
bash
export UID=$(id -u)
export GID=$(id -g)
On Windows:
Since Windows doesn't have UNIX-style UIDs and GIDs, you can set them to a default value that matches what's in the container.
Step 9: Testing the Setup
9.1 Verify User Inside the Container
Open a terminal inside VS Code (this will be inside the container):
bash
whoami
id
You should see vscode as the user, with UID and GID matching what was set.
9.2 Test File Creation
Create a file inside the /workspace directory:
bash
touch test-file.txt
From your local machine, verify that the file exists and that there are no permission issues.
Conclusion
You've successfully set up a development environment using VS Code, developing inside a Podman-managed dev container running Alpine Linux on a remote host. By ensuring consistent UID and GID settings and carefully configuring Dockerfile and devcontainer.json, multiple developers can share the same configuration without running into permission issues when mounting their working directories.
Appendix
Full Dockerfile Example
dockerfile
# Use the latest Alpine Linux image
FROM alpine:latest
# Install necessary packages (example: bash and curl)
RUN apk update && apk add --no-cache bash curl
# Set the working directory
WORKDIR /workspace
# Set a non-root user with a fixed UID and GID
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=1000
RUN addgroup -g $USER_GID $USERNAME \
&& adduser -u $USER_UID -G $USERNAME -h /home/$USERNAME -D $USERNAME \
&& chown -R $USERNAME:$USERNAME /workspace
USER $USERNAME
# Expose port (if necessary)
EXPOSE 8080
CMD [ "sleep", "infinity" ]
Extension Management: You can specify VS Code extensions to be installed inside the container by adding them to the extensions array in devcontainer.json.
Custom Configurations: For more complex setups, consider adding environment variables, custom scripts, and more as needed.
Collaborative Development: Ensure all team members use the same Dockerfile and devcontainer.json files to maintain consistency.
Permission Troubleshooting: If permission issues persist, double-check UID/GID settings and file ownership both on the host and inside the container.
By following this guide, you should have a robust and collaborative development environment leveraging the power of containers, remote development, and VS Code's rich feature set.