2015-11-12 14:55:17 -05:00
|
|
|
package container
|
2013-01-18 19:13:39 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2013-03-21 03:25:00 -04:00
|
|
|
"fmt"
|
2014-04-28 17:36:04 -04:00
|
|
|
"io"
|
|
|
|
"os"
|
2014-05-11 16:49:46 -04:00
|
|
|
"path/filepath"
|
2015-05-05 16:25:05 -04:00
|
|
|
"sync"
|
2014-04-28 17:36:04 -04:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2015-03-26 18:22:04 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-12-18 13:36:17 -05:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2015-11-20 17:35:16 -05:00
|
|
|
"github.com/docker/docker/daemon/exec"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-02-04 14:04:58 -05:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
|
|
|
"github.com/docker/docker/daemon/logger/jsonfilelog"
|
2015-04-04 00:06:48 -04:00
|
|
|
"github.com/docker/docker/daemon/network"
|
2015-09-17 14:54:14 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-11-18 17:20:54 -05:00
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
2014-09-30 02:16:27 -04:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2015-08-04 16:51:48 -04:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/symlink"
|
|
|
|
"github.com/docker/docker/runconfig"
|
2015-05-19 16:05:25 -04:00
|
|
|
"github.com/docker/docker/volume"
|
2015-12-18 12:58:48 -05:00
|
|
|
"github.com/docker/go-connections/nat"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
const configFileName = "config.v2.json"
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// CommonContainer holds the fields for a container which are
|
|
|
|
// applicable across all platforms supported by the daemon.
|
2015-04-29 18:53:35 -04:00
|
|
|
type CommonContainer struct {
|
2015-11-17 19:21:44 -05:00
|
|
|
*runconfig.StreamConfig
|
2015-07-30 17:01:53 -04:00
|
|
|
// embed for Container to support states directly.
|
|
|
|
*State `json:"State"` // Needed for remote api version <= 1.11
|
2015-11-12 14:55:17 -05:00
|
|
|
Root string `json:"-"` // Path to the "home" of the container, including metadata.
|
|
|
|
BaseFS string `json:"-"` // Path to the graphdriver mountpoint
|
|
|
|
RWLayer layer.RWLayer `json:"-"`
|
2015-07-30 17:01:53 -04:00
|
|
|
ID string
|
|
|
|
Created time.Time
|
|
|
|
Path string
|
|
|
|
Args []string
|
2015-12-18 13:36:17 -05:00
|
|
|
Config *containertypes.Config
|
2015-11-18 17:20:54 -05:00
|
|
|
ImageID image.ID `json:"Image"`
|
2015-07-30 17:01:53 -04:00
|
|
|
NetworkSettings *network.Settings
|
|
|
|
LogPath string
|
|
|
|
Name string
|
|
|
|
Driver string
|
|
|
|
// MountLabel contains the options for the 'mount' command
|
|
|
|
MountLabel string
|
|
|
|
ProcessLabel string
|
|
|
|
RestartCount int
|
|
|
|
HasBeenStartedBefore bool
|
|
|
|
HasBeenManuallyStopped bool // used for unless-stopped restart policy
|
2015-09-09 22:23:06 -04:00
|
|
|
MountPoints map[string]*volume.MountPoint
|
2015-12-18 13:36:17 -05:00
|
|
|
HostConfig *containertypes.HostConfig `json:"-"` // do not serialize the host config in the json, otherwise we'll make the container unportable
|
|
|
|
Command *execdriver.Command `json:"-"`
|
2015-07-30 17:01:53 -04:00
|
|
|
monitor *containerMonitor
|
2015-11-12 14:55:17 -05:00
|
|
|
ExecCommands *exec.Store `json:"-"`
|
2015-02-04 14:04:58 -05:00
|
|
|
// logDriver for closing
|
2015-11-12 14:55:17 -05:00
|
|
|
LogDriver logger.Logger `json:"-"`
|
|
|
|
LogCopier *logger.Copier `json:"-"`
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// NewBaseContainer creates a new container with its
|
2015-11-18 05:04:23 -05:00
|
|
|
// basic configuration.
|
2015-11-12 14:55:17 -05:00
|
|
|
func NewBaseContainer(id, root string) *Container {
|
2015-11-18 05:04:23 -05:00
|
|
|
return &Container{
|
|
|
|
CommonContainer: CommonContainer{
|
|
|
|
ID: id,
|
|
|
|
State: NewState(),
|
2015-11-12 14:55:17 -05:00
|
|
|
ExecCommands: exec.NewStore(),
|
|
|
|
Root: root,
|
2015-11-18 05:04:23 -05:00
|
|
|
MountPoints: make(map[string]*volume.MountPoint),
|
2015-11-17 19:21:44 -05:00
|
|
|
StreamConfig: runconfig.NewStreamConfig(),
|
2015-11-18 05:04:23 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// FromDisk loads the container configuration stored in the host.
|
|
|
|
func (container *Container) FromDisk() error {
|
|
|
|
pth, err := container.ConfigPath()
|
2014-05-27 22:15:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-04 08:43:58 -05:00
|
|
|
jsonSource, err := os.Open(pth)
|
2013-01-25 17:39:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-04 08:43:58 -05:00
|
|
|
defer jsonSource.Close()
|
|
|
|
|
|
|
|
dec := json.NewDecoder(jsonSource)
|
|
|
|
|
2013-03-21 03:25:00 -04:00
|
|
|
// Load container settings
|
2015-09-03 05:01:55 -04:00
|
|
|
if err := dec.Decode(container); err != nil {
|
2013-01-25 17:39:02 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-04-28 17:36:04 -04:00
|
|
|
|
|
|
|
if err := label.ReserveLabel(container.ProcessLabel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-31 17:58:43 -04:00
|
|
|
return container.readHostConfig()
|
2013-01-25 17:39:02 -05:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// ToDisk saves the container configuration on disk.
|
|
|
|
func (container *Container) ToDisk() error {
|
|
|
|
pth, err := container.ConfigPath()
|
2013-01-18 19:13:39 -05:00
|
|
|
if err != nil {
|
2014-05-27 22:15:42 -04:00
|
|
|
return err
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2015-10-30 12:04:25 -04:00
|
|
|
jsonSource, err := os.Create(pth)
|
2013-10-31 17:58:43 -04:00
|
|
|
if err != nil {
|
2014-05-27 22:15:42 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-10-30 12:04:25 -04:00
|
|
|
defer jsonSource.Close()
|
|
|
|
|
|
|
|
enc := json.NewEncoder(jsonSource)
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2015-10-30 12:04:25 -04:00
|
|
|
// Save container settings
|
|
|
|
if err := enc.Encode(container); err != nil {
|
2014-05-27 22:15:42 -04:00
|
|
|
return err
|
2013-10-31 17:58:43 -04:00
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
return container.WriteHostConfig()
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// ToDiskLocking saves the container configuration on disk in a thread safe way.
|
|
|
|
func (container *Container) ToDiskLocking() error {
|
2014-07-11 13:49:24 -04:00
|
|
|
container.Lock()
|
2015-11-12 14:55:17 -05:00
|
|
|
err := container.ToDisk()
|
2014-07-11 13:49:24 -04:00
|
|
|
container.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// readHostConfig reads the host configuration from disk for the container.
|
2013-10-31 17:58:43 -04:00
|
|
|
func (container *Container) readHostConfig() error {
|
2015-12-18 13:36:17 -05:00
|
|
|
container.HostConfig = &containertypes.HostConfig{}
|
2013-10-31 17:58:43 -04:00
|
|
|
// If the hostconfig file does not exist, do not read it.
|
2015-11-12 14:55:17 -05:00
|
|
|
// (We still have to initialize container.HostConfig,
|
2013-10-31 17:58:43 -04:00
|
|
|
// but that's OK, since we just did that above.)
|
2015-11-12 14:55:17 -05:00
|
|
|
pth, err := container.HostConfigPath()
|
2014-05-27 22:15:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-21 15:59:59 -04:00
|
|
|
f, err := os.Open(pth)
|
2013-07-02 08:19:25 -04:00
|
|
|
if err != nil {
|
2015-10-30 17:54:09 -04:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2013-10-31 17:58:43 -04:00
|
|
|
return err
|
2013-07-02 08:19:25 -04:00
|
|
|
}
|
2015-04-21 15:59:59 -04:00
|
|
|
defer f.Close()
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
if err := json.NewDecoder(f).Decode(&container.HostConfig); err != nil {
|
2015-11-06 17:22:48 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
container.InitDNSHostConfig()
|
2015-11-06 17:22:48 -05:00
|
|
|
|
|
|
|
return nil
|
2013-07-02 08:19:25 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// WriteHostConfig saves the host configuration on disk for the container.
|
|
|
|
func (container *Container) WriteHostConfig() error {
|
|
|
|
pth, err := container.HostConfigPath()
|
2013-07-02 08:19:25 -04:00
|
|
|
if err != nil {
|
2014-05-27 22:15:42 -04:00
|
|
|
return err
|
2013-07-02 08:19:25 -04:00
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2015-10-30 17:45:35 -04:00
|
|
|
f, err := os.Create(pth)
|
2014-05-27 22:15:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-30 17:45:35 -04:00
|
|
|
defer f.Close()
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
return json.NewEncoder(f).Encode(&container.HostConfig)
|
2013-07-02 08:19:25 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// GetResourcePath evaluates `path` in the scope of the container's BaseFS, with proper path
|
|
|
|
// sanitisation. Symlinks are all scoped to the BaseFS of the container, as
|
|
|
|
// though the container's BaseFS was `/`.
|
2015-03-05 20:53:06 -05:00
|
|
|
//
|
2015-11-12 14:55:17 -05:00
|
|
|
// The BaseFS of a container is the host-facing path which is bind-mounted as
|
2015-03-05 20:53:06 -05:00
|
|
|
// `/` inside the container. This method is essentially used to access a
|
|
|
|
// particular path inside the container as though you were a process in that
|
|
|
|
// container.
|
|
|
|
//
|
2015-11-12 14:55:17 -05:00
|
|
|
// NOTE: The returned path is *only* safely scoped inside the container's BaseFS
|
2015-03-05 20:53:06 -05:00
|
|
|
// if no component of the returned path changes (such as a component
|
|
|
|
// symlinking to a different path) between using this method and using the
|
|
|
|
// path. See symlink.FollowSymlinkInScope for more details.
|
|
|
|
func (container *Container) GetResourcePath(path string) (string, error) {
|
2015-06-01 19:42:27 -04:00
|
|
|
// IMPORTANT - These are paths on the OS where the daemon is running, hence
|
|
|
|
// any filepath operations must be done in an OS agnostic way.
|
|
|
|
cleanPath := filepath.Join(string(os.PathSeparator), path)
|
2015-11-12 14:55:17 -05:00
|
|
|
r, e := symlink.FollowSymlinkInScope(filepath.Join(container.BaseFS, cleanPath), container.BaseFS)
|
2015-06-01 19:42:27 -04:00
|
|
|
return r, e
|
2014-05-11 16:49:46 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// GetRootResourcePath evaluates `path` in the scope of the container's root, with proper path
|
2015-03-05 20:53:06 -05:00
|
|
|
// sanitisation. Symlinks are all scoped to the root of the container, as
|
|
|
|
// though the container's root was `/`.
|
|
|
|
//
|
|
|
|
// The root of a container is the host-facing configuration metadata directory.
|
|
|
|
// Only use this method to safely access the container's `container.json` or
|
|
|
|
// other metadata files. If in doubt, use container.GetResourcePath.
|
|
|
|
//
|
|
|
|
// NOTE: The returned path is *only* safely scoped inside the container's root
|
|
|
|
// if no component of the returned path changes (such as a component
|
|
|
|
// symlinking to a different path) between using this method and using the
|
|
|
|
// path. See symlink.FollowSymlinkInScope for more details.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (container *Container) GetRootResourcePath(path string) (string, error) {
|
2015-06-01 19:42:27 -04:00
|
|
|
// IMPORTANT - These are paths on the OS where the daemon is running, hence
|
|
|
|
// any filepath operations must be done in an OS agnostic way.
|
|
|
|
cleanPath := filepath.Join(string(os.PathSeparator), path)
|
2015-11-12 14:55:17 -05:00
|
|
|
return symlink.FollowSymlinkInScope(filepath.Join(container.Root, cleanPath), container.Root)
|
2014-05-11 16:49:46 -04:00
|
|
|
}
|
|
|
|
|
2015-11-02 18:25:26 -05:00
|
|
|
// ExitOnNext signals to the monitor that it should not restart the container
|
|
|
|
// after we send the kill signal.
|
|
|
|
func (container *Container) ExitOnNext() {
|
2014-08-04 20:05:56 -04:00
|
|
|
container.monitor.ExitOnNext()
|
2015-01-19 07:44:04 -05:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// Resize changes the TTY of the process running inside the container
|
|
|
|
// to the given height and width. The container must be running.
|
2015-09-29 13:51:40 -04:00
|
|
|
func (container *Container) Resize(h, w int) error {
|
2015-11-12 14:55:17 -05:00
|
|
|
if err := container.Command.ProcessConfig.Terminal.Resize(h, w); err != nil {
|
2015-05-24 14:19:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2013-05-23 22:33:28 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// HostConfigPath returns the path to the container's JSON hostconfig
|
|
|
|
func (container *Container) HostConfigPath() (string, error) {
|
|
|
|
return container.GetRootResourcePath("hostconfig.json")
|
2013-03-21 03:25:00 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// ConfigPath returns the path to the container's JSON config
|
|
|
|
func (container *Container) ConfigPath() (string, error) {
|
|
|
|
return container.GetRootResourcePath(configFileName)
|
2013-03-21 03:25:00 -04:00
|
|
|
}
|
|
|
|
|
2013-06-04 14:00:22 -04:00
|
|
|
func validateID(id string) error {
|
2013-03-21 03:25:00 -04:00
|
|
|
if id == "" {
|
2015-09-16 14:56:26 -04:00
|
|
|
return derr.ErrorCodeEmptyID
|
2013-03-21 03:25:00 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-05-13 09:10:26 -04:00
|
|
|
|
2013-10-04 22:25:15 -04:00
|
|
|
// Returns true if the container exposes a certain port
|
2015-07-30 17:01:53 -04:00
|
|
|
func (container *Container) exposes(p nat.Port) bool {
|
2013-10-04 22:25:15 -04:00
|
|
|
_, exists := container.Config.ExposedPorts[p]
|
|
|
|
return exists
|
|
|
|
}
|
2013-11-29 10:40:44 -05:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// GetLogConfig returns the log configuration for the container.
|
2015-12-18 13:36:17 -05:00
|
|
|
func (container *Container) GetLogConfig(defaultConfig containertypes.LogConfig) containertypes.LogConfig {
|
2015-11-12 14:55:17 -05:00
|
|
|
cfg := container.HostConfig.LogConfig
|
2015-06-30 20:40:13 -04:00
|
|
|
if cfg.Type != "" || len(cfg.Config) > 0 { // container has log driver configured
|
|
|
|
if cfg.Type == "" {
|
|
|
|
cfg.Type = jsonfilelog.Name
|
|
|
|
}
|
2015-04-09 00:23:30 -04:00
|
|
|
return cfg
|
2014-04-14 02:40:49 -04:00
|
|
|
}
|
2015-04-09 00:23:30 -04:00
|
|
|
// Use daemon's default log config for containers
|
2015-11-03 13:45:12 -05:00
|
|
|
return defaultConfig
|
2015-04-09 00:23:30 -04:00
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2015-11-03 13:45:12 -05:00
|
|
|
// StartLogger starts a new logger driver for the container.
|
2015-12-18 13:36:17 -05:00
|
|
|
func (container *Container) StartLogger(cfg containertypes.LogConfig) (logger.Logger, error) {
|
2015-04-09 00:23:30 -04:00
|
|
|
c, err := logger.GetLogDriver(cfg.Type)
|
|
|
|
if err != nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodeLoggingFactory.WithArgs(err)
|
2015-04-09 00:23:30 -04:00
|
|
|
}
|
|
|
|
ctx := logger.Context{
|
2015-05-29 17:00:46 -04:00
|
|
|
Config: cfg.Config,
|
|
|
|
ContainerID: container.ID,
|
|
|
|
ContainerName: container.Name,
|
|
|
|
ContainerEntrypoint: container.Path,
|
|
|
|
ContainerArgs: container.Args,
|
2015-11-18 17:20:54 -05:00
|
|
|
ContainerImageID: container.ImageID.String(),
|
2015-05-29 17:00:46 -04:00
|
|
|
ContainerImageName: container.Config.Image,
|
|
|
|
ContainerCreated: container.Created,
|
2015-08-11 07:27:19 -04:00
|
|
|
ContainerEnv: container.Config.Env,
|
|
|
|
ContainerLabels: container.Config.Labels,
|
2015-04-09 00:23:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set logging file for "json-logger"
|
|
|
|
if cfg.Type == jsonfilelog.Name {
|
2015-11-12 14:55:17 -05:00
|
|
|
ctx.LogPath, err = container.GetRootResourcePath(fmt.Sprintf("%s-json.log", container.ID))
|
2015-04-20 14:26:39 -04:00
|
|
|
if err != nil {
|
2015-04-09 00:23:30 -04:00
|
|
|
return nil, err
|
2015-04-20 14:26:39 -04:00
|
|
|
}
|
2015-04-09 00:23:30 -04:00
|
|
|
}
|
|
|
|
return c(ctx)
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// GetProcessLabel returns the process label for the container.
|
|
|
|
func (container *Container) GetProcessLabel() string {
|
2014-04-29 04:08:19 -04:00
|
|
|
// even if we have a process label return "" if we are running
|
|
|
|
// in privileged mode
|
2015-11-12 14:55:17 -05:00
|
|
|
if container.HostConfig.Privileged {
|
2014-04-29 04:08:19 -04:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return container.ProcessLabel
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// GetMountLabel returns the mounting label for the container.
|
|
|
|
// This label is empty if the container is privileged.
|
|
|
|
func (container *Container) GetMountLabel() string {
|
|
|
|
if container.HostConfig.Privileged {
|
2014-04-29 04:08:19 -04:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return container.MountLabel
|
|
|
|
}
|
2014-05-02 19:59:28 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// GetExecIDs returns the list of exec commands running on the container.
|
|
|
|
func (container *Container) GetExecIDs() []string {
|
|
|
|
return container.ExecCommands.List()
|
2015-05-04 08:39:51 -04:00
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// Attach connects to the container's TTY, delegating to standard
|
|
|
|
// streams or websockets depending on the configuration.
|
2016-01-03 17:03:39 -05:00
|
|
|
func (container *Container) Attach(stdin io.ReadCloser, stdout io.Writer, stderr io.Writer, keys []byte) chan error {
|
|
|
|
return AttachStreams(container.StreamConfig, container.Config.OpenStdin, container.Config.StdinOnce, container.Config.Tty, stdin, stdout, stderr, keys)
|
2015-05-05 16:25:05 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// AttachStreams connects streams to a TTY.
|
|
|
|
// Used by exec too. Should this move somewhere else?
|
2016-01-03 17:03:39 -05:00
|
|
|
func AttachStreams(streamConfig *runconfig.StreamConfig, openStdin, stdinOnce, tty bool, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer, keys []byte) chan error {
|
2015-05-05 16:25:05 -04:00
|
|
|
var (
|
|
|
|
cStdout, cStderr io.ReadCloser
|
|
|
|
cStdin io.WriteCloser
|
|
|
|
wg sync.WaitGroup
|
|
|
|
errors = make(chan error, 3)
|
|
|
|
)
|
|
|
|
|
|
|
|
if stdin != nil && openStdin {
|
|
|
|
cStdin = streamConfig.StdinPipe()
|
|
|
|
wg.Add(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if stdout != nil {
|
|
|
|
cStdout = streamConfig.StdoutPipe()
|
|
|
|
wg.Add(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if stderr != nil {
|
|
|
|
cStderr = streamConfig.StderrPipe()
|
|
|
|
wg.Add(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect stdin of container to the http conn.
|
|
|
|
go func() {
|
|
|
|
if stdin == nil || !openStdin {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logrus.Debugf("attach: stdin: begin")
|
|
|
|
defer func() {
|
|
|
|
if stdinOnce && !tty {
|
|
|
|
cStdin.Close()
|
|
|
|
} else {
|
|
|
|
// No matter what, when stdin is closed (io.Copy unblock), close stdout and stderr
|
|
|
|
if cStdout != nil {
|
|
|
|
cStdout.Close()
|
|
|
|
}
|
|
|
|
if cStderr != nil {
|
|
|
|
cStderr.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
logrus.Debugf("attach: stdin: end")
|
|
|
|
}()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if tty {
|
2016-01-03 17:03:39 -05:00
|
|
|
_, err = copyEscapable(cStdin, stdin, keys)
|
2015-05-05 16:25:05 -04:00
|
|
|
} else {
|
|
|
|
_, err = io.Copy(cStdin, stdin)
|
|
|
|
|
|
|
|
}
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("attach: stdin: %s", err)
|
|
|
|
errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
attachStream := func(name string, stream io.Writer, streamPipe io.ReadCloser) {
|
|
|
|
if stream == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
// Make sure stdin gets closed
|
|
|
|
if stdin != nil {
|
|
|
|
stdin.Close()
|
|
|
|
}
|
|
|
|
streamPipe.Close()
|
|
|
|
wg.Done()
|
|
|
|
logrus.Debugf("attach: %s: end", name)
|
|
|
|
}()
|
|
|
|
|
|
|
|
logrus.Debugf("attach: %s: begin", name)
|
|
|
|
_, err := io.Copy(stream, streamPipe)
|
|
|
|
if err == io.ErrClosedPipe {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("attach: %s: %v", name, err)
|
|
|
|
errors <- err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go attachStream("stdout", stdout, cStdout)
|
|
|
|
go attachStream("stderr", stderr, cStderr)
|
|
|
|
|
|
|
|
return promise.Go(func() error {
|
|
|
|
wg.Wait()
|
|
|
|
close(errors)
|
|
|
|
for err := range errors {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Code c/c from io.Copy() modified to handle escape sequence
|
2016-01-03 17:03:39 -05:00
|
|
|
func copyEscapable(dst io.Writer, src io.ReadCloser, keys []byte) (written int64, err error) {
|
|
|
|
if len(keys) == 0 {
|
|
|
|
// Default keys : ctrl-p ctrl-q
|
|
|
|
keys = []byte{16, 17}
|
|
|
|
}
|
2015-05-05 16:25:05 -04:00
|
|
|
buf := make([]byte, 32*1024)
|
|
|
|
for {
|
|
|
|
nr, er := src.Read(buf)
|
|
|
|
if nr > 0 {
|
|
|
|
// ---- Docker addition
|
2016-01-03 17:03:39 -05:00
|
|
|
for i, key := range keys {
|
|
|
|
if nr != 1 || buf[0] != key {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if i == len(keys)-1 {
|
2015-05-05 16:25:05 -04:00
|
|
|
if err := src.Close(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
2016-01-03 17:03:39 -05:00
|
|
|
nr, er = src.Read(buf)
|
2015-05-05 16:25:05 -04:00
|
|
|
}
|
|
|
|
// ---- End of docker
|
|
|
|
nw, ew := dst.Write(buf[0:nr])
|
|
|
|
if nw > 0 {
|
|
|
|
written += int64(nw)
|
|
|
|
}
|
|
|
|
if ew != nil {
|
|
|
|
err = ew
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if nr != nw {
|
|
|
|
err = io.ErrShortWrite
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if er == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if er != nil {
|
|
|
|
err = er
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return written, err
|
|
|
|
}
|
2015-05-19 16:05:25 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// ShouldRestart decides whether the daemon should restart the container or not.
|
|
|
|
// This is based on the container's restart policy.
|
|
|
|
func (container *Container) ShouldRestart() bool {
|
|
|
|
return container.HostConfig.RestartPolicy.Name == "always" ||
|
|
|
|
(container.HostConfig.RestartPolicy.Name == "unless-stopped" && !container.HasBeenManuallyStopped) ||
|
|
|
|
(container.HostConfig.RestartPolicy.Name == "on-failure" && container.ExitCode != 0)
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
2015-05-22 13:37:00 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// AddBindMountPoint adds a new bind mount point configuration to the container.
|
|
|
|
func (container *Container) AddBindMountPoint(name, source, destination string, rw bool) {
|
2015-09-09 22:23:06 -04:00
|
|
|
container.MountPoints[destination] = &volume.MountPoint{
|
|
|
|
Name: name,
|
|
|
|
Source: source,
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// AddLocalMountPoint adds a new local mount point configuration to the container.
|
|
|
|
func (container *Container) AddLocalMountPoint(name, destination string, rw bool) {
|
2015-09-09 22:23:06 -04:00
|
|
|
container.MountPoints[destination] = &volume.MountPoint{
|
|
|
|
Name: name,
|
|
|
|
Driver: volume.DefaultDriverName,
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// AddMountPointWithVolume adds a new mount point configured with a volume to the container.
|
|
|
|
func (container *Container) AddMountPointWithVolume(destination string, vol volume.Volume, rw bool) {
|
2015-09-09 22:23:06 -04:00
|
|
|
container.MountPoints[destination] = &volume.MountPoint{
|
|
|
|
Name: vol.Name(),
|
|
|
|
Driver: vol.DriverName(),
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
Volume: vol,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// IsDestinationMounted checks whether a path is mounted on the container or not.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (container *Container) IsDestinationMounted(destination string) bool {
|
2015-09-09 22:23:06 -04:00
|
|
|
return container.MountPoints[destination] != nil
|
2015-05-22 13:37:00 -04:00
|
|
|
}
|
2015-08-04 16:51:48 -04:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// StopSignal returns the signal used to stop the container.
|
|
|
|
func (container *Container) StopSignal() int {
|
2015-08-04 16:51:48 -04:00
|
|
|
var stopSignal syscall.Signal
|
|
|
|
if container.Config.StopSignal != "" {
|
|
|
|
stopSignal, _ = signal.ParseSignal(container.Config.StopSignal)
|
|
|
|
}
|
|
|
|
|
|
|
|
if int(stopSignal) == 0 {
|
|
|
|
stopSignal, _ = signal.ParseSignal(signal.DefaultStopSignal)
|
|
|
|
}
|
|
|
|
return int(stopSignal)
|
|
|
|
}
|
2015-11-30 17:44:34 -05:00
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
// InitDNSHostConfig ensures that the dns fields are never nil.
|
2015-11-30 17:44:34 -05:00
|
|
|
// New containers don't ever have those fields nil,
|
|
|
|
// but pre created containers can still have those nil values.
|
|
|
|
// The non-recommended host configuration in the start api can
|
|
|
|
// make these fields nil again, this corrects that issue until
|
|
|
|
// we remove that behavior for good.
|
|
|
|
// See https://github.com/docker/docker/pull/17779
|
|
|
|
// for a more detailed explanation on why we don't want that.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (container *Container) InitDNSHostConfig() {
|
2015-12-10 21:33:13 -05:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
2015-11-12 14:55:17 -05:00
|
|
|
if container.HostConfig.DNS == nil {
|
|
|
|
container.HostConfig.DNS = make([]string, 0)
|
2015-11-30 17:44:34 -05:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
if container.HostConfig.DNSSearch == nil {
|
|
|
|
container.HostConfig.DNSSearch = make([]string, 0)
|
2015-11-30 17:44:34 -05:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
if container.HostConfig.DNSOptions == nil {
|
|
|
|
container.HostConfig.DNSOptions = make([]string, 0)
|
2015-11-30 17:44:34 -05:00
|
|
|
}
|
|
|
|
}
|