Skip to content

Commit d8b1680

Browse files
paultyngarekkas
authored andcommitted
core: add ExposedPorts to configuration (#92)
Signed-off-by: Paul Tyng <[email protected]>
1 parent f2a4ab9 commit d8b1680

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

dockertest.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ func NewPool(endpoint string) (*Pool, error) {
9292

9393
// RunOptions is used to pass in optional parameters when running a container.
9494
type RunOptions struct {
95-
Repository string
96-
Tag string
97-
Env []string
98-
Cmd []string
99-
Mounts []string
100-
Links []string
95+
Repository string
96+
Tag string
97+
Env []string
98+
Cmd []string
99+
Mounts []string
100+
Links []string
101+
ExposedPorts []string
101102
}
102103

103104
// RunWithOptions starts a docker container.
@@ -108,6 +109,14 @@ func (d *Pool) RunWithOptions(opts *RunOptions) (*Resource, error) {
108109
tag := opts.Tag
109110
env := opts.Env
110111
cmd := opts.Cmd
112+
var exp map[dc.Port]struct{}
113+
114+
if len(opts.ExposedPorts) > 0 {
115+
exp = map[dc.Port]struct{}{}
116+
for _, p := range opts.ExposedPorts {
117+
exp[dc.Port(p)] = struct{}{}
118+
}
119+
}
111120

112121
mounts := []dc.Mount{}
113122

@@ -140,10 +149,11 @@ func (d *Pool) RunWithOptions(opts *RunOptions) (*Resource, error) {
140149

141150
c, err := d.Client.CreateContainer(dc.CreateContainerOptions{
142151
Config: &dc.Config{
143-
Image: fmt.Sprintf("%s:%s", repository, tag),
144-
Env: env,
145-
Cmd: cmd,
146-
Mounts: mounts,
152+
Image: fmt.Sprintf("%s:%s", repository, tag),
153+
Env: env,
154+
Cmd: cmd,
155+
Mounts: mounts,
156+
ExposedPorts: exp,
147157
},
148158
HostConfig: &dc.HostConfig{
149159
PublishAllPorts: true,

dockertest_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ func TestMongo(t *testing.T) {
6262
options := &RunOptions{
6363
Repository: "mongo",
6464
Tag: "3.3.12",
65-
Cmd: []string{"mongod", "--smallfiles"},
65+
Cmd: []string{"mongod", "--smallfiles", "--port", "3000"},
66+
// expose a different port
67+
ExposedPorts: []string{"3000"},
6668
}
6769
resource, err := pool.RunWithOptions(options)
6870
require.Nil(t, err)
69-
port := resource.GetPort("27017/tcp")
71+
port := resource.GetPort("3000/tcp")
7072
assert.NotEmpty(t, port)
7173

7274
err = pool.Retry(func() error {

0 commit comments

Comments
 (0)