Skip to content

Commit 8df5f4d

Browse files
SparkLabScoutPangjiping
authored andcommitted
fix(execd): Add fallback from bash to sh for Alpine-based images
This addresses issue #405 by adding a getShell() helper function that first tries bash, then falls back to sh for Alpine-based Docker images that don't have bash installed by default.
1 parent cda2ff2 commit 8df5f4d

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

components/execd/pkg/runtime/command.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ import (
3535
"github.com/alibaba/opensandbox/execd/pkg/util/safego"
3636
)
3737

38+
// getShell returns the preferred shell, falling back to sh if bash is not available.
39+
// This is needed for Alpine-based Docker images that only have sh by default.
40+
func getShell() string {
41+
if _, err := exec.LookPath("bash"); err == nil {
42+
return "bash"
43+
}
44+
return "sh"
45+
}
46+
3847
func buildCredential(uid, gid *uint32) (*syscall.Credential, error) {
3948
if uid == nil && gid == nil {
4049
return nil, nil
@@ -93,7 +102,8 @@ func (c *Controller) runCommand(ctx context.Context, request *ExecuteCodeRequest
93102

94103
startAt := time.Now()
95104
log.Info("received command: %v", request.Code)
96-
cmd := exec.CommandContext(ctx, "bash", "-c", request.Code)
105+
shell := getShell()
106+
cmd := exec.CommandContext(ctx, shell, "-c", request.Code)
97107

98108
cmd.Stdout = stdout
99109
cmd.Stderr = stderr
@@ -218,7 +228,8 @@ func (c *Controller) runBackgroundCommand(ctx context.Context, cancel context.Ca
218228

219229
startAt := time.Now()
220230
log.Info("received command: %v", request.Code)
221-
cmd := exec.CommandContext(ctx, "bash", "-c", request.Code)
231+
shell := getShell()
232+
cmd := exec.CommandContext(ctx, shell, "-c", request.Code)
222233

223234
cmd.Dir = request.Cwd
224235

0 commit comments

Comments
 (0)