2018-02-05 16:05:59 -05:00
|
|
|
package container // import "github.com/docker/docker/container"
|
2013-01-18 19:13:39 -05:00
|
|
|
|
|
|
|
import (
|
2017-04-06 13:43:10 -04:00
|
|
|
"bytes"
|
2013-01-18 19:13:39 -05:00
|
|
|
"encoding/json"
|
2013-03-21 03:25:00 -04:00
|
|
|
"fmt"
|
2014-04-28 17:36:04 -04:00
|
|
|
"io"
|
2016-03-09 23:33:21 -05:00
|
|
|
"net"
|
2014-04-28 17:36:04 -04:00
|
|
|
"os"
|
2014-05-11 16:49:46 -04:00
|
|
|
"path/filepath"
|
2017-05-16 17:19:19 -04:00
|
|
|
"runtime"
|
2016-03-09 23:33:21 -05:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-05-05 16:25:05 -04:00
|
|
|
"sync"
|
2014-04-28 17:36:04 -04:00
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
"github.com/containerd/containerd/cio"
|
2016-09-06 14:18:12 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
Add new `HostConfig` field, `Mounts`.
`Mounts` allows users to specify in a much safer way the volumes they
want to use in the container.
This replaces `Binds` and `Volumes`, which both still exist, but
`Mounts` and `Binds`/`Volumes` are exclussive.
The CLI will continue to use `Binds` and `Volumes` due to concerns with
parsing the volume specs on the client side and cross-platform support
(for now).
The new API follows exactly the services mount API.
Example usage of `Mounts`:
```
$ curl -XPOST localhost:2375/containers/create -d '{
"Image": "alpine:latest",
"HostConfig": {
"Mounts": [{
"Type": "Volume",
"Target": "/foo"
},{
"Type": "bind",
"Source": "/var/run/docker.sock",
"Target": "/var/run/docker.sock",
},{
"Type": "volume",
"Name": "important_data",
"Target": "/var/data",
"ReadOnly": true,
"VolumeOptions": {
"DriverConfig": {
Name: "awesomeStorage",
Options: {"size": "10m"},
Labels: {"some":"label"}
}
}]
}
}'
```
There are currently 2 types of mounts:
- **bind**: Paths on the host that get mounted into the
container. Paths must exist prior to creating the container.
- **volume**: Volumes that persist after the
container is removed.
Not all fields are available in each type, and validation is done to
ensure these fields aren't mixed up between types.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2016-04-26 14:25:35 -04:00
|
|
|
mounttypes "github.com/docker/docker/api/types/mount"
|
2016-09-06 14:18:12 -04:00
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
2016-11-15 10:04:36 -05:00
|
|
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
2016-11-14 15:15:09 -05:00
|
|
|
"github.com/docker/docker/container/stream"
|
2015-11-20 17:35:16 -05:00
|
|
|
"github.com/docker/docker/daemon/exec"
|
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-11-18 17:20:54 -05:00
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/layer"
|
2016-12-23 14:09:12 -05:00
|
|
|
"github.com/docker/docker/opts"
|
2017-08-03 20:22:00 -04:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2016-03-08 10:40:30 -05:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2016-04-20 20:08:47 -04:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
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"
|
2017-05-26 19:14:18 -04:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2016-03-18 14:50:19 -04:00
|
|
|
"github.com/docker/docker/restartmanager"
|
2014-07-24 18:19:50 -04:00
|
|
|
"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"
|
2016-11-22 21:55:27 -05:00
|
|
|
"github.com/docker/go-units"
|
2016-03-09 23:33:21 -05:00
|
|
|
"github.com/docker/libnetwork"
|
|
|
|
"github.com/docker/libnetwork/netlabel"
|
|
|
|
"github.com/docker/libnetwork/options"
|
|
|
|
"github.com/docker/libnetwork/types"
|
2016-11-15 10:04:36 -05:00
|
|
|
agentexec "github.com/docker/swarmkit/agent/exec"
|
2017-07-19 10:20:13 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-04-06 13:43:10 -04:00
|
|
|
"golang.org/x/net/context"
|
2013-01-18 19:13:39 -05:00
|
|
|
)
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
const configFileName = "config.v2.json"
|
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
var (
|
2017-07-19 10:20:13 -04:00
|
|
|
errInvalidEndpoint = errors.New("invalid endpoint while building port map info")
|
|
|
|
errInvalidNetwork = errors.New("invalid network settings while building port map info")
|
2016-03-09 23:33:21 -05:00
|
|
|
)
|
|
|
|
|
2017-09-22 09:52:41 -04:00
|
|
|
// ExitStatus provides exit reasons for a container.
|
|
|
|
type ExitStatus struct {
|
|
|
|
// The exit code with which the container exited.
|
|
|
|
ExitCode int
|
|
|
|
|
|
|
|
// Whether the container encountered an OOM.
|
|
|
|
OOMKilled bool
|
|
|
|
|
|
|
|
// Time at which the container died
|
|
|
|
ExitedAt time.Time
|
|
|
|
}
|
|
|
|
|
2017-04-25 15:03:45 -04:00
|
|
|
// Container holds the structure defining a container object.
|
|
|
|
type Container struct {
|
2016-11-14 15:15:09 -05:00
|
|
|
StreamConfig *stream.Config
|
2015-07-30 17:01:53 -04:00
|
|
|
// embed for Container to support states directly.
|
2017-08-03 20:22:00 -04:00
|
|
|
*State `json:"State"` // Needed for Engine API version <= 1.11
|
|
|
|
Root string `json:"-"` // Path to the "home" of the container, including metadata.
|
|
|
|
BaseFS containerfs.ContainerFS `json:"-"` // interface containing graphdriver mount
|
|
|
|
RWLayer layer.RWLayer `json:"-"`
|
2015-07-30 17:01:53 -04:00
|
|
|
ID string
|
|
|
|
Created time.Time
|
2016-06-13 22:52:49 -04:00
|
|
|
Managed bool
|
2015-07-30 17:01:53 -04:00
|
|
|
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
|
2017-08-08 15:43:48 -04:00
|
|
|
OS string
|
2015-07-30 17:01:53 -04:00
|
|
|
// 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
|
2016-11-15 10:04:36 -05:00
|
|
|
HostConfig *containertypes.HostConfig `json:"-"` // do not serialize the host config in the json, otherwise we'll make the container unportable
|
|
|
|
ExecCommands *exec.Store `json:"-"`
|
2017-03-16 17:23:33 -04:00
|
|
|
DependencyStore agentexec.DependencyGetter `json:"-"`
|
2016-11-15 10:04:36 -05:00
|
|
|
SecretReferences []*swarmtypes.SecretReference
|
2017-03-16 17:23:33 -04:00
|
|
|
ConfigReferences []*swarmtypes.ConfigReference
|
2015-02-04 14:04:58 -05:00
|
|
|
// logDriver for closing
|
2016-03-18 14:50:19 -04:00
|
|
|
LogDriver logger.Logger `json:"-"`
|
|
|
|
LogCopier *logger.Copier `json:"-"`
|
|
|
|
restartManager restartmanager.RestartManager
|
|
|
|
attachContext *attachContext
|
2017-04-25 15:03:45 -04:00
|
|
|
|
|
|
|
// Fields here are specific to Unix platforms
|
|
|
|
AppArmorProfile string
|
|
|
|
HostnamePath string
|
|
|
|
HostsPath string
|
|
|
|
ShmPath string
|
|
|
|
ResolvConfPath string
|
|
|
|
SeccompProfile string
|
|
|
|
NoNewPrivileges bool
|
|
|
|
|
|
|
|
// Fields here are specific to Windows
|
2017-06-12 18:20:23 -04:00
|
|
|
NetworkSharedContainerID string `json:"-"`
|
|
|
|
SharedEndpointList []string `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{
|
2017-04-25 15:03:45 -04:00
|
|
|
ID: id,
|
|
|
|
State: NewState(),
|
|
|
|
ExecCommands: exec.NewStore(),
|
|
|
|
Root: root,
|
|
|
|
MountPoints: make(map[string]*volume.MountPoint),
|
|
|
|
StreamConfig: stream.NewConfig(),
|
|
|
|
attachContext: &attachContext{},
|
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
|
|
|
|
2017-08-08 15:43:48 -04:00
|
|
|
// Ensure the operating system is set if blank. Assume it is the OS of the
|
|
|
|
// host OS if not, to ensure containers created before multiple-OS
|
2017-05-16 17:19:19 -04:00
|
|
|
// support are migrated
|
2017-08-08 15:43:48 -04:00
|
|
|
if container.OS == "" {
|
|
|
|
container.OS = runtime.GOOS
|
2017-05-16 17:19:19 -04:00
|
|
|
}
|
|
|
|
|
2013-10-31 17:58:43 -04:00
|
|
|
return container.readHostConfig()
|
2013-01-25 17:39:02 -05:00
|
|
|
}
|
|
|
|
|
2017-04-06 13:43:10 -04:00
|
|
|
// toDisk saves the container configuration on disk and returns a deep copy.
|
|
|
|
func (container *Container) toDisk() (*Container, error) {
|
|
|
|
var (
|
|
|
|
buf bytes.Buffer
|
|
|
|
deepCopy Container
|
|
|
|
)
|
2015-11-12 14:55:17 -05:00
|
|
|
pth, err := container.ConfigPath()
|
2013-01-18 19:13:39 -05:00
|
|
|
if err != nil {
|
2017-04-06 13:43:10 -04:00
|
|
|
return nil, err
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2017-04-06 13:43:10 -04:00
|
|
|
// Save container settings
|
2017-08-07 06:10:08 -04:00
|
|
|
f, err := ioutils.NewAtomicFileWriter(pth, 0600)
|
2013-10-31 17:58:43 -04:00
|
|
|
if err != nil {
|
2017-04-06 13:43:10 -04:00
|
|
|
return nil, err
|
2014-05-27 22:15:42 -04:00
|
|
|
}
|
2017-04-06 13:43:10 -04:00
|
|
|
defer f.Close()
|
2015-10-30 12:04:25 -04:00
|
|
|
|
2017-04-06 13:43:10 -04:00
|
|
|
w := io.MultiWriter(&buf, f)
|
|
|
|
if err := json.NewEncoder(w).Encode(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2017-04-06 13:43:10 -04:00
|
|
|
if err := json.NewDecoder(&buf).Decode(&deepCopy); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
deepCopy.HostConfig, err = container.WriteHostConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2013-10-31 17:58:43 -04:00
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2017-04-06 13:43:10 -04:00
|
|
|
return &deepCopy, nil
|
2013-01-18 19:13:39 -05:00
|
|
|
}
|
|
|
|
|
2017-03-27 13:18:53 -04:00
|
|
|
// CheckpointTo makes the Container's current state visible to queries, and persists state.
|
2017-02-23 18:12:18 -05:00
|
|
|
// Callers must hold a Container lock.
|
|
|
|
func (container *Container) CheckpointTo(store ViewDB) error {
|
2017-04-06 13:43:10 -04:00
|
|
|
deepCopy, err := container.toDisk()
|
|
|
|
if err != nil {
|
2017-02-23 18:12:18 -05:00
|
|
|
return err
|
|
|
|
}
|
2017-04-06 13:43:10 -04:00
|
|
|
return store.Save(deepCopy)
|
2017-02-23 18:12:18 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-06 13:43:10 -04:00
|
|
|
// WriteHostConfig saves the host configuration on disk for the container,
|
|
|
|
// and returns a deep copy of the saved object. Callers must hold a Container lock.
|
|
|
|
func (container *Container) WriteHostConfig() (*containertypes.HostConfig, error) {
|
|
|
|
var (
|
|
|
|
buf bytes.Buffer
|
|
|
|
deepCopy containertypes.HostConfig
|
|
|
|
)
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
pth, err := container.HostConfigPath()
|
2013-07-02 08:19:25 -04:00
|
|
|
if err != nil {
|
2017-04-06 13:43:10 -04:00
|
|
|
return nil, err
|
2013-07-02 08:19:25 -04:00
|
|
|
}
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2016-11-01 16:00:17 -04:00
|
|
|
f, err := ioutils.NewAtomicFileWriter(pth, 0644)
|
2014-05-27 22:15:42 -04:00
|
|
|
if err != nil {
|
2017-04-06 13:43:10 -04:00
|
|
|
return nil, err
|
2014-05-27 22:15:42 -04:00
|
|
|
}
|
2015-10-30 17:45:35 -04:00
|
|
|
defer f.Close()
|
2014-05-27 22:15:42 -04:00
|
|
|
|
2017-04-06 13:43:10 -04:00
|
|
|
w := io.MultiWriter(&buf, f)
|
|
|
|
if err := json.NewEncoder(w).Encode(&container.HostConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(&buf).Decode(&deepCopy); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &deepCopy, nil
|
2013-07-02 08:19:25 -04:00
|
|
|
}
|
|
|
|
|
2016-01-27 16:03:09 -05:00
|
|
|
// SetupWorkingDirectory sets up the container's working directory as set in container.Config.WorkingDir
|
2017-05-19 18:06:46 -04:00
|
|
|
func (container *Container) SetupWorkingDirectory(rootIDs idtools.IDPair) error {
|
2017-08-04 14:39:23 -04:00
|
|
|
// TODO @jhowardmsft, @gupta-ak LCOW Support. This will need revisiting.
|
|
|
|
// We will need to do remote filesystem operations here.
|
2017-08-08 15:43:48 -04:00
|
|
|
if container.OS != runtime.GOOS {
|
2017-08-04 14:39:23 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 16:03:09 -05:00
|
|
|
if container.Config.WorkingDir == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-01 13:23:43 -05:00
|
|
|
|
2016-05-26 16:24:22 -04:00
|
|
|
container.Config.WorkingDir = filepath.Clean(container.Config.WorkingDir)
|
2016-01-27 16:03:09 -05:00
|
|
|
pth, err := container.GetResourcePath(container.Config.WorkingDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-19 18:06:46 -04:00
|
|
|
if err := idtools.MkdirAllAndChownNew(pth, 0755, rootIDs); err != nil {
|
2016-01-27 16:03:09 -05:00
|
|
|
pthInfo, err2 := os.Stat(pth)
|
|
|
|
if err2 == nil && pthInfo != nil && !pthInfo.IsDir() {
|
2017-07-19 10:20:13 -04:00
|
|
|
return errors.Errorf("Cannot mkdir: %s is not a directory", container.Config.WorkingDir)
|
2016-01-27 16:03:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
2017-08-03 20:22:00 -04:00
|
|
|
r, e := container.BaseFS.ResolveScopedPath(path, false)
|
2016-04-19 23:55:30 -04:00
|
|
|
|
|
|
|
// Log this here on the daemon side as there's otherwise no indication apart
|
|
|
|
// from the error being propagated all the way back to the client. This makes
|
|
|
|
// debugging significantly easier and clearly indicates the error comes from the daemon.
|
|
|
|
if e != nil {
|
2017-08-03 20:22:00 -04:00
|
|
|
logrus.Errorf("Failed to ResolveScopedPath BaseFS %s path %s %s\n", container.BaseFS.Path(), path, e)
|
2016-04-19 23:55:30 -04:00
|
|
|
}
|
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() {
|
2016-10-05 16:29:56 -04:00
|
|
|
container.RestartManager().Cancel()
|
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
|
|
|
}
|
|
|
|
|
2016-05-12 10:52:00 -04:00
|
|
|
// CheckpointDir returns the directory checkpoints are stored in
|
|
|
|
func (container *Container) CheckpointDir() string {
|
|
|
|
return filepath.Join(container.Root, "checkpoints")
|
|
|
|
}
|
|
|
|
|
2015-11-03 13:45:12 -05:00
|
|
|
// StartLogger starts a new logger driver for the container.
|
2016-11-22 06:40:54 -05:00
|
|
|
func (container *Container) StartLogger() (logger.Logger, error) {
|
|
|
|
cfg := container.HostConfig.LogConfig
|
2016-11-22 21:55:27 -05:00
|
|
|
initDriver, err := logger.GetLogDriver(cfg.Type)
|
2015-04-09 00:23:30 -04:00
|
|
|
if err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Wrap(err, "failed to get logging factory")
|
2015-04-09 00:23:30 -04:00
|
|
|
}
|
2016-11-26 00:08:34 -05:00
|
|
|
info := logger.Info{
|
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,
|
2016-04-27 22:46:54 -04:00
|
|
|
DaemonName: "docker",
|
2015-04-09 00:23:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set logging file for "json-logger"
|
|
|
|
if cfg.Type == jsonfilelog.Name {
|
2016-11-26 00:08:34 -05:00
|
|
|
info.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
|
|
|
}
|
2016-11-22 21:55:27 -05:00
|
|
|
|
|
|
|
l, err := initDriver(info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if containertypes.LogMode(cfg.Config["mode"]) == containertypes.LogModeNonBlock {
|
|
|
|
bufferSize := int64(-1)
|
|
|
|
if s, exists := cfg.Config["max-buffer-size"]; exists {
|
|
|
|
bufferSize, err = units.RAMInBytes(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l = logger.NewRingLogger(l, info, bufferSize)
|
|
|
|
}
|
|
|
|
return l, nil
|
2015-04-09 00:23:30 -04:00
|
|
|
}
|
|
|
|
|
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 {
|
2014-04-29 04:08:19 -04:00
|
|
|
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-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 {
|
2016-10-05 16:29:56 -04:00
|
|
|
shouldRestart, _, _ := container.RestartManager().ShouldRestart(uint32(container.ExitCode()), container.HasBeenManuallyStopped, container.FinishedAt.Sub(container.StartedAt))
|
2016-03-22 11:46:40 -04:00
|
|
|
return shouldRestart
|
2015-05-19 16:05:25 -04:00
|
|
|
}
|
2015-05-22 13:37:00 -04:00
|
|
|
|
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) {
|
2017-08-08 15:43:48 -04:00
|
|
|
operatingSystem := container.OS
|
2017-08-01 13:32:44 -04:00
|
|
|
if operatingSystem == "" {
|
|
|
|
operatingSystem = runtime.GOOS
|
|
|
|
}
|
|
|
|
volumeParser := volume.NewParser(operatingSystem)
|
2015-09-09 22:23:06 -04:00
|
|
|
container.MountPoints[destination] = &volume.MountPoint{
|
Add new `HostConfig` field, `Mounts`.
`Mounts` allows users to specify in a much safer way the volumes they
want to use in the container.
This replaces `Binds` and `Volumes`, which both still exist, but
`Mounts` and `Binds`/`Volumes` are exclussive.
The CLI will continue to use `Binds` and `Volumes` due to concerns with
parsing the volume specs on the client side and cross-platform support
(for now).
The new API follows exactly the services mount API.
Example usage of `Mounts`:
```
$ curl -XPOST localhost:2375/containers/create -d '{
"Image": "alpine:latest",
"HostConfig": {
"Mounts": [{
"Type": "Volume",
"Target": "/foo"
},{
"Type": "bind",
"Source": "/var/run/docker.sock",
"Target": "/var/run/docker.sock",
},{
"Type": "volume",
"Name": "important_data",
"Target": "/var/data",
"ReadOnly": true,
"VolumeOptions": {
"DriverConfig": {
Name: "awesomeStorage",
Options: {"size": "10m"},
Labels: {"some":"label"}
}
}]
}
}'
```
There are currently 2 types of mounts:
- **bind**: Paths on the host that get mounted into the
container. Paths must exist prior to creating the container.
- **volume**: Volumes that persist after the
container is removed.
Not all fields are available in each type, and validation is done to
ensure these fields aren't mixed up between types.
Signed-off-by: Brian Goff <cpuguy83@gmail.com>
2016-04-26 14:25:35 -04:00
|
|
|
Type: mounttypes.TypeVolume,
|
2015-09-09 22:23:06 -04:00
|
|
|
Name: vol.Name(),
|
|
|
|
Driver: vol.DriverName(),
|
|
|
|
Destination: destination,
|
|
|
|
RW: rw,
|
|
|
|
Volume: vol,
|
2017-08-01 13:32:44 -04:00
|
|
|
CopyData: volumeParser.DefaultCopyMode(),
|
2015-09-09 22:23:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 13:53:06 -04:00
|
|
|
// UnmountVolumes unmounts all volumes
|
|
|
|
func (container *Container) UnmountVolumes(volumeEventLog func(name, action string, attributes map[string]string)) error {
|
|
|
|
var errors []string
|
|
|
|
for _, volumeMount := range container.MountPoints {
|
2017-04-27 23:33:33 -04:00
|
|
|
if volumeMount.Volume == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-10-03 13:53:06 -04:00
|
|
|
|
2017-04-27 23:33:33 -04:00
|
|
|
if err := volumeMount.Cleanup(); err != nil {
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes := map[string]string{
|
|
|
|
"driver": volumeMount.Volume.DriverName(),
|
|
|
|
"container": container.ID,
|
2016-10-03 13:53:06 -04:00
|
|
|
}
|
2017-04-27 23:33:33 -04:00
|
|
|
volumeEventLog(volumeMount.Volume.Name(), "unmount", attributes)
|
2016-10-03 13:53:06 -04:00
|
|
|
}
|
|
|
|
if len(errors) > 0 {
|
|
|
|
return fmt.Errorf("error while unmounting volumes for container %s: %s", container.ID, strings.Join(errors, "; "))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-05-26 16:34:48 -04:00
|
|
|
// StopTimeout returns the timeout (in seconds) used to stop the container.
|
|
|
|
func (container *Container) StopTimeout() int {
|
|
|
|
if container.Config.StopTimeout != nil {
|
|
|
|
return *container.Config.StopTimeout
|
|
|
|
}
|
2016-06-06 23:29:05 -04:00
|
|
|
return DefaultStopTimeout
|
2016-05-26 16:34:48 -04: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
|
|
|
}
|
|
|
|
}
|
2016-01-04 10:58:20 -05:00
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
// GetEndpointInNetwork returns the container's endpoint to the provided network.
|
|
|
|
func (container *Container) GetEndpointInNetwork(n libnetwork.Network) (libnetwork.Endpoint, error) {
|
|
|
|
endpointName := strings.TrimPrefix(container.Name, "/")
|
|
|
|
return n.EndpointByName(endpointName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (container *Container) buildPortMapInfo(ep libnetwork.Endpoint) error {
|
|
|
|
if ep == nil {
|
|
|
|
return errInvalidEndpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
networkSettings := container.NetworkSettings
|
|
|
|
if networkSettings == nil {
|
|
|
|
return errInvalidNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(networkSettings.Ports) == 0 {
|
|
|
|
pm, err := getEndpointPortMapInfo(ep)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
networkSettings.Ports = pm
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEndpointPortMapInfo(ep libnetwork.Endpoint) (nat.PortMap, error) {
|
|
|
|
pm := nat.PortMap{}
|
|
|
|
driverInfo, err := ep.DriverInfo()
|
|
|
|
if err != nil {
|
|
|
|
return pm, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if driverInfo == nil {
|
|
|
|
// It is not an error for epInfo to be nil
|
|
|
|
return pm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if expData, ok := driverInfo[netlabel.ExposedPorts]; ok {
|
|
|
|
if exposedPorts, ok := expData.([]types.TransportPort); ok {
|
|
|
|
for _, tp := range exposedPorts {
|
|
|
|
natPort, err := nat.NewPort(tp.Proto.String(), strconv.Itoa(int(tp.Port)))
|
|
|
|
if err != nil {
|
|
|
|
return pm, fmt.Errorf("Error parsing Port value(%v):%v", tp.Port, err)
|
|
|
|
}
|
|
|
|
pm[natPort] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mapData, ok := driverInfo[netlabel.PortMap]
|
|
|
|
if !ok {
|
|
|
|
return pm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if portMapping, ok := mapData.([]types.PortBinding); ok {
|
|
|
|
for _, pp := range portMapping {
|
|
|
|
natPort, err := nat.NewPort(pp.Proto.String(), strconv.Itoa(int(pp.Port)))
|
|
|
|
if err != nil {
|
|
|
|
return pm, err
|
|
|
|
}
|
|
|
|
natBndg := nat.PortBinding{HostIP: pp.HostIP.String(), HostPort: strconv.Itoa(int(pp.HostPort))}
|
|
|
|
pm[natPort] = append(pm[natPort], natBndg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pm, nil
|
|
|
|
}
|
|
|
|
|
2016-05-25 16:47:38 -04:00
|
|
|
// GetSandboxPortMapInfo retrieves the current port-mapping programmed for the given sandbox
|
|
|
|
func GetSandboxPortMapInfo(sb libnetwork.Sandbox) nat.PortMap {
|
2016-03-09 23:33:21 -05:00
|
|
|
pm := nat.PortMap{}
|
|
|
|
if sb == nil {
|
|
|
|
return pm
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ep := range sb.Endpoints() {
|
|
|
|
pm, _ = getEndpointPortMapInfo(ep)
|
|
|
|
if len(pm) > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pm
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildEndpointInfo sets endpoint-related fields on container.NetworkSettings based on the provided network and endpoint.
|
|
|
|
func (container *Container) BuildEndpointInfo(n libnetwork.Network, ep libnetwork.Endpoint) error {
|
|
|
|
if ep == nil {
|
|
|
|
return errInvalidEndpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
networkSettings := container.NetworkSettings
|
|
|
|
if networkSettings == nil {
|
|
|
|
return errInvalidNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
epInfo := ep.Info()
|
|
|
|
if epInfo == nil {
|
|
|
|
// It is not an error to get an empty endpoint info
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := networkSettings.Networks[n.Name()]; !ok {
|
2016-08-23 19:50:15 -04:00
|
|
|
networkSettings.Networks[n.Name()] = &network.EndpointSettings{
|
|
|
|
EndpointSettings: &networktypes.EndpointSettings{},
|
|
|
|
}
|
2016-03-09 23:33:21 -05:00
|
|
|
}
|
|
|
|
networkSettings.Networks[n.Name()].NetworkID = n.ID()
|
|
|
|
networkSettings.Networks[n.Name()].EndpointID = ep.ID()
|
|
|
|
|
|
|
|
iface := epInfo.Iface()
|
|
|
|
if iface == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if iface.MacAddress() != nil {
|
|
|
|
networkSettings.Networks[n.Name()].MacAddress = iface.MacAddress().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
if iface.Address() != nil {
|
|
|
|
ones, _ := iface.Address().Mask.Size()
|
|
|
|
networkSettings.Networks[n.Name()].IPAddress = iface.Address().IP.String()
|
|
|
|
networkSettings.Networks[n.Name()].IPPrefixLen = ones
|
|
|
|
}
|
|
|
|
|
|
|
|
if iface.AddressIPv6() != nil && iface.AddressIPv6().IP.To16() != nil {
|
|
|
|
onesv6, _ := iface.AddressIPv6().Mask.Size()
|
|
|
|
networkSettings.Networks[n.Name()].GlobalIPv6Address = iface.AddressIPv6().IP.String()
|
|
|
|
networkSettings.Networks[n.Name()].GlobalIPv6PrefixLen = onesv6
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-23 18:21:41 -04:00
|
|
|
type named interface {
|
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
// UpdateJoinInfo updates network settings when container joins network n with endpoint ep.
|
2017-08-23 18:21:41 -04:00
|
|
|
func (container *Container) UpdateJoinInfo(n named, ep libnetwork.Endpoint) error {
|
2016-03-09 23:33:21 -05:00
|
|
|
if err := container.buildPortMapInfo(ep); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
epInfo := ep.Info()
|
|
|
|
if epInfo == nil {
|
|
|
|
// It is not an error to get an empty endpoint info
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if epInfo.Gateway() != nil {
|
|
|
|
container.NetworkSettings.Networks[n.Name()].Gateway = epInfo.Gateway().String()
|
|
|
|
}
|
|
|
|
if epInfo.GatewayIPv6().To16() != nil {
|
|
|
|
container.NetworkSettings.Networks[n.Name()].IPv6Gateway = epInfo.GatewayIPv6().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateSandboxNetworkSettings updates the sandbox ID and Key.
|
|
|
|
func (container *Container) UpdateSandboxNetworkSettings(sb libnetwork.Sandbox) error {
|
|
|
|
container.NetworkSettings.SandboxID = sb.ID()
|
|
|
|
container.NetworkSettings.SandboxKey = sb.Key()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildJoinOptions builds endpoint Join options from a given network.
|
2017-08-23 18:21:41 -04:00
|
|
|
func (container *Container) BuildJoinOptions(n named) ([]libnetwork.EndpointOption, error) {
|
2016-03-09 23:33:21 -05:00
|
|
|
var joinOptions []libnetwork.EndpointOption
|
|
|
|
if epConfig, ok := container.NetworkSettings.Networks[n.Name()]; ok {
|
|
|
|
for _, str := range epConfig.Links {
|
2016-12-23 14:09:12 -05:00
|
|
|
name, alias, err := opts.ParseLink(str)
|
2016-03-09 23:33:21 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
joinOptions = append(joinOptions, libnetwork.CreateOptionAlias(name, alias))
|
|
|
|
}
|
2017-05-09 19:10:08 -04:00
|
|
|
for k, v := range epConfig.DriverOpts {
|
|
|
|
joinOptions = append(joinOptions, libnetwork.EndpointOptionGeneric(options.Generic{k: v}))
|
|
|
|
}
|
2016-03-09 23:33:21 -05:00
|
|
|
}
|
2017-05-09 19:10:08 -04:00
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
return joinOptions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BuildCreateEndpointOptions builds endpoint options from a given network.
|
2016-09-12 20:17:09 -04:00
|
|
|
func (container *Container) BuildCreateEndpointOptions(n libnetwork.Network, epConfig *networktypes.EndpointSettings, sb libnetwork.Sandbox, daemonDNS []string) ([]libnetwork.EndpointOption, error) {
|
2016-03-09 23:33:21 -05:00
|
|
|
var (
|
|
|
|
bindings = make(nat.PortMap)
|
|
|
|
pbList []types.PortBinding
|
|
|
|
exposeList []types.TransportPort
|
|
|
|
createOptions []libnetwork.EndpointOption
|
|
|
|
)
|
|
|
|
|
|
|
|
defaultNetName := runconfig.DefaultDaemonNetworkMode().NetworkName()
|
|
|
|
|
2016-09-12 20:17:09 -04:00
|
|
|
if (!container.EnableServiceDiscoveryOnDefaultNetwork() && n.Name() == defaultNetName) ||
|
|
|
|
container.NetworkSettings.IsAnonymousEndpoint {
|
2016-03-09 23:33:21 -05:00
|
|
|
createOptions = append(createOptions, libnetwork.CreateOptionAnonymous())
|
|
|
|
}
|
|
|
|
|
|
|
|
if epConfig != nil {
|
|
|
|
ipam := epConfig.IPAMConfig
|
2017-02-07 21:14:11 -05:00
|
|
|
|
|
|
|
if ipam != nil {
|
|
|
|
var (
|
|
|
|
ipList []net.IP
|
|
|
|
ip, ip6, linkip net.IP
|
|
|
|
)
|
|
|
|
|
2016-06-09 18:10:59 -04:00
|
|
|
for _, ips := range ipam.LinkLocalIPs {
|
2017-02-07 21:14:11 -05:00
|
|
|
if linkip = net.ParseIP(ips); linkip == nil && ips != "" {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("Invalid link-local IP address: %s", ipam.LinkLocalIPs)
|
2016-06-09 18:10:59 -04:00
|
|
|
}
|
2017-02-07 21:14:11 -05:00
|
|
|
ipList = append(ipList, linkip)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ip = net.ParseIP(ipam.IPv4Address); ip == nil && ipam.IPv4Address != "" {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("Invalid IPv4 address: %s)", ipam.IPv4Address)
|
2016-06-09 18:10:59 -04:00
|
|
|
}
|
2017-02-07 21:14:11 -05:00
|
|
|
|
|
|
|
if ip6 = net.ParseIP(ipam.IPv6Address); ip6 == nil && ipam.IPv6Address != "" {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Errorf("Invalid IPv6 address: %s)", ipam.IPv6Address)
|
2017-02-07 21:14:11 -05:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
createOptions = append(createOptions,
|
2017-02-07 21:14:11 -05:00
|
|
|
libnetwork.CreateOptionIpam(ip, ip6, ipList, nil))
|
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, alias := range epConfig.Aliases {
|
|
|
|
createOptions = append(createOptions, libnetwork.CreateOptionMyAlias(alias))
|
|
|
|
}
|
2017-07-19 20:14:41 -04:00
|
|
|
for k, v := range epConfig.DriverOpts {
|
|
|
|
createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(options.Generic{k: v}))
|
|
|
|
}
|
2016-03-09 23:33:21 -05:00
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
if container.NetworkSettings.Service != nil {
|
|
|
|
svcCfg := container.NetworkSettings.Service
|
|
|
|
|
|
|
|
var vip string
|
|
|
|
if svcCfg.VirtualAddresses[n.ID()] != nil {
|
|
|
|
vip = svcCfg.VirtualAddresses[n.ID()].IPv4
|
|
|
|
}
|
|
|
|
|
|
|
|
var portConfigs []*libnetwork.PortConfig
|
|
|
|
for _, portConfig := range svcCfg.ExposedPorts {
|
|
|
|
portConfigs = append(portConfigs, &libnetwork.PortConfig{
|
|
|
|
Name: portConfig.Name,
|
|
|
|
Protocol: libnetwork.PortConfig_Protocol(portConfig.Protocol),
|
|
|
|
TargetPort: portConfig.TargetPort,
|
|
|
|
PublishedPort: portConfig.PublishedPort,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-14 17:55:05 -04:00
|
|
|
createOptions = append(createOptions, libnetwork.CreateOptionService(svcCfg.Name, svcCfg.ID, net.ParseIP(vip), portConfigs, svcCfg.Aliases[n.ID()]))
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
if !containertypes.NetworkMode(n.Name()).IsUserDefined() {
|
|
|
|
createOptions = append(createOptions, libnetwork.CreateOptionDisableResolution())
|
|
|
|
}
|
|
|
|
|
|
|
|
// configs that are applicable only for the endpoint in the network
|
|
|
|
// to which container was connected to on docker run.
|
|
|
|
// Ideally all these network-specific endpoint configurations must be moved under
|
|
|
|
// container.NetworkSettings.Networks[n.Name()]
|
|
|
|
if n.Name() == container.HostConfig.NetworkMode.NetworkName() ||
|
|
|
|
(n.Name() == defaultNetName && container.HostConfig.NetworkMode.IsDefault()) {
|
|
|
|
if container.Config.MacAddress != "" {
|
|
|
|
mac, err := net.ParseMAC(container.Config.MacAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
genericOption := options.Generic{
|
|
|
|
netlabel.MacAddress: mac,
|
|
|
|
}
|
|
|
|
|
|
|
|
createOptions = append(createOptions, libnetwork.EndpointOptionGeneric(genericOption))
|
|
|
|
}
|
2017-05-09 19:10:08 -04:00
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Port-mapping rules belong to the container & applicable only to non-internal networks
|
2016-05-25 16:47:38 -04:00
|
|
|
portmaps := GetSandboxPortMapInfo(sb)
|
2016-03-09 23:33:21 -05:00
|
|
|
if n.Info().Internal() || len(portmaps) > 0 {
|
|
|
|
return createOptions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.HostConfig.PortBindings != nil {
|
|
|
|
for p, b := range container.HostConfig.PortBindings {
|
|
|
|
bindings[p] = []nat.PortBinding{}
|
|
|
|
for _, bb := range b {
|
|
|
|
bindings[p] = append(bindings[p], nat.PortBinding{
|
|
|
|
HostIP: bb.HostIP,
|
|
|
|
HostPort: bb.HostPort,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
portSpecs := container.Config.ExposedPorts
|
|
|
|
ports := make([]nat.Port, len(portSpecs))
|
|
|
|
var i int
|
|
|
|
for p := range portSpecs {
|
|
|
|
ports[i] = p
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
nat.SortPortMap(ports, bindings)
|
|
|
|
for _, port := range ports {
|
|
|
|
expose := types.TransportPort{}
|
|
|
|
expose.Proto = types.ParseProtocol(port.Proto())
|
|
|
|
expose.Port = uint16(port.Int())
|
|
|
|
exposeList = append(exposeList, expose)
|
|
|
|
|
|
|
|
pb := types.PortBinding{Port: expose.Port, Proto: expose.Proto}
|
|
|
|
binding := bindings[port]
|
|
|
|
for i := 0; i < len(binding); i++ {
|
|
|
|
pbCopy := pb.GetCopy()
|
|
|
|
newP, err := nat.NewPort(nat.SplitProtoPort(binding[i].HostPort))
|
|
|
|
var portStart, portEnd int
|
|
|
|
if err == nil {
|
|
|
|
portStart, portEnd, err = newP.Range()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, errors.Wrapf(err, "Error parsing HostPort value (%s)", binding[i].HostPort)
|
2016-03-09 23:33:21 -05:00
|
|
|
}
|
|
|
|
pbCopy.HostPort = uint16(portStart)
|
|
|
|
pbCopy.HostPortEnd = uint16(portEnd)
|
|
|
|
pbCopy.HostIP = net.ParseIP(binding[i].HostIP)
|
|
|
|
pbList = append(pbList, pbCopy)
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.HostConfig.PublishAllPorts && len(binding) == 0 {
|
|
|
|
pbList = append(pbList, pb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 20:17:09 -04:00
|
|
|
var dns []string
|
|
|
|
|
|
|
|
if len(container.HostConfig.DNS) > 0 {
|
|
|
|
dns = container.HostConfig.DNS
|
|
|
|
} else if len(daemonDNS) > 0 {
|
|
|
|
dns = daemonDNS
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(dns) > 0 {
|
|
|
|
createOptions = append(createOptions,
|
|
|
|
libnetwork.CreateOptionDNS(dns))
|
|
|
|
}
|
|
|
|
|
2016-03-09 23:33:21 -05:00
|
|
|
createOptions = append(createOptions,
|
|
|
|
libnetwork.CreateOptionPortMapping(pbList),
|
|
|
|
libnetwork.CreateOptionExposedPorts(exposeList))
|
|
|
|
|
|
|
|
return createOptions, nil
|
|
|
|
}
|
|
|
|
|
2016-01-04 10:58:20 -05:00
|
|
|
// UpdateMonitor updates monitor configure for running container
|
|
|
|
func (container *Container) UpdateMonitor(restartPolicy containertypes.RestartPolicy) {
|
2016-03-18 14:50:19 -04:00
|
|
|
type policySetter interface {
|
|
|
|
SetPolicy(containertypes.RestartPolicy)
|
|
|
|
}
|
|
|
|
|
2016-10-05 16:29:56 -04:00
|
|
|
if rm, ok := container.RestartManager().(policySetter); ok {
|
2016-03-18 14:50:19 -04:00
|
|
|
rm.SetPolicy(restartPolicy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FullHostname returns hostname and optional domain appended to it.
|
|
|
|
func (container *Container) FullHostname() string {
|
|
|
|
fullHostname := container.Config.Hostname
|
|
|
|
if container.Config.Domainname != "" {
|
|
|
|
fullHostname = fmt.Sprintf("%s.%s", fullHostname, container.Config.Domainname)
|
2016-01-04 10:58:20 -05:00
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
return fullHostname
|
|
|
|
}
|
2016-01-04 10:58:20 -05:00
|
|
|
|
2016-04-08 10:08:58 -04:00
|
|
|
// RestartManager returns the current restartmanager instance connected to container.
|
2016-10-05 16:29:56 -04:00
|
|
|
func (container *Container) RestartManager() restartmanager.RestartManager {
|
2016-03-18 14:50:19 -04:00
|
|
|
if container.restartManager == nil {
|
2016-03-22 11:46:40 -04:00
|
|
|
container.restartManager = restartmanager.New(container.HostConfig.RestartPolicy, container.RestartCount)
|
2016-01-04 10:58:20 -05:00
|
|
|
}
|
2016-03-18 14:50:19 -04:00
|
|
|
return container.restartManager
|
2016-01-04 10:58:20 -05:00
|
|
|
}
|
2016-03-08 19:54:33 -05:00
|
|
|
|
2016-10-05 16:29:56 -04:00
|
|
|
// ResetRestartManager initializes new restartmanager based on container config
|
|
|
|
func (container *Container) ResetRestartManager(resetCount bool) {
|
|
|
|
if container.restartManager != nil {
|
|
|
|
container.restartManager.Cancel()
|
|
|
|
}
|
|
|
|
if resetCount {
|
|
|
|
container.RestartCount = 0
|
|
|
|
}
|
|
|
|
container.restartManager = nil
|
|
|
|
}
|
|
|
|
|
2016-03-08 19:54:33 -05:00
|
|
|
type attachContext struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
2016-09-04 03:17:58 -04:00
|
|
|
// InitAttachContext initializes or returns existing context for attach calls to
|
2016-03-08 19:54:33 -05:00
|
|
|
// track container liveness.
|
|
|
|
func (container *Container) InitAttachContext() context.Context {
|
|
|
|
container.attachContext.mu.Lock()
|
|
|
|
defer container.attachContext.mu.Unlock()
|
|
|
|
if container.attachContext.ctx == nil {
|
|
|
|
container.attachContext.ctx, container.attachContext.cancel = context.WithCancel(context.Background())
|
|
|
|
}
|
|
|
|
return container.attachContext.ctx
|
|
|
|
}
|
|
|
|
|
2016-09-04 03:17:58 -04:00
|
|
|
// CancelAttachContext cancels attach context. All attach calls should detach
|
2016-03-08 19:54:33 -05:00
|
|
|
// after this call.
|
|
|
|
func (container *Container) CancelAttachContext() {
|
|
|
|
container.attachContext.mu.Lock()
|
|
|
|
if container.attachContext.ctx != nil {
|
|
|
|
container.attachContext.cancel()
|
|
|
|
container.attachContext.ctx = nil
|
|
|
|
}
|
|
|
|
container.attachContext.mu.Unlock()
|
|
|
|
}
|
2016-10-17 17:39:52 -04:00
|
|
|
|
|
|
|
func (container *Container) startLogging() error {
|
|
|
|
if container.HostConfig.LogConfig.Type == "none" {
|
|
|
|
return nil // do not start logging routines
|
|
|
|
}
|
|
|
|
|
2016-11-22 06:40:54 -05:00
|
|
|
l, err := container.StartLogger()
|
2016-10-17 17:39:52 -04:00
|
|
|
if err != nil {
|
2016-11-22 06:40:54 -05:00
|
|
|
return fmt.Errorf("failed to initialize logging driver: %v", err)
|
2016-10-17 17:39:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
copier := logger.NewCopier(map[string]io.Reader{"stdout": container.StdoutPipe(), "stderr": container.StderrPipe()}, l)
|
|
|
|
container.LogCopier = copier
|
|
|
|
copier.Run()
|
|
|
|
container.LogDriver = l
|
|
|
|
|
|
|
|
// set LogPath field only for json-file logdriver
|
|
|
|
if jl, ok := l.(*jsonfilelog.JSONFileLogger); ok {
|
|
|
|
container.LogPath = jl.LogPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-14 15:15:09 -05:00
|
|
|
// StdinPipe gets the stdin stream of the container
|
|
|
|
func (container *Container) StdinPipe() io.WriteCloser {
|
|
|
|
return container.StreamConfig.StdinPipe()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StdoutPipe gets the stdout stream of the container
|
|
|
|
func (container *Container) StdoutPipe() io.ReadCloser {
|
|
|
|
return container.StreamConfig.StdoutPipe()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StderrPipe gets the stderr stream of the container
|
|
|
|
func (container *Container) StderrPipe() io.ReadCloser {
|
|
|
|
return container.StreamConfig.StderrPipe()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseStreams closes the container's stdio streams
|
|
|
|
func (container *Container) CloseStreams() error {
|
|
|
|
return container.StreamConfig.CloseStreams()
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:39:52 -04:00
|
|
|
// InitializeStdio is called by libcontainerd to connect the stdio.
|
2017-12-07 14:26:27 -05:00
|
|
|
func (container *Container) InitializeStdio(iop *cio.DirectIO) (cio.IO, error) {
|
2016-10-17 17:39:52 -04:00
|
|
|
if err := container.startLogging(); err != nil {
|
|
|
|
container.Reset(false)
|
2017-09-22 09:52:41 -04:00
|
|
|
return nil, err
|
2016-10-17 17:39:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
container.StreamConfig.CopyToPipe(iop)
|
|
|
|
|
2016-11-14 15:15:09 -05:00
|
|
|
if container.StreamConfig.Stdin() == nil && !container.Config.Tty {
|
2016-10-17 17:39:52 -04:00
|
|
|
if iop.Stdin != nil {
|
|
|
|
if err := iop.Stdin.Close(); err != nil {
|
2016-10-25 21:34:35 -04:00
|
|
|
logrus.Warnf("error closing stdin: %+v", err)
|
2016-10-17 17:39:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
return &rio{IO: iop, sc: container.StreamConfig}, nil
|
2016-10-17 17:39:52 -04:00
|
|
|
}
|
2017-04-11 13:34:19 -04:00
|
|
|
|
2017-12-18 16:02:23 -05:00
|
|
|
// MountsResourcePath returns the path where mounts are stored for the given mount
|
|
|
|
func (container *Container) MountsResourcePath(mount string) (string, error) {
|
|
|
|
return container.GetRootResourcePath(filepath.Join("mounts", mount))
|
|
|
|
}
|
|
|
|
|
2017-04-11 13:34:19 -04:00
|
|
|
// SecretMountPath returns the path of the secret mount for the container
|
2017-12-18 16:02:23 -05:00
|
|
|
func (container *Container) SecretMountPath() (string, error) {
|
|
|
|
return container.MountsResourcePath("secrets")
|
2017-04-11 13:34:19 -04:00
|
|
|
}
|
|
|
|
|
2017-04-28 14:48:52 -04:00
|
|
|
// SecretFilePath returns the path to the location of a secret on the host.
|
2017-12-18 16:02:23 -05:00
|
|
|
func (container *Container) SecretFilePath(secretRef swarmtypes.SecretReference) (string, error) {
|
|
|
|
secrets, err := container.SecretMountPath()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(secrets, secretRef.SecretID), nil
|
2017-04-11 13:34:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func getSecretTargetPath(r *swarmtypes.SecretReference) string {
|
|
|
|
if filepath.IsAbs(r.File.Name) {
|
|
|
|
return r.File.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(containerSecretMountPath, r.File.Name)
|
|
|
|
}
|
2017-03-16 17:23:33 -04:00
|
|
|
|
|
|
|
// ConfigsDirPath returns the path to the directory where configs are stored on
|
|
|
|
// disk.
|
2017-12-18 16:02:23 -05:00
|
|
|
func (container *Container) ConfigsDirPath() (string, error) {
|
|
|
|
return container.GetRootResourcePath("configs")
|
2017-03-16 17:23:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigFilePath returns the path to the on-disk location of a config.
|
2017-12-18 16:02:23 -05:00
|
|
|
func (container *Container) ConfigFilePath(configRef swarmtypes.ConfigReference) (string, error) {
|
|
|
|
configs, err := container.ConfigsDirPath()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(configs, configRef.ConfigID), nil
|
2017-03-16 17:23:33 -04:00
|
|
|
}
|
2017-05-26 19:14:18 -04:00
|
|
|
|
|
|
|
// CreateDaemonEnvironment creates a new environment variable slice for this container.
|
|
|
|
func (container *Container) CreateDaemonEnvironment(tty bool, linkedEnv []string) []string {
|
|
|
|
// Setup environment
|
2017-08-08 15:43:48 -04:00
|
|
|
os := container.OS
|
|
|
|
if os == "" {
|
|
|
|
os = runtime.GOOS
|
2017-05-26 19:14:18 -04:00
|
|
|
}
|
|
|
|
env := []string{}
|
2017-08-08 15:43:48 -04:00
|
|
|
if runtime.GOOS != "windows" || (runtime.GOOS == "windows" && os == "linux") {
|
2017-05-26 19:14:18 -04:00
|
|
|
env = []string{
|
2017-08-08 15:43:48 -04:00
|
|
|
"PATH=" + system.DefaultPathEnv(os),
|
2017-05-26 19:14:18 -04:00
|
|
|
"HOSTNAME=" + container.Config.Hostname,
|
|
|
|
}
|
|
|
|
if tty {
|
|
|
|
env = append(env, "TERM=xterm")
|
|
|
|
}
|
|
|
|
env = append(env, linkedEnv...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// because the env on the container can override certain default values
|
|
|
|
// we need to replace the 'env' keys where they match and append anything
|
|
|
|
// else.
|
2017-06-26 12:11:54 -04:00
|
|
|
env = ReplaceOrAppendEnvValues(env, container.Config.Env)
|
|
|
|
return env
|
2017-05-26 19:14:18 -04:00
|
|
|
}
|
2017-09-22 09:52:41 -04:00
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
type rio struct {
|
|
|
|
cio.IO
|
2017-09-22 09:52:41 -04:00
|
|
|
|
|
|
|
sc *stream.Config
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
func (i *rio) Close() error {
|
2017-09-22 09:52:41 -04:00
|
|
|
i.IO.Close()
|
|
|
|
|
|
|
|
return i.sc.CloseStreams()
|
|
|
|
}
|
|
|
|
|
2017-11-29 19:15:20 -05:00
|
|
|
func (i *rio) Wait() {
|
2017-09-22 09:52:41 -04:00
|
|
|
i.sc.Wait()
|
|
|
|
|
|
|
|
i.IO.Wait()
|
|
|
|
}
|