2015-07-21 18:04:41 +02:00
|
|
|
package container
|
2015-07-14 22:51:12 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-21 19:58:19 +02:00
|
|
|
"github.com/containrrr/watchtower/internal/util"
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
imageConfig := c.imageInfo.Config
|
|
|
|
|
|
|
|
if config.WorkingDir == imageConfig.WorkingDir {
|
|
|
|
config.WorkingDir = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.User == imageConfig.User {
|
|
|
|
config.User = ""
|
|
|
|
}
|
|
|
|
|
2019-07-21 19:58:19 +02:00
|
|
|
if util.SliceEqual(config.Cmd, imageConfig.Cmd) {
|
2015-07-21 00:54:18 +02:00
|
|
|
config.Cmd = nil
|
2015-07-14 22:51:12 +02:00
|
|
|
}
|
|
|
|
|
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
|
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
|
|
|
|
}
|