add pause option

This commit is contained in:
Morgan Patterson
2022-09-02 00:32:16 -07:00
committed by GitHub
parent 2e69b8d6d1
commit c1e068be6c
5 changed files with 36 additions and 12 deletions

View File

@@ -21,8 +21,8 @@ $ docker-compose up
container_name: lazytainer
image: ghcr.io/vmorganp/lazytainer:master
environment:
- PORT=81 # comma separated list of ports...or just the one
- LABEL=lazytainer # value of lazytainer.marker for other containers that lazytainer checks
- PORT=81 # comma separated list of ports...or just the one
- LABEL=lazytainer # value of lazytainer.marker for other containers that lazytainer checks
# - TIMEOUT=30 # OPTIONAL number of seconds to let container idle
# - MINPACKETTHRESH=10 # OPTIONAL number of packets that must be recieved to keepalive/start container
# - POLLRATE=1 # OPTIONAL number of seconds to sleep between polls
@@ -41,6 +41,7 @@ $ docker-compose up
- lazytainer # wait for lazytainer to start before starting
labels:
- "lazytainer.marker=lazytainer" # required label to make it work
- "lazytainer.marker=stop" # can be either "stop" or "pause", or left blank for stop
```
## Configuration

View File

@@ -25,6 +25,7 @@ services:
- lazytainer
labels:
- "lazytainer.marker=lazytainer"
- "lazytainer.sleepMethod=pause" # can be either "stop" or "pause", or left blank for stop
whoami2:
container_name: whoami2
@@ -34,4 +35,5 @@ services:
depends_on:
- lazytainer
labels:
- "lazytainer.marker=lazytainer"
- "lazytainer.marker=lazytainer"
- "lazytainer.sleepMethod=stop" # can be either "stop" or "pause", or left blank for stop

View File

@@ -193,10 +193,19 @@ func stopContainers() {
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
check(err)
for _, c := range getContainers() {
if err := dockerClient.ContainerStop(context.Background(), c.ID, nil); err != nil {
fmt.Printf("Unable to stop container %s: %s\n", c.Names[0], err)
} else {
fmt.Println("stopped container ", c.Names[0])
stopMethod := strings.ToLower(c.Labels["lazytainer.sleepMethod"])
if stopMethod == "stop" || stopMethod == "" {
if err := dockerClient.ContainerStop(context.Background(), c.ID, nil); err != nil {
fmt.Printf("Unable to stop container %s: %s\n", c.Names[0], err)
} else {
fmt.Println("stopped container ", c.Names[0])
}
} else if stopMethod == "pause" {
if err := dockerClient.ContainerPause(context.Background(), c.ID); err != nil {
fmt.Printf("Unable to pause container %s: %s\n", c.Names[0], err)
} else {
fmt.Println("paused container ", c.Names[0])
}
}
}
}
@@ -206,10 +215,19 @@ func startContainers() {
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
check(err)
for _, c := range getContainers() {
if err := dockerClient.ContainerStart(context.Background(), c.ID, types.ContainerStartOptions{}); err != nil {
fmt.Printf("Unable to start container %s: %s\n", c.Names[0], err)
} else {
fmt.Println("started container ", c.Names[0])
stopMethod := strings.ToLower(c.Labels["lazytainer.sleepMethod"])
if stopMethod == "stop" || stopMethod == "" {
if err := dockerClient.ContainerStart(context.Background(), c.ID, types.ContainerStartOptions{}); err != nil {
fmt.Printf("Unable to start container %s: %s\n", c.Names[0], err)
} else {
fmt.Println("started container ", c.Names[0])
}
} else if stopMethod == "pause" {
if err := dockerClient.ContainerUnpause(context.Background(), c.ID); err != nil {
fmt.Printf("Unable to unpause container %s: %s\n", c.Names[0], err)
} else {
fmt.Println("unpaused container ", c.Names[0])
}
}
}
}

View File

@@ -7,7 +7,7 @@ docker-compose up
```
## Watch the magic happen
after 90 seconds of no activity the server should stop
After a configurable period of no activity the server should stop
if you generate some traffic by trying to connect to the instance or running a command like
`telnet localhost 25565`

View File

@@ -23,6 +23,9 @@ services:
restart: unless-stopped
labels:
- "lazytainer.marker=lazytainer"
- "lazytainer.sleepMethod=stop" # can be either "stop" or "pause", or left blank for stop
# using "stop" will release the memory associated with the server
# using "pause" will keep the memory allocated, however it will restart much faster
depends_on:
- lazytainer
volumes: