Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[build][packaging] Add resilience when docker build #22050

Merged
9 changes: 8 additions & 1 deletion dev-tools/mage/dockerbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os/exec"
"path/filepath"
"strings"
"time"

"github.com/magefile/mage/sh"
"github.com/pkg/errors"
Expand Down Expand Up @@ -71,7 +72,13 @@ func (b *dockerBuilder) Build() error {

tag, err := b.dockerBuild()
if err != nil {
return errors.Wrap(err, "failed to build docker")
fmt.Println(">> Building docker images again (after 10 seconds)")
// This sleep is to avoid hitting the docker build issues when resources are not available.
time.Sleep(10)
tag, err := b.dockerBuild()
if err != nil {
return errors.Wrap(err, "failed to build docker")
}
}

if err := b.dockerSave(tag); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions libbeat/tests/compose/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,23 @@ func (d *wrapperDriver) Up(ctx context.Context, opts UpOptions, service string)
return errors.Wrapf(err, "failed to pull images using docker-compose: %s", stderr.String())
}

// Try to build the docker images
var stderr bytes.Buffer
build := d.cmd(ctx, "build", "--pull", service)
build.Stdout = nil
build.Stderr = &stderr
if err := build.Run(); err != nil {
fmt.Println(">> Building docker images again (after 10 seconds)")
// This sleep is to avoid hitting the docker build issues when resources are not available.
time.Sleep(10)
build := d.cmd(ctx, "build", service)
build.Stdout = nil
build.Stderr = &stderr
if err := build.Run(); err != nil {
fmt.Println(">> Building docker images failed using docker-compose (let's try now with docker-compose up --build): %s", stderr.String())
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are already some retries in the higher-level EnsureUp method, that is the one used out of this package.

Also, we shouldn't add a explicit build here, we should take into account opts.Create.Build and so on.

I think this code is not needed.

err := d.cmd(ctx, "up", args...).Run()
if err != nil {
return err
Expand Down
13 changes: 11 additions & 2 deletions x-pack/elastic-agent/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,13 @@ func runAgent(env map[string]string) error {
}

// build docker image
if err := sh.Run("docker", "build", "-t", tag, "."); err != nil {
return err
if err := dockerBuild(tag); err != nil {
fmt.Println(">> Building docker images again (after 10 seconds)")
// This sleep is to avoid hitting the docker build issues when resources are not available.
time.Sleep(10)
if err := dockerBuild(tag); err != nil {
return err
}
}
}

Expand Down Expand Up @@ -625,6 +630,10 @@ func copyAll(from, to string) error {
})
}

func dockerBuild(tag string) string {
return sh.Run("docker", "build", "-t", tag, ".")
}

func dockerTag() string {
const commitLen = 7
tagBase := "elastic-agent"
Expand Down