1
- package docker
1
+ package builder
2
2
3
3
import (
4
4
"fmt"
@@ -19,8 +19,8 @@ import (
19
19
"github.com/docker/docker/pkg/progress"
20
20
"github.com/docker/docker/pkg/streamformatter"
21
21
"github.com/docker/docker/pkg/term"
22
+ "github.com/docker/engine-api/client"
22
23
"github.com/docker/engine-api/types"
23
- "github.com/docker/libcompose/project"
24
24
)
25
25
26
26
// DefaultDockerfileName is the default name of a Dockerfile
@@ -29,29 +29,22 @@ const DefaultDockerfileName = "Dockerfile"
29
29
// Builder defines methods to provide a docker builder. This makes libcompose
30
30
// not tied up to the docker daemon builder.
31
31
type Builder interface {
32
- Build (imageName string , p * project. Project , service project. Service ) error
32
+ Build (imageName string ) error
33
33
}
34
34
35
35
// DaemonBuilder is the daemon "docker build" Builder implementation.
36
36
type DaemonBuilder struct {
37
- context * Context
38
- }
39
-
40
- // NewDaemonBuilder creates a DaemonBuilder based on the specified context.
41
- func NewDaemonBuilder (ctx * Context ) * DaemonBuilder {
42
- return & DaemonBuilder {
43
- context : ctx ,
44
- }
37
+ Client client.APIClient
38
+ ContextDirectory string
39
+ Dockerfile string
40
+ AuthConfigs map [string ]types.AuthConfig
41
+ NoCache bool
45
42
}
46
43
47
44
// Build implements Builder. It consumes the docker build API endpoint and sends
48
45
// a tar of the specified service build context.
49
- func (d * DaemonBuilder ) Build (imageName string , p * project.Project , service project.Service ) error {
50
- if service .Config ().Build == "" {
51
- return fmt .Errorf ("Specified service does not have a build section" )
52
- }
53
-
54
- ctx , err := CreateTar (p , service .Name ())
46
+ func (d * DaemonBuilder ) Build (imageName string ) error {
47
+ ctx , err := createTar (d .ContextDirectory , d .Dockerfile )
55
48
if err != nil {
56
49
return err
57
50
}
@@ -65,19 +58,20 @@ func (d *DaemonBuilder) Build(imageName string, p *project.Project, service proj
65
58
66
59
var body io.Reader = progress .NewProgressReader (ctx , progressOutput , 0 , "" , "Sending build context to Docker daemon" )
67
60
68
- client := d .context .ClientFactory .Create (service )
69
-
70
61
logrus .Infof ("Building %s..." , imageName )
71
62
72
63
outFd , isTerminalOut := term .GetFdInfo (os .Stdout )
73
64
74
- response , err := client .ImageBuild (context .Background (), body , types.ImageBuildOptions {
65
+ response , err := d . Client .ImageBuild (context .Background (), body , types.ImageBuildOptions {
75
66
Tags : []string {imageName },
76
- NoCache : d .context . NoCache ,
67
+ NoCache : d .NoCache ,
77
68
Remove : true ,
78
- Dockerfile : service . Config () .Dockerfile ,
79
- AuthConfigs : d .context . ConfigFile . AuthConfigs ,
69
+ Dockerfile : d .Dockerfile ,
70
+ AuthConfigs : d .AuthConfigs ,
80
71
})
72
+ if err != nil {
73
+ return err
74
+ }
81
75
82
76
err = jsonmessage .DisplayJSONMessagesStream (response .Body , buildBuff , outFd , isTerminalOut , nil )
83
77
if err != nil {
@@ -86,37 +80,33 @@ func (d *DaemonBuilder) Build(imageName string, p *project.Project, service proj
86
80
if jerr .Code == 0 {
87
81
jerr .Code = 1
88
82
}
89
- fmt .Fprintf (os .Stderr , "%s%s" , progBuff , buildBuff )
90
83
return fmt .Errorf ("Status: %s, Code: %d" , jerr .Message , jerr .Code )
91
84
}
92
85
}
93
86
return err
94
87
}
95
88
96
89
// CreateTar create a build context tar for the specified project and service name.
97
- func CreateTar ( p * project. Project , name string ) (io.ReadCloser , error ) {
90
+ func createTar ( contextDirectory , dockerfile string ) (io.ReadCloser , error ) {
98
91
// This code was ripped off from docker/api/client/build.go
99
- serviceConfig , _ := p .Configs .Get (name )
100
-
101
- root := serviceConfig .Build
102
- dockerfileName := filepath .Join (root , serviceConfig .Dockerfile )
92
+ dockerfileName := filepath .Join (contextDirectory , dockerfile )
103
93
104
- absRoot , err := filepath .Abs (root )
94
+ absContextDirectory , err := filepath .Abs (contextDirectory )
105
95
if err != nil {
106
96
return nil , err
107
97
}
108
98
109
99
filename := dockerfileName
110
100
111
- if dockerfileName == "" {
101
+ if dockerfile == "" {
112
102
// No -f/--file was specified so use the default
113
103
dockerfileName = DefaultDockerfileName
114
- filename = filepath .Join (absRoot , dockerfileName )
104
+ filename = filepath .Join (absContextDirectory , dockerfileName )
115
105
116
106
// Just to be nice ;-) look for 'dockerfile' too but only
117
107
// use it if we found it, otherwise ignore this check
118
108
if _ , err = os .Lstat (filename ); os .IsNotExist (err ) {
119
- tmpFN := path .Join (absRoot , strings .ToLower (dockerfileName ))
109
+ tmpFN := path .Join (absContextDirectory , strings .ToLower (dockerfileName ))
120
110
if _ , err = os .Lstat (tmpFN ); err == nil {
121
111
dockerfileName = strings .ToLower (dockerfileName )
122
112
filename = tmpFN
@@ -130,7 +120,7 @@ func CreateTar(p *project.Project, name string) (io.ReadCloser, error) {
130
120
}
131
121
132
122
// Now reset the dockerfileName to be relative to the build context
133
- dockerfileName , err = filepath .Rel (absRoot , filename )
123
+ dockerfileName , err = filepath .Rel (absContextDirectory , filename )
134
124
if err != nil {
135
125
return nil , err
136
126
}
@@ -147,7 +137,7 @@ func CreateTar(p *project.Project, name string) (io.ReadCloser, error) {
147
137
var includes = []string {"." }
148
138
var excludes []string
149
139
150
- dockerIgnorePath := path .Join (root , ".dockerignore" )
140
+ dockerIgnorePath := path .Join (contextDirectory , ".dockerignore" )
151
141
dockerIgnore , err := os .Open (dockerIgnorePath )
152
142
if err != nil {
153
143
if ! os .IsNotExist (err ) {
@@ -174,7 +164,7 @@ func CreateTar(p *project.Project, name string) (io.ReadCloser, error) {
174
164
includes = append (includes , ".dockerignore" , dockerfileName )
175
165
}
176
166
177
- if err := builder .ValidateContextDirectory (root , excludes ); err != nil {
167
+ if err := builder .ValidateContextDirectory (contextDirectory , excludes ); err != nil {
178
168
return nil , fmt .Errorf ("Error checking context is accessible: '%s'. Please check permissions and try again." , err )
179
169
}
180
170
@@ -184,5 +174,5 @@ func CreateTar(p *project.Project, name string) (io.ReadCloser, error) {
184
174
IncludeFiles : includes ,
185
175
}
186
176
187
- return archive .TarWithOptions (root , options )
177
+ return archive .TarWithOptions (contextDirectory , options )
188
178
}
0 commit comments