devcontainers / ci

A GitHub Action and Azure DevOps Task designed to simplify using Dev Containers (https://containers.dev) in CI/CD systems.
MIT License
303 stars 46 forks source link

Add documentation on container/image clean-up for self-hosted runners #207

Open stuartleeks opened 1 year ago

stuartleeks commented 1 year ago

As per the discussion here, it might be useful to include documentation that discusses approaches to cleaning up the containers/images from dev container builds.

On hosted agents this isn't an issue, but on private agents this maintenance is required to avoid running out of disk space.

alexanderilyin commented 11 months ago

image

.devcontainer/devcontainer.sh

#!/usr/bin/env bash

set -euxo pipefail

DEVCONTAINER_DIR=$(dirname $0)
WORKSPACE_FOLDER=$(dirname "${DEVCONTAINER_DIR}")

function cleanup()
{
    CONTAINER_ID=$(jq -r .containerId .devcontainer/up.json)
    docker rm -f "${CONTAINER_ID}"
}

trap cleanup EXIT

devcontainer --workspace-folder "${WORKSPACE_FOLDER}" build | jq . > "${DEVCONTAINER_DIR}/build.json"
devcontainer --workspace-folder "${WORKSPACE_FOLDER}" up | jq . > "${DEVCONTAINER_DIR}/up.json"
devcontainer exec --workspace-folder "${WORKSPACE_FOLDER}" "$@"

Example

$ .devcontainer/devcontainer.sh date
…
+ devcontainer --workspace-folder . build
…
+ devcontainer --workspace-folder . up
Thu Aug 10 05:04:59 UTC 2023
…
+ docker rm -f 8c679b4f2cf01a0c4683dd0cb804a9acb627127edb34c1e9f742d8863c49250d

.devcontainer/build.json

{
  "outcome": "success",
  "imageName": [
    "vsc-lorem-ipsum-388f8b1048b835a5024f54faadc273fc158a3c9e0c84d2cc5e6397577206cca4-features"
  ]
}

.devcontainer/up.json

{
  "outcome": "success",
  "containerId": "8c679b4f2cf01a0c4683dd0cb804a9acb627127edb34c1e9f742d8863c49250d",
  "remoteUser": "vscode",
  "remoteWorkspaceFolder": "/workspaces/lorem-ipsum"
}

.gitignore

.devcontainer/build.json
.devcontainer/up.json