Skip to content

Commit 059451e

Browse files
committed
add --network option for docker build
Upstream reference: moby#27702 Fix Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1486809 Signed-off-by: Antonio Murdaca <runcom@redhat.com>
1 parent 90e29fe commit 059451e

7 files changed

Lines changed: 43 additions & 4 deletions

File tree

api/client/image/build.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type buildOptions struct {
5656
rm bool
5757
forceRm bool
5858
pull bool
59+
networkMode string
5960
}
6061

6162
// NewBuildCommand creates a new `docker build` command
@@ -101,6 +102,7 @@ func NewBuildCommand(dockerCli *client.DockerCli) *cobra.Command {
101102
flags.BoolVar(&options.forceRm, "force-rm", false, "Always remove intermediate containers")
102103
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Suppress the build output and print image ID on success")
103104
flags.BoolVar(&options.pull, "pull", false, "Always attempt to pull a newer version of the image")
105+
flags.StringVar(&options.networkMode, "network", "default", "Connect a container to a network")
104106

105107
flags.VarP(&options.buildVolumes, "volume", "v", "Set build-time bind mounts")
106108

api/server/router/build/build_routes.go

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

5556
if r.Form.Get("shmsize") != "" {

builder/dockerfile/internals.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,11 @@ func (b *Builder) create() (string, error) {
535535

536536
// TODO: why not embed a hostconfig in builder?
537537
hostConfig := &container.HostConfig{
538-
Isolation: b.options.Isolation,
539-
ShmSize: b.options.ShmSize,
540-
Resources: resources,
541-
Binds: b.options.Binds,
538+
Isolation: b.options.Isolation,
539+
ShmSize: b.options.ShmSize,
540+
Resources: resources,
541+
NetworkMode: container.NetworkMode(b.options.NetworkMode),
542+
Binds: b.options.Binds,
542543
}
543544

544545
config := *b.runConfig

integration-cli/docker_cli_build_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7283,3 +7283,31 @@ func (s *DockerRegistrySuite) TestBuildWithPublicRegistryBlocked(c *check.C) {
72837283
func (s *DockerRegistrySuite) TestBuildWithAllRegistriesBlocked(c *check.C) {
72847284
s.doTestBuildWithPublicRegistryBlocked(c, "testbuildwithallregistriesblocked", []string{"--block-registry=all"})
72857285
}
7286+
7287+
func (s *DockerSuite) TestBuildNetNone(c *check.C) {
7288+
testRequires(c, DaemonIsLinux)
7289+
7290+
name := "testbuildnetnone"
7291+
_, out, err := buildImageWithOut(name, `
7292+
FROM busybox
7293+
RUN ping -c 1 8.8.8.8
7294+
`, true, "--network=none")
7295+
c.Assert(err, checker.NotNil)
7296+
c.Assert(out, checker.Contains, "unreachable")
7297+
}
7298+
7299+
func (s *DockerSuite) TestBuildNetContainer(c *check.C) {
7300+
testRequires(c, DaemonIsLinux)
7301+
7302+
id, _ := dockerCmd(c, "run", "--hostname", "foobar", "-d", "busybox", "nc", "-ll", "-p", "1234", "-e", "hostname")
7303+
7304+
name := "testbuildnetcontainer"
7305+
out, err := buildImage(name, `
7306+
FROM busybox
7307+
RUN nc localhost 1234 > /otherhost
7308+
`, true, "--network=container:"+strings.TrimSpace(id))
7309+
c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
7310+
7311+
host, _ := dockerCmd(c, "run", "testbuildnetcontainer", "cat", "/otherhost")
7312+
c.Assert(strings.TrimSpace(host), check.Equals, "foobar")
7313+
}

man/docker-build.1.md

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

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

vendor/src/github.com/docker/engine-api/client/image_build.go

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

8181
query.Set("cpusetcpus", options.CPUSetCPUs)
82+
query.Set("networkmode", options.NetworkMode)
8283
query.Set("cpusetmems", options.CPUSetMems)
8384
query.Set("cpushares", strconv.FormatInt(options.CPUShares, 10))
8485
query.Set("cpuquota", strconv.FormatInt(options.CPUQuota, 10))

vendor/src/github.com/docker/engine-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

0 commit comments

Comments
 (0)