Skip to content

Commit

Permalink
fix memory limit (#205)
Browse files Browse the repository at this point in the history
* fix memory limit

* use only necessary
  • Loading branch information
MateoLostanlen authored Jun 11, 2024
1 parent 91551e7 commit 0da320d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ run:
bash scripts/setup-docker-compose.sh
docker build . -t pyronear/pyro-engine:latest
docker compose up -d
rm docker-compose.yml.bak

# Get log from engine wrapper
log:
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ services:
resources:
limits:
cpus: "3"
memory: ""
logging:
driver: "json-file"
options:
Expand Down
41 changes: 21 additions & 20 deletions scripts/setup-docker-compose.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
#!/bin/bash

# Define the percentage of host memory you want to allocate
PERCENTAGE=70
PERCENTAGE=90

# Get the total memory of the host system in kilobytes
TOTAL_MEM_KB=$(grep MemTotal /proc/meminfo | awk '{print $2}')

# Check if TOTAL_MEM_KB was successfully retrieved
if [ -z "$TOTAL_MEM_KB" ]; then
echo "Failed to retrieve total memory."
exit 1
fi

# Calculate the memory limit in kilobytes
LIMIT_MEM_KB=$((TOTAL_MEM_KB * PERCENTAGE / 100))

# Convert the limit to a format Docker understands (e.g., "m" for megabytes)
LIMIT_MEM_MB=$((LIMIT_MEM_KB / 1024))m

# Define the Docker Compose file to modify
DOCKER_COMPOSE_FILE="docker-compose.yml"

# Backup the original Docker Compose file
cp $DOCKER_COMPOSE_FILE "${DOCKER_COMPOSE_FILE}.bak"

# Use awk to update the memory limits in the Docker Compose file, preserving indentation
awk -v mem_limit="$LIMIT_MEM_MB" '
/services:/ { in_services=1 }
in_services && /deploy:/ { in_deploy=1 }
in_deploy && /resources:/ { in_resources=1 }
in_resources && /limits:/ { in_limits=1 }
in_limits && /memory:/ {
$0 = gensub(/memory:.*/, "memory: " mem_limit, 1)
}
{ print }
' "${DOCKER_COMPOSE_FILE}.bak" > "docker-compose.override.yml"

echo "Memory limits set to $LIMIT_MEM_MB in $DOCKER_COMPOSE_FILE"
# Define the Docker Compose override file to create/update
DOCKER_COMPOSE_OVERRIDE_FILE="docker-compose.override.yml"

# Create/update the docker-compose.override.yml with the memory limit
cat <<EOF > "$DOCKER_COMPOSE_OVERRIDE_FILE"
services:
run:
deploy:
resources:
limits:
memory: $LIMIT_MEM_MB
EOF

echo "Memory limits set to $LIMIT_MEM_MB in $DOCKER_COMPOSE_OVERRIDE_FILE"

0 comments on commit 0da320d

Please sign in to comment.