From d07d395b09ec1a87677712e7b50ac520495ed573 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Wed, 1 Jul 2026 22:35:54 +0200 Subject: Port `dbx` To Use `container machine` With `container` v1, a new `machine` subcommand was introduced in order to support long-lived machines. Port the `dbx` utility to use `container machine` instead of using regular containers. The main motivation for this is: - `container machine`s are more elastic; when they are stopped their containers are removed entirely (which is different to a container that is stopped, but not removed) - `container machine`s can modify settings (such as CPU and memory limits) after creation, allowing for the machine to change over time as needed Overall, the structure stays largely the same as before; the main difference being how initialization is done (since `container machine` has built-in support for initializing a user with passwordless `sudo`). Note we explicitly set `--home-mount=none` in order to keep the same filesystem isolation guarantees that existed before (files can be copied with `scp`; there is no `container cp` analogy for machines). One notable difference is network management. `container machine`s are assigned their own virtual network device and get a completely separate IP to the host (which is different than before where container ports were being published to the host). I do not know which one is more secure... --- .gitignore | 7 ++++--- Containerfile | 59 ++++++-------------------------------------------------- Makefile | 11 ++++------- README.md | 2 +- custom/README.md | 10 +++++++--- dbx | 9 +++++---- dbx-create | 38 ++++++++++++++++++++++++++++-------- dbx-enter | 9 +++++---- dbx-init | 1 + dbx-proxy | 16 --------------- dbx-start | 15 ++++++++++++-- dbx-stop | 15 ++++++++++++++ ssh/config.in | 10 +++++----- 13 files changed, 96 insertions(+), 106 deletions(-) delete mode 100755 dbx-proxy create mode 100755 dbx-stop diff --git a/.gitignore b/.gitignore index 869cb3b..006e32b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .container -custom/* -!custom/README.md +custom/extra-packages +custom/post-install +custom/skel/ +ssh/*.config ssh/id_* -ssh/config diff --git a/Containerfile b/Containerfile index 84ba12e..24698e9 100644 --- a/Containerfile +++ b/Containerfile @@ -1,8 +1,5 @@ FROM docker.io/library/debian:13 -ARG USER=dbx -EXPOSE 22/tcp - # Flag that we are a devbox RUN touch /run/.dbxenv @@ -10,62 +7,18 @@ RUN touch /run/.dbxenv RUN rm /etc/apt/apt.conf.d/docker-* && \ apt-get update && \ apt-get install -y \ - bash-completion \ - bzip2 \ - curl \ - git \ - gnupg \ - htop \ - less \ - man-db \ - manpages \ - nano \ - openssh-client \ openssh-server \ - procps \ sudo \ tini \ - tree \ - vim \ - wget \ - zip \ && \ apt-get clean && \ rm -dr /var/lib/apt/lists/* -# Passwordless sudo -RUN sed -i -e 's/ ALL$/ NOPASSWD:ALL/' /etc/sudoers - -# Configure automatic removal of stale forwarded sockets -RUN echo "StreamLocalBindUnlink yes" >> /etc/ssh/sshd_config - -# Create and configure the devbox user -RUN useradd \ - --create-home \ - --groups sudo \ - --shell /usr/bin/bash \ - --uid 1000 \ - --user-group \ - ${USER} - -# Configure SSH login -RUN mkdir -m 700 /home/${USER}/.ssh && chown 1000:1000 /home/${USER}/.ssh -COPY --chmod=600 --chown=1000:1000 ssh/id_ed25519.pub /home/${USER}/.ssh/authorized_keys - -# User customizations -COPY --chown=1000:1000 custom/ /home/${USER}/ -RUN rm /home/${USER}/README.md -RUN if [ -f /home/${USER}/extra-packages ]; then \ - apt-get update && \ - apt-get install -y $(cat /home/${USER}/extra-packages | xargs) && \ - apt-get clean && \ - rm -dr /var/lib/apt/lists/* /home/${USER}/extra-packages; \ - fi -RUN if [ -x /home/${USER}/post-install ]; then \ - su - ${USER} /home/${USER}/post-install && \ - rm /home/${USER}/post-install; \ - fi +# Configure SSH server +COPY ssh/id_ed25519.pub /etc/ssh/authorized_keys +RUN echo "StreamLocalBindUnlink yes" >> /etc/ssh/sshd_config && \ + echo "AuthorizedKeysFile /etc/ssh/authorized_keys" >> /etc/ssh/sshd_config # Set up the entrypoint -COPY dbx-init /usr/local/bin/dbx-init -ENTRYPOINT ["dbx-init"] +COPY dbx-init /sbin/init +ENTRYPOINT ["/sbin/init"] diff --git a/Makefile b/Makefile index c1da571..7377817 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ +DBX_IMAGE ?= dbx + .PHONY: all -all: .container ssh/config ; +all: .container ssh/id_ed25519 ; .PHONY: install install: all @@ -7,13 +9,8 @@ install: all ln -s $(PWD)/dbx $(HOME)/.local/bin/dbx .container: Containerfile dbx-init ssh/id_ed25519.pub - container build --build-arg USER=$(USER) --pull --tag dbx . + container build --pull --tag $(DBX_IMAGE) . touch $@ -ssh/config: ssh/config.in ssh/id_ed25519.pub - cat $< \ - | sed -e 's|$$PWD|$(PWD)|' \ - > $@ - ssh/id_% ssh/id_%.pub: ssh-keygen -C "$$(id -un)@$$(hostname)" -f ssh/id_$* -N "" -t $* diff --git a/README.md b/README.md index a7d1faa..bb7d6ab 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Containerized Development Environments -`dbx` is a set of scripts for managing a local containerized development environment. It works by creating a long-lived container named `dbx` that that has no access to the host where you can install various developer tools and dependencies without losing sleep at night. It uses Apple `container` tool, which creates a dedicated virtual machine for the `dbx` container, providing increased isolation and security. The host connects to the container via SSH, which allows any development tools with remote editing capabilities to be used (such as Emacs, Zed or Visual Studio Code). +`dbx` is a set of scripts for managing a local containerized development environment. It works by creating a long-lived container machine named `dbx` that that has no access to the host where you can install various developer tools and dependencies without losing sleep at night. It uses Apple `container` tool, which creates a dedicated virtual machine for the `dbx` container machine, providing increased isolation and security. The host connects to the container via SSH, which allows any development tools with remote editing capabilities to be used (such as Emacs, Zed or Visual Studio Code). At its core, it is similar to Toolbx and Distrobox, but with improved isolation and security. In general, it does not try to create a convenient container that is seamlessly integrated with the host, but rather a restricted sandbox where you can install development CLI tools, and reduce `npm install` risk. diff --git a/custom/README.md b/custom/README.md index 04d6b03..5d9a8a5 100644 --- a/custom/README.md +++ b/custom/README.md @@ -1,11 +1,15 @@ # Customizations -This directory includes customizations that are not tracked by Git. All files in this directory will be copied over the devbox user's home directory (overwriting any files that may exist there from `/etc/skel`), allowing for easy customization of the devbox user. +This directory includes customizations that are not tracked by Git that can be used to configure the devbox on its first run. + +## Skeleton Directory + +All files in the `skel` directory will be copied over the devbox user's home directory (overwriting any files that may exist there from `/etc/skel`), allowing for easy customization of the devbox user. ## Extra Packages -A special `extra-packages` file can be added to this directory to include additional packages to install when building the container. +A special `extra-packages` file can be added to this directory to include additional packages to install on first run. ## Post Installation Script -A special `post-install` executable can be added to this directory which will be run as a final step when building the container. This allows you to include a one-time setup with the development container image. The post install script is run as the devbox user (with `sudo` access). +A special `post-install` executable can be added to this directory which will be run as a final step when creating the devbox. The post install script is run as the devbox user (with passwordless `sudo` access). diff --git a/dbx b/dbx index 0441346..993ddda 100755 --- a/dbx +++ b/dbx @@ -1,9 +1,10 @@ #!/usr/bin/env bash # SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello set -euo pipefail -R="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +root="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" usage() { cat < set -euo pipefail -C="dbx" +root="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +machine="${DBX_MACHINE:-dbx}" +image="${DBX_IMAGE:-dbx}" if ! container system status &>/dev/null; then container system start >/dev/null fi -if container list --all | grep $C >/dev/null; then +if container machine inspect "$machine" &>/dev/null; then echo "already created" exit fi -container create \ - --cpus 4 \ - --memory 8G \ - --name $C \ - --publish 127.0.0.1:4222:22 \ - $C \ +container machine create \ + --name="$machine" \ + --no-boot \ + --home-mount=none \ + "$image" \ >/dev/null + +# Perform the initial setup for the devbox +"$root/dbx-start" +if [ -f "$root/custom/extra-packages" ]; then + container machine run --name="$machine" --root \ + apt-get update + container machine run --name="$machine" --root \ + apt-get install -y $(cat "$root/custom/extra-packages") +fi +if [ -d "$root/custom/skel" ]; then + home="$(container machine run --name="$machine" bash -c 'echo $HOME')" + scp -F "$root/ssh/$machine.config" -r \ + "$root/custom/skel/." "$machine:$home" +fi +if [ -x "$root/custom/post-install" ]; then + scp -F "$root/ssh/$machine.config" -r \ + "$root/custom/post-install" "$machine:/tmp/post-install" + container machine run --name="$machine" /tmp/post-install + container machine run --name="$machine" rm /tmp/post-install +fi diff --git a/dbx-enter b/dbx-enter index 71d7d9a..88db503 100755 --- a/dbx-enter +++ b/dbx-enter @@ -1,10 +1,11 @@ #!/usr/bin/env bash # SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello set -euo pipefail -R="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" -C="dbx" +root="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +machine="${DBX_MACHINE:-dbx}" -"$R/dbx-start" -exec ssh -F "$R/ssh/config" $C +"$root/dbx-start" +exec ssh -F "$root/ssh/$machine.config" "$machine" diff --git a/dbx-init b/dbx-init index 184e4f1..3877c92 100755 --- a/dbx-init +++ b/dbx-init @@ -1,5 +1,6 @@ #!/usr/bin/env bash # SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello set -euo pipefail diff --git a/dbx-proxy b/dbx-proxy deleted file mode 100755 index b889f7f..0000000 --- a/dbx-proxy +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env bash -# SPDX-License-Identifier: GPL-3.0-only - -set -euo pipefail - -if [[ $# -ne 1 ]]; then - echo "ERROR: missing port to proxy" >&2 - exit 1 -fi - -R="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" -C="dbx" - -port="$1" -"$R/dbx-start" -exec ssh -F "$R/ssh/config" -L "localhost:$port:localhost:$port" -N $C diff --git a/dbx-start b/dbx-start index 0fd1419..1b602e1 100755 --- a/dbx-start +++ b/dbx-start @@ -1,12 +1,23 @@ #!/usr/bin/env bash # SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello set -euo pipefail -C="dbx" +root="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +machine="${DBX_MACHINE:-dbx}" if ! container system status &>/dev/null; then container system start >/dev/null fi -container start $C >/dev/null +container machine run --name="$machine" true >/dev/null + +# The IP address assigned to the machine changes on restarts, so update the SSH +# configuration with the current value. +ip="$(container machine inspect "$machine" | jq -r '.[0].ipAddress')" +cat "$root/ssh/config.in" \ + | sed -e 's|$ROOT|'"$root"'|g' \ + | sed -e 's|$MACHINE|'"$machine"'|g' \ + | sed -e 's|$IP|'"$ip"'|g' \ + >"$root/ssh/$machine.config" diff --git a/dbx-stop b/dbx-stop new file mode 100755 index 0000000..dcef4a2 --- /dev/null +++ b/dbx-stop @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: GPL-3.0-only +# SPDX-FileCopyrightText: 2026 Nicholas Rodrigues Lordello + +set -euo pipefail + +machine="${DBX_MACHINE:-dbx}" + +if ! container system status &>/dev/null; then + exit 0 +fi + +container machine stop "$machine" &>/dev/null \ + || true +container system stop >/dev/null diff --git a/ssh/config.in b/ssh/config.in index 6e0e420..294754e 100644 --- a/ssh/config.in +++ b/ssh/config.in @@ -1,6 +1,6 @@ -Host dbx - HostName localhost - Port 4222 +Host $MACHINE + HostName $IP IdentitiesOnly yes - IdentityFile $PWD/ssh/id_ed25519 - NoHostAuthenticationForLocalhost yes + IdentityFile $ROOT/ssh/id_ed25519 + StrictHostKeyChecking no + UserKnownHostsFile /dev/null -- cgit v1.2.3