Skip to content

Commit

Permalink
chore: review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
atzoum committed Oct 15, 2024
1 parent c8f33be commit d0e025e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
39 changes: 25 additions & 14 deletions dockertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func (d *Pool) RunWithOptions(opts *RunOptions, hcOpts ...func(*dc.HostConfig))
return nil, err
}

c, err = d.inspectContainerWithRetries(c.ID, 0)
c, err = d.inspectContainerWithRetries(c.ID)
if err != nil {
return nil, err
}
Expand All @@ -492,23 +492,34 @@ func (d *Pool) RunWithOptions(opts *RunOptions, hcOpts ...func(*dc.HostConfig))
}

// inspectContainerWithRetries will repeat the inspect call until the container has port bindings assigned.
func (d *Pool) inspectContainerWithRetries(id string, retry int) (*dc.Container, error) {
c, err := d.Client.InspectContainer(id)
if err != nil {
return nil, err
}
func (d *Pool) inspectContainerWithRetries(id string) (*dc.Container, error) {
const maxRetries = 10
if retry > maxRetries {
return c, nil
}
// wait for port bindings to be assigned
for _, bindings := range c.NetworkSettings.Ports {
if len(bindings) == 0 {
var (
retryNum int
c *dc.Container
err error
)
for retryNum < maxRetries {
if retryNum > 0 {
time.Sleep(100 * time.Millisecond)
return d.inspectContainerWithRetries(id, retry+1)
}
c, err = d.Client.InspectContainer(id)
if err != nil {
return nil, err
}
if hasEmptyPortBindings := func() bool {
for _, bindings := range c.NetworkSettings.Ports {
if len(bindings) == 0 {
return true
}
}
return false
}(); !hasEmptyPortBindings {
return c, nil
}
retryNum++
}
return c, nil
return c, err
}

// Run starts a docker container.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/lib/pq v1.10.9
github.com/moby/term v0.5.0
github.com/opencontainers/image-spec v1.1.0
github.com/opencontainers/runc v1.1.14
github.com/opencontainers/runc v1.1.13
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
golang.org/x/sys v0.21.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/opencontainers/runc v1.1.14 h1:rgSuzbmgz5DUJjeSnw337TxDbRuqjs6iqQck/2weR6w=
github.com/opencontainers/runc v1.1.14/go.mod h1:E4C2z+7BxR7GHXp0hAY53mek+x49X1LjPNeMTfRGvOA=
github.com/opencontainers/runc v1.1.13 h1:98S2srgG9vw0zWcDpFMn5TRrh8kLxa/5OFUstuUhmRs=
github.com/opencontainers/runc v1.1.13/go.mod h1:R016aXacfp/gwQBYw2FDGa9m+n6atbLWrYY8hNMT/sA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down

0 comments on commit d0e025e

Please sign in to comment.