2014-04-17 21:43:01 +00:00
|
|
|
package daemon
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2013-08-15 18:38:17 +00:00
|
|
|
"errors"
|
2013-03-21 07:25:00 +00:00
|
|
|
"fmt"
|
2014-04-28 21:36:04 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2014-05-11 20:49:46 +00:00
|
|
|
"path/filepath"
|
2015-09-10 02:23:06 +00:00
|
|
|
"strings"
|
2015-05-05 20:25:05 +00:00
|
|
|
"sync"
|
2014-04-28 21:36:04 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2015-07-16 23:00:55 +00:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2014-07-24 20:37:44 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/daemon/execdriver"
|
2015-02-04 19:04:58 +00:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
|
|
|
"github.com/docker/docker/daemon/logger/jsonfilelog"
|
2015-04-04 04:06:48 +00:00
|
|
|
"github.com/docker/docker/daemon/network"
|
2015-09-17 18:54:14 +00:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-07-20 17:57:15 +00:00
|
|
|
"github.com/docker/docker/image"
|
2014-09-30 06:23:36 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2015-10-06 17:24:49 +00:00
|
|
|
"github.com/docker/docker/pkg/broadcaster"
|
2015-07-08 03:15:09 +00:00
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
2014-08-12 16:10:43 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2015-05-19 20:05:25 +00:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2015-06-30 16:41:01 +00:00
|
|
|
"github.com/docker/docker/pkg/nat"
|
2014-09-30 06:16:27 +00:00
|
|
|
"github.com/docker/docker/pkg/promise"
|
2015-08-04 20:51:48 +00:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/pkg/symlink"
|
2015-09-10 02:23:06 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/runconfig"
|
2015-05-19 20:05:25 +00:00
|
|
|
"github.com/docker/docker/volume"
|
2015-09-10 02:23:06 +00:00
|
|
|
"github.com/docker/docker/volume/store"
|
2013-01-19 00:13:39 +00:00
|
|
|
)
|
|
|
|
|
2013-11-29 15:40:44 +00:00
|
|
|
var (
|
2015-07-30 21:01:53 +00:00
|
|
|
// ErrRootFSReadOnly is returned when a container
|
|
|
|
// rootfs is marked readonly.
|
|
|
|
ErrRootFSReadOnly = errors.New("container rootfs is marked read-only")
|
2013-11-29 15:40:44 +00:00
|
|
|
)
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
type streamConfig struct {
|
2015-10-06 17:24:49 +00:00
|
|
|
stdout *broadcaster.Unbuffered
|
|
|
|
stderr *broadcaster.Unbuffered
|
2014-08-26 22:05:37 +00:00
|
|
|
stdin io.ReadCloser
|
|
|
|
stdinPipe io.WriteCloser
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// CommonContainer holds the fields for a container which are
|
|
|
|
// applicable across all platforms supported by the daemon.
|
2015-04-29 22:53:35 +00:00
|
|
|
type CommonContainer struct {
|
2015-07-30 21:01:53 +00:00
|
|
|
streamConfig
|
|
|
|
// embed for Container to support states directly.
|
|
|
|
*State `json:"State"` // Needed for remote api version <= 1.11
|
|
|
|
root string // Path to the "home" of the container, including metadata.
|
|
|
|
basefs string // Path to the graphdriver mountpoint
|
|
|
|
ID string
|
|
|
|
Created time.Time
|
|
|
|
Path string
|
|
|
|
Args []string
|
|
|
|
Config *runconfig.Config
|
|
|
|
ImageID string `json:"Image"`
|
|
|
|
NetworkSettings *network.Settings
|
|
|
|
LogPath string
|
|
|
|
Name string
|
|
|
|
Driver string
|
|
|
|
ExecDriver 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-10 02:23:06 +00:00
|
|
|
MountPoints map[string]*volume.MountPoint
|
2015-07-30 21:01:53 +00:00
|
|
|
hostConfig *runconfig.HostConfig
|
|
|
|
command *execdriver.Command
|
|
|
|
monitor *containerMonitor
|
|
|
|
execCommands *execStore
|
|
|
|
daemon *Daemon
|
2015-02-04 19:04:58 +00:00
|
|
|
// logDriver for closing
|
2015-04-29 22:53:35 +00:00
|
|
|
logDriver logger.Logger
|
|
|
|
logCopier *logger.Copier
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (container *Container) fromDisk() error {
|
2014-05-28 02:15:42 +00:00
|
|
|
pth, err := container.jsonPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-04 13:43:58 +00:00
|
|
|
jsonSource, err := os.Open(pth)
|
2013-01-25 22:39:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-04 13:43:58 +00:00
|
|
|
defer jsonSource.Close()
|
|
|
|
|
|
|
|
dec := json.NewDecoder(jsonSource)
|
|
|
|
|
2013-03-21 07:25:00 +00:00
|
|
|
// Load container settings
|
2015-09-03 09:01:55 +00:00
|
|
|
if err := dec.Decode(container); err != nil {
|
2013-01-25 22:39:02 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-04-28 21:36:04 +00:00
|
|
|
|
|
|
|
if err := label.ReserveLabel(container.ProcessLabel); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-10-31 21:58:43 +00:00
|
|
|
return container.readHostConfig()
|
2013-01-25 22:39:02 +00:00
|
|
|
}
|
|
|
|
|
2014-07-11 17:49:24 +00:00
|
|
|
func (container *Container) toDisk() error {
|
2015-10-30 16:04:25 +00:00
|
|
|
pth, err := container.jsonPath()
|
2013-01-19 00:13:39 +00:00
|
|
|
if err != nil {
|
2014-05-28 02:15:42 +00:00
|
|
|
return err
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
2014-05-28 02:15:42 +00:00
|
|
|
|
2015-10-30 16:04:25 +00:00
|
|
|
jsonSource, err := os.Create(pth)
|
2013-10-31 21:58:43 +00:00
|
|
|
if err != nil {
|
2014-05-28 02:15:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-10-30 16:04:25 +00:00
|
|
|
defer jsonSource.Close()
|
|
|
|
|
|
|
|
enc := json.NewEncoder(jsonSource)
|
2014-05-28 02:15:42 +00:00
|
|
|
|
2015-10-30 16:04:25 +00:00
|
|
|
// Save container settings
|
|
|
|
if err := enc.Encode(container); err != nil {
|
2014-05-28 02:15:42 +00:00
|
|
|
return err
|
2013-10-31 21:58:43 +00:00
|
|
|
}
|
2014-05-28 02:15:42 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
return container.writeHostConfig()
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (container *Container) toDiskLocking() error {
|
2014-07-11 17:49:24 +00:00
|
|
|
container.Lock()
|
|
|
|
err := container.toDisk()
|
|
|
|
container.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-31 21:58:43 +00:00
|
|
|
func (container *Container) readHostConfig() error {
|
2014-02-12 04:04:39 +00:00
|
|
|
container.hostConfig = &runconfig.HostConfig{}
|
2013-10-31 21:58:43 +00:00
|
|
|
// If the hostconfig file does not exist, do not read it.
|
|
|
|
// (We still have to initialize container.hostConfig,
|
|
|
|
// but that's OK, since we just did that above.)
|
2014-05-28 02:15:42 +00:00
|
|
|
pth, err := container.hostConfigPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-04-21 19:59:59 +00:00
|
|
|
f, err := os.Open(pth)
|
2013-07-02 12:19:25 +00:00
|
|
|
if err != nil {
|
2015-10-30 21:54:09 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
2013-10-31 21:58:43 +00:00
|
|
|
return err
|
2013-07-02 12:19:25 +00:00
|
|
|
}
|
2015-04-21 19:59:59 +00:00
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
return json.NewDecoder(f).Decode(&container.hostConfig)
|
2013-07-02 12:19:25 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (container *Container) writeHostConfig() error {
|
2015-10-30 21:45:35 +00:00
|
|
|
pth, err := container.hostConfigPath()
|
2013-07-02 12:19:25 +00:00
|
|
|
if err != nil {
|
2014-05-28 02:15:42 +00:00
|
|
|
return err
|
2013-07-02 12:19:25 +00:00
|
|
|
}
|
2014-05-28 02:15:42 +00:00
|
|
|
|
2015-10-30 21:45:35 +00:00
|
|
|
f, err := os.Create(pth)
|
2014-05-28 02:15:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-10-30 21:45:35 +00:00
|
|
|
defer f.Close()
|
2014-05-28 02:15:42 +00:00
|
|
|
|
2015-10-30 21:45:35 +00:00
|
|
|
return json.NewEncoder(f).Encode(&container.hostConfig)
|
2013-07-02 12:19:25 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) logEvent(action string) {
|
2014-07-31 11:50:59 +00:00
|
|
|
d := container.daemon
|
2015-04-03 22:17:49 +00:00
|
|
|
d.EventsService.Log(
|
|
|
|
action,
|
|
|
|
container.ID,
|
2015-03-29 02:17:17 +00:00
|
|
|
container.Config.Image,
|
2015-04-03 22:17:49 +00:00
|
|
|
)
|
2014-07-31 11:50:59 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// GetResourcePath evaluates `path` in the scope of the container's basefs, with proper path
|
2015-03-06 01:53:06 +00:00
|
|
|
// sanitisation. Symlinks are all scoped to the basefs of the container, as
|
|
|
|
// though the container's basefs was `/`.
|
|
|
|
//
|
|
|
|
// The basefs of a container is the host-facing path which is bind-mounted as
|
|
|
|
// `/` 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.
|
|
|
|
//
|
|
|
|
// NOTE: The returned path is *only* safely scoped inside the container's basefs
|
|
|
|
// 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 23:42:27 +00: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)
|
|
|
|
r, e := symlink.FollowSymlinkInScope(filepath.Join(container.basefs, cleanPath), container.basefs)
|
|
|
|
return r, e
|
2014-05-11 20:49:46 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 01:53:06 +00:00
|
|
|
// Evaluates `path` in the scope of the container's root, with proper path
|
|
|
|
// 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-07-30 21:01:53 +00:00
|
|
|
func (container *Container) getRootResourcePath(path string) (string, error) {
|
2015-06-01 23:42:27 +00: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)
|
2014-05-28 23:12:29 +00:00
|
|
|
return symlink.FollowSymlinkInScope(filepath.Join(container.root, cleanPath), container.root)
|
2014-05-11 20:49:46 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (container *Container) exportContainerRw() (archive.Archive, error) {
|
2015-07-25 00:49:43 +00:00
|
|
|
if container.daemon == nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeUnregisteredContainer.WithArgs(container.ID)
|
2015-07-25 00:49:43 +00:00
|
|
|
}
|
2015-07-30 21:01:53 +00:00
|
|
|
archive, err := container.daemon.diff(container)
|
2015-07-25 00:49:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ioutils.NewReadCloserWrapper(archive, func() error {
|
|
|
|
err := archive.Close()
|
|
|
|
return err
|
|
|
|
}),
|
|
|
|
nil
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// Start prepares the container to run by setting up everything the
|
|
|
|
// container needs, such as storage and networking, as well as links
|
|
|
|
// between containers. The container is left waiting for a signal to
|
|
|
|
// begin running.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) Start() (err error) {
|
2013-11-21 20:21:03 +00:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2014-08-31 15:20:35 +00:00
|
|
|
if container.Running {
|
2014-03-30 05:12:34 +00:00
|
|
|
return nil
|
2013-11-12 02:15:38 +00:00
|
|
|
}
|
2014-08-04 22:53:10 +00:00
|
|
|
|
2015-03-12 19:26:17 +00:00
|
|
|
if container.removalInProgress || container.Dead {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeContainerBeingRemoved
|
2015-03-12 19:26:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 01:23:57 +00:00
|
|
|
// if we encounter an error during start we need to ensure that any other
|
2014-04-14 06:40:49 +00:00
|
|
|
// setup has been cleaned up properly
|
2013-10-16 20:12:56 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2014-09-30 08:30:58 +00:00
|
|
|
container.setError(err)
|
2014-11-06 02:23:42 +00:00
|
|
|
// if no one else has set it, make sure we don't leave it at zero
|
|
|
|
if container.ExitCode == 0 {
|
|
|
|
container.ExitCode = 128
|
|
|
|
}
|
2014-09-30 08:30:58 +00:00
|
|
|
container.toDisk()
|
2015-09-29 17:51:40 +00:00
|
|
|
container.cleanup()
|
|
|
|
container.logEvent("die")
|
2013-10-16 19:01:55 +00:00
|
|
|
}
|
2013-10-16 20:12:56 +00:00
|
|
|
}()
|
2014-01-30 02:34:43 +00:00
|
|
|
|
2015-09-19 01:21:57 +00:00
|
|
|
if err := container.conditionalMountOnStart(); err != nil {
|
2013-10-16 20:12:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-06-11 18:29:29 +00:00
|
|
|
|
2015-07-09 22:12:36 +00:00
|
|
|
// Make sure NetworkMode has an acceptable value. We do this to ensure
|
|
|
|
// backwards API compatibility.
|
|
|
|
container.hostConfig = runconfig.SetDefaultNetModeIfBlank(container.hostConfig)
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := container.initializeNetworking(); err != nil {
|
2014-04-14 06:40:49 +00:00
|
|
|
return err
|
2013-10-16 20:12:56 +00:00
|
|
|
}
|
2015-09-29 17:51:40 +00:00
|
|
|
linkedEnv, err := container.setupLinkedContainers()
|
2013-10-05 02:25:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-14 06:40:49 +00:00
|
|
|
if err := container.setupWorkingDirectory(); err != nil {
|
2014-01-30 15:43:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-05-05 21:45:14 +00:00
|
|
|
env := container.createDaemonEnvironment(linkedEnv)
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := populateCommand(container, env); err != nil {
|
2014-04-30 22:46:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-05-19 20:05:25 +00:00
|
|
|
|
2015-08-26 12:00:01 +00:00
|
|
|
if !container.hostConfig.IpcMode.IsContainer() && !container.hostConfig.IpcMode.IsHost() {
|
2015-08-03 22:05:34 +00:00
|
|
|
if err := container.setupIpcDirs(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:05:25 +00:00
|
|
|
mounts, err := container.setupMounts()
|
|
|
|
if err != nil {
|
2013-10-16 20:12:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-08-03 22:05:34 +00:00
|
|
|
mounts = append(mounts, container.ipcMounts()...)
|
2014-01-13 23:37:17 +00:00
|
|
|
|
2015-05-19 20:05:25 +00:00
|
|
|
container.command.Mounts = mounts
|
2015-09-29 17:51:40 +00:00
|
|
|
return container.waitForStart()
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// streamConfig.StdinPipe returns a WriteCloser which can be used to feed data
|
2013-10-16 17:41:36 +00:00
|
|
|
// to the standard input of the container's active process.
|
|
|
|
// Container.StdoutPipe and Container.StderrPipe each return a ReadCloser
|
|
|
|
// which can be used to retrieve the standard output (and error) generated
|
|
|
|
// by the container's active process. The output (and error) are actually
|
|
|
|
// copied and delivered to all StdoutPipe and StderrPipe consumers, using
|
|
|
|
// a kind of "broadcaster".
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (streamConfig *streamConfig) StdinPipe() io.WriteCloser {
|
2014-12-04 21:12:29 +00:00
|
|
|
return streamConfig.stdinPipe
|
2013-01-29 01:50:12 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (streamConfig *streamConfig) StdoutPipe() io.ReadCloser {
|
2013-01-19 00:13:39 +00:00
|
|
|
reader, writer := io.Pipe()
|
2015-10-06 17:24:49 +00:00
|
|
|
streamConfig.stdout.Add(writer)
|
2014-12-04 21:12:29 +00:00
|
|
|
return ioutils.NewBufReader(reader)
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (streamConfig *streamConfig) StderrPipe() io.ReadCloser {
|
2013-01-19 00:13:39 +00:00
|
|
|
reader, writer := io.Pipe()
|
2015-10-06 17:24:49 +00:00
|
|
|
streamConfig.stderr.Add(writer)
|
2014-08-12 16:10:43 +00:00
|
|
|
return ioutils.NewBufReader(reader)
|
2014-04-02 19:26:06 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 22:53:10 +00:00
|
|
|
// cleanup releases any network resources allocated to the container along with any rules
|
|
|
|
// around how containers are linked together. It also unmounts the container's root filesystem.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) cleanup() {
|
2015-07-30 21:01:53 +00:00
|
|
|
container.releaseNetwork()
|
2014-10-09 22:15:17 +00:00
|
|
|
|
2015-10-30 18:55:52 +00:00
|
|
|
if err := container.unmountIpcMounts(detachMounted); err != nil {
|
2015-08-26 12:00:01 +00:00
|
|
|
logrus.Errorf("%s: Failed to umount ipc filesystems: %v", container.ID, err)
|
2015-08-03 22:05:34 +00:00
|
|
|
}
|
|
|
|
|
2015-09-19 01:21:57 +00:00
|
|
|
container.conditionalUnmountOnCleanup()
|
2014-11-17 23:50:09 +00:00
|
|
|
|
|
|
|
for _, eConfig := range container.execCommands.s {
|
|
|
|
container.daemon.unregisterExecCommand(eConfig)
|
|
|
|
}
|
2015-05-22 17:37:00 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
container.unmountVolumes(false)
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// killSig sends the container the given signal. This wrapper for the
|
|
|
|
// host specific kill command prepares the container before attempting
|
|
|
|
// to send the signal. An error is returned if the container is paused
|
|
|
|
// or not running, or if there is a problem returned from the
|
|
|
|
// underlying kill command.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) killSig(sig int) error {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Debugf("Sending %d to %s", sig, container.ID)
|
2013-11-21 20:21:03 +00:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
2013-10-08 19:15:29 +00:00
|
|
|
|
2014-06-03 15:44:21 +00:00
|
|
|
// We could unpause the container for them rather than returning this error
|
2014-08-31 15:20:35 +00:00
|
|
|
if container.Paused {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeUnpauseContainer.WithArgs(container.ID)
|
2014-06-03 15:44:21 +00:00
|
|
|
}
|
|
|
|
|
2014-08-31 15:20:35 +00:00
|
|
|
if !container.Running {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
|
2013-03-24 02:51:35 +00:00
|
|
|
}
|
2014-08-05 00:05:56 +00:00
|
|
|
|
|
|
|
// signal to the monitor that it should not restart the container
|
|
|
|
// after we send the kill signal
|
|
|
|
container.monitor.ExitOnNext()
|
2014-08-04 22:53:10 +00:00
|
|
|
|
2014-08-11 18:35:18 +00:00
|
|
|
// if the container is currently restarting we do not need to send the signal
|
|
|
|
// to the process. Telling the monitor that it should exit on it's next event
|
|
|
|
// loop is enough
|
2014-08-31 15:20:35 +00:00
|
|
|
if container.Restarting {
|
2014-08-11 18:35:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
if err := container.daemon.kill(container, sig); err != nil {
|
2015-05-24 18:19:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-09-29 17:51:40 +00:00
|
|
|
container.logEvent("kill")
|
2015-05-24 18:19:39 +00:00
|
|
|
return nil
|
2013-10-08 19:15:29 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// Wrapper aroung killSig() suppressing "no such process" error.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) killPossiblyDeadProcess(sig int) error {
|
|
|
|
err := container.killSig(sig)
|
2015-01-19 12:44:04 +00:00
|
|
|
if err == syscall.ESRCH {
|
2015-07-30 21:01:53 +00:00
|
|
|
logrus.Debugf("Cannot kill process (pid=%d) with signal %d: no such process.", container.getPID(), sig)
|
2015-01-19 12:44:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) pause() error {
|
2015-05-09 09:01:07 +00:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2015-05-30 19:32:29 +00:00
|
|
|
// We cannot Pause the container which is not running
|
|
|
|
if !container.Running {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
|
2015-05-30 19:32:29 +00:00
|
|
|
}
|
|
|
|
|
2015-05-09 09:01:07 +00:00
|
|
|
// We cannot Pause the container which is already paused
|
|
|
|
if container.Paused {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeAlreadyPaused.WithArgs(container.ID)
|
2014-05-21 21:06:18 +00:00
|
|
|
}
|
2015-05-09 09:01:07 +00:00
|
|
|
|
|
|
|
if err := container.daemon.execDriver.Pause(container.command); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.Paused = true
|
2015-09-29 17:51:40 +00:00
|
|
|
container.logEvent("pause")
|
2015-05-09 09:01:07 +00:00
|
|
|
return nil
|
2014-05-21 21:06:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) unpause() error {
|
2015-05-09 09:01:07 +00:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
|
|
|
// We cannot unpause the container which is not running
|
|
|
|
if !container.Running {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
|
2015-05-30 19:32:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We cannot unpause the container which is not paused
|
|
|
|
if !container.Paused {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeNotPaused.WithArgs(container.ID)
|
2014-05-21 21:06:18 +00:00
|
|
|
}
|
2015-05-09 09:01:07 +00:00
|
|
|
|
|
|
|
if err := container.daemon.execDriver.Unpause(container.command); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.Paused = false
|
2015-09-29 17:51:40 +00:00
|
|
|
container.logEvent("unpause")
|
2015-05-09 09:01:07 +00:00
|
|
|
return nil
|
2014-05-21 21:06:18 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// Kill forcefully terminates a container.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) Kill() error {
|
2014-08-31 15:20:35 +00:00
|
|
|
if !container.IsRunning() {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
|
2013-10-08 19:15:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1. Send SIGKILL
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := container.killPossiblyDeadProcess(int(syscall.SIGKILL)); err != nil {
|
2015-05-25 03:05:10 +00:00
|
|
|
// While normally we might "return err" here we're not going to
|
|
|
|
// because if we can't stop the container by this point then
|
|
|
|
// its probably because its already stopped. Meaning, between
|
|
|
|
// the time of the IsRunning() call above and now it stopped.
|
|
|
|
// Also, since the err return will be exec driver specific we can't
|
|
|
|
// look for any particular (common) error that would indicate
|
|
|
|
// that the process is already dead vs something else going wrong.
|
|
|
|
// So, instead we'll give it up to 2 more seconds to complete and if
|
|
|
|
// by that time the container is still running, then the error
|
|
|
|
// we got is probably valid and so we return it to the caller.
|
|
|
|
|
|
|
|
if container.IsRunning() {
|
|
|
|
container.WaitStop(2 * time.Second)
|
|
|
|
if container.IsRunning() {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
2013-04-11 16:02:34 +00:00
|
|
|
|
|
|
|
// 2. Wait for the process to die, in last resort, try to kill the process directly
|
2015-04-29 22:53:35 +00:00
|
|
|
if err := killProcessDirectly(container); err != nil {
|
|
|
|
return err
|
2013-04-11 16:02:34 +00:00
|
|
|
}
|
|
|
|
|
2014-08-31 15:20:35 +00:00
|
|
|
container.WaitStop(-1 * time.Second)
|
2013-01-22 02:03:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-04 20:51:48 +00:00
|
|
|
// Stop halts a container by sending a stop signal, waiting for the given
|
2015-07-30 21:01:53 +00:00
|
|
|
// duration in seconds, and then calling SIGKILL and waiting for the
|
|
|
|
// process to exit. If a negative duration is given, Stop will wait
|
2015-08-04 20:51:48 +00:00
|
|
|
// for the initial signal forever. If the container is not running Stop returns
|
2015-07-30 21:01:53 +00:00
|
|
|
// immediately.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) Stop(seconds int) error {
|
2014-08-31 15:20:35 +00:00
|
|
|
if !container.IsRunning() {
|
2013-01-19 00:13:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
2013-01-22 02:03:23 +00:00
|
|
|
|
|
|
|
// 1. Send a SIGTERM
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := container.killPossiblyDeadProcess(container.stopSignal()); err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Infof("Failed to send SIGTERM to the process, force killing")
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := container.killPossiblyDeadProcess(9); err != nil {
|
2014-03-24 12:03:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-01-22 02:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Wait for the process to exit on its own
|
2014-08-31 15:20:35 +00:00
|
|
|
if _, err := container.WaitStop(time.Duration(seconds) * time.Second); err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Infof("Container %v failed to exit within %d seconds of SIGTERM - using the force", container.ID, seconds)
|
2014-03-24 12:03:56 +00:00
|
|
|
// 3. If it doesn't, then send SIGKILL
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := container.Kill(); err != nil {
|
2014-08-31 15:20:35 +00:00
|
|
|
container.WaitStop(-1 * time.Second)
|
2014-03-24 12:03:56 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-01-22 02:03:23 +00:00
|
|
|
}
|
2015-05-22 17:37:00 +00:00
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
container.logEvent("stop")
|
2013-01-19 00:13:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// Restart attempts to gracefully stop and then start the
|
|
|
|
// container. When stopping, wait for the given duration in seconds to
|
|
|
|
// gracefully stop, before forcefully terminating the container. If
|
|
|
|
// given a negative duration, wait forever for a graceful stop.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) Restart(seconds int) error {
|
2014-02-17 15:10:54 +00:00
|
|
|
// Avoid unnecessarily unmounting and then directly mounting
|
|
|
|
// the container when the container stops and then starts
|
|
|
|
// again
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := container.Mount(); err == nil {
|
|
|
|
defer container.Unmount()
|
2014-02-17 15:10:54 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := container.Stop(seconds); err != nil {
|
2013-01-22 23:03:40 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-05-24 18:19:39 +00:00
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := container.Start(); err != nil {
|
2015-05-24 18:19:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
container.logEvent("restart")
|
2015-05-24 18:19:39 +00:00
|
|
|
return nil
|
2013-01-22 23:03:40 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00: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 17:51:40 +00:00
|
|
|
func (container *Container) Resize(h, w int) error {
|
2014-10-23 20:30:18 +00:00
|
|
|
if !container.IsRunning() {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
|
2014-10-23 20:30:18 +00:00
|
|
|
}
|
2015-05-24 18:19:39 +00:00
|
|
|
if err := container.command.ProcessConfig.Terminal.Resize(h, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-29 17:51:40 +00:00
|
|
|
container.logEvent("resize")
|
2015-05-24 18:19:39 +00:00
|
|
|
return nil
|
2013-05-24 02:33:28 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) export() (archive.Archive, error) {
|
|
|
|
if err := container.Mount(); err != nil {
|
2013-03-21 07:25:00 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2013-12-05 21:18:02 +00:00
|
|
|
|
2015-10-08 15:51:41 +00:00
|
|
|
uidMaps, gidMaps := container.daemon.GetUIDGIDMaps()
|
|
|
|
archive, err := archive.TarWithOptions(container.basefs, &archive.TarOptions{
|
|
|
|
Compression: archive.Uncompressed,
|
|
|
|
UIDMaps: uidMaps,
|
|
|
|
GIDMaps: gidMaps,
|
|
|
|
})
|
2013-12-05 21:18:02 +00:00
|
|
|
if err != nil {
|
2015-09-29 17:51:40 +00:00
|
|
|
container.Unmount()
|
2013-12-05 21:18:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-24 18:19:39 +00:00
|
|
|
arch := ioutils.NewReadCloserWrapper(archive, func() error {
|
|
|
|
err := archive.Close()
|
2015-09-29 17:51:40 +00:00
|
|
|
container.Unmount()
|
2015-05-24 18:19:39 +00:00
|
|
|
return err
|
|
|
|
})
|
2015-09-29 17:51:40 +00:00
|
|
|
container.logEvent("export")
|
2015-05-24 18:19:39 +00:00
|
|
|
return arch, err
|
2013-03-21 07:25:00 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// Mount sets container.basefs
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) Mount() error {
|
|
|
|
return container.daemon.Mount(container)
|
2013-03-21 07:25:00 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 07:44:25 +00:00
|
|
|
func (container *Container) changes() ([]archive.Change, error) {
|
2014-05-27 19:04:20 +00:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
2015-07-30 21:01:53 +00:00
|
|
|
return container.daemon.changes(container)
|
2013-03-21 07:25:00 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) getImage() (*image.Image, error) {
|
2014-04-17 21:43:01 +00:00
|
|
|
if container.daemon == nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeImageUnregContainer
|
2013-03-21 07:25:00 +00:00
|
|
|
}
|
2014-10-28 21:06:23 +00:00
|
|
|
return container.daemon.graph.Get(container.ImageID)
|
2013-03-21 07:25:00 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// Unmount asks the daemon to release the layered filesystems that are
|
|
|
|
// mounted by the container.
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) Unmount() error {
|
2015-07-30 21:01:53 +00:00
|
|
|
return container.daemon.unmount(container)
|
2013-03-21 07:25:00 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 02:15:42 +00:00
|
|
|
func (container *Container) hostConfigPath() (string, error) {
|
2015-07-30 21:01:53 +00:00
|
|
|
return container.getRootResourcePath("hostconfig.json")
|
2013-07-02 12:19:25 +00:00
|
|
|
}
|
|
|
|
|
2014-05-28 02:15:42 +00:00
|
|
|
func (container *Container) jsonPath() (string, error) {
|
2015-07-30 21:01:53 +00:00
|
|
|
return container.getRootResourcePath("config.json")
|
2013-03-21 07:25:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method must be exported to be used from the lxc template
|
2014-01-30 15:43:53 +00:00
|
|
|
// This directory is only usable when the container is running
|
2015-07-30 21:01:53 +00:00
|
|
|
func (container *Container) rootfsPath() string {
|
2014-01-30 15:43:53 +00:00
|
|
|
return container.basefs
|
2013-03-21 07:25:00 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 18:00:22 +00:00
|
|
|
func validateID(id string) error {
|
2013-03-21 07:25:00 +00:00
|
|
|
if id == "" {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeEmptyID
|
2013-03-21 07:25:00 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2013-05-13 13:10:26 +00:00
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) copy(resource string) (rc io.ReadCloser, err error) {
|
2015-02-24 22:17:13 +00:00
|
|
|
container.Lock()
|
2015-05-13 01:21:26 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// Wait to unlock the container until the archive is fully read
|
|
|
|
// (see the ReadCloseWrapper func below) or if there is an error
|
|
|
|
// before that occurs.
|
|
|
|
container.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
if err := container.Mount(); err != nil {
|
2013-07-17 04:07:41 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-13 01:21:26 +00:00
|
|
|
|
2015-02-24 22:17:13 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2015-05-19 20:05:25 +00:00
|
|
|
// unmount any volumes
|
2015-07-30 21:01:53 +00:00
|
|
|
container.unmountVolumes(true)
|
2015-05-19 20:05:25 +00:00
|
|
|
// unmount the container's rootfs
|
2015-09-29 17:51:40 +00:00
|
|
|
container.Unmount()
|
2015-02-24 22:17:13 +00:00
|
|
|
}
|
|
|
|
}()
|
2015-05-13 01:21:26 +00:00
|
|
|
|
|
|
|
if err := container.mountVolumes(); err != nil {
|
2014-05-23 07:48:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-13 01:21:26 +00:00
|
|
|
|
2015-03-06 01:53:06 +00:00
|
|
|
basePath, err := container.GetResourcePath(resource)
|
2015-02-24 22:17:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-03-29 10:58:57 +00:00
|
|
|
}
|
2013-07-17 14:25:49 +00:00
|
|
|
stat, err := os.Stat(basePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-24 13:07:11 +00:00
|
|
|
var filter []string
|
2013-07-17 14:25:49 +00:00
|
|
|
if !stat.IsDir() {
|
2015-04-29 22:53:35 +00:00
|
|
|
d, f := filepath.Split(basePath)
|
2013-07-17 14:25:49 +00:00
|
|
|
basePath = d
|
|
|
|
filter = []string{f}
|
|
|
|
} else {
|
2015-04-29 22:53:35 +00:00
|
|
|
filter = []string{filepath.Base(basePath)}
|
|
|
|
basePath = filepath.Dir(basePath)
|
2013-07-17 14:25:49 +00:00
|
|
|
}
|
2014-02-14 00:05:36 +00:00
|
|
|
archive, err := archive.TarWithOptions(basePath, &archive.TarOptions{
|
2014-10-23 21:30:11 +00:00
|
|
|
Compression: archive.Uncompressed,
|
|
|
|
IncludeFiles: filter,
|
2013-11-11 22:30:38 +00:00
|
|
|
})
|
2013-12-05 21:18:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-11 18:29:29 +00:00
|
|
|
|
2015-05-24 18:19:39 +00:00
|
|
|
reader := ioutils.NewReadCloserWrapper(archive, func() error {
|
|
|
|
err := archive.Close()
|
2015-07-30 21:01:53 +00:00
|
|
|
container.unmountVolumes(true)
|
2015-09-29 17:51:40 +00:00
|
|
|
container.Unmount()
|
2015-05-13 01:21:26 +00:00
|
|
|
container.Unlock()
|
2015-05-24 18:19:39 +00:00
|
|
|
return err
|
|
|
|
})
|
2015-09-29 17:51:40 +00:00
|
|
|
container.logEvent("copy")
|
2015-05-24 18:19:39 +00:00
|
|
|
return reader, nil
|
2013-07-17 04:07:41 +00:00
|
|
|
}
|
2013-10-05 02:25:15 +00:00
|
|
|
|
|
|
|
// Returns true if the container exposes a certain port
|
2015-07-30 21:01:53 +00:00
|
|
|
func (container *Container) exposes(p nat.Port) bool {
|
2013-10-05 02:25:15 +00:00
|
|
|
_, exists := container.Config.ExposedPorts[p]
|
|
|
|
return exists
|
|
|
|
}
|
2013-11-29 15:40:44 +00:00
|
|
|
|
2015-04-09 04:23:30 +00:00
|
|
|
func (container *Container) getLogConfig() runconfig.LogConfig {
|
2015-02-04 19:04:58 +00:00
|
|
|
cfg := container.hostConfig.LogConfig
|
2015-07-01 00:40:13 +00:00
|
|
|
if cfg.Type != "" || len(cfg.Config) > 0 { // container has log driver configured
|
|
|
|
if cfg.Type == "" {
|
|
|
|
cfg.Type = jsonfilelog.Name
|
|
|
|
}
|
2015-04-09 04:23:30 +00:00
|
|
|
return cfg
|
2014-04-14 06:40:49 +00:00
|
|
|
}
|
2015-04-09 04:23:30 +00:00
|
|
|
// Use daemon's default log config for containers
|
|
|
|
return container.daemon.defaultLogConfig
|
|
|
|
}
|
2014-05-28 02:15:42 +00:00
|
|
|
|
2015-04-09 04:23:30 +00:00
|
|
|
func (container *Container) getLogger() (logger.Logger, error) {
|
2015-07-03 13:50:06 +00:00
|
|
|
if container.logDriver != nil && container.IsRunning() {
|
|
|
|
return container.logDriver, nil
|
|
|
|
}
|
2015-04-09 04:23:30 +00:00
|
|
|
cfg := container.getLogConfig()
|
2015-07-01 00:40:13 +00:00
|
|
|
if err := logger.ValidateLogOpts(cfg.Type, cfg.Config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-04-09 04:23:30 +00:00
|
|
|
c, err := logger.GetLogDriver(cfg.Type)
|
|
|
|
if err != nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return nil, derr.ErrorCodeLoggingFactory.WithArgs(err)
|
2015-04-09 04:23:30 +00:00
|
|
|
}
|
|
|
|
ctx := logger.Context{
|
2015-05-29 21:00:46 +00:00
|
|
|
Config: cfg.Config,
|
|
|
|
ContainerID: container.ID,
|
|
|
|
ContainerName: container.Name,
|
|
|
|
ContainerEntrypoint: container.Path,
|
|
|
|
ContainerArgs: container.Args,
|
|
|
|
ContainerImageID: container.ImageID,
|
|
|
|
ContainerImageName: container.Config.Image,
|
|
|
|
ContainerCreated: container.Created,
|
2015-08-11 11:27:19 +00:00
|
|
|
ContainerEnv: container.Config.Env,
|
|
|
|
ContainerLabels: container.Config.Labels,
|
2015-04-09 04:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set logging file for "json-logger"
|
|
|
|
if cfg.Type == jsonfilelog.Name {
|
2015-07-30 21:01:53 +00:00
|
|
|
ctx.LogPath, err = container.getRootResourcePath(fmt.Sprintf("%s-json.log", container.ID))
|
2015-04-20 18:26:39 +00:00
|
|
|
if err != nil {
|
2015-04-09 04:23:30 +00:00
|
|
|
return nil, err
|
2015-04-20 18:26:39 +00:00
|
|
|
}
|
2015-04-09 04:23:30 +00:00
|
|
|
}
|
|
|
|
return c(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) startLogging() error {
|
|
|
|
cfg := container.getLogConfig()
|
|
|
|
if cfg.Type == "none" {
|
|
|
|
return nil // do not start logging routines
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := container.getLogger()
|
|
|
|
if err != nil {
|
2015-09-16 18:56:26 +00:00
|
|
|
return derr.ErrorCodeInitLogger.WithArgs(err)
|
2014-04-14 06:40:49 +00:00
|
|
|
}
|
2014-05-28 02:15:42 +00:00
|
|
|
|
2015-07-28 18:17:25 +00:00
|
|
|
copier := logger.NewCopier(container.ID, map[string]io.Reader{"stdout": container.StdoutPipe(), "stderr": container.StderrPipe()}, l)
|
2015-03-18 18:44:14 +00:00
|
|
|
container.logCopier = copier
|
|
|
|
copier.Run()
|
2015-02-04 19:04:58 +00:00
|
|
|
container.logDriver = l
|
2014-05-28 02:15:42 +00:00
|
|
|
|
2015-04-09 04:23:30 +00:00
|
|
|
// set LogPath field only for json-file logdriver
|
|
|
|
if jl, ok := l.(*jsonfilelog.JSONFileLogger); ok {
|
|
|
|
container.LogPath = jl.LogPath()
|
|
|
|
}
|
|
|
|
|
2014-04-14 06:40:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) waitForStart() error {
|
2014-08-05 00:05:56 +00:00
|
|
|
container.monitor = newContainerMonitor(container, container.hostConfig.RestartPolicy)
|
2014-08-04 22:53:10 +00:00
|
|
|
|
2014-08-13 01:03:11 +00:00
|
|
|
// block until we either receive an error from the initial start of the container's
|
|
|
|
// process or until the process is running in the container
|
2014-04-14 06:40:49 +00:00
|
|
|
select {
|
2014-08-13 01:03:11 +00:00
|
|
|
case <-container.monitor.startSignal:
|
2015-09-29 17:51:40 +00:00
|
|
|
case err := <-promise.Go(container.monitor.Start):
|
2014-04-14 06:40:49 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-08-13 01:03:11 +00:00
|
|
|
|
2014-04-14 06:40:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-04-14 11:23:43 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (container *Container) getProcessLabel() string {
|
2014-04-29 08:08:19 +00:00
|
|
|
// even if we have a process label return "" if we are running
|
|
|
|
// in privileged mode
|
|
|
|
if container.hostConfig.Privileged {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return container.ProcessLabel
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (container *Container) getMountLabel() string {
|
2014-04-29 08:08:19 +00:00
|
|
|
if container.hostConfig.Privileged {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return container.MountLabel
|
|
|
|
}
|
2014-05-02 23:59:28 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (container *Container) stats() (*execdriver.ResourceStats, error) {
|
|
|
|
return container.daemon.stats(container)
|
2015-02-06 00:24:47 +00:00
|
|
|
}
|
2015-05-04 12:39:51 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func (container *Container) getExecIDs() []string {
|
2015-05-04 12:39:51 +00:00
|
|
|
return container.execCommands.List()
|
|
|
|
}
|
|
|
|
|
2015-10-06 00:21:50 +00:00
|
|
|
func (container *Container) exec(ec *ExecConfig) error {
|
2015-05-04 12:39:51 +00:00
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
callback := func(processConfig *execdriver.ProcessConfig, pid int, chOOM <-chan struct{}) error {
|
2015-05-04 12:39:51 +00:00
|
|
|
if processConfig.Tty {
|
|
|
|
// The callback is called after the process Start()
|
|
|
|
// so we are in the parent process. In TTY mode, stdin/out/err is the PtySlave
|
|
|
|
// which we close here.
|
|
|
|
if c, ok := processConfig.Stdout.(io.Closer); ok {
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
}
|
2015-10-06 00:21:50 +00:00
|
|
|
close(ec.waitStart)
|
2015-09-11 19:05:57 +00:00
|
|
|
return nil
|
2015-05-04 12:39:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We use a callback here instead of a goroutine and an chan for
|
2015-08-07 22:24:18 +00:00
|
|
|
// synchronization purposes
|
2015-10-06 00:21:50 +00:00
|
|
|
cErr := promise.Go(func() error { return container.monitorExec(ec, callback) })
|
2015-05-04 12:39:51 +00:00
|
|
|
|
|
|
|
// Exec should not return until the process is actually running
|
|
|
|
select {
|
2015-10-06 00:21:50 +00:00
|
|
|
case <-ec.waitStart:
|
2015-05-04 12:39:51 +00:00
|
|
|
case err := <-cErr:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) monitorExec(ExecConfig *ExecConfig, callback execdriver.DriverCallback) error {
|
2015-05-04 12:39:51 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
exitCode int
|
|
|
|
)
|
2015-07-30 21:01:53 +00:00
|
|
|
pipes := execdriver.NewPipes(ExecConfig.streamConfig.stdin, ExecConfig.streamConfig.stdout, ExecConfig.streamConfig.stderr, ExecConfig.OpenStdin)
|
2015-09-29 17:51:40 +00:00
|
|
|
exitCode, err = container.daemon.Exec(container, ExecConfig, pipes, callback)
|
2015-05-04 12:39:51 +00:00
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Error running command in existing container %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
logrus.Debugf("Exec task in container %s exited with code %d", container.ID, exitCode)
|
2015-07-30 21:01:53 +00:00
|
|
|
if ExecConfig.OpenStdin {
|
|
|
|
if err := ExecConfig.streamConfig.stdin.Close(); err != nil {
|
2015-05-04 12:39:51 +00:00
|
|
|
logrus.Errorf("Error closing stdin while running in %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
}
|
2015-07-30 21:01:53 +00:00
|
|
|
if err := ExecConfig.streamConfig.stdout.Clean(); err != nil {
|
2015-05-04 12:39:51 +00:00
|
|
|
logrus.Errorf("Error closing stdout while running in %s: %s", container.ID, err)
|
|
|
|
}
|
2015-07-30 21:01:53 +00:00
|
|
|
if err := ExecConfig.streamConfig.stderr.Clean(); err != nil {
|
2015-05-04 12:39:51 +00:00
|
|
|
logrus.Errorf("Error closing stderr while running in %s: %s", container.ID, err)
|
|
|
|
}
|
2015-07-30 21:01:53 +00:00
|
|
|
if ExecConfig.ProcessConfig.Terminal != nil {
|
|
|
|
if err := ExecConfig.ProcessConfig.Terminal.Close(); err != nil {
|
2015-05-04 12:39:51 +00:00
|
|
|
logrus.Errorf("Error closing terminal while running in container %s: %s", container.ID, err)
|
|
|
|
}
|
|
|
|
}
|
2015-07-08 17:55:42 +00:00
|
|
|
// remove the exec command from the container's store only and not the
|
|
|
|
// daemon's store so that the exec command can be inspected.
|
2015-07-30 21:01:53 +00:00
|
|
|
container.execCommands.Delete(ExecConfig.ID)
|
2015-05-04 12:39:51 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-05-05 20:25:05 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// Attach connects to the container's TTY, delegating to standard
|
|
|
|
// streams or websockets depending on the configuration.
|
|
|
|
func (container *Container) Attach(stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) chan error {
|
|
|
|
return attach(&container.streamConfig, container.Config.OpenStdin, container.Config.StdinOnce, container.Config.Tty, stdin, stdout, stderr)
|
2015-05-05 20:25:05 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
func (container *Container) attachWithLogs(stdin io.ReadCloser, stdout, stderr io.Writer, logs, stream bool) error {
|
2015-05-05 20:25:05 +00:00
|
|
|
if logs {
|
2015-07-30 21:01:53 +00:00
|
|
|
logDriver, err := container.getLogger()
|
2015-04-09 04:23:30 +00:00
|
|
|
if err != nil {
|
2015-07-01 00:40:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-07-03 13:50:06 +00:00
|
|
|
cLog, ok := logDriver.(logger.LogReader)
|
|
|
|
if !ok {
|
|
|
|
return logger.ErrReadLogsNotSupported
|
|
|
|
}
|
|
|
|
logs := cLog.ReadLogs(logger.ReadConfig{Tail: -1})
|
|
|
|
|
|
|
|
LogLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg, ok := <-logs.Msg:
|
|
|
|
if !ok {
|
|
|
|
break LogLoop
|
|
|
|
}
|
|
|
|
if msg.Source == "stdout" && stdout != nil {
|
|
|
|
stdout.Write(msg.Line)
|
|
|
|
}
|
|
|
|
if msg.Source == "stderr" && stderr != nil {
|
|
|
|
stderr.Write(msg.Line)
|
2015-05-05 20:25:05 +00:00
|
|
|
}
|
2015-07-03 13:50:06 +00:00
|
|
|
case err := <-logs.Err:
|
|
|
|
logrus.Errorf("Error streaming logs: %v", err)
|
|
|
|
break LogLoop
|
2015-05-05 20:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 17:51:40 +00:00
|
|
|
container.logEvent("attach")
|
2015-05-24 18:19:39 +00:00
|
|
|
|
2015-05-05 20:25:05 +00:00
|
|
|
//stream
|
|
|
|
if stream {
|
|
|
|
var stdinPipe io.ReadCloser
|
|
|
|
if stdin != nil {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
|
|
|
defer logrus.Debugf("Closing buffered stdin pipe")
|
|
|
|
io.Copy(w, stdin)
|
|
|
|
}()
|
|
|
|
stdinPipe = r
|
|
|
|
}
|
2015-07-30 21:01:53 +00:00
|
|
|
<-container.Attach(stdinPipe, stdout, stderr)
|
2015-05-05 20:25:05 +00:00
|
|
|
// If we are in stdinonce mode, wait for the process to end
|
|
|
|
// otherwise, simply return
|
2015-07-30 21:01:53 +00:00
|
|
|
if container.Config.StdinOnce && !container.Config.Tty {
|
|
|
|
container.WaitStop(-1 * time.Second)
|
2015-05-05 20:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
func attach(streamConfig *streamConfig, openStdin, stdinOnce, tty bool, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) chan error {
|
2015-05-05 20:25:05 +00: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 {
|
|
|
|
_, err = copyEscapable(cStdin, stdin)
|
|
|
|
} 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
|
|
|
|
func copyEscapable(dst io.Writer, src io.ReadCloser) (written int64, err error) {
|
|
|
|
buf := make([]byte, 32*1024)
|
|
|
|
for {
|
|
|
|
nr, er := src.Read(buf)
|
|
|
|
if nr > 0 {
|
|
|
|
// ---- Docker addition
|
|
|
|
// char 16 is C-p
|
|
|
|
if nr == 1 && buf[0] == 16 {
|
|
|
|
nr, er = src.Read(buf)
|
|
|
|
// char 17 is C-q
|
|
|
|
if nr == 1 && buf[0] == 17 {
|
|
|
|
if err := src.Close(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ---- 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 20:05:25 +00:00
|
|
|
|
2015-05-20 21:53:53 +00:00
|
|
|
func (container *Container) shouldRestart() bool {
|
2015-05-19 20:05:25 +00:00
|
|
|
return container.hostConfig.RestartPolicy.Name == "always" ||
|
2015-08-05 21:09:08 +00:00
|
|
|
(container.hostConfig.RestartPolicy.Name == "unless-stopped" && !container.HasBeenManuallyStopped) ||
|
2015-05-19 20:05:25 +00:00
|
|
|
(container.hostConfig.RestartPolicy.Name == "on-failure" && container.ExitCode != 0)
|
|
|
|
}
|
2015-05-22 17:37:00 +00:00
|
|
|
|
2015-05-13 01:21:26 +00:00
|
|
|
func (container *Container) mountVolumes() error {
|
|
|
|
mounts, err := container.setupMounts()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range mounts {
|
|
|
|
dest, err := container.GetResourcePath(m.Destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var stat os.FileInfo
|
|
|
|
stat, err = os.Stat(m.Source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = fileutils.CreateIfNotExists(dest, stat.IsDir()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := "rbind,ro"
|
|
|
|
if m.Writable {
|
|
|
|
opts = "rbind,rw"
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := mount.Mount(m.Source, dest, "bind", opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-10 02:23:06 +00:00
|
|
|
func (container *Container) prepareMountPoints() error {
|
|
|
|
for _, config := range container.MountPoints {
|
|
|
|
if len(config.Driver) > 0 {
|
|
|
|
v, err := container.daemon.createVolume(config.Name, config.Driver, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.Volume = v
|
|
|
|
}
|
2015-05-22 17:37:00 +00:00
|
|
|
}
|
2015-09-10 02:23:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-05-22 17:37:00 +00:00
|
|
|
|
2015-09-10 02:23:06 +00:00
|
|
|
func (container *Container) removeMountPoints(rm bool) error {
|
|
|
|
var rmErrors []string
|
|
|
|
for _, m := range container.MountPoints {
|
|
|
|
if m.Volume == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
container.daemon.volumes.Decrement(m.Volume)
|
|
|
|
if rm {
|
|
|
|
err := container.daemon.volumes.Remove(m.Volume)
|
|
|
|
// ErrVolumeInUse is ignored because having this
|
|
|
|
// volume being referenced by other container is
|
|
|
|
// not an error, but an implementation detail.
|
|
|
|
// This prevents docker from logging "ERROR: Volume in use"
|
|
|
|
// where there is another container using the volume.
|
|
|
|
if err != nil && err != store.ErrVolumeInUse {
|
|
|
|
rmErrors = append(rmErrors, err.Error())
|
|
|
|
}
|
2015-05-22 17:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-10 02:23:06 +00:00
|
|
|
if len(rmErrors) > 0 {
|
|
|
|
return derr.ErrorCodeRemovingVolume.WithArgs(strings.Join(rmErrors, "\n"))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-05-22 17:37:00 +00:00
|
|
|
|
2015-09-10 02:23:06 +00:00
|
|
|
func (container *Container) unmountVolumes(forceSyscall bool) error {
|
|
|
|
var (
|
|
|
|
volumeMounts []volume.MountPoint
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, mntPoint := range container.MountPoints {
|
|
|
|
dest, err := container.GetResourcePath(mntPoint.Destination)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
volumeMounts = append(volumeMounts, volume.MountPoint{Destination: dest, Volume: mntPoint.Volume})
|
2015-05-22 17:37:00 +00:00
|
|
|
}
|
|
|
|
|
2015-09-10 02:23:06 +00:00
|
|
|
// Append any network mounts to the list (this is a no-op on Windows)
|
|
|
|
if volumeMounts, err = appendNetworkMounts(container, volumeMounts); err != nil {
|
2015-05-22 17:37:00 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-09-10 02:23:06 +00:00
|
|
|
for _, volumeMount := range volumeMounts {
|
|
|
|
if forceSyscall {
|
2015-10-26 20:34:49 +00:00
|
|
|
system.Unmount(volumeMount.Destination)
|
2015-09-10 02:23:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if volumeMount.Volume != nil {
|
|
|
|
if err := volumeMount.Volume.Unmount(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) addBindMountPoint(name, source, destination string, rw bool) {
|
|
|
|
container.MountPoints[destination] = &volume.MountPoint{
|
|
|
|
Name: name,
|
|
|
|
Source: source,
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) addLocalMountPoint(name, destination string, rw bool) {
|
|
|
|
container.MountPoints[destination] = &volume.MountPoint{
|
|
|
|
Name: name,
|
|
|
|
Driver: volume.DefaultDriverName,
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) addMountPointWithVolume(destination string, vol volume.Volume, rw bool) {
|
|
|
|
container.MountPoints[destination] = &volume.MountPoint{
|
|
|
|
Name: vol.Name(),
|
|
|
|
Driver: vol.DriverName(),
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
Volume: vol,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) isDestinationMounted(destination string) bool {
|
|
|
|
return container.MountPoints[destination] != nil
|
2015-05-22 17:37:00 +00:00
|
|
|
}
|
2015-08-04 20:51:48 +00:00
|
|
|
|
|
|
|
func (container *Container) stopSignal() int {
|
|
|
|
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)
|
|
|
|
}
|