Skip to content

Commit db0b41b

Browse files
committed
fix: improve init error message for port usage
Signed-off-by: Vikram Vaswani <2571660+vvaswani@users.noreply.github.com>
1 parent 5b67241 commit db0b41b

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

pkg/standalone/standalone.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,28 @@ const (
9191
schedulerEtcdPort = 2379
9292

9393
daprVersionsWithScheduler = ">= 1.14.x"
94+
95+
portInUseHint = "Stop the process/container using that port or use `dapr init --network <name>` to avoid host port mapping."
9496
)
9597

9698
var (
9799
defaultImageRegistryName string
98100
isAirGapInit bool
99101
)
100102

103+
func ensureHostPortsAvailable(component string, ports []int, extraHint string) error {
104+
for _, port := range ports {
105+
if err := utils.CheckIfPortAvailable(port); err != nil {
106+
message := fmt.Sprintf("cannot start %s because host port %d is already in use", component, port)
107+
if extraHint != "" {
108+
message = fmt.Sprintf("%s. %s", message, extraHint)
109+
}
110+
return errors.New(message)
111+
}
112+
}
113+
return nil
114+
}
115+
101116
type configuration struct {
102117
APIVersion string `yaml:"apiVersion"`
103118
Kind string `yaml:"kind"`
@@ -387,6 +402,14 @@ func runZipkin(wg *sync.WaitGroup, errorChan chan<- error, info initInfo) {
387402
// do not create container again if it exists.
388403
args = append(args, "start", zipkinContainerName)
389404
} else {
405+
if info.dockerNetwork == "" {
406+
err = ensureHostPortsAvailable("Zipkin tracing", []int{9411}, portInUseHint+" To skip Zipkin, use `dapr init --slim`.")
407+
if err != nil {
408+
errorChan <- err
409+
return
410+
}
411+
}
412+
390413
imageName, err = resolveImageURI(daprImageInfo{
391414
ghcrImageName: zipkinGhcrImageName,
392415
dockerHubImageName: zipkinDockerImageName,
@@ -453,6 +476,14 @@ func runRedis(wg *sync.WaitGroup, errorChan chan<- error, info initInfo) {
453476
// do not create container again if it exists.
454477
args = append(args, "start", redisContainerName)
455478
} else {
479+
if info.dockerNetwork == "" {
480+
err = ensureHostPortsAvailable("Redis state store", []int{6379}, portInUseHint+" To skip Redis, use `dapr init --slim`.")
481+
if err != nil {
482+
errorChan <- err
483+
return
484+
}
485+
}
486+
456487
imageName, err = resolveImageURI(daprImageInfo{
457488
ghcrImageName: redisGhcrImageName,
458489
dockerHubImageName: redisDockerImageName,
@@ -515,6 +546,18 @@ func runPlacementService(wg *sync.WaitGroup, errorChan chan<- error, info initIn
515546
errorChan <- fmt.Errorf("%s container exists or is running. %s", placementContainerName, errInstallTemplate)
516547
return
517548
}
549+
550+
if info.dockerNetwork == "" {
551+
osPort := 50005
552+
if runtime.GOOS == daprWindowsOS {
553+
osPort = 6050
554+
}
555+
err = ensureHostPortsAvailable("placement service", []int{osPort, healthPort, metricPort}, portInUseHint)
556+
if err != nil {
557+
errorChan <- err
558+
return
559+
}
560+
}
518561
var image string
519562

520563
imgInfo := daprImageInfo{
@@ -610,6 +653,18 @@ func runSchedulerService(wg *sync.WaitGroup, errorChan chan<- error, info initIn
610653
errorChan <- fmt.Errorf("%s container exists or is running. %s", schedulerContainerName, errInstallTemplate)
611654
return
612655
}
656+
657+
if info.dockerNetwork == "" {
658+
osPort := 50006
659+
if runtime.GOOS == daprWindowsOS {
660+
osPort = 6060
661+
}
662+
err = ensureHostPortsAvailable("scheduler service", []int{osPort, schedulerEtcdPort, schedulerHealthPort, schedulerMetricPort}, portInUseHint)
663+
if err != nil {
664+
errorChan <- err
665+
return
666+
}
667+
}
613668
var image string
614669

615670
imgInfo := daprImageInfo{

0 commit comments

Comments
 (0)