Middle
What happens to the container if the memory limit is exceeded?
sobes.tech AI
Answer from AI
When the memory limit set for a Docker container is exceeded, the following main events occur:
- OOM (Out of Memory) Event: The operating system kernel detects that a process inside the container is trying to allocate memory beyond the set limit.
- Killed Signal: The kernel sends the
SIGKILLsignal to the process inside the container. This signal cannot be caught or ignored by the application running inside the container. - Container termination: The process inside the container is forcibly terminated. The Docker daemon detects that the main process of the container has exited and changes the container's status to
Exited. - Exit code: The container exits with a non-zero exit code (usually 137), indicating it was forcibly terminated by a signal.
Implications for the application and system:
- The application running inside the container crashes.
- If a restart policy is configured, Docker will attempt to restart the container.
- An OOM event and container termination will be logged in the container and/or host logs.
Example of setting a memory limit:
# Docker Compose example
services:
myapp:
image: myimage
mem_limit: 512m # Sets a memory limit of 512 MB
# Docker run example
docker run -m 512m myimage # Sets a memory limit of 512 MB