I have a docker-compose file that creates 3 Hello World applications and uses nginx to load balance traffic across the different containers.
The docker-compose code is as follows:
version: '3.2'
services:
backend1:
image: rafaelmarques7/hello-node:latest
restart: always
backend2:
image: rafaelmarques7/hello-node:latest
restart: always
backend3:
image: rafaelmarques7/hello-node:latest
restart: always
loadbalancer:
image: nginx:latest
restart: always
links:
- backend1
- backend2
- backend3
ports:
- '80:80'
volumes:
- ./container-balancer/nginx.conf:/etc/nginx/nginx.conf:ro
I would like to verify that the restart: always
policy actually works.
The approach I tried is as follows:
docker-compose up
;docker container ps
;docker stop ID_Container
or docker kill ID_Container
.I was expecting that after the 3rd step (stop/kill the container. this makes it exist with code 137), the restart policy would kick in and create a new container again.
However, this does not happen. I have read that this is intentional, as to have a way to be able to manually stop containers that have a restart policy.
Despite this, I would like to know how I can kill a container in such a way that it triggers the restart policy so that I can actually verify that it is working.
Thank you for your help.
If you want to stop a docker container here's the command to do it. The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL. This will stop a running container. This would restart the container.
Users can type docker ps to check if the restart policy is active; it will be shown as either Up , when the container is up and running, or Restarting when the container is in the restart state.
Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. Restart policies ensure that linked containers are started in the correct order. Docker recommends that you use restart policies, and avoid using process managers to start containers.
Find out the container ID, stop the whole docker service, modify /var/lib/docker/containers/CONTAINER_ID/hostconfig. json, set RestartPolicy -> Name to "always", and start docker service. docker commit your container as a new image, stop & rm the current container, and start a new container with the image.
If you run ps
on the host you will be able to see the actual processes in all of your Docker containers. Once you find a container's main process's process ID, you can sudo kill
it (you will have to be root). That will look more like a "crash", especially if you kill -13
to send SIGSEGV.
It is very occasionally useful for validation scenarios like this to have an endpoint that crashes your application that you can enable in test builds and some other similar silly things. Just make sure you do have a gate so that those endpoints don't exist in production builds. (In old-school C, an #ifdef TEST
would do the job; some languages have equivalents but many don't.)
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