Skip to content

Commit df4d49d

Browse files
fedepaolaeneasr
authored andcommitted
Added options to modify host config (#149)
* Added options to modify host config * Updated docs comments Signed-off-by: Federico Paolinelli <[email protected]>
1 parent 64f8235 commit df4d49d

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

dockertest.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,9 @@ type RunOptions struct {
181181
Privileged bool
182182
}
183183

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) {
186187
// Set the Dockerfile folder as build context
187188
dir, file := filepath.Split(dockerfilePath)
188189

@@ -199,7 +200,7 @@ func (d *Pool) BuildAndRunWithOptions(dockerfilePath string, opts *RunOptions) (
199200

200201
opts.Repository = opts.Name
201202

202-
return d.RunWithOptions(opts)
203+
return d.RunWithOptions(opts, hcOpts...)
203204
}
204205

205206
// BuildAndRun builds and starts a docker container
@@ -208,9 +209,13 @@ func (d *Pool) BuildAndRun(name, dockerfilePath string, env []string) (*Resource
208209
}
209210

210211
// RunWithOptions starts a docker container.
212+
// Optional modifier functions can be passed in order to change the hostconfig values not covered in RunOptions
211213
//
212214
// 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) {
214219
repository := opts.Repository
215220
tag := opts.Tag
216221
env := opts.Env
@@ -262,6 +267,22 @@ func (d *Pool) RunWithOptions(opts *RunOptions) (*Resource, error) {
262267
}
263268
}
264269

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+
265286
c, err := d.Client.CreateContainer(dc.CreateContainerOptions{
266287
Name: opts.Name,
267288
Config: &dc.Config{
@@ -276,17 +297,7 @@ func (d *Pool) RunWithOptions(opts *RunOptions) (*Resource, error) {
276297
Labels: opts.Labels,
277298
StopSignal: "SIGWINCH", // to support timeouts
278299
},
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,
290301
NetworkingConfig: &networkingConfig,
291302
})
292303
if err != nil {

dockertest_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,19 @@ func TestExpire(t *testing.T) {
166166

167167
require.Nil(t, pool.Purge(resource))
168168
}
169+
170+
func TestContainerWithShMzSize(t *testing.T) {
171+
shmemsize := int64(1024 * 1024)
172+
resource, err := pool.RunWithOptions(
173+
&RunOptions{
174+
Name: "db",
175+
Repository: "postgres",
176+
Tag: "9.5",
177+
}, func(hostConfig *dc.HostConfig) {
178+
hostConfig.ShmSize = shmemsize
179+
})
180+
require.Nil(t, err)
181+
assert.EqualValues(t, shmemsize, resource.Container.HostConfig.ShmSize, "shmsize don't match")
182+
183+
require.Nil(t, pool.Purge(resource))
184+
}

0 commit comments

Comments
 (0)