support for log driver + config in docker container

This commit is contained in:
ryane 2015-11-04 12:42:55 -05:00
parent 4531866d8d
commit 72c86a62c0
4 changed files with 53 additions and 4 deletions

View File

@ -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,
},
},
}
}

View File

@ -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

View File

@ -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
}
}
`

View File

@ -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.
<a id="ports"></a>
## Ports