From a24207b9b70069b750c52230d08c705e4f16ba74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?VERDO=C3=8FA=20Laurent?= Date: Tue, 5 Apr 2016 11:43:59 +0900 Subject: [PATCH] provider/docker: #5298 Add support for docker run --user option --- builtin/providers/docker/resource_docker_container.go | 7 +++++++ .../providers/docker/resource_docker_container_funcs.go | 4 ++++ builtin/providers/docker/resource_docker_container_test.go | 5 +++++ .../source/docs/providers/docker/r/container.html.markdown | 3 +++ 4 files changed, 19 insertions(+) diff --git a/builtin/providers/docker/resource_docker_container.go b/builtin/providers/docker/resource_docker_container.go index 74e75882c..3bcf92231 100644 --- a/builtin/providers/docker/resource_docker_container.go +++ b/builtin/providers/docker/resource_docker_container.go @@ -80,6 +80,13 @@ func resourceDockerContainer() *schema.Resource { Elem: &schema.Schema{Type: schema.TypeString}, }, + "user": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "dns": &schema.Schema{ Type: schema.TypeSet, Optional: true, diff --git a/builtin/providers/docker/resource_docker_container_funcs.go b/builtin/providers/docker/resource_docker_container_funcs.go index 0453b06a1..00090294c 100644 --- a/builtin/providers/docker/resource_docker_container_funcs.go +++ b/builtin/providers/docker/resource_docker_container_funcs.go @@ -57,6 +57,10 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err createOpts.Config.Entrypoint = stringListToStringSlice(v.([]interface{})) } + if v, ok := d.GetOk("user"); ok { + createOpts.Config.User = v.(string) + } + exposedPorts := map[dc.Port]struct{}{} portBindings := map[dc.Port][]dc.PortBinding{} diff --git a/builtin/providers/docker/resource_docker_container_test.go b/builtin/providers/docker/resource_docker_container_test.go index 8536c78ce..9e9158c44 100644 --- a/builtin/providers/docker/resource_docker_container_test.go +++ b/builtin/providers/docker/resource_docker_container_test.go @@ -78,6 +78,10 @@ func TestAccDockerContainer_customized(t *testing.T) { return fmt.Errorf("Container wrong entrypoint: %s", c.Config.Entrypoint) } + if c.Config.User != "root:root" { + return fmt.Errorf("Container wrong user: %s", c.Config.User) + } + if c.HostConfig.RestartPolicy.Name == "on-failure" { if c.HostConfig.RestartPolicy.MaximumRetryCount != 5 { return fmt.Errorf("Container has wrong restart policy max retry count: %d", c.HostConfig.RestartPolicy.MaximumRetryCount) @@ -217,6 +221,7 @@ resource "docker_container" "foo" { name = "tf-test" image = "${docker_image.foo.latest}" entrypoint = ["/bin/bash", "-c", "ping localhost"] + user = "root:root" restart = "on-failure" max_retry_count = 5 memory = 512 diff --git a/website/source/docs/providers/docker/r/container.html.markdown b/website/source/docs/providers/docker/r/container.html.markdown index 582e8b492..7441e88ac 100644 --- a/website/source/docs/providers/docker/r/container.html.markdown +++ b/website/source/docs/providers/docker/r/container.html.markdown @@ -42,6 +42,9 @@ The following arguments are supported: container to run as an executable. For example, to run `/usr/bin/myprogram` when starting a container, set the entrypoint to be `["/usr/bin/myprogram"]`. +* `user` - (Optional, string) User used for run the first process. Format is + `user` or `user:group` which user and group can be passed literraly or + by name. * `dns` - (Optional, set of strings) Set of DNS servers. * `env` - (Optional, set of strings) Environmental variables to set. * `labels` - (Optional, map of strings) Key/value pairs to set as labels on the