How to Configure vm.overcommit_memory=1 for Specific Docker Containers Without Affecting Host System


2 views

When running memory-intensive applications in Docker containers, you might encounter warnings like:

WARNING overcommit_memory is set to 0! Background save may fail under low memory condition.
To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot
or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

Setting vm.overcommit_memory=1 system-wide affects all processes and containers, which can lead to:

  • Potential system instability when memory is overcommitted
  • Unexpected behavior in other containers
  • Security implications for the host system

Option 1: Using Docker's --memory-swap Flag

For applications like Papermerge that need memory flexibility:

docker run -d \
  --name papermerge \
  --memory=2g \
  --memory-swap=4g \
  -p 8000:8000 \
  papermerge/papermerge

Option 2: Custom cgroups Configuration

Create a custom cgroup with specific overcommit settings:

# Create a new cgroup
sudo cgcreate -g memory:papermerge_group

# Configure memory overcommit
echo 1 | sudo tee /sys/fs/cgroup/memory/papermerge_group/memory.overcommit_ratio

# Run container in this cgroup
docker run --cgroup-parent=/papermerge_group/ papermerge/papermerge

Option 3: WSL2-Specific Configuration

For Docker Desktop on WSL2:

# Create or modify WSL config
echo "[wsl2]
memory=4GB
swap=8GB" > %USERPROFILE%\.wslconfig

For applications like Redis (if you're seeing similar warnings):

# In redis.conf
maxmemory 2gb
maxmemory-policy allkeys-lru

After implementing any solution, verify with:

docker exec -it papermerge cat /proc/sys/vm/overcommit_memory

# Or check cgroup settings
cat /sys/fs/cgroup/memory/papermerge_group/memory.overcommit_ratio

When running certain memory-intensive applications in Docker containers (like Papermerge in this case), you might encounter warnings about vm.overcommit_memory settings. The typical recommendation suggests modifying this setting system-wide via /etc/sysctl.conf, but this affects all containers and the host system - which may not be desirable.

The vm.overcommit_memory parameter is a kernel-level setting that controls how the Linux kernel handles memory allocation requests. By design, this is a global setting that affects the entire system. Docker containers share the host's kernel, which means we can't directly set different overcommit_memory values for different containers.

Here are two practical approaches to handle this scenario:

1. Temporary Setting During Container Runtime

You can modify the setting just before starting your specific container:

# Set overcommit temporarily
sudo sysctl vm.overcommit_memory=1

# Start your container
docker run -d --name papermerge your/papermerge-image

# Optionally revert after container starts
# sudo sysctl vm.overcommit_memory=0

2. Use a Privileged Container with Custom sysctl

For more isolation, you can create a privileged container with its own sysctl namespace:

docker run --privileged --sysctl vm.overcommit_memory=1 -d your/papermerge-image

Note: This requires Docker 20.10+ and comes with security considerations.

Instead of modifying overcommit settings, consider properly configuring container memory limits:

docker run -d --memory="2g" --memory-swap="4g" --name papermerge your/papermerge-image

When using Docker Desktop with WSL2 backend, you'll need to modify the WSL2 VM's settings:

wsl -d docker-desktop
sysctl vm.overcommit_memory=1
exit

Create a .wslconfig file in your Windows user directory to make this persistent:

[wsl2]
kernelCommandLine = "sysctl.vm.overcommit_memory=1"

After making changes, monitor your system's behavior with:

cat /proc/meminfo | grep Commit
sysctl vm.overcommit_memory
docker stats papermerge