Skip to content

Commit

Permalink
feat: support custom docker network id
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Jun 4, 2024
1 parent 36b8285 commit d854093
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func init() {
flags.Bool("debug", false, "output debug logs to stderr")
flags.String("workdir", "", "path to a Supabase project directory")
flags.Bool("experimental", false, "enable experimental features")
flags.String("network-id", "", "use the specified docker network instead of a generated one")
flags.Var(&utils.DNSResolver, "dns-resolver", "lookup domain names using the specified resolver")
flags.BoolVar(&createTicket, "create-ticket", false, "create a support ticket for any CLI error")
cobra.CheckErr(viper.BindPFlags(flags))
Expand Down
2 changes: 1 addition & 1 deletion internal/db/diff/migra.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func DiffSchemaMigra(ctx context.Context, source, target string, schema []string
Cmd: cmd,
},
container.HostConfig{
NetworkMode: container.NetworkMode("host"),
NetworkMode: network.NetworkHost,
},
network.NetworkingConfig{},
"",
Expand Down
2 changes: 1 addition & 1 deletion internal/db/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func dump(ctx context.Context, config pgconn.Config, script string, env []string
Cmd: []string{"bash", "-c", script, "--"},
},
container.HostConfig{
NetworkMode: container.NetworkMode("host"),
NetworkMode: network.NetworkHost,
},
network.NetworkingConfig{},
"",
Expand Down
10 changes: 4 additions & 6 deletions internal/db/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ func Run(ctx context.Context, testFiles []string, config pgconn.Config, fsys afe
}()
}
// Use custom network when connecting to local database
networkID := "host"
hostConfig := container.HostConfig{Binds: binds}
if utils.IsLocalDatabase(config) {
config.Host = utils.DbAliases[0]
config.Port = 5432
networkID = utils.NetId
} else {
hostConfig.NetworkMode = network.NetworkHost
}
// Run pg_prove on volume mount
return utils.DockerRunOnceWithConfig(
Expand All @@ -87,10 +88,7 @@ func Run(ctx context.Context, testFiles []string, config pgconn.Config, fsys afe
Cmd: cmd,
WorkingDir: dstPath,
},
container.HostConfig{
NetworkMode: container.NetworkMode(networkID),
Binds: binds,
},
hostConfig,
network.NetworkingConfig{},
"",
os.Stdout,
Expand Down
9 changes: 4 additions & 5 deletions internal/gen/types/typescript/typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas
return nil
}

networkID := "host"
hostConfig := container.HostConfig{}
if utils.IsLocalDatabase(dbConfig) {
if err := utils.AssertSupabaseDbIsRunning(); err != nil {
return err
Expand All @@ -53,7 +53,8 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas
// Use custom network when connecting to local database
dbConfig.Host = utils.DbAliases[0]
dbConfig.Port = 5432
networkID = utils.NetId
} else {
hostConfig.NetworkMode = network.NetworkHost
}
// pg-meta does not set username as the default database, ie. postgres
if len(dbConfig.Database) == 0 {
Expand Down Expand Up @@ -81,9 +82,7 @@ func Run(ctx context.Context, projectId string, dbConfig pgconn.Config, schemas
},
Cmd: []string{"node", "dist/server/server.js"},
},
container.HostConfig{
NetworkMode: container.NetworkMode(networkID),
},
hostConfig,
network.NetworkingConfig{},
"",
os.Stdout,
Expand Down
1 change: 0 additions & 1 deletion internal/stop/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func Run(ctx context.Context, backup bool, projectId string, fsys afero.Fs) erro
// Sanity checks.
if len(projectId) > 0 {
utils.Config.ProjectId = projectId
utils.UpdateDockerIds()
} else if err := utils.LoadConfigFS(fsys); err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion internal/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func GetId(name string) string {
}

func UpdateDockerIds() {
NetId = GetId("network")
if NetId = viper.GetString("network-id"); len(NetId) == 0 {
NetId = GetId("network")
}
DbId = GetId(DbAliases[0])
ConfigId = GetId("config")
KongId = GetId(KongAliases[0])
Expand Down
6 changes: 4 additions & 2 deletions internal/utils/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,13 @@ func DockerStart(ctx context.Context, config container.Config, hostConfig contai
}
config.Labels[CliProjectLabel] = Config.ProjectId
config.Labels[composeProjectLabel] = Config.ProjectId
if len(hostConfig.NetworkMode) == 0 {
if networkId := viper.GetString("network-id"); len(networkId) > 0 {
hostConfig.NetworkMode = container.NetworkMode(networkId)
} else if len(hostConfig.NetworkMode) == 0 {
hostConfig.NetworkMode = container.NetworkMode(NetId)
}
// Create network with name
if hostConfig.NetworkMode.IsUserDefined() && hostConfig.NetworkMode.UserDefined() != "host" {
if hostConfig.NetworkMode.IsUserDefined() && hostConfig.NetworkMode.UserDefined() != network.NetworkHost {
if err := DockerNetworkCreateIfNotExists(ctx, hostConfig.NetworkMode.NetworkName()); err != nil {
return "", err
}
Expand Down

0 comments on commit d854093

Please sign in to comment.