@@ -181,8 +181,9 @@ type RunOptions struct {
181
181
Privileged bool
182
182
}
183
183
184
- // BuildAndRunWithOptions builds and starts a docker container
185
- func (d * Pool ) BuildAndRunWithOptions (dockerfilePath string , opts * RunOptions ) (* Resource , error ) {
184
+ // BuildAndRunWithOptions builds and starts a docker container.
185
+ // Optional modifier functions can be passed in order to change the hostconfig values not covered in RunOptions
186
+ func (d * Pool ) BuildAndRunWithOptions (dockerfilePath string , opts * RunOptions , hcOpts ... func (* dc.HostConfig )) (* Resource , error ) {
186
187
// Set the Dockerfile folder as build context
187
188
dir , file := filepath .Split (dockerfilePath )
188
189
@@ -199,7 +200,7 @@ func (d *Pool) BuildAndRunWithOptions(dockerfilePath string, opts *RunOptions) (
199
200
200
201
opts .Repository = opts .Name
201
202
202
- return d .RunWithOptions (opts )
203
+ return d .RunWithOptions (opts , hcOpts ... )
203
204
}
204
205
205
206
// BuildAndRun builds and starts a docker container
@@ -208,9 +209,13 @@ func (d *Pool) BuildAndRun(name, dockerfilePath string, env []string) (*Resource
208
209
}
209
210
210
211
// RunWithOptions starts a docker container.
212
+ // Optional modifier functions can be passed in order to change the hostconfig values not covered in RunOptions
211
213
//
212
214
// pool.Run(&RunOptions{Repository: "mongo", Cmd: []string{"mongod", "--smallfiles"}})
213
- func (d * Pool ) RunWithOptions (opts * RunOptions ) (* Resource , error ) {
215
+ // pool.Run(&RunOptions{Repository: "mongo", Cmd: []string{"mongod", "--smallfiles"}}, func(hostConfig *dc.HostConfig) {
216
+ // hostConfig.ShmSize = shmemsize
217
+ // })
218
+ func (d * Pool ) RunWithOptions (opts * RunOptions , hcOpts ... func (* dc.HostConfig )) (* Resource , error ) {
214
219
repository := opts .Repository
215
220
tag := opts .Tag
216
221
env := opts .Env
@@ -262,6 +267,22 @@ func (d *Pool) RunWithOptions(opts *RunOptions) (*Resource, error) {
262
267
}
263
268
}
264
269
270
+ hostConfig := dc.HostConfig {
271
+ PublishAllPorts : true ,
272
+ Binds : opts .Mounts ,
273
+ Links : opts .Links ,
274
+ PortBindings : opts .PortBindings ,
275
+ ExtraHosts : opts .ExtraHosts ,
276
+ CapAdd : opts .CapAdd ,
277
+ SecurityOpt : opts .SecurityOpt ,
278
+ Privileged : opts .Privileged ,
279
+ DNS : opts .DNS ,
280
+ }
281
+
282
+ for _ , hostConfigOption := range hcOpts {
283
+ hostConfigOption (& hostConfig )
284
+ }
285
+
265
286
c , err := d .Client .CreateContainer (dc.CreateContainerOptions {
266
287
Name : opts .Name ,
267
288
Config : & dc.Config {
@@ -276,17 +297,7 @@ func (d *Pool) RunWithOptions(opts *RunOptions) (*Resource, error) {
276
297
Labels : opts .Labels ,
277
298
StopSignal : "SIGWINCH" , // to support timeouts
278
299
},
279
- HostConfig : & dc.HostConfig {
280
- PublishAllPorts : true ,
281
- Binds : opts .Mounts ,
282
- Links : opts .Links ,
283
- PortBindings : opts .PortBindings ,
284
- ExtraHosts : opts .ExtraHosts ,
285
- CapAdd : opts .CapAdd ,
286
- SecurityOpt : opts .SecurityOpt ,
287
- Privileged : opts .Privileged ,
288
- DNS : opts .DNS ,
289
- },
300
+ HostConfig : & hostConfig ,
290
301
NetworkingConfig : & networkingConfig ,
291
302
})
292
303
if err != nil {
0 commit comments