2024-01-25 10:18:44 +00:00
|
|
|
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
|
|
|
|
//go:build go1.19
|
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2014-05-20 19:36:15 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-06 15:02:37 +00:00
|
|
|
"context"
|
2018-02-07 23:25:50 +00:00
|
|
|
"errors"
|
2014-06-25 22:24:14 +00:00
|
|
|
"fmt"
|
2015-07-26 13:00:53 +00:00
|
|
|
"time"
|
2014-05-20 19:36:15 +00:00
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-02-10 20:16:59 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2024-01-23 09:40:54 +00:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2016-09-06 18:18:12 +00:00
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
|
|
|
"github.com/docker/docker/api/types/versions"
|
2015-11-12 19:55:17 +00:00
|
|
|
"github.com/docker/docker/container"
|
2022-08-17 21:13:49 +00:00
|
|
|
"github.com/docker/docker/daemon/config"
|
2015-10-27 02:35:49 +00:00
|
|
|
"github.com/docker/docker/daemon/network"
|
2018-01-11 19:53:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2023-11-29 22:02:56 +00:00
|
|
|
"github.com/docker/docker/internal/sliceutil"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-02-01 02:03:51 +00:00
|
|
|
"github.com/docker/go-connections/nat"
|
2014-05-20 19:36:15 +00:00
|
|
|
)
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// ContainerInspect returns low-level information about a
|
|
|
|
// container. Returns an error if the container cannot be found, or if
|
|
|
|
// there is an error getting the data.
|
2023-03-06 15:02:37 +00:00
|
|
|
func (daemon *Daemon) ContainerInspect(ctx context.Context, name string, size bool, version string) (interface{}, error) {
|
2015-11-24 17:55:45 +00:00
|
|
|
switch {
|
2023-11-29 22:02:56 +00:00
|
|
|
case versions.LessThan(version, "1.45"):
|
|
|
|
ctr, err := daemon.ContainerInspectCurrent(ctx, name, size)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
shortCID := stringid.TruncateID(ctr.ID)
|
2024-01-23 09:40:54 +00:00
|
|
|
for nwName, ep := range ctr.NetworkSettings.Networks {
|
|
|
|
if containertypes.NetworkMode(nwName).IsUserDefined() {
|
|
|
|
ep.Aliases = sliceutil.Dedup(append(ep.Aliases, shortCID, ctr.Config.Hostname))
|
|
|
|
}
|
2023-11-29 22:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctr, nil
|
2023-07-04 10:40:35 +00:00
|
|
|
default:
|
|
|
|
return daemon.ContainerInspectCurrent(ctx, name, size)
|
2015-11-24 17:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-14 02:52:49 +00:00
|
|
|
// ContainerInspectCurrent returns low-level information about a
|
|
|
|
// container in a most recent api version.
|
2023-03-06 15:02:37 +00:00
|
|
|
func (daemon *Daemon) ContainerInspectCurrent(ctx context.Context, name string, size bool) (*types.ContainerJSON, error) {
|
2019-08-09 12:10:07 +00:00
|
|
|
ctr, err := daemon.GetContainer(name)
|
2015-04-13 14:17:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-12-16 23:06:35 +00:00
|
|
|
}
|
2014-05-31 01:13:37 +00:00
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
ctr.Lock()
|
2014-06-25 22:24:14 +00:00
|
|
|
|
2022-08-31 20:12:30 +00:00
|
|
|
base, err := daemon.getInspectData(&daemon.config().Config, ctr)
|
2015-06-02 21:37:59 +00:00
|
|
|
if err != nil {
|
2019-08-09 12:10:07 +00:00
|
|
|
ctr.Unlock()
|
2015-06-02 21:37:59 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-23 23:50:15 +00:00
|
|
|
apiNetworks := make(map[string]*networktypes.EndpointSettings)
|
2022-12-28 10:23:06 +00:00
|
|
|
for nwName, epConf := range ctr.NetworkSettings.Networks {
|
2016-08-23 23:50:15 +00:00
|
|
|
if epConf.EndpointSettings != nil {
|
2017-02-01 02:03:51 +00:00
|
|
|
// We must make a copy of this pointer object otherwise it can race with other operations
|
2022-12-28 10:23:06 +00:00
|
|
|
apiNetworks[nwName] = epConf.EndpointSettings.Copy()
|
2016-08-23 23:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
mountPoints := ctr.GetMountPoints()
|
2015-10-27 02:35:49 +00:00
|
|
|
networkSettings := &types.NetworkSettings{
|
2015-10-30 18:57:15 +00:00
|
|
|
NetworkSettingsBase: types.NetworkSettingsBase{
|
2019-08-09 12:10:07 +00:00
|
|
|
Bridge: ctr.NetworkSettings.Bridge,
|
|
|
|
SandboxID: ctr.NetworkSettings.SandboxID,
|
2023-08-12 12:23:20 +00:00
|
|
|
SandboxKey: ctr.NetworkSettings.SandboxKey,
|
2019-08-09 12:10:07 +00:00
|
|
|
HairpinMode: ctr.NetworkSettings.HairpinMode,
|
|
|
|
LinkLocalIPv6Address: ctr.NetworkSettings.LinkLocalIPv6Address,
|
|
|
|
LinkLocalIPv6PrefixLen: ctr.NetworkSettings.LinkLocalIPv6PrefixLen,
|
|
|
|
SecondaryIPAddresses: ctr.NetworkSettings.SecondaryIPAddresses,
|
|
|
|
SecondaryIPv6Addresses: ctr.NetworkSettings.SecondaryIPv6Addresses,
|
2015-10-27 02:35:49 +00:00
|
|
|
},
|
2019-08-09 12:10:07 +00:00
|
|
|
DefaultNetworkSettings: daemon.getDefaultNetworkSettings(ctr.NetworkSettings.Networks),
|
2016-08-23 23:50:15 +00:00
|
|
|
Networks: apiNetworks,
|
2015-10-27 02:35:49 +00:00
|
|
|
}
|
2015-06-03 19:21:38 +00:00
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
ports := make(nat.PortMap, len(ctr.NetworkSettings.Ports))
|
|
|
|
for k, pm := range ctr.NetworkSettings.Ports {
|
2017-02-01 02:03:51 +00:00
|
|
|
ports[k] = pm
|
|
|
|
}
|
|
|
|
networkSettings.NetworkSettingsBase.Ports = ports
|
|
|
|
|
2019-08-09 12:10:07 +00:00
|
|
|
ctr.Unlock()
|
2017-02-19 02:11:48 +00:00
|
|
|
|
|
|
|
if size {
|
2023-03-06 15:02:37 +00:00
|
|
|
sizeRw, sizeRootFs, err := daemon.imageService.GetContainerLayerSize(ctx, base.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-19 02:11:48 +00:00
|
|
|
base.SizeRw = &sizeRw
|
|
|
|
base.SizeRootFs = &sizeRootFs
|
|
|
|
}
|
|
|
|
|
2015-11-02 16:28:34 +00:00
|
|
|
return &types.ContainerJSON{
|
|
|
|
ContainerJSONBase: base,
|
|
|
|
Mounts: mountPoints,
|
2019-08-09 12:10:07 +00:00
|
|
|
Config: ctr.Config,
|
2015-11-02 16:28:34 +00:00
|
|
|
NetworkSettings: networkSettings,
|
|
|
|
}, nil
|
2015-06-02 21:37:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-17 21:13:49 +00:00
|
|
|
func (daemon *Daemon) getInspectData(daemonCfg *config.Config, container *container.Container) (*types.ContainerJSONBase, error) {
|
2015-04-13 14:17:14 +00:00
|
|
|
// make a copy to play with
|
2015-11-12 19:55:17 +00:00
|
|
|
hostConfig := *container.HostConfig
|
2014-12-23 22:03:20 +00:00
|
|
|
|
2015-09-04 00:51:04 +00:00
|
|
|
children := daemon.children(container)
|
|
|
|
hostConfig.Links = nil // do not expose the internal structure
|
|
|
|
for linkAlias, child := range children {
|
|
|
|
hostConfig.Links = append(hostConfig.Links, fmt.Sprintf("%s:%s", child.Name, linkAlias))
|
2014-12-16 23:06:35 +00:00
|
|
|
}
|
2015-09-04 00:51:04 +00:00
|
|
|
|
2016-09-08 04:23:56 +00:00
|
|
|
// We merge the Ulimits from hostConfig with daemon default
|
2022-08-17 21:13:49 +00:00
|
|
|
daemon.mergeUlimits(&hostConfig, daemonCfg)
|
2016-09-08 04:23:56 +00:00
|
|
|
|
2023-07-04 10:40:35 +00:00
|
|
|
// Migrate the container's default network's MacAddress to the top-level
|
|
|
|
// Config.MacAddress field for older API versions (< 1.44). We set it here
|
|
|
|
// unconditionally, to keep backward compatibility with clients that use
|
|
|
|
// unversioned API endpoints.
|
|
|
|
if container.Config != nil && container.Config.MacAddress == "" { //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
|
2024-02-22 16:35:11 +00:00
|
|
|
if nwm := hostConfig.NetworkMode; nwm.IsBridge() || nwm.IsUserDefined() {
|
|
|
|
if epConf, ok := container.NetworkSettings.Networks[nwm.NetworkName()]; ok {
|
2024-01-29 18:38:05 +00:00
|
|
|
container.Config.MacAddress = epConf.DesiredMacAddress //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
|
2023-07-04 10:40:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 09:48:13 +00:00
|
|
|
var containerHealth *types.Health
|
|
|
|
if container.State.Health != nil {
|
|
|
|
containerHealth = &types.Health{
|
2017-11-16 03:28:36 +00:00
|
|
|
Status: container.State.Health.Status(),
|
2016-04-18 09:48:13 +00:00
|
|
|
FailingStreak: container.State.Health.FailingStreak,
|
|
|
|
Log: append([]*types.HealthcheckResult{}, container.State.Health.Log...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 14:17:14 +00:00
|
|
|
containerState := &types.ContainerState{
|
2015-07-28 00:48:27 +00:00
|
|
|
Status: container.State.StateString(),
|
2015-04-13 14:17:14 +00:00
|
|
|
Running: container.State.Running,
|
|
|
|
Paused: container.State.Paused,
|
|
|
|
Restarting: container.State.Restarting,
|
|
|
|
OOMKilled: container.State.OOMKilled,
|
|
|
|
Dead: container.State.Dead,
|
|
|
|
Pid: container.State.Pid,
|
2016-06-14 18:11:43 +00:00
|
|
|
ExitCode: container.State.ExitCode(),
|
2017-03-31 03:01:41 +00:00
|
|
|
Error: container.State.ErrorMsg,
|
2015-07-26 13:00:53 +00:00
|
|
|
StartedAt: container.State.StartedAt.Format(time.RFC3339Nano),
|
|
|
|
FinishedAt: container.State.FinishedAt.Format(time.RFC3339Nano),
|
2016-04-18 09:48:13 +00:00
|
|
|
Health: containerHealth,
|
2015-04-13 14:17:14 +00:00
|
|
|
}
|
2014-06-25 22:24:14 +00:00
|
|
|
|
2015-06-02 21:37:59 +00:00
|
|
|
contJSONBase := &types.ContainerJSONBase{
|
2015-10-27 02:35:49 +00:00
|
|
|
ID: container.ID,
|
|
|
|
Created: container.Created.Format(time.RFC3339Nano),
|
|
|
|
Path: container.Path,
|
|
|
|
Args: container.Args,
|
|
|
|
State: containerState,
|
2015-11-18 22:20:54 +00:00
|
|
|
Image: container.ImageID.String(),
|
2015-10-27 02:35:49 +00:00
|
|
|
LogPath: container.LogPath,
|
|
|
|
Name: container.Name,
|
|
|
|
RestartCount: container.RestartCount,
|
|
|
|
Driver: container.Driver,
|
2017-10-09 20:47:28 +00:00
|
|
|
Platform: container.OS,
|
2015-10-27 02:35:49 +00:00
|
|
|
MountLabel: container.MountLabel,
|
|
|
|
ProcessLabel: container.ProcessLabel,
|
2015-11-12 19:55:17 +00:00
|
|
|
ExecIDs: container.GetExecIDs(),
|
2015-10-27 02:35:49 +00:00
|
|
|
HostConfig: &hostConfig,
|
2014-05-20 19:36:15 +00:00
|
|
|
}
|
2015-04-13 14:17:14 +00:00
|
|
|
|
2015-07-16 21:14:58 +00:00
|
|
|
// Now set any platform-specific fields
|
|
|
|
contJSONBase = setPlatformSpecificContainerFields(container, contJSONBase)
|
|
|
|
|
2015-06-15 18:05:10 +00:00
|
|
|
contJSONBase.GraphDriver.Name = container.Driver
|
2015-11-18 22:20:54 +00:00
|
|
|
|
2022-08-01 12:38:45 +00:00
|
|
|
if daemon.UsesSnapshotter() {
|
|
|
|
// Additional information only applies to graphDrivers, so we're done.
|
|
|
|
return contJSONBase, nil
|
|
|
|
}
|
|
|
|
|
2018-02-07 23:25:50 +00:00
|
|
|
if container.RWLayer == nil {
|
|
|
|
if container.Dead {
|
|
|
|
return contJSONBase, nil
|
|
|
|
}
|
|
|
|
return nil, errdefs.System(errors.New("RWLayer of container " + container.ID + " is unexpectedly nil"))
|
|
|
|
}
|
|
|
|
|
2015-12-16 22:13:50 +00:00
|
|
|
graphDriverData, err := container.RWLayer.Metadata()
|
2018-02-07 23:25:50 +00:00
|
|
|
if err != nil {
|
2022-08-24 16:20:29 +00:00
|
|
|
if container.Dead {
|
|
|
|
// container is marked as Dead, and its graphDriver metadata may
|
|
|
|
// have been removed; we can ignore errors.
|
|
|
|
return contJSONBase, nil
|
2018-02-07 23:25:50 +00:00
|
|
|
}
|
2022-08-24 16:20:29 +00:00
|
|
|
return nil, errdefs.System(err)
|
2015-06-15 18:05:10 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:20:29 +00:00
|
|
|
contJSONBase.GraphDriver.Data = graphDriverData
|
2015-06-02 21:37:59 +00:00
|
|
|
return contJSONBase, nil
|
2014-05-20 19:36:15 +00:00
|
|
|
}
|
2014-11-17 23:50:09 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// ContainerExecInspect returns low-level information about the exec
|
|
|
|
// command. An error is returned if the exec cannot be found.
|
2016-02-10 20:16:59 +00:00
|
|
|
func (daemon *Daemon) ContainerExecInspect(id string) (*backend.ExecInspect, error) {
|
2017-04-27 15:26:51 +00:00
|
|
|
e := daemon.execCommands.Get(id)
|
|
|
|
if e == nil {
|
|
|
|
return nil, errExecNotFound(id)
|
|
|
|
}
|
|
|
|
|
2022-05-10 19:59:00 +00:00
|
|
|
if ctr := daemon.containers.Get(e.Container.ID); ctr == nil {
|
2017-04-27 15:26:51 +00:00
|
|
|
return nil, errExecNotFound(id)
|
2014-11-17 23:50:09 +00:00
|
|
|
}
|
2016-02-10 20:16:59 +00:00
|
|
|
|
2022-08-24 23:35:07 +00:00
|
|
|
e.Lock()
|
|
|
|
defer e.Unlock()
|
2016-02-10 20:16:59 +00:00
|
|
|
pc := inspectExecProcessConfig(e)
|
2022-05-10 19:59:00 +00:00
|
|
|
var pid int
|
|
|
|
if e.Process != nil {
|
|
|
|
pid = int(e.Process.Pid())
|
|
|
|
}
|
2016-02-10 20:16:59 +00:00
|
|
|
|
|
|
|
return &backend.ExecInspect{
|
|
|
|
ID: e.ID,
|
|
|
|
Running: e.Running,
|
|
|
|
ExitCode: e.ExitCode,
|
|
|
|
ProcessConfig: pc,
|
|
|
|
OpenStdin: e.OpenStdin,
|
|
|
|
OpenStdout: e.OpenStdout,
|
|
|
|
OpenStderr: e.OpenStderr,
|
|
|
|
CanRemove: e.CanRemove,
|
2022-05-10 19:59:00 +00:00
|
|
|
ContainerID: e.Container.ID,
|
2016-02-10 20:16:59 +00:00
|
|
|
DetachKeys: e.DetachKeys,
|
2022-05-10 19:59:00 +00:00
|
|
|
Pid: pid,
|
2016-02-10 20:16:59 +00:00
|
|
|
}, nil
|
2014-11-17 23:50:09 +00:00
|
|
|
}
|
2015-06-12 13:25:32 +00:00
|
|
|
|
2015-10-30 18:57:15 +00:00
|
|
|
// getDefaultNetworkSettings creates the deprecated structure that holds the information
|
|
|
|
// about the bridge network for a container.
|
2016-08-23 23:50:15 +00:00
|
|
|
func (daemon *Daemon) getDefaultNetworkSettings(networks map[string]*network.EndpointSettings) types.DefaultNetworkSettings {
|
2015-10-30 18:57:15 +00:00
|
|
|
var settings types.DefaultNetworkSettings
|
|
|
|
|
2023-09-10 11:33:21 +00:00
|
|
|
if defaultNetwork, ok := networks[networktypes.NetworkBridge]; ok && defaultNetwork.EndpointSettings != nil {
|
2015-10-30 18:57:15 +00:00
|
|
|
settings.EndpointID = defaultNetwork.EndpointID
|
|
|
|
settings.Gateway = defaultNetwork.Gateway
|
|
|
|
settings.GlobalIPv6Address = defaultNetwork.GlobalIPv6Address
|
|
|
|
settings.GlobalIPv6PrefixLen = defaultNetwork.GlobalIPv6PrefixLen
|
|
|
|
settings.IPAddress = defaultNetwork.IPAddress
|
|
|
|
settings.IPPrefixLen = defaultNetwork.IPPrefixLen
|
|
|
|
settings.IPv6Gateway = defaultNetwork.IPv6Gateway
|
|
|
|
settings.MacAddress = defaultNetwork.MacAddress
|
|
|
|
}
|
|
|
|
return settings
|
|
|
|
}
|