Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test restart policy - how to crash a container such that it restarts

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:

  • First, I run my application with docker-compose up;
  • I identify the containers IDs with docker container ps;
  • I kill/stop one of the containers with 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.

like image 637
Rafael Marques Avatar asked Jan 31 '19 12:01

Rafael Marques


People also ask

How do I stop a restarting container?

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.

How do I check my container restart policy?

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.

How will we make sure that every time a docker container fails it's restarted automatically?

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.

How do I change the restart policy on a running container?

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.


1 Answers

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.)

like image 156
David Maze Avatar answered Sep 22 '22 22:09

David Maze