Skip to content

Commit 652580f

Browse files
committed
feat: allow overriding docker executable
So that you can run with DOCKER=nerdctl
1 parent 8ab81a2 commit 652580f

7 files changed

Lines changed: 39 additions & 14 deletions

File tree

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.PHONY: build
22

3+
DOCKER = docker
4+
35
# Development
46

57
build_rev := "main"
@@ -35,11 +37,11 @@ run:
3537
image:
3638
@[ -n "$(name)" ] || (echo "Syntax: make image name=<image-name>" >&2; exit 1)
3739
@echo "Building image codapi/$(name)"
38-
@docker build --file sandboxes/$(name)/Dockerfile --tag codapi/$(name):latest sandboxes/$(name)/
40+
@$(DOCKER) build --file sandboxes/$(name)/Dockerfile --tag codapi/$(name):latest sandboxes/$(name)/
3941
@echo "✓ codapi/$(name)"
4042

4143
network:
42-
docker network create --internal codapi
44+
$(DOCKER) network create --internal codapi
4345

4446
# Host OS
4547

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ rm -f codapi.tar.gz
3838
3. Build the sample `ash` sandbox image:
3939

4040
```sh
41-
docker build --file sandboxes/ash/Dockerfile --tag codapi/ash:latest sandboxes/ash
41+
# optional: $DOCKER
42+
export DOCKER=docker
43+
44+
$DOCKER build --file sandboxes/ash/Dockerfile --tag codapi/ash:latest sandboxes/ash
4245
```
4346

4447
4. Start the server:

codapi-cli

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set -euo pipefail
33

44
cd "$(dirname "$0")"
5+
docker=${DOCKER:-docker}
56
sandboxes_dir="sandboxes"
67
codapi_url="${CODAPI_URL:-localhost:1313}"
78

@@ -225,9 +226,9 @@ do_sandbox_rm() {
225226
fi
226227

227228
# 1. Remove the container if it exists.
228-
if docker ps -a -q --filter "name=$name" | grep -q .; then
229+
if $docker ps -a -q --filter "name=$name" | grep -q .; then
229230
echo "Removing container '$name'..."
230-
docker rm -f "$name"
231+
$docker rm -f "$name"
231232
fi
232233

233234
# 2. Remove the directory and its contents.

internal/engine/docker.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ const (
3131
type Docker struct {
3232
cfg *config.Config
3333
cmd *config.Command
34+
exe string // docker
3435
}
3536

3637
// NewDocker creates a new Docker engine for a specific command.
3738
func NewDocker(cfg *config.Config, sandbox, command string) Engine {
3839
cmd := cfg.Commands[sandbox][command]
39-
return &Docker{cfg, cmd}
40+
exe := "docker"
41+
// override docker with similar command
42+
if env, ok := os.LookupEnv("DOCKER"); ok {
43+
exe = env
44+
}
45+
return &Docker{cfg, cmd, exe}
4046
}
4147

4248
// Exec executes the command and returns the output.
@@ -196,10 +202,10 @@ func (e *Docker) exec(box *config.Box, step *config.Step, req Request, dir strin
196202
if step.Stdin {
197203
// pass files to container from stdin
198204
stdin := filesReader(files)
199-
stdout, stderr, err = prog.RunStdin(stdin, req.ID, "docker", args...)
205+
stdout, stderr, err = prog.RunStdin(stdin, req.ID, e.exe, args...)
200206
} else {
201207
// pass files to container from temp directory
202-
stdout, stderr, err = prog.Run(req.ID, "docker", args...)
208+
stdout, stderr, err = prog.Run(req.ID, e.exe, args...)
203209
}
204210

205211
if err == nil {
@@ -213,7 +219,7 @@ func (e *Docker) exec(box *config.Box, step *config.Step, req Request, dir strin
213219
// inside the container is not related to the "docker run" process,
214220
// and will hang forever after the "docker run" process is killed
215221
go func() {
216-
err = dockerKill(req.ID)
222+
err = dockerKill(e.exe, req.ID)
217223
if err == nil {
218224
logx.Debug("%s: docker kill ok", req.ID)
219225
} else {
@@ -259,7 +265,7 @@ func (e *Docker) buildArgs(box *config.Box, step *config.Step, req Request, dir
259265

260266
command := expandVars(step.Command, req.ID)
261267
args = append(args, command...)
262-
logx.Debug("%v", args)
268+
logx.Debug("%s %v", e.exe, args)
263269
return args
264270
}
265271

@@ -345,9 +351,9 @@ func expandVars(command []string, name string) []string {
345351
}
346352

347353
// dockerKill kills the container with the specified id/name.
348-
func dockerKill(id string) error {
354+
func dockerKill(exe, id string) error {
349355
ctx, cancel := context.WithTimeout(context.Background(), killTimeout)
350356
defer cancel()
351-
cmd := exec.CommandContext(ctx, "docker", "kill", id)
357+
cmd := exec.CommandContext(ctx, exe, "kill", id)
352358
return execy.Run(cmd)
353359
}

internal/engine/docker_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ var dockerCfg = &config.Config{
139139

140140
func TestDockerRun(t *testing.T) {
141141
logx.Mock()
142+
t.Setenv("DOCKER", "docker")
142143
commands := map[string]execy.CmdOut{
143144
"docker run": {Stdout: "hello world", Stderr: "", Err: nil},
144145
}
@@ -257,6 +258,7 @@ func TestDockerRun(t *testing.T) {
257258

258259
func TestDockerExec(t *testing.T) {
259260
logx.Mock()
261+
t.Setenv("DOCKER", "docker")
260262
commands := map[string]execy.CmdOut{
261263
"docker exec": {Stdout: "hello world", Stderr: "", Err: nil},
262264
}
@@ -287,6 +289,7 @@ func TestDockerExec(t *testing.T) {
287289

288290
func TestDockerStop(t *testing.T) {
289291
logx.Mock()
292+
t.Setenv("DOCKER", "docker")
290293
commands := map[string]execy.CmdOut{
291294
"docker run": {Stdout: "c958ff2", Stderr: "", Err: nil},
292295
"docker exec": {Stdout: "hello", Stderr: "", Err: nil},

internal/sandbox/sandbox_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sandbox
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/nalgeon/be"
@@ -56,9 +57,13 @@ func TestValidate(t *testing.T) {
5657

5758
func TestExec(t *testing.T) {
5859
_ = ApplyConfig(cfg)
60+
docker := os.Getenv("DOCKER")
61+
if docker == "" {
62+
docker = "docker"
63+
}
5964
t.Run("exec", func(t *testing.T) {
6065
execy.Mock(map[string]execy.CmdOut{
61-
"docker run": {Stdout: "hello"},
66+
docker + " run": {Stdout: "hello"},
6267
})
6368
req := engine.Request{
6469
ID: "http_42",

internal/server/router_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"net/http"
77
"net/http/httptest"
8+
"os"
89
"testing"
910

1011
"github.com/nalgeon/be"
@@ -57,8 +58,12 @@ func (s *server) close() {
5758

5859
func Test_exec(t *testing.T) {
5960
_ = sandbox.ApplyConfig(cfg)
61+
docker := os.Getenv("DOCKER")
62+
if docker == "" {
63+
docker = "docker"
64+
}
6065
execy.Mock(map[string]execy.CmdOut{
61-
"docker run": {Stdout: "hello"},
66+
docker + " run": {Stdout: "hello"},
6267
})
6368

6469
srv := newServer()

0 commit comments

Comments
 (0)