I want to use swap memory in my dockerized application because sometimes the container's memory consumption exceeds the limit and they get crashed.
I am using the below configuration in docker-compose which produces an error
ERROR: The Compose file './docker-compose.yml' is invalid because:
services.app1-cnn.deploy.resources.reservations value Additional properties are not allowed ('memory-swap' was unexpected)
docker-compose.yml
version: "3.3"
services:
app-cnn:
build: ./app
image: "app-cnn"
restart: always
container_name: app-cnn
ports:
- "5000:5000"
deploy:
replicas: 1
resources:
limits:
memory: 3G
reservations:
memory-swap: 6G
refer to https://docs.docker.com/compose/compose-file/#memswap_limit
from my test result by default memswap is double of the memory size. if you need more, use memswap_limit to overwrite it.
have to say the structure is wierd somehow, not sure why it is designed like that. Anyway, below configuration works for me.
...
deploy:
resources:
limits:
memory: 512M
reservations:
memory: 128M
memswap_limit: 6G
Related
I am trying to set resources limits in my docker-compose.yml file.
Here it is:
version: "3.7"
services:
postgres:
build: "docker/postgres"
container_name: "postgres"
ports:
- 5432:5432
environment:
POSTGRES_USER: prodev
POSTGRES_PASSWORD: prodev
POSTGRES_MULTIPLE_DATABASES: pro_dev, pro_test
networks:
- my_proto_app
the_api:
deploy:
resources:
limits:
cpus: '0.001'
memory: 50M
reservations:
cpus: '0.0001'
memory: 20M
image: the_api:latest
ports:
- 8080:8080
depends_on:
- postgres
links:
- postgres
networks:
- my_proto_app
networks:
my_proto_app:
internal: false
However, when I run docker stats in order to gain insight into my resources limits, I notice that my limits are not taken into account:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
c0b7d2fffc42 postgres 0.04% 20.89MiB / 1.952GiB 1.05% 148kB / 171kB 0B / 856kB 16
0a0f9e482f86 api_the_api_1 2.16% 739.5MiB / 1.952GiB 37.00% 409kB / 464kB 0B / 73.7kB 59
Can someone please help ?
edit: I run the app with the following command: docker-compose up
The deploy key in the docker-compose file doesn't work on docker-compose up (with compose file format version 3 and above). The deploy key which will be working only in swarm mode. To run it in swarm mode
docker swarm init
Sample docker-compose.yml to deploy in swarm deployments with CPU and Memory resource limits
version: "3.3"
services:
tomcat:
image: tomcat
deploy:
resources:
limits:
cpus: '0.5'
memory: 250M
reservations:
cpus: '0.5'
memory: 120M
Command to deploy in docker stack
docker stack deploy --compose-file=docker-compose.yml stackname
Check the CPU and memory resouse limits using docker stats
Ref URL : https://docs.docker.com/compose/compose-file/compose-versioning/#version-2x-to-3x
If you want to set resource constraints on non swarm deployments, use Compose file format version 2.
Sample docker-compose.yml to deploy in non-swarm deployments with CPU and Memory resource limits
version: "2.2"
services:
tomcat:
image: tomcat
cpus: "0.5"
mem_limit: 512m
Run the docker-compose.yml file with command
docker-compose up
Check the CPU and memory resouse limits using docker stats
Ref : https://docs.docker.com/compose/compose-file/compose-file-v2/#cpu-and-other-resources
Hope this helps !!!
I am using rmohr/activemq activemq docker image. Right now it is -Xms64M -Xmx1G. I need to increase it to -Xms2g -Xmx3g.
I am using docker-compose. Here are the docker-compose.yml file entries:
version: '3'
services:
queue:
image: rmohr/activemq:5.15.6
ports:
- "8161:8161"
- "61616:61616"
I ended up adding the ACTIVEMQ_OPTS parameter under environment to resolve this.
Here is what the docker-compose file looks like:
version: '3'
services:
queue:
image: rmohr/activemq:5.15.6
ports:
- "8161:8161"
- "61616:61616"
environment:
ACTIVEMQ_OPTS: "-Xms2g -Xmx3g"
Here are the logs from activemq
version: "3"
services:
queue:
image: rmohr/activemq:5.15.6
ports:
- "8161:8161"
- "61616:61616"
environment:
ACTIVEMQ_OPTS_MEMORY: "-Xms2g -Xmx3g"
deploy:
resources:
limits:
cpus: '0.50' #set the memory and CPU time based on requirement
memory: 3g
reservations:
cpus: '0.25'
memory: 2g
Please refer link , Active MQ memory increase ,ACTIVEMQ_OPTS_MEMORY environment variable usage
I am trying to set resources limits in my docker-compose.yml file.
Here it is:
version: "3.7"
services:
postgres:
build: "docker/postgres"
container_name: "postgres"
ports:
- 5432:5432
environment:
POSTGRES_USER: prodev
POSTGRES_PASSWORD: prodev
POSTGRES_MULTIPLE_DATABASES: pro_dev, pro_test
networks:
- my_proto_app
the_api:
deploy:
resources:
limits:
cpus: '0.001'
memory: 50M
reservations:
cpus: '0.0001'
memory: 20M
image: the_api:latest
ports:
- 8080:8080
depends_on:
- postgres
links:
- postgres
networks:
- my_proto_app
networks:
my_proto_app:
internal: false
However, when I run docker stats in order to gain insight into my resources limits, I notice that my limits are not taken into account:
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
c0b7d2fffc42 postgres 0.04% 20.89MiB / 1.952GiB 1.05% 148kB / 171kB 0B / 856kB 16
0a0f9e482f86 api_the_api_1 2.16% 739.5MiB / 1.952GiB 37.00% 409kB / 464kB 0B / 73.7kB 59
Can someone please help ?
edit: I run the app with the following command: docker-compose up
The deploy key in the docker-compose file doesn't work on docker-compose up (with compose file format version 3 and above). The deploy key which will be working only in swarm mode. To run it in swarm mode
docker swarm init
Sample docker-compose.yml to deploy in swarm deployments with CPU and Memory resource limits
version: "3.3"
services:
tomcat:
image: tomcat
deploy:
resources:
limits:
cpus: '0.5'
memory: 250M
reservations:
cpus: '0.5'
memory: 120M
Command to deploy in docker stack
docker stack deploy --compose-file=docker-compose.yml stackname
Check the CPU and memory resouse limits using docker stats
Ref URL : https://docs.docker.com/compose/compose-file/compose-versioning/#version-2x-to-3x
If you want to set resource constraints on non swarm deployments, use Compose file format version 2.
Sample docker-compose.yml to deploy in non-swarm deployments with CPU and Memory resource limits
version: "2.2"
services:
tomcat:
image: tomcat
cpus: "0.5"
mem_limit: 512m
Run the docker-compose.yml file with command
docker-compose up
Check the CPU and memory resouse limits using docker stats
Ref : https://docs.docker.com/compose/compose-file/compose-file-v2/#cpu-and-other-resources
Hope this helps !!!
I am unable to specify CPU and memory limitation for services specified in version 3.
With version 2 it works fine with mem_limit & cpu_shares parameters under the services. But it fails while using version 3, putting them under deploy section doesn't seem worthy unless I am using swarm mode.
Can somebody help?
version: "3"
services:
node:
build:
context: .
dockerfile: ./docker-build/Dockerfile.node
restart: always
environment:
- VIRTUAL_HOST=localhost
volumes:
- logs:/app/out/
expose:
- 8083
command: ["npm","start"]
cap_drop:
- NET_ADMIN
- SYS_ADMIN
I know the topic is a bit old and seems stale, but anyway I was able to use these options:
deploy:
resources:
limits:
cpus: '0.001'
memory: 50M
when using 3.7 version of docker-compose
What helped in my case, was using this command:
docker-compose --compatibility up
--compatibility flag stands for (taken from the documentation):
If set, Compose will attempt to convert deploy keys in v3 files to
their non-Swarm equivalent
Think it's great, that I don't have to revert my docker-compose file back to v2.
deploy:
resources:
limits:
cpus: '0.001'
memory: 50M
reservations:
cpus: '0.0001'
memory: 20M
More: https://docs.docker.com/compose/compose-file/compose-file-v3/#resources
In you specific case:
version: "3"
services:
node:
image: USER/Your-Pre-Built-Image
environment:
- VIRTUAL_HOST=localhost
volumes:
- logs:/app/out/
command: ["npm","start"]
cap_drop:
- NET_ADMIN
- SYS_ADMIN
deploy:
resources:
limits:
cpus: '0.001'
memory: 50M
reservations:
cpus: '0.0001'
memory: 20M
volumes:
- logs
networks:
default:
driver: overlay
Note:
Expose is not necessary, it will be exposed per default on your stack network.
Images have to be pre-built. Build within v3 is not possible
"Restart" is also deprecated. You can use restart under deploy with on-failure action
You can use a standalone one node "swarm", v3 most improvements (if not all) are for swarm
Also Note:
Networks in Swarm mode do not bridge. If you would like to connect internally only, you have to attach to the network. You can 1) specify an external network within an other compose file, or have to create the network with --attachable parameter (docker network create -d overlay My-Network --attachable)
Otherwise you have to publish the port like this:
ports:
- 80:80
Docker Compose v1 does not support the deploy key. It's only respected when you use your version 3 YAML file in a Docker Stack.
This message is printed when you add the deploy key to you docker-compose.yml file and then run docker-compose up -d
WARNING: Some services (database) use the 'deploy' key, which will be
ignored. Compose does not support 'deploy' configuration - use docker stack deploy to deploy to a swarm.
The documentation (https://docs.docker.com/compose/compose-file/#deploy) says:
Specify configuration related to the deployment and running of
services. This only takes effect when deploying to a swarm with docker
stack deploy, and is ignored by docker-compose up and docker-compose
run.
Nevertheless you can use Docker Compose v2. Given the following Docker composition you can use the deploy key to limit your containers resources.
version: "3.9"
services:
database:
image: mariadb:10.10.2-jammy
container_name: mydb
environment:
MYSQL_ROOT_PASSWORD: root_secret
MYSQL_DATABASE: mydb
MYSQL_USER: myuser
MYSQL_PASSWORD: secret
TZ: "Europe/Zurich"
MARIADB_AUTO_UPGRADE: "true"
tmpfs:
- /var/lib/mysql:rw
ports:
- "127.0.0.1:3306:3306"
deploy:
resources:
limits:
cpus: "4.0"
memory: 200M
networks:
- mynetwork
When you run docker compose up -d (Note: in version 2 of Docker Compose you call the docker binary at not the docker-compose python application) and then inspect the resources you see that the memory is limited to 200 MB. The CPU limit is not exposed by docker stats.
❯ docker stats --no-stream
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
2c71fb8de607 mydb 0.04% 198MiB / 200MiB 99.02% 2.67MB / 3.77MB 70.6MB / 156MB 18
This is possible with version >= 3.8. Here is an example using docker-compose >= 1.28.x :
version: '3.9'
services:
app:
image: nginx
cpus: "0.5"
mem_reservation: "10M"
mem_limit: "250M"
Proof of it working (see the MEM USAGE) column :
The expected behavior when reaching memory limit is the container getting killed. In this case, whether set restart: always or adjust your app code.
Limits and restarts settings in Docker compose v3 should now be set using (restart: always is also deprecated):
deploy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120s
resources:
limits:
cpus: '0.50'
memory: 50M
reservations:
cpus: '0.25'
memory: 20M
I have other experiences, maybe somebody can explain this.
Maybe this is bug(i think this is a feature), but, I am able to use deployments limits (memory limits) in docker-compose without swarm, hovever CPU limits doesn't work but replication does.
$> docker-compose --version
docker-compose version 1.29.2
$> docker --version
Docker version 20.10.12
version: '3.2'
services:
limits-test:
image: alexeiled/stress-ng
command: [
'--vm', '1', '--vm-bytes', '20%', '--vm-method', 'all', '--verify', '-t', ' 10m', '-v'
]
deploy:
resources:
limits:
cpus: '0.50'
memory: 1024M
Docker stats
b647e0dad247 dc-limits_limits-test_1 0.01% 547.1MiB / 1GiB 53.43% 942B / 0B 0B / 0B 3
Edited, thx #Jimmix
I think there is confusion here over using docker-compose and docker compose (with a space). You can install the compose plugin using https://docs.docker.com/compose/install if you don't already have it.
Here is an example compose file just running Elasticsearch
version: "3.7"
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.15.2
restart: always
ports:
- "9222:9200"
deploy:
resources:
limits:
cpus: "4"
memory: "2g"
environment:
- "node.name=elasticsearch"
- "bootstrap.memory_lock=true"
- "discovery.type=single-node"
- "xpack.security.enabled=false"
- "ingest.geoip.downloader.enabled=false"
I have it in a directory called estest the file is called es-compose.yaml. The file sets CPU and memory limits.
If you launch using docker-compose e.g.
docker-compose -f es-compose.yaml up
Then look at docker stats you see
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
e3b6253ee730 estest_elasticsearch_1 342.13% 32.39GiB / 62.49GiB 51.83% 7.7kB / 0B 27.3MB / 381kB 46
so the cpu and memory resource limits are ignored. During the launch you see the warning
WARNING: Some services (elasticsearch) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use `docker stack deploy` to deploy to a swarm.
Which I think is what leads people to look at Docker stack/swarm. However if you just switch to using the newer docker compose now built in to the docker CLI https://docs.docker.com/engine/reference/commandline/compose/ e.g.
docker compose -f es-compose.yaml up
And look again at docker stats you see
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
d062eda10ffe estest-elasticsearch-1 0.41% 1.383GiB / 2GiB 69.17% 8.6kB / 0B 369MB / 44MB 6
Therefore the limits have been applied.
This is better in my opinion than swarm as it still allows you to build containers as part of the compose project and pass environment easily via a file. I would recommend removing docker-compose and switching over to use the newer docker compose wherever possible.
This is my sample docker-compose.yml file.
version: '2'
config-server:
image: ccc/config-server
restart: always
registration-server:
image: ccc/registration-server
restart: always
ports:
- 1111:1111
When I use docker-compose up -d I get an error:
"ERROR: The Compose file './docker-compose.yml' is invalid because:
Additional properties are not allowed ('registration-server', 'config-server' were unexpected)
You might be seeing this error because you're using the wrong Compose file version. Either specify a version of "2" (or "2.0") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.
For more on the Compose file format versions, see https://docs.docker.com/compose/compose-file/
You are missing a services keyword, your correct .yml is:
version: '2'
services:
config-server:
image: ccc/config-server
restart: always
registration-server:
image: ccc/registration-server
restart: always
ports:
- 1111:1111
This can also happpen if one of the keys is misspelled. In my case memory was spelled incorrectly:
After fixing it:
version: "3.8"
services:
redis:
image: redis:alpine
deploy:
resources:
limits:
cpus: '0.50'
memory: 50M
reservations:
cpus: '0.25'
memory: 20M
In the This can also happen if, this can also happens if global level volumes is indented wrong, like not starting at column 0.