@@ -16,6 +16,7 @@ import (
16
16
"github.com/docker/docker/api/types/mount"
17
17
"github.com/docker/docker/api/types/network"
18
18
"github.com/docker/docker/client"
19
+ "github.com/docker/go-connections/nat"
19
20
)
20
21
21
22
// Common socket paths
@@ -125,12 +126,14 @@ func findContainerSocket() (string, RuntimeType, error) {
125
126
}
126
127
127
128
// CreateContainer creates a container without starting it
129
+ // If options is nil, default options will be used
128
130
func (c * Client ) CreateContainer (
129
131
ctx context.Context ,
130
132
image , name string ,
131
133
command []string ,
132
134
envVars , labels map [string ]string ,
133
135
permissionConfig PermissionConfig ,
136
+ options * CreateContainerOptions ,
134
137
) (string , error ) {
135
138
// Convert environment variables to slice
136
139
env := make ([]string , 0 , len (envVars ))
@@ -149,19 +152,35 @@ func (c *Client) CreateContainer(
149
152
})
150
153
}
151
154
155
+ // Determine if we should attach stdio
156
+ // Default to true if options is nil, otherwise use options.AttachStdio
157
+ attachStdio := options == nil || options .AttachStdio
158
+
152
159
// Create container configuration
153
160
config := & container.Config {
154
161
Image : image ,
155
162
Cmd : command ,
156
163
Env : env ,
157
164
Labels : labels ,
158
- AttachStdin : true ,
159
- AttachStdout : true ,
160
- AttachStderr : true ,
161
- OpenStdin : true ,
165
+ AttachStdin : attachStdio ,
166
+ AttachStdout : attachStdio ,
167
+ AttachStderr : attachStdio ,
168
+ OpenStdin : attachStdio ,
162
169
Tty : false ,
163
170
}
164
171
172
+ // Add exposed ports if provided
173
+ if options != nil && len (options .ExposedPorts ) > 0 {
174
+ config .ExposedPorts = nat.PortSet {}
175
+ for port := range options .ExposedPorts {
176
+ natPort , err := nat .NewPort ("tcp" , strings .Split (port , "/" )[0 ])
177
+ if err != nil {
178
+ return "" , NewContainerError (err , "" , fmt .Sprintf ("failed to parse port: %v" , err ))
179
+ }
180
+ config .ExposedPorts [natPort ] = struct {}{}
181
+ }
182
+ }
183
+
165
184
// Create host configuration
166
185
hostConfig := & container.HostConfig {
167
186
Mounts : mounts ,
@@ -171,6 +190,26 @@ func (c *Client) CreateContainer(
171
190
SecurityOpt : permissionConfig .SecurityOpt ,
172
191
}
173
192
193
+ // Add port bindings if provided
194
+ if options != nil && len (options .PortBindings ) > 0 {
195
+ hostConfig .PortBindings = nat.PortMap {}
196
+ for port , bindings := range options .PortBindings {
197
+ natPort , err := nat .NewPort ("tcp" , strings .Split (port , "/" )[0 ])
198
+ if err != nil {
199
+ return "" , NewContainerError (err , "" , fmt .Sprintf ("failed to parse port: %v" , err ))
200
+ }
201
+
202
+ natBindings := make ([]nat.PortBinding , len (bindings ))
203
+ for i , binding := range bindings {
204
+ natBindings [i ] = nat.PortBinding {
205
+ HostIP : binding .HostIP ,
206
+ HostPort : binding .HostPort ,
207
+ }
208
+ }
209
+ hostConfig .PortBindings [natPort ] = natBindings
210
+ }
211
+ }
212
+
174
213
// Create network configuration
175
214
networkConfig := & network.NetworkingConfig {}
176
215
0 commit comments