Sobes.tech
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:

  1. 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.
  2. Killed Signal: The kernel sends the SIGKILL signal to the process inside the container. This signal cannot be caught or ignored by the application running inside the container.
  3. 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.
  4. 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