Skip to content

Commit

Permalink
Debug logs for port bindings when Docker deploy image fails
Browse files Browse the repository at this point in the history
  • Loading branch information
MadLittleMods committed Jan 29, 2025
1 parent 8f0c763 commit 3a11cb8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
42 changes: 42 additions & 0 deletions internal/docker/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,48 @@ func printLogs(docker *client.Client, containerID, contextStr string) {
log.Printf("============== %s : END LOGS ==============\n\n\n", contextStr)
}

func printPortBindingsOfAllComplementContainers(docker *client.Client, contextStr string) {
ctx := context.Background()

containers, err := docker.ContainerList(ctx, container.ListOptions{
All: true,
Filters: label(
complementLabel,
),
})
if err != nil {
log.Printf("%s : Failed to list containers while trying to `printPortBindingsOfAllComplementContainers`: %s\n", contextStr, err)
return
}

log.Printf("============== %s : START ALL COMPLEMENT DOCKER PORT BINDINGS ==============\n", contextStr)

for _, container := range containers {
log.Printf("Container: %s: %s", container.ID, container.Names)

inspectRes, err := docker.ContainerInspect(ctx, container.ID)
if err != nil {
log.Printf("%s : Failed to inspect container (%s) while trying to `printPortBindingsOfAllComplementContainers`: %s\n", contextStr, container.ID, err)
return
}

// Print an example so it's easier to understand the output
log.Printf(" (host) -> (container)\n")
// Then print the actual port bindings
for containerPort, portBindings := range inspectRes.NetworkSettings.Ports {
hostPortBindingStrings := make([]string, len(portBindings))
for portBindingIndex, portBinding := range portBindings {
hostPortBindingStrings[portBindingIndex] = fmt.Sprintf("%s:%s", portBinding.HostIP, portBinding.HostPort)
}

log.Printf(" %s -> %s\n", strings.Join(hostPortBindingStrings, ", "), containerPort)
}

}

log.Printf("=============== %s : END ALL COMPLEMENT DOCKER PORT BINDINGS ===============\n\n\n", contextStr)
}

func endpoints(p nat.PortMap, csPort, ssPort int) (baseURL, fedBaseURL string, err error) {
csapiPort := fmt.Sprintf("%d/tcp", csPort)
csapiPortInfo, ok := p[nat.Port(csapiPort)]
Expand Down
16 changes: 14 additions & 2 deletions internal/docker/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ func (d *Deployer) CreateDirtyServer(hsName string) (*HomeserverDeployment, erro
baseImageURI = uri
}

containerName := fmt.Sprintf("complement_%s_dirty_%s", d.config.PackageNamespace, hsName)
hsDeployment, err := deployImage(
d.Docker, baseImageURI, fmt.Sprintf("complement_%s_dirty_%s", d.config.PackageNamespace, hsName),
d.Docker, baseImageURI, containerName,
d.config.PackageNamespace, "", hsName, nil, "dirty",
networkName, d.config,
)
Expand All @@ -101,6 +102,11 @@ func (d *Deployer) CreateDirtyServer(hsName string) (*HomeserverDeployment, erro
// print logs to help debug
printLogs(d.Docker, hsDeployment.ContainerID, "dirty")
}

// Give some context for what the port bindings look like the time of the failure.
// This gives better context for when `bind: address already in use` errors happen.
printPortBindingsOfAllComplementContainers(d.Docker, "While dirty deploying "+containerName)

return nil, fmt.Errorf("CreateDirtyServer: Failed to deploy image %v : %w", baseImageURI, err)
}
return hsDeployment, nil
Expand Down Expand Up @@ -164,15 +170,21 @@ func (d *Deployer) Deploy(ctx context.Context, blueprintName string) (*Deploymen
asIDToRegistrationMap := asIDToRegistrationFromLabels(img.Labels)

// TODO: Make CSAPI port configurable
containerName := fmt.Sprintf("complement_%s_%s_%s_%d", d.config.PackageNamespace, d.DeployNamespace, contextStr, counter)
deployment, err := deployImage(
d.Docker, img.ID, fmt.Sprintf("complement_%s_%s_%s_%d", d.config.PackageNamespace, d.DeployNamespace, contextStr, counter),
d.Docker, img.ID, containerName,
d.config.PackageNamespace, blueprintName, hsName, asIDToRegistrationMap, contextStr, networkName, d.config,
)
if err != nil {
if deployment != nil && deployment.ContainerID != "" {
// print logs to help debug
printLogs(d.Docker, deployment.ContainerID, contextStr)
}

// Give some context for what the port bindings look like the time of the failure.
// This gives better context for when `bind: address already in use` errors happen.
printPortBindingsOfAllComplementContainers(d.Docker, "While deploying "+containerName)

return fmt.Errorf("Deploy: Failed to deploy image %+v : %w", img, err)
}
mu.Lock()
Expand Down

0 comments on commit 3a11cb8

Please sign in to comment.