Skip to content

Commit 60813af

Browse files
sandyskiestonistiigi
authored andcommitted
add --network option for docker build
Signed-off-by: sandyskies <[email protected]> Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 67b85f9 commit 60813af

10 files changed

Lines changed: 25 additions & 1 deletion

File tree

api/server/router/build/build_routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBui
5151
options.CPUSetCPUs = r.FormValue("cpusetcpus")
5252
options.CPUSetMems = r.FormValue("cpusetmems")
5353
options.CgroupParent = r.FormValue("cgroupparent")
54+
options.NetworkMode = r.FormValue("networkmode")
5455
options.Tags = r.Form["t"]
5556
options.SecurityOpt = r.Form["securityopt"]
5657

api/types/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ type ImageBuildOptions struct {
141141
Memory int64
142142
MemorySwap int64
143143
CgroupParent string
144+
NetworkMode string
144145
ShmSize int64
145146
Dockerfile string
146147
Ulimits []*units.Ulimit

builder/dockerfile/internals.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ func (b *Builder) create() (string, error) {
487487
Isolation: b.options.Isolation,
488488
ShmSize: b.options.ShmSize,
489489
Resources: resources,
490+
NetworkMode: container.NetworkMode(b.options.NetworkMode),
490491
}
491492

492493
config := *b.runConfig

cli/command/image/build.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type buildOptions struct {
5858
cacheFrom []string
5959
compress bool
6060
securityOpt []string
61+
networkMode string
6162
}
6263

6364
// NewBuildCommand creates a new `docker build` command
@@ -105,6 +106,7 @@ func NewBuildCommand(dockerCli *command.DockerCli) *cobra.Command {
105106
flags.StringSliceVar(&options.cacheFrom, "cache-from", []string{}, "Images to consider as cache sources")
106107
flags.BoolVar(&options.compress, "compress", false, "Compress the build context using gzip")
107108
flags.StringSliceVar(&options.securityOpt, "security-opt", []string{}, "Security options")
109+
flags.StringVar(&options.networkMode, "network", "default", "Connect a container to a network")
108110

109111
command.AddTrustedFlags(flags, true)
110112

@@ -302,6 +304,7 @@ func runBuild(dockerCli *command.DockerCli, options buildOptions) error {
302304
Labels: runconfigopts.ConvertKVStringsToMap(options.labels.GetAll()),
303305
CacheFrom: options.cacheFrom,
304306
SecurityOpt: options.securityOpt,
307+
NetworkMode: options.networkMode,
305308
}
306309

307310
response, err := dockerCli.Client().ImageBuild(ctx, body, buildOptions)

client/image_build.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func imageBuildOptionsToQuery(options types.ImageBuildOptions) (url.Values, erro
8484
}
8585

8686
query.Set("cpusetcpus", options.CPUSetCPUs)
87+
query.Set("networkmode", options.NetworkMode)
8788
query.Set("cpusetmems", options.CPUSetMems)
8889
query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
8990
query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))

docs/reference/api/docker_remote_api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ This section lists each version from latest to oldest. Each listing includes a
130130

131131
[Docker Remote API v1.25](docker_remote_api_v1.25.md) documentation
132132

133+
* `POST /build` accepts `networkmode` parameter to specify network used during build.
133134
* `GET /images/(name)/json` now returns `OsVersion` if populated
134135
* `GET /info` now returns `Isolation`.
135136
* `POST /containers/create` now takes `AutoRemove` in HostConfig, to enable auto-removal of the container on daemon side when the container's process exits.

docs/reference/api/docker_remote_api_v1.25.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,6 +1788,11 @@ or being killed.
17881788
passing secret values. [Read more about the buildargs instruction](../../reference/builder.md#arg)
17891789
- **shmsize** - Size of `/dev/shm` in bytes. The size must be greater than 0. If omitted the system uses 64MB.
17901790
- **labels** – JSON map of string pairs for labels to set on the image.
1791+
- **networkmode** - Sets the networking mode for the run commands during
1792+
build. Supported standard values are: `bridge`, `host`, `none`, and
1793+
`container:<name|id>`. Any other value is taken as a custom network's
1794+
name to which this container should connect to.
1795+
17911796

17921797
**Request Headers**:
17931798

docs/reference/commandline/build.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ Options:
3838
--label value Set metadata for an image (default [])
3939
-m, --memory string Memory limit
4040
--memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap
41+
--network string Set the networking mode for the run commands
42+
during build.
43+
'bridge': use default Docker bridge
44+
'none': no networking
45+
'container:<name|id>': reuse another container's network stack
46+
'host': use the Docker host network stack
47+
'<network-name>|<network-id>': connect to a user-defined network
4148
--no-cache Do not use cache when building the image
4249
--pull Always attempt to pull a newer version of the image
4350
-q, --quiet Suppress the build output and print image ID on success

man/docker-build.1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ docker-build - Build a new image from the source code at PATH
2222
[**-t**|**--tag**[=*[]*]]
2323
[**-m**|**--memory**[=*MEMORY*]]
2424
[**--memory-swap**[=*LIMIT*]]
25+
[**--network**[=*"default"*]]
2526
[**--shm-size**[=*SHM-SIZE*]]
2627
[**--cpu-period**[=*0*]]
2728
[**--cpu-quota**[=*0*]]
@@ -111,6 +112,9 @@ set as the **URL**, the repository is cloned locally and then sent as the contex
111112
`k` (kilobytes), `m` (megabytes), or `g` (gigabytes). If you don't specify a
112113
unit, `b` is used. Set LIMIT to `-1` to enable unlimited swap.
113114

115+
**--network**=*NETWORK*
116+
117+
114118
**--shm-size**=*SHM-SIZE*
115119
Size of `/dev/shm`. The format is `<number><unit>`. `number` must be greater than `0`.
116120
Unit is optional and can be `b` (bytes), `k` (kilobytes), `m` (megabytes), or `g` (gigabytes). If you omit the unit, the system uses bytes.

man/docker-run.1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ string name. The name is useful when defining links (see **--link**) (or any
388388
other place you need to identify a container). This works for both background
389389
and foreground Docker containers.
390390

391-
**--net**="*bridge*"
391+
**--network**="*bridge*"
392392
Set the Network mode for the container
393393
'bridge': create a network stack on the default Docker bridge
394394
'none': no networking

0 commit comments

Comments
 (0)