Skip to content

Commit 5551be1

Browse files
authored
devcontainer improvements (#260)
- use initializeComment to make sure environment files and maven cache bind mounts are present - support building docker containers inside devcontainer
1 parent cc78aec commit 5551be1

File tree

8 files changed

+87
-30
lines changed

8 files changed

+87
-30
lines changed

.devcontainer/devcontainer.json

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,56 @@
1-
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2-
// https://github.com/microsoft/vscode-dev-containers/tree/v0.231.6/containers/docker-existing-dockerfile
1+
// For format details, see https://aka.ms/devcontainer.json.
32
{
43
"name": "Workspace Dockerfile",
5-
64
// We have no context for the devcontainer.
75
"context": ".",
8-
96
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
107
"dockerFile": "../docker/benchbase/devcontainer/Dockerfile",
118
"build": {
129
"args": {
10+
"http_proxy": "${localEnv:http_proxy}",
11+
"https_proxy": "${localEnv:https_proxy}",
12+
"no_proxy": "${localEnv:no_proxy}",
1313
"--tag": "benchbase-dev:latest"
1414
},
1515
"cacheFrom": "benchbase.azurecr.io/benchbase-dev"
1616
},
17-
18-
// Set *default* container specific settings.json values on container create.
19-
"settings": {},
20-
21-
// Add the IDs of extensions you want installed when the container is created.
22-
"extensions": [
23-
"vscjava.vscode-java-pack",
24-
"EditorConfig.EditorConfig"
17+
"initializeCommand": [
18+
".devcontainer/scripts/initializeCommand"
19+
],
20+
"runArgs": [
21+
// Secret injection as environment variables
22+
// https://code.visualstudio.com/remote/advancedcontainers/environment-variables#_option-2-use-an-env-file
23+
"--env-file=.env"
2524
],
26-
25+
"features": {
26+
"ghcr.io/devcontainers/features/docker-outside-of-docker": {},
27+
"ghcr.io/stuartleeks/dev-container-features/shell-history": {}
28+
},
29+
"containerEnv": {
30+
"http_proxy": "${localEnv:http_proxy}",
31+
"https_proxy": "${localEnv:https_proxy}",
32+
"no_proxy": "${localEnv:no_proxy}",
33+
"MAVEN_CONFIG": "${localEnv:HOME}/.m2",
34+
"LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}",
35+
"CONTAINER_WORKSPACE_FOLDER": "${containerWorkspaceFolder}",
36+
},
37+
// Set *default* container specific settings.json values on container create.
38+
"customizations": {
39+
"vscode": {
40+
"settings": {},
41+
// Add the IDs of extensions you want installed when the container is created.
42+
"extensions": [
43+
"vscjava.vscode-java-pack",
44+
"EditorConfig.EditorConfig"
45+
]
46+
}
47+
},
2748
// Use 'forwardPorts' to make a list of ports inside the container available locally.
2849
// "forwardPorts": [],
29-
30-
// Uncomment the next line to run commands after the container is created - for example installing curl.
31-
// "postCreateCommand": "apt-get update && apt-get install -y curl",
32-
33-
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
34-
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
35-
36-
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
37-
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
3850
// Use the local maven cache.
3951
"mounts": [
4052
"source=${localEnv:HOME}${localEnv:USERPROFILE}/.m2,target=/home/containeruser/.m2,type=bind,consistency=cached"
4153
],
42-
43-
// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
44-
"remoteUser": "containeruser"
45-
}
54+
"remoteUser": "containeruser",
55+
"containerUser": "containeruser"
56+
}

.devcontainer/scripts/.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
initializeCommand text eol=lf
2+
initializeCommand.cmd text eol=crlf
3+
initializeCommand.ps1 text eol=crlf
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
set -x
6+
7+
scriptdir=$(dirname "$(readlink -f "$0")")
8+
# Start in the root of the repository.
9+
cd "$scriptdir/../../"
10+
11+
# Make sure the .env file exists for the devcontainer to load.
12+
if [ ! -f .env ]; then
13+
echo "Creating empty .env file for devcontainer."
14+
touch .env
15+
fi
16+
# Attempt to pre-create the maven cache directory for bind mounting into the devcontainer.
17+
mkdir -p ~/.m2 || true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cd %~dp0/../../
2+
powershell.exe -Version 3.0 -NoProfile .devcontainer/scripts/initializeCommand.ps1
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Requires -Version 3.0
2+
# A script to prepare the build/run environment for the devcontainer.
3+
4+
$ErrorActionPreference = 'Stop'
5+
6+
# Make sure we're in the root of the repository.
7+
Set-Location "$PSScriptRoot/../../"
8+
9+
# Make sure the .env file exists for the devcontainer to load.
10+
if (!(Test-Path -PathType Leaf '.env')) {
11+
Write-Host "Creating empty .env file for devcontainer."
12+
New-Item -ErrorAction SilentlyContinue -Type File -Name '.env'
13+
}
14+
15+
# Attempt to pre-create the maven cache directory for bind mounting into the devcontainer.
16+
New-Item -ErrorAction SilentlyContinue -Type Directory -Name ~/.m2

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,5 @@ build/
5454

5555
# vim swap files
5656
.*.swp
57+
58+
.env

docker/benchbase/devcontainer/Dockerfile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@ FROM maven:3.8.5-eclipse-temurin-17 AS devcontainer
55
LABEL org.opencontainers.image.source = "https://github.com/cmu-db/benchbase/"
66

77
# Make sure the image is patched and up to date.
8-
RUN apt update && apt -y upgrade && apt clean && rm -rf /var/lib/apt/lists/*
8+
# Also add a few nice cli tools.
9+
RUN apt-get update \
10+
&& apt-get -y upgrade \
11+
&& apt-get -y install --no-install-recommends sudo vim-nox neovim less bash-completion colordiff git \
12+
&& apt-get clean && rm -rf /var/lib/apt/lists/*
913

1014
# Add a containeruser that allows vscode/codespaces to map the local host user
1115
# (often uid 1000) to some non-root user inside the container.
1216
ARG CONTAINERUSER_UID=1000
1317
ARG CONTAINERUSER_GID=1000
1418
RUN groupadd --non-unique --gid ${CONTAINERUSER_GID} containergroup \
1519
&& useradd --non-unique --create-home --no-user-group --comment 'Container User' \
16-
--uid ${CONTAINERUSER_UID} --gid ${CONTAINERUSER_GID} containeruser
20+
--uid ${CONTAINERUSER_UID} --gid ${CONTAINERUSER_GID} containeruser \
21+
&& echo 'containeruser ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
1722
RUN mkdir -p /benchbase/results && chown -R containeruser:containergroup /benchbase/
1823
USER containeruser
1924
ENV MAVEN_CONFIG=/home/containeruser/.m2

docker/benchbase/run-dev-image.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ fi
2727

2828
cd "$rootdir"
2929
MAVEN_CONFIG="${MAVEN_CONFIG:-$HOME/.m2}"
30-
mkdir -p "$MAVEN_CONFIG"
30+
mkdir -p "$MAVEN_CONFIG" || true
3131
set -x
32+
SRC_DIR="${LOCAL_WORKSPACE_FOLDER:-$PWD}"
3233
docker run ${INTERACTIVE_ARGS:-} --rm \
3334
--env=http_proxy="${http_proxy:-}" --env=https_proxy="${https_proxy:-}" --env=no_proxy="${no_proxy:-}" \
3435
--env MAVEN_OPTS="-Dhttp.proxyHost=${http_proxy_host} -Dhttp.proxyPort=${http_proxy_port} -Dhttps.proxyHost=${https_proxy_host} -Dhttps.proxyPort=${https_proxy_port}" \
@@ -37,5 +38,5 @@ docker run ${INTERACTIVE_ARGS:-} --rm \
3738
--env SKIP_TESTS="$SKIP_TESTS" \
3839
--user "$CONTAINERUSER_UID:$CONTAINERUSER_GID" \
3940
-v "$MAVEN_CONFIG:/home/containeruser/.m2" \
40-
-v "$PWD:/benchbase" benchbase-dev:latest $*
41+
-v "$SRC_DIR:/benchbase" benchbase-dev:latest $*
4142
set +x

0 commit comments

Comments
 (0)