Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop a Nginx Docker container

Tags:

docker

nginx

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.

like image 567
0x6C77 Avatar asked Oct 19 '22 14:10

0x6C77


1 Answers

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
$
like image 170
christian Avatar answered Nov 15 '22 06:11

christian