Skip to content

Commit a28f875

Browse files
authored
Mount Docker socket and add service port range (#103)
1 parent 6e58904 commit a28f875

File tree

15 files changed

+284
-12
lines changed

15 files changed

+284
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ lstk --non-interactive
141141
|---|---|
142142
| `LOCALSTACK_AUTH_TOKEN` | Auth token used for non-interactive runs or to skip browser login |
143143
| `LOCALSTACK_DISABLE_EVENTS=1` | Disables telemetry event reporting |
144+
| `DOCKER_HOST` | Override the Docker daemon socket (e.g. `unix:///home/user/.colima/default/docker.sock`). When unset, lstk tries the default socket and then probes common alternatives (Colima, OrbStack). |
144145

145146
## Usage
146147

cmd/logout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func newLogoutCmd(cfg *env.Env, logger log.Logger) *cobra.Command {
2929
return fmt.Errorf("failed to get config: %w", err)
3030
}
3131
var rt runtime.Runtime
32-
if dockerRuntime, err := runtime.NewDockerRuntime(); err == nil {
32+
if dockerRuntime, err := runtime.NewDockerRuntime(cfg.DockerHost); err == nil {
3333
rt = dockerRuntime
3434
}
3535

cmd/logs.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import (
66

77
"github.com/localstack/lstk/internal/config"
88
"github.com/localstack/lstk/internal/container"
9+
"github.com/localstack/lstk/internal/env"
910
"github.com/localstack/lstk/internal/output"
1011
"github.com/localstack/lstk/internal/runtime"
1112
"github.com/spf13/cobra"
1213
)
1314

14-
func newLogsCmd() *cobra.Command {
15+
func newLogsCmd(cfg *env.Env) *cobra.Command {
1516
cmd := &cobra.Command{
1617
Use: "logs",
1718
Short: "Show emulator logs",
@@ -22,7 +23,7 @@ func newLogsCmd() *cobra.Command {
2223
if err != nil {
2324
return err
2425
}
25-
rt, err := runtime.NewDockerRuntime()
26+
rt, err := runtime.NewDockerRuntime(cfg.DockerHost)
2627
if err != nil {
2728
return err
2829
}

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewRootCmd(cfg *env.Env, tel *telemetry.Client, logger log.Logger) *cobra.C
2626
Long: "lstk is the command-line interface for LocalStack.",
2727
PreRunE: initConfig,
2828
RunE: func(cmd *cobra.Command, args []string) error {
29-
rt, err := runtime.NewDockerRuntime()
29+
rt, err := runtime.NewDockerRuntime(cfg.DockerHost)
3030
if err != nil {
3131
return err
3232
}
@@ -53,7 +53,7 @@ func NewRootCmd(cfg *env.Env, tel *telemetry.Client, logger log.Logger) *cobra.C
5353
newLoginCmd(cfg, logger),
5454
newLogoutCmd(cfg, logger),
5555
newStatusCmd(cfg),
56-
newLogsCmd(),
56+
newLogsCmd(cfg),
5757
newConfigCmd(),
5858
newVersionCmd(),
5959
newUpdateCmd(cfg),

cmd/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func newStartCmd(cfg *env.Env, tel *telemetry.Client, logger log.Logger) *cobra.
1515
Long: "Start emulator and services.",
1616
PreRunE: initConfig,
1717
RunE: func(cmd *cobra.Command, args []string) error {
18-
rt, err := runtime.NewDockerRuntime()
18+
rt, err := runtime.NewDockerRuntime(cfg.DockerHost)
1919
if err != nil {
2020
return err
2121
}

cmd/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func newStatusCmd(cfg *env.Env) *cobra.Command {
2222
Long: "Show the status of a running emulator and its deployed resources",
2323
PreRunE: initConfig,
2424
RunE: func(cmd *cobra.Command, args []string) error {
25-
rt, err := runtime.NewDockerRuntime()
25+
rt, err := runtime.NewDockerRuntime(cfg.DockerHost)
2626
if err != nil {
2727
return err
2828
}

cmd/stop.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func newStopCmd(cfg *env.Env) *cobra.Command {
2020
Long: "Stop emulator and services",
2121
PreRunE: initConfig,
2222
RunE: func(cmd *cobra.Command, args []string) error {
23-
rt, err := runtime.NewDockerRuntime()
23+
rt, err := runtime.NewDockerRuntime(cfg.DockerHost)
2424
if err != nil {
2525
return err
2626
}

internal/container/start.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99
stdruntime "runtime"
1010
"slices"
11+
"strconv"
1112
"strings"
1213
"time"
1314

@@ -82,16 +83,31 @@ func Start(ctx context.Context, rt runtime.Runtime, sink output.Sink, opts Start
8283
if err != nil {
8384
return err
8485
}
85-
env := append(resolvedEnv, "LOCALSTACK_AUTH_TOKEN="+token)
86+
87+
containerName := c.Name()
88+
env := append(resolvedEnv,
89+
"LOCALSTACK_AUTH_TOKEN="+token,
90+
"GATEWAY_LISTEN=:4566",
91+
"MAIN_CONTAINER_NAME="+containerName,
92+
)
93+
94+
var binds []runtime.BindMount
95+
if socketPath := rt.SocketPath(); socketPath != "" {
96+
binds = append(binds, runtime.BindMount{HostPath: socketPath, ContainerPath: "/var/run/docker.sock"})
97+
env = append(env, "DOCKER_HOST=unix:///var/run/docker.sock")
98+
}
99+
86100
containers[i] = runtime.ContainerConfig{
87101
Image: image,
88-
Name: c.Name(),
102+
Name: containerName,
89103
Port: c.Port,
90104
ContainerPort: containerPort,
91105
HealthPath: healthPath,
92106
Env: env,
93107
Tag: c.Tag,
94108
ProductName: productName,
109+
Binds: binds,
110+
ExtraPorts: servicePortRange(),
95111
}
96112
}
97113

@@ -341,3 +357,14 @@ func hasDuplicateContainerTypes(containers []config.ContainerConfig) bool {
341357
}
342358
return false
343359
}
360+
361+
func servicePortRange() []runtime.PortMapping {
362+
const start = 4510
363+
const end = 4559
364+
var ports []runtime.PortMapping
365+
for p := start; p <= end; p++ {
366+
ps := strconv.Itoa(p)
367+
ports = append(ports, runtime.PortMapping{ContainerPort: ps, HostPort: ps})
368+
}
369+
return ports
370+
}

internal/container/start_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"errors"
77
"io"
8+
"strconv"
89
"testing"
910

1011
"github.com/localstack/lstk/internal/log"
@@ -56,3 +57,19 @@ func TestEmitPostStartPointers_WithoutWebApp(t *testing.T) {
5657
out.String(),
5758
)
5859
}
60+
61+
func TestServicePortRange_Returns50Entries(t *testing.T) {
62+
ports := servicePortRange()
63+
64+
require.Len(t, ports, 50)
65+
assert.Equal(t, "4510", ports[0].ContainerPort)
66+
assert.Equal(t, "4510", ports[0].HostPort)
67+
assert.Equal(t, "4559", ports[49].ContainerPort)
68+
assert.Equal(t, "4559", ports[49].HostPort)
69+
70+
for i, p := range ports {
71+
expected := strconv.Itoa(4510 + i)
72+
assert.Equal(t, expected, p.ContainerPort)
73+
assert.Equal(t, expected, p.HostPort)
74+
}
75+
}

internal/env/env.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
type Env struct {
1111
AuthToken string
1212
LocalStackHost string
13+
DockerHost string
1314
DisableEvents bool
1415

1516
APIEndpoint string
@@ -35,6 +36,7 @@ func Init() *Env {
3536
return &Env{
3637
AuthToken: os.Getenv("LOCALSTACK_AUTH_TOKEN"),
3738
LocalStackHost: os.Getenv("LOCALSTACK_HOST"),
39+
DockerHost: os.Getenv("DOCKER_HOST"),
3840
DisableEvents: os.Getenv("LOCALSTACK_DISABLE_EVENTS") == "1",
3941
APIEndpoint: viper.GetString("api_endpoint"),
4042
WebAppURL: viper.GetString("web_app_url"),

0 commit comments

Comments
 (0)