diff --git a/builtin/providers/docker/resource_docker_container.go b/builtin/providers/docker/resource_docker_container.go index 0a29ab73d..92331fc79 100644 --- a/builtin/providers/docker/resource_docker_container.go +++ b/builtin/providers/docker/resource_docker_container.go @@ -195,6 +195,27 @@ func resourceDockerContainer() *schema.Resource { Optional: true, ForceNew: true, }, + + "log_driver": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Default: "json-file", + ValidateFunc: func(v interface{}, k string) (ws []string, es []error) { + value := v.(string) + if !regexp.MustCompile(`^(json-file|syslog|journald|gelf|fluentd)$`).MatchString(value) { + es = append(es, fmt.Errorf( + "%q must be one of \"json-file\", \"syslog\", \"journald\", \"gelf\", or \"fluentd\"", k)) + } + return + }, + }, + + "log_opts": &schema.Schema{ + Type: schema.TypeMap, + Optional: true, + ForceNew: true, + }, }, } } diff --git a/builtin/providers/docker/resource_docker_container_funcs.go b/builtin/providers/docker/resource_docker_container_funcs.go index 4a617480e..443f9ef3f 100644 --- a/builtin/providers/docker/resource_docker_container_funcs.go +++ b/builtin/providers/docker/resource_docker_container_funcs.go @@ -83,7 +83,7 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err } if v, ok := d.GetOk("labels"); ok { - createOpts.Config.Labels = mapLabels(v.(map[string]interface{})) + createOpts.Config.Labels = mapTypeMapValsToString(v.(map[string]interface{})) } var retContainer *dc.Container @@ -103,6 +103,9 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err Name: d.Get("restart").(string), MaximumRetryCount: d.Get("max_retry_count").(int), }, + LogConfig: dc.LogConfig{ + Type: d.Get("log_driver").(string), + }, } if len(portBindings) != 0 { @@ -148,6 +151,10 @@ func resourceDockerContainerCreate(d *schema.ResourceData, meta interface{}) err } } + if v, ok := d.GetOk("log_opts"); ok { + hostConfig.LogConfig.Config = mapTypeMapValsToString(v.(map[string]interface{})) + } + creationTime = time.Now() if err := client.StartContainer(retContainer.ID, hostConfig); err != nil { return fmt.Errorf("Unable to start container: %s", err) @@ -259,9 +266,9 @@ func stringSetToStringSlice(stringSet *schema.Set) []string { return ret } -func mapLabels(labels map[string]interface{}) map[string]string { - mapped := make(map[string]string, len(labels)) - for k, v := range labels { +func mapTypeMapValsToString(typeMap map[string]interface{}) map[string]string { + mapped := make(map[string]string, len(typeMap)) + for k, v := range typeMap { mapped[k] = v.(string) } return mapped diff --git a/builtin/providers/docker/resource_docker_container_test.go b/builtin/providers/docker/resource_docker_container_test.go index e194d1a10..4b3dfce9a 100644 --- a/builtin/providers/docker/resource_docker_container_test.go +++ b/builtin/providers/docker/resource_docker_container_test.go @@ -60,6 +60,18 @@ func TestAccDockerContainer_customized(t *testing.T) { return fmt.Errorf("Container does not have the correct labels") } + if c.HostConfig.LogConfig.Type != "json-file" { + return fmt.Errorf("Container does not have the correct log config: %s", c.HostConfig.LogConfig.Type) + } + + if c.HostConfig.LogConfig.Config["max-size"] != "10m" { + return fmt.Errorf("Container does not have the correct max-size log option: %v", c.HostConfig.LogConfig.Config["max-size"]) + } + + if c.HostConfig.LogConfig.Config["max-file"] != "20" { + return fmt.Errorf("Container does not have the correct max-file log option: %v", c.HostConfig.LogConfig.Config["max-file"]) + } + return nil } @@ -138,5 +150,10 @@ resource "docker_container" "foo" { env = "prod" role = "test" } + log_driver = "json-file" + log_opts = { + max-size = "10m" + max-file = 20 + } } ` diff --git a/website/source/docs/providers/docker/r/container.html.markdown b/website/source/docs/providers/docker/r/container.html.markdown index b83387aef..920288eb2 100644 --- a/website/source/docs/providers/docker/r/container.html.markdown +++ b/website/source/docs/providers/docker/r/container.html.markdown @@ -64,6 +64,10 @@ The following arguments are supported: * `memory_swap` - (Optional, int) The total memory limit (memory + swap) for the container in MBs. * `cpu_shares` - (Optional, int) CPU shares (relative weight) for the container. +* `log_driver` - (Optional, string) The logging driver to use for the container. + Defaults to "json-file". +* `log_opts` - (Optional) Key/value pairs to use as options for the logging + driver. ## Ports