2014-02-11 23:04:39 -05:00
|
|
|
package runconfig
|
|
|
|
|
|
|
|
import (
|
2015-04-10 20:05:21 -04:00
|
|
|
"encoding/json"
|
2015-09-09 22:23:06 -04:00
|
|
|
"fmt"
|
2015-04-10 20:05:21 -04:00
|
|
|
"io"
|
|
|
|
|
2015-06-30 12:41:01 -04:00
|
|
|
"github.com/docker/docker/pkg/nat"
|
2015-08-28 11:29:10 -04:00
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2015-09-09 22:23:06 -04:00
|
|
|
"github.com/docker/docker/volume"
|
2014-02-11 23:04:39 -05:00
|
|
|
)
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
// Config contains the configuration data about a container.
|
|
|
|
// It should hold only portable information about the container.
|
2014-02-11 23:04:39 -05:00
|
|
|
// Here, "portable" means "independent from the host we are running on".
|
|
|
|
// Non-portable information *should* appear in HostConfig.
|
2015-08-26 17:58:56 -04:00
|
|
|
// All fields added to this struct must be marked `omitempty` to keep getting
|
|
|
|
// predictable hashes from the old `v1Compatibility` configuration.
|
2014-02-11 23:04:39 -05:00
|
|
|
type Config struct {
|
2015-11-09 15:28:14 -05:00
|
|
|
Hostname string // Hostname
|
|
|
|
Domainname string // Domainname
|
|
|
|
User string // User that will run the command(s) inside the container
|
2015-07-25 05:11:45 -04:00
|
|
|
AttachStdin bool // Attach the standard input, makes possible user interaction
|
|
|
|
AttachStdout bool // Attach the standard output
|
|
|
|
AttachStderr bool // Attach the standard error
|
2015-08-26 17:58:56 -04:00
|
|
|
ExposedPorts map[nat.Port]struct{} `json:",omitempty"` // List of exposed ports
|
2015-11-09 15:28:14 -05:00
|
|
|
PublishService string `json:",omitempty"` // Name of the network service exposed by the container
|
|
|
|
Tty bool // Attach standard streams to a tty, including stdin if it is not closed.
|
2015-07-25 05:11:45 -04:00
|
|
|
OpenStdin bool // Open stdin
|
|
|
|
StdinOnce bool // If true, close stdin after the 1 attached client disconnects.
|
2015-11-09 15:28:14 -05:00
|
|
|
Env []string // List of environment variable to set in the container
|
|
|
|
Cmd *stringutils.StrSlice // Command to run when starting the container
|
2015-11-09 14:49:16 -05:00
|
|
|
ArgsEscaped bool `json:",omitempty"` // True if command is already escaped (Windows specific)
|
2015-11-09 15:28:14 -05:00
|
|
|
Image string // Name of the image as it was passed by the operator (eg. could be symbolic)
|
2015-07-25 05:11:45 -04:00
|
|
|
Volumes map[string]struct{} // List of volumes (mounts) used for the container
|
|
|
|
WorkingDir string // Current directory (PWD) in the command will be launched
|
2015-11-09 15:28:14 -05:00
|
|
|
Entrypoint *stringutils.StrSlice // Entrypoint to run when starting the container
|
|
|
|
NetworkDisabled bool `json:",omitempty"` // Is network disabled
|
|
|
|
MacAddress string `json:",omitempty"` // Mac Address of the container
|
|
|
|
OnBuild []string // ONBUILD metadata that were defined on the image Dockerfile
|
|
|
|
Labels map[string]string // List of labels set to this container
|
|
|
|
StopSignal string `json:",omitempty"` // Signal to stop a container
|
2015-07-25 05:11:45 -04:00
|
|
|
}
|
|
|
|
|
2015-06-06 12:41:42 -04:00
|
|
|
// DecodeContainerConfig decodes a json encoded config into a ContainerConfigWrapper
|
|
|
|
// struct and returns both a Config and an HostConfig struct
|
|
|
|
// Be aware this function is not checking whether the resulted structs are nil,
|
|
|
|
// it's your business to do so
|
2015-04-10 20:05:21 -04:00
|
|
|
func DecodeContainerConfig(src io.Reader) (*Config, *HostConfig, error) {
|
|
|
|
var w ContainerConfigWrapper
|
2015-09-09 22:23:06 -04:00
|
|
|
|
|
|
|
decoder := json.NewDecoder(src)
|
2015-04-10 20:05:21 -04:00
|
|
|
if err := decoder.Decode(&w); err != nil {
|
|
|
|
return nil, nil, err
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
2015-04-10 20:05:21 -04:00
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
hc := w.getHostConfig()
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
// Perform platform-specific processing of Volumes and Binds.
|
|
|
|
if w.Config != nil && hc != nil {
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// Initialize the volumes map if currently nil
|
2015-09-09 22:23:06 -04:00
|
|
|
if w.Config.Volumes == nil {
|
|
|
|
w.Config.Volumes = make(map[string]struct{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now validate all the volumes and binds
|
|
|
|
if err := validateVolumesAndBindSettings(w.Config, hc); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-09 18:12:36 -04:00
|
|
|
// Certain parameters need daemon-side validation that cannot be done
|
|
|
|
// on the client, as only the daemon knows what is valid for the platform.
|
|
|
|
if err := ValidateNetMode(w.Config, hc); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-18 21:21:57 -04:00
|
|
|
// Validate the isolation level
|
|
|
|
if err := ValidateIsolationLevel(hc); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-07-09 18:12:36 -04:00
|
|
|
return w.Config, hc, nil
|
2014-02-11 23:04:39 -05:00
|
|
|
}
|
2015-09-09 22:23:06 -04:00
|
|
|
|
|
|
|
// validateVolumesAndBindSettings validates each of the volumes and bind settings
|
|
|
|
// passed by the caller to ensure they are valid.
|
|
|
|
func validateVolumesAndBindSettings(c *Config, hc *HostConfig) error {
|
|
|
|
|
|
|
|
// Ensure all volumes and binds are valid.
|
|
|
|
for spec := range c.Volumes {
|
|
|
|
if _, err := volume.ParseMountSpec(spec, hc.VolumeDriver); err != nil {
|
|
|
|
return fmt.Errorf("Invalid volume spec %q: %v", spec, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, spec := range hc.Binds {
|
|
|
|
if _, err := volume.ParseMountSpec(spec, hc.VolumeDriver); err != nil {
|
|
|
|
return fmt.Errorf("Invalid bind mount spec %q: %v", spec, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|