2015-07-21 18:04:41 +02:00
|
|
|
package container
|
2015-07-14 22:51:12 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-10-17 06:53:30 +02:00
|
|
|
"strconv"
|
2015-07-14 22:51:12 +02:00
|
|
|
"strings"
|
2019-04-20 16:44:41 +02:00
|
|
|
|
2019-11-25 22:11:12 +02:00
|
|
|
"github.com/containrrr/watchtower/internal/util"
|
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
dockercontainer "github.com/docker/docker/api/types/container"
|
2015-07-14 22:51:12 +02:00
|
|
|
)
|
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// NewContainer returns a new Container instance instantiated with the
|
|
|
|
// specified ContainerInfo and ImageInfo structs.
|
2016-10-13 22:34:24 +02:00
|
|
|
func NewContainer(containerInfo *types.ContainerJSON, imageInfo *types.ImageInspect) *Container {
|
2015-07-21 21:37:18 +02:00
|
|
|
return &Container{
|
|
|
|
containerInfo: containerInfo,
|
|
|
|
imageInfo: imageInfo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// Container represents a running Docker container.
|
2015-07-14 22:51:12 +02:00
|
|
|
type Container struct {
|
2019-07-27 01:37:16 +02:00
|
|
|
Linked bool
|
|
|
|
Stale bool
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2016-10-13 22:34:24 +02:00
|
|
|
containerInfo *types.ContainerJSON
|
|
|
|
imageInfo *types.ImageInspect
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2020-01-12 00:35:25 +02:00
|
|
|
// ContainerInfo fetches JSON info for the container
|
|
|
|
func (c Container) ContainerInfo() *types.ContainerJSON {
|
|
|
|
return c.containerInfo
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// ID returns the Docker container ID.
|
2015-07-23 00:52:22 +02:00
|
|
|
func (c Container) ID() string {
|
2016-10-13 22:34:24 +02:00
|
|
|
return c.containerInfo.ID
|
2015-07-23 00:52:22 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 09:29:52 +02:00
|
|
|
// IsRunning returns a boolean flag indicating whether or not the current
|
|
|
|
// container is running. The status is determined by the value of the
|
|
|
|
// container's "State.Running" property.
|
|
|
|
func (c Container) IsRunning() bool {
|
|
|
|
return c.containerInfo.State.Running
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// Name returns the Docker container name.
|
2015-07-14 22:51:12 +02:00
|
|
|
func (c Container) Name() string {
|
|
|
|
return c.containerInfo.Name
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// ImageID returns the ID of the Docker image that was used to start the
|
|
|
|
// container.
|
2015-07-31 20:24:27 +02:00
|
|
|
func (c Container) ImageID() string {
|
2016-10-13 22:34:24 +02:00
|
|
|
return c.imageInfo.ID
|
2015-07-31 20:24:27 +02:00
|
|
|
}
|
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// ImageName returns the name of the Docker image that was used to start the
|
|
|
|
// container. If the original image was specified without a particular tag, the
|
|
|
|
// "latest" tag is assumed.
|
2015-07-23 00:52:22 +02:00
|
|
|
func (c Container) ImageName() string {
|
2015-08-12 22:49:45 +02:00
|
|
|
// Compatibility w/ Zodiac deployments
|
2019-07-27 01:37:16 +02:00
|
|
|
imageName, ok := c.getLabelValue(zodiacLabel)
|
2015-08-12 22:49:45 +02:00
|
|
|
if !ok {
|
|
|
|
imageName = c.containerInfo.Config.Image
|
|
|
|
}
|
2015-07-23 00:52:22 +02:00
|
|
|
|
|
|
|
if !strings.Contains(imageName, ":") {
|
|
|
|
imageName = fmt.Sprintf("%s:latest", imageName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageName
|
|
|
|
}
|
|
|
|
|
2017-10-17 06:53:30 +02:00
|
|
|
// Enabled returns the value of the container enabled label and if the label
|
|
|
|
// was set.
|
|
|
|
func (c Container) Enabled() (bool, bool) {
|
2019-07-27 01:37:16 +02:00
|
|
|
rawBool, ok := c.getLabelValue(enableLabel)
|
2017-10-17 06:53:30 +02:00
|
|
|
if !ok {
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedBool, err := strconv.ParseBool(rawBool)
|
|
|
|
if err != nil {
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedBool, true
|
|
|
|
}
|
|
|
|
|
2020-10-03 22:00:02 +02:00
|
|
|
// IsMonitorOnly returns the value of the monitor-only label. If the label
|
|
|
|
// is not set then false is returned.
|
|
|
|
func (c Container) IsMonitorOnly() bool {
|
|
|
|
rawBool, ok := c.getLabelValue(monitorOnlyLabel)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedBool, err := strconv.ParseBool(rawBool)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedBool
|
|
|
|
}
|
|
|
|
|
2020-08-21 20:13:47 +02:00
|
|
|
// Scope returns the value of the scope UID label and if the label
|
|
|
|
// was set.
|
|
|
|
func (c Container) Scope() (string, bool) {
|
|
|
|
rawString, ok := c.getLabelValue(scope)
|
|
|
|
if !ok {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
return rawString, true
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// Links returns a list containing the names of all the containers to which
|
|
|
|
// this container is linked.
|
2015-07-14 22:51:12 +02:00
|
|
|
func (c Container) Links() []string {
|
2015-07-21 23:40:22 +02:00
|
|
|
var links []string
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2020-04-24 13:41:04 +02:00
|
|
|
dependsOnLabelValue := c.getLabelValueOrEmpty(dependsOnLabel)
|
|
|
|
|
|
|
|
if dependsOnLabelValue != "" {
|
|
|
|
links := strings.Split(dependsOnLabelValue, ",")
|
|
|
|
return links
|
|
|
|
}
|
|
|
|
|
2015-07-14 22:51:12 +02:00
|
|
|
if (c.containerInfo != nil) && (c.containerInfo.HostConfig != nil) {
|
|
|
|
for _, link := range c.containerInfo.HostConfig.Links {
|
|
|
|
name := strings.Split(link, ":")[0]
|
|
|
|
links = append(links, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return links
|
|
|
|
}
|
|
|
|
|
2019-07-27 01:37:16 +02:00
|
|
|
// ToRestart return whether the container should be restarted, either because
|
|
|
|
// is stale or linked to another stale container.
|
|
|
|
func (c Container) ToRestart() bool {
|
|
|
|
return c.Stale || c.Linked
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// IsWatchtower returns a boolean flag indicating whether or not the current
|
|
|
|
// container is the watchtower container itself. The watchtower container is
|
|
|
|
// identified by the presence of the "com.centurylinklabs.watchtower" label in
|
|
|
|
// the container metadata.
|
2015-07-21 00:54:18 +02:00
|
|
|
func (c Container) IsWatchtower() bool {
|
2019-04-20 16:44:41 +02:00
|
|
|
return ContainsWatchtowerLabel(c.containerInfo.Config.Labels)
|
2015-07-21 00:54:18 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 22:11:12 +02:00
|
|
|
// PreUpdateTimeout checks whether a container has a specific timeout set
|
|
|
|
// for how long the pre-update command is allowed to run. This value is expressed
|
2020-03-28 20:48:04 +02:00
|
|
|
// either as an integer, in minutes, or as 0 which will allow the command/script
|
|
|
|
// to run indefinitely. Users should be cautious with the 0 option, as that
|
2019-11-25 22:11:12 +02:00
|
|
|
// could result in watchtower waiting forever.
|
|
|
|
func (c Container) PreUpdateTimeout() int {
|
|
|
|
var minutes int
|
|
|
|
var err error
|
|
|
|
|
|
|
|
val := c.getLabelValueOrEmpty(preUpdateTimeoutLabel)
|
|
|
|
|
|
|
|
minutes, err = strconv.Atoi(val)
|
|
|
|
if err != nil || val == "" {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return minutes
|
|
|
|
}
|
|
|
|
|
2015-08-01 00:23:17 +02:00
|
|
|
// StopSignal returns the custom stop signal (if any) that is encoded in the
|
|
|
|
// container's metadata. If the container has not specified a custom stop
|
|
|
|
// signal, the empty string "" is returned.
|
2015-07-23 00:52:22 +02:00
|
|
|
func (c Container) StopSignal() string {
|
2019-07-27 01:37:16 +02:00
|
|
|
return c.getLabelValueOrEmpty(signalLabel)
|
2015-07-23 00:52:22 +02:00
|
|
|
}
|
|
|
|
|
2015-07-14 22:51:12 +02:00
|
|
|
// Ideally, we'd just be able to take the ContainerConfig from the old container
|
|
|
|
// and use it as the starting point for creating the new container; however,
|
|
|
|
// the ContainerConfig that comes back from the Inspect call merges the default
|
|
|
|
// configuration (the stuff specified in the metadata for the image itself)
|
|
|
|
// with the overridden configuration (the stuff that you might specify as part
|
|
|
|
// of the "docker run"). In order to avoid unintentionally overriding the
|
|
|
|
// defaults in the new image we need to separate the override options from the
|
|
|
|
// default options. To do this we have to compare the ContainerConfig for the
|
|
|
|
// running container with the ContainerConfig from the image that container was
|
|
|
|
// started from. This function returns a ContainerConfig which contains just
|
|
|
|
// the options overridden at runtime.
|
2016-10-13 22:34:24 +02:00
|
|
|
func (c Container) runtimeConfig() *dockercontainer.Config {
|
2015-07-14 22:51:12 +02:00
|
|
|
config := c.containerInfo.Config
|
2020-02-07 22:23:49 +02:00
|
|
|
hostConfig := c.containerInfo.HostConfig
|
2015-07-14 22:51:12 +02:00
|
|
|
imageConfig := c.imageInfo.Config
|
|
|
|
|
|
|
|
if config.WorkingDir == imageConfig.WorkingDir {
|
|
|
|
config.WorkingDir = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.User == imageConfig.User {
|
|
|
|
config.User = ""
|
|
|
|
}
|
|
|
|
|
2020-02-07 22:23:49 +02:00
|
|
|
if hostConfig.NetworkMode.IsContainer() {
|
|
|
|
config.Hostname = ""
|
|
|
|
}
|
|
|
|
|
2019-07-21 19:58:19 +02:00
|
|
|
if util.SliceEqual(config.Entrypoint, imageConfig.Entrypoint) {
|
2015-07-21 00:54:18 +02:00
|
|
|
config.Entrypoint = nil
|
2019-12-19 01:40:02 +02:00
|
|
|
if util.SliceEqual(config.Cmd, imageConfig.Cmd) {
|
|
|
|
config.Cmd = nil
|
|
|
|
}
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
2019-07-21 19:58:19 +02:00
|
|
|
config.Env = util.SliceSubtract(config.Env, imageConfig.Env)
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2019-07-21 19:58:19 +02:00
|
|
|
config.Labels = util.StringMapSubtract(config.Labels, imageConfig.Labels)
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2019-07-21 19:58:19 +02:00
|
|
|
config.Volumes = util.StructMapSubtract(config.Volumes, imageConfig.Volumes)
|
2015-07-14 22:51:12 +02:00
|
|
|
|
2016-11-07 21:21:47 +02:00
|
|
|
// subtract ports exposed in image from container
|
2018-03-02 23:37:50 +02:00
|
|
|
for k := range config.ExposedPorts {
|
2016-11-07 21:21:47 +02:00
|
|
|
if _, ok := imageConfig.ExposedPorts[k]; ok {
|
|
|
|
delete(config.ExposedPorts, k)
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 23:40:22 +02:00
|
|
|
for p := range c.containerInfo.HostConfig.PortBindings {
|
2015-07-14 22:51:12 +02:00
|
|
|
config.ExposedPorts[p] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2015-08-12 22:49:45 +02:00
|
|
|
config.Image = c.ImageName()
|
2015-07-14 22:51:12 +02:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any links in the HostConfig need to be re-written before they can be
|
|
|
|
// re-submitted to the Docker create API.
|
2016-10-13 22:34:24 +02:00
|
|
|
func (c Container) hostConfig() *dockercontainer.HostConfig {
|
2015-07-14 22:51:12 +02:00
|
|
|
hostConfig := c.containerInfo.HostConfig
|
|
|
|
|
|
|
|
for i, link := range hostConfig.Links {
|
|
|
|
name := link[0:strings.Index(link, ":")]
|
|
|
|
alias := link[strings.LastIndex(link, "/"):]
|
|
|
|
|
|
|
|
hostConfig.Links[i] = fmt.Sprintf("%s:%s", name, alias)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostConfig
|
|
|
|
}
|
2020-08-18 20:55:35 +02:00
|
|
|
|
|
|
|
// HasImageInfo returns whether image information could be retrieved for the container
|
|
|
|
func (c Container) HasImageInfo() bool {
|
|
|
|
return c.imageInfo != nil
|
|
|
|
}
|
2020-12-06 14:21:04 +02:00
|
|
|
|
|
|
|
// ImageInfo fetches the ImageInspect data of the current container
|
|
|
|
func (c Container) ImageInfo() *types.ImageInspect {
|
|
|
|
return c.imageInfo
|
|
|
|
}
|