Notes to Self

Alex Sokolsky's Notes on Computers and Programming

vscode devcontainer

https://code.visualstudio.com/docs/devcontainers/containers

Step by step

  1. Create .devcontainer/.env and customize it:
GIT_USER_NAME='Al So'
GIT_EMAIL='also@foo.bar'
  1. Create .devcontainer/.gitignore:
    .env
    
  2. Create .devcontainer/Dockerfile:

See, e.g. Dockerfile for terraform

# Dockerfile to be use in awesome
#
FROM mirror.gcr.io/debian:trixie-slim

# Define build arguments
ARG USER_NAME

RUN apt-get update && \
    apt-get install -y locales mandoc git curl unzip sudo vim pre-commit python3-pip rbenv ruby-build ruby-dev
RUN sed -i 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g' /etc/locale.gen && \
    echo 'LANG=en_US.UTF-8' > /etc/default/locale && \
    locale-gen
RUN useradd -ms /bin/bash ${USER_NAME} -G sudo

WORKDIR /tmp

# install aws-cli
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip" -o "awscliv2.zip" && \
    unzip awscliv2.zip && \
    ./aws/install

# install tfenv
RUN git clone --depth=1 https://github.com/tfutils/tfenv.git /opt/tfenv && \
    ln -s /opt/tfenv/bin/* /usr/local/bin && \
    mkdir -p /opt/tfenv/versions && chmod 777 /opt/tfenv/versions && \
    touch /opt/tfenv/version && chmod 777 /opt/tfenv/version

# install bundler
RUN gem install bundler

WORKDIR /

# fixup sudoers
RUN echo '%sudo  ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/nopass

# cleanup installs
RUN rm -rf /tmp/*

USER ${USER_NAME}

# add local path to env
RUN echo '# set PATH so it includes private bin if it exists\n\
if [ -d "$HOME/.local/bin" ] ; then\n\
    PATH="$HOME/.local/bin:$PATH"\n\
fi' >> /home/${USER_NAME}/.bashrc

# persist bash history
ARG USERNAME=${USER_NAME}

RUN SNIPPET="export PROMPT_COMMAND='history -a' \
    && export HISTFILE=$HOME/commandhistory/.bash_history" \
    && sudo mkdir "$HOME/commandhistory" \
    && sudo touch "$HOME/commandhistory/.bash_history" \
    && sudo chown -R $USERNAME "$HOME/commandhistory" \
    && echo "$SNIPPET" >> "$HOME/.bashrc"

# disable vim visual mode on mouse click
RUN echo "set mouse-=v" > "$HOME/.vimrc"

# config git
COPY .env /tmp/git.env
RUN set -a && . /tmp/git.env && set +a && \
    if [ -z "$GIT_USER_NAME" ] || [ -z "$GIT_EMAIL" ]; then \
        echo "Error: GIT_USER_NAME and GIT_EMAIL must be provided in .env file."; \
        exit 55; \
    fi && \
    git config --global user.name "$GIT_USER_NAME" && \
    git config --global user.email "$GIT_EMAIL" && \
    git config --global pull.rebase true && \
    git config --global push.default current && \
    git config --global push.autoSetupRemote true && \
    git config --global --add safe.directory /workspaces/awesome

# install python packages
RUN pip install --break-system-packages --user -U commitizen

RUN rbenv install 3.1.6 && rbenv global 3.1.6

#RUN bundle install --gemfile=awesome/Gemfile

RUN tfenv install 1.5.7

  1. Create .devcontainer/devcontainer.json: ```json // For format details, see https://aka.ms/devcontainer.json. // For config options, see https://github.com/devcontainers/templates/tree/main/src/debian { “name”: “Debian”, // Or use a Dockerfile or Docker Compose file. // More info: https://containers.dev/guide/dockerfile “build”: { “dockerfile”: “Dockerfile”, “args”: { “USER_NAME”: “${env:USER}” } },

    // Features to add to the dev container: https://containers.dev/features // “features”: {},

    // Use ‘forwardPorts’ to make a list of ports inside the container available locally. // “forwardPorts”: [],

    // Configure tool-specific properties. “customizations”: { “vscode”: { “extensions”: [ “hashicorp.hcl” ] } },

    // Uncomment to connect as root instead: https://aka.ms/dev-containers-non-root. // “remoteUser”: “root” “mounts”: [ “source=${localEnv:HOME}/.aws,target=/home/${env:USER}/.aws,type=bind,consistency=cached”, “source=awesome-bashhistory,target=/home/${env:USER}/commandhistory,type=volume” ],

    “postCreateCommand”: “sudo chown -R $(whoami): /home/$(whoami)/commandhistory && sudo bundle install –gemfile=Gemfile” }

```