Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --add-host for docker build #30383

Merged
merged 1 commit into from
Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/server/router/build/build_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func newImageBuildOptions(ctx context.Context, r *http.Request) (*types.ImageBui
options.CgroupParent = r.FormValue("cgroupparent")
options.NetworkMode = r.FormValue("networkmode")
options.Tags = r.Form["t"]
options.ExtraHosts = r.Form["extrahosts"]
options.SecurityOpt = r.Form["securityopt"]
options.Squash = httputils.BoolValue(r, "squash")

Expand Down
4 changes: 4 additions & 0 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4365,6 +4365,10 @@ paths:
in: "query"
description: "A name and optional tag to apply to the image in the `name:tag` format. If you omit the tag the default `latest` value is assumed. You can provide several `t` parameters."
type: "string"
- name: "extrahosts"
in: "query"
description: "Extra hosts to add to /etc/hosts"
type: "string"
- name: "remote"
in: "query"
description: "A Git repository URI or HTTP/HTTPS context URI. If the URI points to a single text file, the file’s contents are placed into a file called `Dockerfile` and the image is built from that file. If the URI points to a tarball, the file is downloaded by the daemon and the contents therein used as the context for the build. If the URI points to a tarball and the `dockerfile` parameter is also specified, there must be a file with the corresponding path inside the tarball."
Expand Down
1 change: 1 addition & 0 deletions api/types/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ type ImageBuildOptions struct {
// specified here do not need to have a valid parent chain to match cache.
CacheFrom []string
SecurityOpt []string
ExtraHosts []string // List of extra hosts
}

// ImageBuildResponse holds information
Expand Down
3 changes: 2 additions & 1 deletion builder/dockerfile/internals.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ func (b *Builder) create() (string, error) {
Resources: resources,
NetworkMode: container.NetworkMode(b.options.NetworkMode),
// Set a log config to override any default value set on the daemon
LogConfig: defaultLogConfig,
LogConfig: defaultLogConfig,
ExtraHosts: b.options.ExtraHosts,
}

config := *b.runConfig
Expand Down
12 changes: 8 additions & 4 deletions cli/command/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type buildOptions struct {
tags opts.ListOpts
labels opts.ListOpts
buildArgs opts.ListOpts
extraHosts opts.ListOpts
ulimits *opts.UlimitOpt
memory string
memorySwap string
Expand Down Expand Up @@ -65,10 +66,11 @@ type buildOptions struct {
func NewBuildCommand(dockerCli *command.DockerCli) *cobra.Command {
ulimits := make(map[string]*units.Ulimit)
options := buildOptions{
tags: opts.NewListOpts(validateTag),
buildArgs: opts.NewListOpts(opts.ValidateEnv),
ulimits: opts.NewUlimitOpt(&ulimits),
labels: opts.NewListOpts(opts.ValidateEnv),
tags: opts.NewListOpts(validateTag),
buildArgs: opts.NewListOpts(opts.ValidateEnv),
ulimits: opts.NewUlimitOpt(&ulimits),
labels: opts.NewListOpts(opts.ValidateEnv),
extraHosts: opts.NewListOpts(opts.ValidateExtraHost),
}

cmd := &cobra.Command{
Expand Down Expand Up @@ -108,6 +110,7 @@ func NewBuildCommand(dockerCli *command.DockerCli) *cobra.Command {
flags.StringSliceVar(&options.securityOpt, "security-opt", []string{}, "Security options")
flags.StringVar(&options.networkMode, "network", "default", "Set the networking mode for the RUN instructions during build")
flags.SetAnnotation("network", "version", []string{"1.25"})
flags.Var(&options.extraHosts, "add-host", "Add a custom host-to-IP mapping (host:ip)")

command.AddTrustVerificationFlags(flags)

Expand Down Expand Up @@ -301,6 +304,7 @@ func runBuild(dockerCli *command.DockerCli, options buildOptions) error {
SecurityOpt: options.securityOpt,
NetworkMode: options.networkMode,
Squash: options.squash,
ExtraHosts: options.extraHosts.GetAll(),
}

response, err := dockerCli.Client().ImageBuild(ctx, body, buildOptions)
Expand Down
1 change: 1 addition & 0 deletions client/image_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (cli *Client) imageBuildOptionsToQuery(options types.ImageBuildOptions) (ur
query := url.Values{
"t": options.Tags,
"securityopt": options.SecurityOpt,
"extrahosts": options.ExtraHosts,
}
if options.SuppressOutput {
query.Set("q", "1")
Expand Down
1 change: 1 addition & 0 deletions docs/api/version-history.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ keywords: "API, Docker, rcli, REST, documentation"
* `GET /networks` is optimised only to return list of all networks and network specific information. List of all containers attached to a specific network is removed from this API and is only available using the network specific `GET /networks/{network-id}.
* `GET /containers/json` now supports `publish` and `expose` filters to filter containers that expose or publish certain ports.
* `POST /services/create` and `POST /services/(id or name)/update` now accept the `ReadOnly` parameter, which mounts the container's root filesystem as read only.
* `POST /build` now accepts `extrahosts` parameter to specify a host to ip mapping to use during the build.

## v1.26 API changes

Expand Down
9 changes: 9 additions & 0 deletions docs/reference/commandline/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile

Options:
--add-host value Add a custom host-to-IP mapping (host:ip) (default [])
--build-arg value Set build-time variables (default [])
--cache-from value Images to consider as cache sources (default [])
--cgroup-parent string Optional parent cgroup for the container
Expand Down Expand Up @@ -435,6 +436,13 @@ Linux namespaces. On Microsoft Windows, you can specify these values:

Specifying the `--isolation` flag without a value is the same as setting `--isolation="default"`.

### Add entries to container hosts file (--add-host)

You can add other hosts into a container's `/etc/hosts` file by using one or
more `--add-host` flags. This example adds a static address for a host named
`docker`:

$ docker build --add-host=docker:10.180.0.1 .

### Squash an image's layers (--squash) **Experimental Only**

Expand All @@ -451,3 +459,4 @@ space.
**Note**: using this option you may see significantly more space used due to
storing two copies of the image, one for the build cache with all the cache
layers in tact, and one for the squashed version.

43 changes: 43 additions & 0 deletions integration-cli/docker_cli_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5546,6 +5546,49 @@ func (s *DockerSuite) TestBuildNetContainer(c *check.C) {
c.Assert(strings.TrimSpace(host), check.Equals, "foobar")
}

func (s *DockerSuite) TestBuildWithExtraHost(c *check.C) {
testRequires(c, DaemonIsLinux)

name := "testbuildwithextrahost"
buildImageSuccessfully(c, name,
withBuildFlags(
"--add-host", "foo:127.0.0.1",
"--add-host", "bar:127.0.0.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we're in the design phase and this is premature, but if I don't ask now I'll probably forget....
can you add some negative tests? For example --add-host myhost: or --add-host myhost and ones with bad IPs. And ones with ipv6

Copy link
Contributor Author

@tabboud tabboud Feb 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes total sense. I was thinking about this when I wrote the initial test. I will add some more to this PR and push it again.

),
withDockerfile(`
FROM busybox
RUN ping -c 1 foo
RUN ping -c 1 bar
`))
}

func (s *DockerSuite) TestBuildWithExtraHostInvalidFormat(c *check.C) {
testRequires(c, DaemonIsLinux)
dockerfile := `
FROM busybox
RUN ping -c 1 foo`

testCases := []struct {
testName string
dockerfile string
buildFlag string
}{
{"extra_host_missing_ip", dockerfile, "--add-host=foo"},
{"extra_host_missing_ip_with_delimeter", dockerfile, "--add-host=foo:"},
{"extra_host_missing_hostname", dockerfile, "--add-host=:127.0.0.1"},
{"extra_host_invalid_ipv4", dockerfile, "--add-host=foo:101.10.2"},
{"extra_host_invalid_ipv6", dockerfile, "--add-host=foo:2001::1::3F"},
}

for _, tc := range testCases {
result := buildImage(tc.testName, withBuildFlags(tc.buildFlag), withDockerfile(tc.dockerfile))
result.Assert(c, icmd.Expected{
ExitCode: 125,
})
}

}

func (s *DockerSuite) TestBuildSquashParent(c *check.C) {
testRequires(c, ExperimentalDaemon)
dockerFile := `
Expand Down
7 changes: 7 additions & 0 deletions man/docker-build.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ docker-build - Build an image from a Dockerfile

# SYNOPSIS
**docker build**
[**--add-host**[=*[]*]]
[**--build-arg**[=*[]*]]
[**--cpu-shares**[=*0*]]
[**--cgroup-parent**[=*CGROUP-PARENT*]]
Expand Down Expand Up @@ -74,6 +75,12 @@ set as the **URL**, the repository is cloned locally and then sent as the contex
storing two copies of the image, one for the build cache with all the cache
layers in tact, and one for the squashed version.

**--add-host**=[]
Add a custom host-to-IP mapping (host:ip)

Add a line to /etc/hosts. The format is hostname:ip. The **--add-host**
option can be set multiple times.

**--build-arg**=*variable*
name and value of a **buildarg**.

Expand Down