I am trying to stop a Docker container running Nginx only after there has been no activity in the access.log of that Nginx instance for a period of time.
Is it possible to stop a Docker container from inside the container? The other solution I can think of is to have a cron running on the host OS that checks the /var/lib/docker/aufs/mnt/[container id]/ but I am planning on starting lots of containers and would prefer not to have to keep a list of IDs.
The docker container stops when the main process in the container stops.
I setup a little dockerfile and a start script to show how this could work in your case:
Dockerfile
FROM nginx
COPY start.sh /
CMD ["/start.sh"]
start.sh
#!/bin/bash
nginx &
sleep 20
# replace sleep 20 with your test of inactivity 
nginx stop
Build container, run and test
$ docker build -t ng .
$ docker run -d ng 
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3a373e721da7        ng:latest           "/start.sh"         4 seconds ago       Up 3 seconds        443/tcp, 80/tcp     distracted_colden   
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3a373e721da7        ng:latest           "/start.sh"         16 seconds ago      Up 16 seconds       80/tcp, 443/tcp     distracted_colden   
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
$
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With