Skip to content

Commit d12ae27

Browse files
committed
Add a script for using dev containers outside VSCode
I want to use the devcontainer info, but I don't want to use VSCode. This commit adds a small script that reads the devcontainer JSON file and runs docker commands based on the information in the JSON file. Usage is like this: In terminal 1, run `tool/devcontainer up` After that thing is running do `tool/devcontainer run-user-commands` to make sure gems are bundled and database is set up. Finally run `tool/devcontainer sh` to get a shell inside the container.
1 parent 1dcfe23 commit d12ae27

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

.devcontainer/boot.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
bundle install
22

3-
. ${NVM_DIR}/nvm.sh && nvm install --lts
4-
yarn install
3+
if [[ ! -z "${NVM_DIR}" ]]; then
4+
. ${NVM_DIR}/nvm.sh && nvm install --lts
5+
yarn install
6+
fi
57

68
cd activerecord
79

.devcontainer/devcontainer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"Shopify.ruby-lsp"
4040
]
4141
}
42-
},
42+
}
4343

4444
// Uncomment to connect as root instead. More info: https://containers.dev/implementors/json_reference/#remoteUser.
4545
// "remoteUser": "root"

tools/devcontainer

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "json"
5+
require "open3"
6+
7+
VARS = {
8+
"localWorkspaceFolderBasename" => Dir.pwd.split(File::SEPARATOR).last
9+
}
10+
11+
data = File.binread ".devcontainer/devcontainer.json"
12+
data.gsub!(/^\s*\/\/.*$/, "") # strip comments
13+
info = JSON.load data
14+
15+
working_dir = info["workspaceFolder"].gsub(/\${([^}]*)}/) { |x|
16+
VARS.fetch($1)
17+
}
18+
19+
env = info["containerEnv"].map { |k, v| "-e #{k}=#{v}" }.join " "
20+
21+
case ARGV[0]
22+
when "up"
23+
system "docker compose -f .devcontainer/#{info["dockerComposeFile"]} up"
24+
when "run-user-commands"
25+
service_id, _, _ = Open3.capture3("docker ps -q -f name=#{info["service"]}")
26+
system "docker exec #{env} -w #{working_dir} -it #{service_id.chomp} /bin/bash -i #{info["postCreateCommand"]}"
27+
when "sh"
28+
service_id, _, _ = Open3.capture3("docker ps -q -f name=#{info["service"]}")
29+
system "docker exec #{env} -w #{working_dir} -it #{service_id.chomp} /bin/bash"
30+
end

0 commit comments

Comments
 (0)