forked from facebookarchive/dockerutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.go
38 lines (31 loc) · 784 Bytes
/
start.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package dockerutil
import (
"github.com/facebookgo/stackerr"
"github.com/samalba/dockerclient"
)
// CreateWithPull is the same as CreateContainer but will pull the image if it
// isn't found and retry creating the container.
func CreateWithPull(
d *dockerclient.DockerClient,
c *dockerclient.ContainerConfig,
name string,
) (string, error) {
id, err := d.CreateContainer(c, name)
if err == nil {
return id, nil
}
// unknown error, bail
if err != dockerclient.ErrNotFound {
return "", stackerr.Wrap(err)
}
// need to pull the image
if err := d.PullImage(c.Image, nil); err != nil {
return "", stackerr.Wrap(err)
}
// try again with the pulled image
id, err = d.CreateContainer(c, name)
if err != nil {
return "", stackerr.Wrap(err)
}
return id, nil
}