4

I want to have a policy in my docker-compose which a docker container memory usage will be higher than the amount of a certain memory limitation it restarts.

This is what I have done so far:

version: '3'

services:
    modbus_collector:
        build: .
        image: modbus_collector:2.0.0
        container_name: modbus_collector
        restart: unless-stopped
        deploy:
          resources:
            limits:
              memory: 28M

I was expecting that to be restarted when the container memory usage exceeds 28M, but when I monitor docker containers by docker stats I see that this container memory usage grow up and not happens about restart!

I also tried by restart: always but the result was the same.


[UPDATE]:

With version 2 it works fine with mem_limit:. But it fails while using version 3, putting them under deploy section doesn't seem worthy unless I am using swarm mode.

Even on version 2.1, I have a problem in restarting the container: limitation applied correctly, but when the container memory usage grows up, this limitation prevents of that but I expected instead of decrease memory it will restart that container.

version: '2.1'

services:
    modbus_collector:
        build: .
        image: modbus_collector:2.0.0
        container_name: modbus_collector
        restart: unless-stopped
        mem_limit: 28m
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
  • 1
    According [to compose file reference](https://docs.docker.com/compose/compose-file/#deploy) the deploy configuration is only supported in Docker Swarm. Are you using Swarm mode? – Markus May 28 '19 at 09:37
  • No, and I'm using docker-compose version `3`. Is there any other manner to handle this purpose? – Benyamin Jafari May 28 '19 at 09:40
  • [Compose file reference for version 3](https://docs.docker.com/compose/compose-file/#resources) suggests to use [this](https://docs.docker.com/compose/compose-file/compose-file-v2/#cpu-and-other-resources). I am not sure if version 2 compose file elements work in version 3 compose files. – Markus May 28 '19 at 09:43
  • Possible duplicate of [How to specify Memory & CPU limit in docker compose version 3](https://stackoverflow.com/questions/42345235/how-to-specify-memory-cpu-limit-in-docker-compose-version-3) – Markus May 28 '19 at 09:50
  • @Markus I tried this answer but not worked. – Benyamin Jafari May 28 '19 at 09:52
  • I did some research in the mean time and found out that those constraints are not being supported in version 3 compose files. You must use version 2. – Markus May 28 '19 at 11:56
  • You are right, Now I switched to version `2.1` with `mem_limit: 28m` and it applied correctly, but when the container memory usage grows up, this limitation prevents of that but I expected instead of decrease memory it will restart that container. – Benyamin Jafari May 28 '19 at 12:06
  • This [issue](https://github.com/docker/compose/issues/4513) has some bits of information on why version 2 is needed. – Markus May 28 '19 at 17:37

0 Answers0