Custom dev container in VSCode

Custom dev container in VSCode

Configure your local containers in VSCode without modifying the project’s Dockerfile

When developing with docker container, developers may need local packages that anren’t required in development, staging, or production environments. For exemple, team member may prefer different shells like zsh ou fish. This guide tutorial explains how to install packages and configure local containers in VSCode without modifyng the project’s Dockerfile.

💡
If you don’t have VSCode running inside a Docker container yet, see this tutorial before continuing.

How install packages only at local container

In file .devcontainer/devcontainer.json change the line postCreateComand.

"postCreateCommand": "chmod +x ./local-entrypoint.sh && ./local-entrypoint.sh",

The command above will execute local-entrypoint.sh after starting local container in vscode. Therefore, create the local-entrypoint.sh at root directory with following content:

#!/bin/sh
set -e

echo "🔄 Updating local system"
apt update && apt install -y curl tree zsh git

echo "🚀 Installing oh-my-zsh..."
yes | sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" --unattended
chsh -s /bin/zsh

echo "⚙️ Setting up Git configs..."
git config --global --add safe.directory /workspaces/$GIT_VERSIONED_DIRECTORY_NAME
git config --global user.name "$GIT_USER_NAME"
git config --global user.email "$GIT_USER_EMAIL"

echo "📋 System info:"
cat /etc/os-release

echo "✅ Setup complete!"

exec "$@"

The script above installs packages (curl, tree, zsh, git, oh-my-zsh) and configures git using environment variables:

  • GIT_VERSIONED_DIRECTORY_NAME - Directory name for Git safe.directory

  • GIT_USER_NAME - Git user name

  • GIT_USER_EMAIL - Git user email

💡
Insert local-entrypoint.sh at .gitignore file.

Customize vscode terminal, theme and extensions

In file .devcontainer/devcontainer.json change the line customizations:

:
"customizations": {
        "vscode": {
            "settings": {
                "terminal.integrated.profiles.linux": {
                    "zsh": {
                        "path": "/bin/zsh"
                    }
                },
                "terminal.integrated.defaultProfile.linux": "zsh",
                "workbench.colorTheme": "Visual Studio Light",
            },
            "extensions": [
                "ms-python.python",
                "ms-azuretools.vscode-docker",
                "eamodio.gitlens",
                "ritwickdey.liveserver"
            ]
        }
    }