feat: expose whether a network was created by dokku

`network:list` and `network:info` now expose a `DokkuManaged` boolean derived from the `com.dokku.network-name` label that `network:create` applies, and `network:list` gains a `--dokku-managed` flag to restrict output to dokku-created networks. This lets tooling distinguish networks dokku created from Docker built-ins and networks created by other tooling such as compose.
This commit is contained in:
Jose Diaz-Gonzalez
2026-07-08 00:03:29 -04:00
parent 24e3809a34
commit ac4b86fd8e
6 changed files with 113 additions and 23 deletions

View File

@@ -11,15 +11,21 @@ import (
"github.com/dokku/dokku/plugins/common"
)
// dokkuNetworkNameLabel is the docker label network:create applies to every
// network it creates. Its presence is the marker used to identify a network as
// dokku-managed.
const dokkuNetworkNameLabel = "com.dokku.network-name"
type DockerNetwork struct {
CreatedAt time.Time
Driver string
ID string
Internal bool
IPv6 bool
Labels map[string]string
Name string
Scope string
CreatedAt time.Time
DokkuManaged bool
Driver string
ID string
Internal bool
IPv6 bool
Labels map[string]string
Name string
Scope string
}
// attachAppToNetwork attaches a container to a network
@@ -206,6 +212,10 @@ func getNetworks() (map[string]DockerNetwork, error) {
network.Labels[key] = value
}
if _, ok := network.Labels[dokkuNetworkNameLabel]; ok {
network.DokkuManaged = true
}
networks[network.Name] = network
}

View File

@@ -22,7 +22,7 @@ Additional commands:`
network:destroy <network>, Destroys a docker network
network:exists <network>, Checks if a docker network exists
network:info <network> [--format text|json], Outputs information about a docker network
network:list [--format text|json], Lists all docker networks
network:list [--format text|json] [--dokku-managed], Lists all docker networks
network:rebuild <app>, Rebuilds network settings for an app
network:rebuildall, Rebuild network settings for all apps
network:report [<app>] [<flag>], Displays a network report for one or more apps

View File

@@ -43,8 +43,9 @@ func main() {
case "list":
args := flag.NewFlagSet("network:list", flag.ExitOnError)
format := args.String("format", "text", "format: [ text | json ]")
dokkuManaged := args.Bool("dokku-managed", false, "show only dokku-managed networks")
args.Parse(os.Args[2:])
err = network.CommandList(*format)
err = network.CommandList(*format, *dokkuManaged)
case "rebuild":
args := flag.NewFlagSet("network:rebuild", flag.ExitOnError)
args.Parse(os.Args[2:])

View File

@@ -15,7 +15,7 @@ func CommandCreate(networkName string) error {
common.LogInfo1Quiet(fmt.Sprintf("Creating network %v", networkName))
result, err := common.CallExecCommand(common.ExecCommandInput{
Command: common.DockerBin(),
Args: []string{"network", "create", "--attachable", "--label", fmt.Sprintf("com.dokku.network-name=%v", networkName), networkName},
Args: []string{"network", "create", "--attachable", "--label", fmt.Sprintf("%s=%v", dokkuNetworkNameLabel, networkName), networkName},
})
if err != nil {
return fmt.Errorf("Unable to create network: %w", err)
@@ -103,18 +103,19 @@ func CommandInfo(networkName string, format string) error {
return nil
}
length := 10
length := 16
common.LogInfo2Quiet(fmt.Sprintf("%s network information", networkName))
common.LogVerbose(fmt.Sprintf("%s%s", common.RightPad("ID:", length, " "), network.ID))
common.LogVerbose(fmt.Sprintf("%s%s", common.RightPad("Name:", length, " "), network.Name))
common.LogVerbose(fmt.Sprintf("%s%s", common.RightPad("Driver:", length, " "), network.Driver))
common.LogVerbose(fmt.Sprintf("%s%s", common.RightPad("Scope:", length, " "), network.Scope))
common.LogVerbose(fmt.Sprintf("%s%v", common.RightPad("Dokku managed:", length, " "), network.DokkuManaged))
return nil
}
// CommandList is an alias for "docker network ls"
func CommandList(format string) error {
func CommandList(format string, dokkuManaged bool) error {
networks, err := getNetworks()
if err != nil {
return err
@@ -123,6 +124,9 @@ func CommandList(format string) error {
if format == "json" {
networkList := []DockerNetwork{}
for _, network := range networks {
if dokkuManaged && !network.DokkuManaged {
continue
}
networkList = append(networkList, network)
}
out, err := json.Marshal(networkList)
@@ -135,6 +139,9 @@ func CommandList(format string) error {
common.LogInfo2Quiet("Networks")
for _, network := range networks {
if dokkuManaged && !network.DokkuManaged {
continue
}
fmt.Println(network.Name)
}