diff --git a/README.md b/README.md index 4a00dad..9227d8d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docker-compose.yaml b/docker-compose.yaml index 67a27b7..6dc8b8c 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -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" \ No newline at end of file + - "lazytainer.marker=lazytainer" + - "lazytainer.sleepMethod=stop" # can be either "stop" or "pause", or left blank for stop diff --git a/lazytainer.go b/lazytainer.go index 83d6fae..fc8a1c7 100644 --- a/lazytainer.go +++ b/lazytainer.go @@ -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]) + } } } } diff --git a/minecraft_example/README.md b/minecraft_example/README.md index a4ad414..ac69ca6 100644 --- a/minecraft_example/README.md +++ b/minecraft_example/README.md @@ -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` diff --git a/minecraft_example/docker-compose.yaml b/minecraft_example/docker-compose.yaml index 134465c..6aa650d 100644 --- a/minecraft_example/docker-compose.yaml +++ b/minecraft_example/docker-compose.yaml @@ -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: