From 7c077d2dae59af9207cc6703a98d428e5b49449d Mon Sep 17 00:00:00 2001 From: Jan-Hendrik Lendholt Date: Tue, 20 Sep 2016 11:22:27 +0200 Subject: [PATCH] Added support to mount host folders into container. For example if you don't want to bake certificates into the container, you can mount them into the directory directly. Furthermore, I added support for volumes-from. Currently, there is no support to move the data from one container to another, hence: If a container spawns on another host, it is very likely, that the data will not be found. --- client/driver/docker.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/client/driver/docker.go b/client/driver/docker.go index bcf6ba521..3e73be12a 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -104,6 +104,8 @@ type DockerDriverConfig struct { ShmSize int64 `mapstructure:"shm_size"` // Size of /dev/shm of the container in bytes WorkDir string `mapstructure:"work_dir"` // Working directory inside the container Logging []DockerLoggingOpts `mapstructure:"logging"` // Logging options for syslog server + Volumes []string `mapstructure:"volumes"` // Host-Volumes to mount in, syntax: /path/to/host/directory:/destination/path/in/container + VolumesFrom []string `mapstructure:"volumes_from"` // List of volumes-from } // Validate validates a docker driver config @@ -127,6 +129,14 @@ func (c *DockerDriverConfig) Validate() error { } } + if c.Volumes == nil { + c.Volumes = make([]string, 0) + } + + if c.VolumesFrom == nil { + c.VolumesFrom = make([]string, 0) + } + return nil } @@ -249,6 +259,12 @@ func (d *DockerDriver) Validate(config map[string]interface{}) error { "logging": &fields.FieldSchema{ Type: fields.TypeArray, }, + "volumes": &fields.FieldSchema{ + Type: fields.TypeArray, + }, + "volumes_from": &fields.FieldSchema{ + Type: fields.TypeArray, + }, }, } @@ -439,6 +455,10 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, d.logger.Printf("[DEBUG] driver.docker: Using config for logging: %+v", driverConfig.Logging[0]) + //Merge nomad container binds and user specified binds + d.logger.Printf("[DEBUG] Unmodified binds from nomad: %+v\n", binds) + binds = append(binds, driverConfig.Volumes...) + hostConfig := &docker.HostConfig{ // Convert MB to bytes. This is an absolute value. Memory: memLimit, @@ -449,7 +469,8 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, // Binds are used to mount a host volume into the container. We mount a // local directory for storage and a shared alloc directory that can be // used to share data between different tasks in the same task group. - Binds: binds, + Binds: binds, + VolumesFrom: driverConfig.VolumesFrom, LogConfig: docker.LogConfig{ Type: driverConfig.Logging[0].Type, Config: driverConfig.Logging[0].Config, @@ -459,6 +480,7 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, d.logger.Printf("[DEBUG] driver.docker: using %d bytes memory for %s", hostConfig.Memory, task.Name) d.logger.Printf("[DEBUG] driver.docker: using %d cpu shares for %s", hostConfig.CPUShares, task.Name) d.logger.Printf("[DEBUG] driver.docker: binding directories %#v for %s", hostConfig.Binds, task.Name) + d.logger.Printf("[DEBUG] driver.docker: binding Volumes-From: %#v for %s", hostConfig.VolumesFrom, task.Name) // set privileged mode hostPrivileged := d.config.ReadBoolDefault("docker.privileged.enabled", false)