ServerHandbook live check: run log ------------------------------------------------------------------------------ Check scripts/live/checks/docker-container-keeps-restarting.mjs (sha256 90b6f10a8ee2b4b9ab2825aa29c1490f0e2f7ed5fc3cc138f98648dfa093e569) Ran at 2026-11-19T05:16:31Z (789 s) Result PASS, all 21 claims held Tested on System Ubuntu 24.04.5 LTS Kernel 6.8.0-147-generic Package docker-ce 5:29.10.0-1~ubuntu.24.04~noble Package docker-ce-cli 5:29.10.0-1~ubuntu.24.04~noble Package containerd.io 2.4.2-4~ubuntu.24.04~noble Setup A fresh Ubuntu 24.04 server from Ubuntu’s cloud image with Docker Engine installed from Docker’s apt repository, as the install article leaves it, reached over SSH as the sudo user deploy (in the docker group). The container that loops is the official postgres:17 image started without a password. Every version above was read from the server at run time. Passwords used on the throwaway test server are shown as [password]. How we test: https://serverhandbook.com/how-we-test ------------------------------------------------------------------------------ What the steps changed on the server (recorded before the first step and after the last) Packages added (0) Packages upgraded (0) Packages removed (0) Services enabled at boot (0) Services disabled (0) Listening sockets opened (0) Listening sockets closed (0) Files added in /etc (0) Files changed in /etc (0) Files removed from /etc (0) Disk used on / (includes logs written during the run): +51.6 MB ------------------------------------------------------------------------------ ------------------------------------------------------------------------------ Check-your-server script (2 items): https://cdn.serverhandbook.com/check/docker-container-keeps-restarting/ubuntu-24.04.5-20261119t051631z.sh SHA-256 3bfe4a91595a73dd4b79becedd4e514c6f36e9f3ebe545cae554c79aa4c68d0b ------------------------------------------------------------------------------ On the stock server, before the first step: FAIL no container is stuck restarting PASS no container was killed for running out of memory After the last step: PASS no container is stuck restarting PASS no container was killed for running out of memory ------------------------------------------------------------------------------ Claims (21), each with what the run saw ------------------------------------------------------------------------------ 1. ok a bare Ubuntu 24.04 server box (web-server) starts 2. ok docker ps -a: the container shows "Restarting (1) … seconds ago" | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | 4004ab638ee6 postgres:17 "docker-entrypoint.s…" 18 seconds ago Restarting (1) 3 seconds ago db 3. ok inspect: exit=1, oom=false, policy=always, restarts counted (3 or more) | status=restarting exit=1 oom=false restarts=8 policy=always 4. ok docker logs: "Database is uninitialized and superuser password is not specified" | See PostgreSQL documentation about "trust": | https://www.postgresql.org/docs/current/auth-trust.html | Error: Database is uninitialized and superuser password is not specified. | You must specify POSTGRES_PASSWORD to a non-empty value for the | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run". | | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all | connections without a password. This is *not* recommended. | | See PostgreSQL documentation about "trust": | https://www.postgresql.org/docs/current/auth-trust.html 5. ok after update --restart=no and stop: Exited (1), no longer restarting | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | 4004ab638ee6 postgres:17 "docker-entrypoint.s…" 19 seconds ago Exited (1) 4 seconds ago db 6. ok the restart count no longer grows | 8 then 8 7. ok the fixed container starts (a container id is printed) | db | 423b7dd186123d2b8e1942b929b53154f98b2b4d1f38154333d76dbf5331035a 8. ok docker ps: Up for more than 10 seconds, no "Restarting" | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | 423b7dd18612 postgres:17 "docker-entrypoint.s…" 20 seconds ago Up 20 seconds 5432/tcp db 9. ok inspect: running, restarts=0, policy unless-stopped | status=running restarts=0 policy=unless-stopped 10. ok docker logs: "database system is ready to accept connections" | 2026-11-19 05:28:29.239 UTC [61] LOG: database system was shut down at 2026-11-19 05:28:29 UTC | 2026-11-19 05:28:29.245 UTC [1] LOG: database system is ready to accept connections 11. ok the memory-limited container shows "Restarting (137)" | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | ca9895d46d28 alpine "sh -c 'x=$(head -c …" 13 seconds ago Restarting (137) Less than a second ago hog 12. ok inspect: exit=137 oom=true limit=33554432 | exit=137 oom=true limit=33554432 13. ok the kernel log says "Memory cgroup out of memory: Killed process" | Nov 19 05:29:10 web-server kernel: Memory cgroup out of memory: Killed process 5774 (sh) total-vm:38512kB, anon-rss:31872kB, file-rss:768kB, shmem-rss:0kB, UID:0 pgtables:116kB oom_score_adj:0 | Nov 19 05:29:14 web-server kernel: Memory cgroup out of memory: Killed process 5854 (sh) total-vm:38512kB, anon-rss:31744kB, file-rss:768kB, shmem-rss:0kB, UID:0 pgtables:108kB oom_score_adj:0 14. ok with a 256 MB limit the same command finishes: "done" | done 15. ok inspect: exited 0, oom=false, restarts=0 | status=exited exit=0 oom=false restarts=0 16. ok a container that ends cleanly under "always" shows "Restarting (0)" | CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES | 3cb49401135f alpine "echo hello" 9 seconds ago Restarting (0) 1 second ago hello 17. ok on-failure:3 gave up: exited, exit=1, restarts=3 | status=exited exit=1 restarts=3 18. ok (no container is stuck restarting at the end of the steps) | 0 19. ok (no container was killed for running out of memory at the end of the steps) | 0 20. ok (tested on) the box reports its OS, kernel and the version of 3 package(s) | PRETTY_NAME="Ubuntu 24.04.5 LTS" | VIRT=none | KERNEL=6.8.0-147-generic | PKG containerd.io 2.4.2-4~ubuntu.24.04~noble | PKG docker-ce 5:29.10.0-1~ubuntu.24.04~noble | PKG docker-ce-cli 5:29.10.0-1~ubuntu.24.04~noble 21. ok undo: docker rm -f removes the container | db ------------------------------------------------------------------------------ Commands and what they printed (25) ------------------------------------------------------------------------------ $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4004ab638ee6 postgres:17 "docker-entrypoint.s…" 18 seconds ago Restarting (1) 3 seconds ago db $ docker inspect -f 'status={{.State.Status}} exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}} policy={{.HostConfig.RestartPolicy.Name}}' db status=restarting exit=1 oom=false restarts=8 policy=always $ docker logs --tail 12 db See PostgreSQL documentation about "trust": https://www.postgresql.org/docs/current/auth-trust.html Error: Database is uninitialized and superuser password is not specified. You must specify POSTGRES_PASSWORD to a non-empty value for the superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run". You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all connections without a password. This is *not* recommended. See PostgreSQL documentation about "trust": https://www.postgresql.org/docs/current/auth-trust.html $ [object Object] db $ [object Object] db $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4004ab638ee6 postgres:17 "docker-entrypoint.s…" 19 seconds ago Exited (1) 4 seconds ago db $ [object Object] db 423b7dd186123d2b8e1942b929b53154f98b2b4d1f38154333d76dbf5331035a $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 423b7dd18612 postgres:17 "docker-entrypoint.s…" 20 seconds ago Up 20 seconds 5432/tcp db $ docker inspect -f 'status={{.State.Status}} restarts={{.RestartCount}} policy={{.HostConfig.RestartPolicy.Name}}' db status=running restarts=0 policy=unless-stopped $ docker logs --tail 2 db 2026-11-19 05:28:29.239 UTC [61] LOG: database system was shut down at 2026-11-19 05:28:29 UTC 2026-11-19 05:28:29.245 UTC [1] LOG: database system is ready to accept connections $ [object Object] Unable to find image 'alpine:latest' locally latest: Pulling from library/alpine 881fc932939e: Downloading [========================================> ] 3.146MB/3.854MB 881fc932939e: Download complete Digest: sha256:f59abe656b692d901e184be3445e084786602108599d2a51d229c0f5a48c71bf Status: Downloaded newer image for alpine:latest ca9895d46d28ada5f5cbb60a8dfb12556f21ea8fd12cfa6c674b7adc1619a65b $ docker ps -a --filter name=hog CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ca9895d46d28 alpine "sh -c 'x=$(head -c …" 13 seconds ago Restarting (137) Less than a second ago hog $ docker inspect -f 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} limit={{.HostConfig.Memory}}' hog exit=137 oom=true limit=33554432 $ sudo journalctl -k --no-pager | grep -i "memory cgroup out of memory" | tail -n 2 Nov 19 05:29:10 web-server kernel: Memory cgroup out of memory: Killed process 5774 (sh) total-vm:38512kB, anon-rss:31872kB, file-rss:768kB, shmem-rss:0kB, UID:0 pgtables:116kB oom_score_adj:0 Nov 19 05:29:14 web-server kernel: Memory cgroup out of memory: Killed process 5854 (sh) total-vm:38512kB, anon-rss:31744kB, file-rss:768kB, shmem-rss:0kB, UID:0 pgtables:108kB oom_score_adj:0 $ [object Object] hog $ docker run --name hog --restart on-failure:3 --memory 256m --memory-swap 256m alpine sh -c 'x=$(head -c 60000000 /dev/zero | tr "\0" a); echo done' done $ docker inspect -f 'status={{.State.Status}} exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}}' hog status=exited exit=0 oom=false restarts=0 $ [object Object] hog $ [object Object] 3cb49401135f132264a288749a89281ce28f61a13160e427e941b69a65b4dc7f $ docker ps -a --filter name=hello CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3cb49401135f alpine "echo hello" 9 seconds ago Restarting (0) 1 second ago hello $ [object Object] hello $ [object Object] de268a08d33ec43d627d195a4b6264366ca15194eecaa0c7d949d292623f6a30 $ docker inspect -f 'status={{.State.Status}} exit={{.State.ExitCode}} restarts={{.RestartCount}}' limited status=exited exit=1 restarts=3 $ [object Object] limited $ docker rm -f db db ------------------------------------------------------------------------------ The check-your-server script, its full screen before the steps ------------------------------------------------------------------------------ ServerHandbook check, docker-container-keeps-restarting (proved 2026-11-19) FAIL no container is stuck restarting fix: https://serverhandbook.com/kb/docker-container-keeps-restarting#stop-the-loop PASS no container was killed for running out of memory 1 passed, 1 failed To fix a FAIL, follow the steps in https://serverhandbook.com/kb/docker-container-keeps-restarting ------------------------------------------------------------------------------ The check-your-server script, its full screen after the steps ------------------------------------------------------------------------------ ServerHandbook check, docker-container-keeps-restarting (proved 2026-11-19) PASS no container is stuck restarting PASS no container was killed for running out of memory 2 passed, 0 failed ------------------------------------------------------------------------------ The check's source (scripts/live/checks/docker-container-keeps-restarting.mjs) As it ran. ------------------------------------------------------------------------------ /** * KB fix: "Docker container keeps restarting" (`docker ps` shows "Restarting (1) 8 seconds ago"). The real loop is a * postgres container started without POSTGRES_PASSWORD (it exits 1 every start; restart policy "always" starts it * again). Steps: see the loop; read policy, exit code and RestartCount; read the logs; stop the loop * (docker update --restart=no, docker stop); fix the cause and prove it stays up (RestartCount 0). Then the other * loops by exit code: 137 + OOMKilled (a 32 MB limit), exit 0 under `restart: always`, and `on-failure:3` giving up. * * Runs on a fresh server (the local VM box). Docker Engine is installed first exactly as the install article does it * (hidden), and the broken container is the stock state the reader starts from (hidden). */ import { bareBox, reportBox } from '../lib.mjs' export const meta = { // The reader's check-your-server script (plan05 phase 3): proved on the stock server and after the steps. reader: [ { item: 'no container is stuck restarting', cmd: "docker ps -a --filter status=restarting -q | wc -l", expect: '^0$', step: 'stop-the-loop', }, { item: 'no container was killed for running out of memory', cmd: "docker inspect -f '{{.State.OOMKilled}}' $(docker ps -aq) | grep -c true", expect: '^0$', step: 'exit-137', }, ], } const RELEASE = process.env.LIVE_BOX_RELEASE || '24.04' const CODENAME = { '24.04': 'noble', '26.04': 'resolute' }[RELEASE] const INSTALL = [ 'sudo apt-get update -qq', 'sudo install -m 0755 -d /etc/apt/keyrings', 'sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc', 'sudo chmod a+r /etc/apt/keyrings/docker.asc', `printf 'Types: deb\\nURIs: https://download.docker.com/linux/ubuntu\\nSuites: ${CODENAME}\\nComponents: stable\\nArchitectures: amd64\\nSigned-By: /etc/apt/keyrings/docker.asc\\n' | sudo tee /etc/apt/sources.list.d/docker.sources >/dev/null`, 'sudo apt-get update -qq', 'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin >/dev/null', ].join(' && ') const check = (ctx) => bareBox(ctx, async ({ sudoUser }) => { const { run, L } = sudoUser() // ── hidden: Docker Engine as the install article leaves it, the docker group, and the broken container ── L(INSTALL, 900000) L('sudo usermod -aG docker $USER') L('sudo docker pull -q postgres:17 >/dev/null && sudo docker pull -q alpine >/dev/null', 600000) L('sudo docker run -d --name db --restart always postgres:17 >/dev/null') L('sleep 14') // the loop is running and has backed off a few times // ── 1. the symptom ────────────────────────────────────────────────────────────────────── const ps = run('docker ps -a', { id: 'ps-loop' }) ctx.check('docker ps -a: the container shows "Restarting (1) … seconds ago"', /\bRestarting \(1\) .* ago/.test(ps.out) && /\bdb\s*$/m.test(ps.out), ps.out) // ── 2. what Docker knows: policy, exit code, count ────────────────────────────────────── const ins = run("docker inspect -f 'status={{.State.Status}} exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}} policy={{.HostConfig.RestartPolicy.Name}}' db", { id: 'inspect-loop' }) ctx.check('inspect: exit=1, oom=false, policy=always, restarts counted (3 or more)', /exit=1 oom=false restarts=([3-9]|\d\d+) policy=always/.test(ins.out), ins.out) // ── 3. the logs say why ───────────────────────────────────────────────────────────────── const lg = run('docker logs --tail 12 db', { id: 'logs' }) ctx.check('docker logs: "Database is uninitialized and superuser password is not specified"', /Database is uninitialized and superuser password is not specified/.test(lg.out), lg.out) // ── 4. stop the loop ──────────────────────────────────────────────────────────────────── run('docker update --restart=no db', { id: 'update-no', show: { none: true } }) run('docker stop db', { id: 'stop', show: { none: true } }) const ps2 = run('docker ps -a', { id: 'ps-stopped' }) ctx.check('after update --restart=no and stop: Exited (1), no longer restarting', /\bExited \(1\) /.test(ps2.out) && !/Restarting/.test(ps2.out), ps2.out) const c1 = L("docker inspect -f '{{.RestartCount}}' db") L('sleep 5') const c2 = L("docker inspect -f '{{.RestartCount}}' db") ctx.check('the restart count no longer grows', c1.trim() === c2.trim(), `${c1.trim()} then ${c2.trim()}`) // ── 5. fix the cause and prove it stays up ────────────────────────────────────────────── const fix = run( 'docker rm db\nexport DB_PASS=$(openssl rand -hex 12)\ndocker run -d --name db --restart unless-stopped -e POSTGRES_PASSWORD="[password]" postgres:17', { id: 'fix', show: { tail: 1 }, timeout: 300000 }, ) ctx.check('the fixed container starts (a container id is printed)', /^[0-9a-f]{64}$/m.test(fix.out), fix.out) L('sleep 20') const up = run('docker ps', { id: 'ps-fixed' }) ctx.check('docker ps: Up for more than 10 seconds, no "Restarting"', /\bUp (\d+ seconds|About a minute|\d+ minutes?)\b/.test(up.out) && /\bdb\s*$/m.test(up.out), up.out) const ok = run("docker inspect -f 'status={{.State.Status}} restarts={{.RestartCount}} policy={{.HostConfig.RestartPolicy.Name}}' db", { id: 'inspect-fixed' }) ctx.check('inspect: running, restarts=0, policy unless-stopped', /status=running restarts=0 policy=unless-stopped/.test(ok.out), ok.out) const rl = run('docker logs --tail 2 db', { id: 'logs-fixed' }) ctx.check('docker logs: "database system is ready to accept connections"', /database system is ready to accept connections/.test(rl.out), rl.out) // ── 6. exit 137: out of memory ────────────────────────────────────────────────────────── const hog = "docker run -d --name hog --restart always --memory 32m --memory-swap 32m alpine sh -c 'x=$(head -c 60000000 /dev/zero | tr \"\\0\" a); echo done'" run(hog, { id: 'hog-start', show: { none: true } }) L('sleep 12') const hps = run('docker ps -a --filter name=hog', { id: 'hog-ps' }) ctx.check('the memory-limited container shows "Restarting (137)"', /\bRestarting \(137\) /.test(hps.out), hps.out) const hin = run("docker inspect -f 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} limit={{.HostConfig.Memory}}' hog", { id: 'hog-inspect' }) ctx.check('inspect: exit=137 oom=true limit=33554432', /exit=137 oom=true limit=33554432/.test(hin.out), hin.out) const kl = run('sudo journalctl -k --no-pager | grep -i "memory cgroup out of memory" | tail -n 2', { id: 'hog-kernel' }) ctx.check('the kernel log says "Memory cgroup out of memory: Killed process"', /Memory cgroup out of memory: Killed process \d+ \(/.test(kl.out), kl.out) run('docker rm -f hog', { id: 'hog-rm', show: { none: true } }) const hog2 = run( "docker run --name hog --restart on-failure:3 --memory 256m --memory-swap 256m alpine sh -c 'x=$(head -c 60000000 /dev/zero | tr \"\\0\" a); echo done'", { id: 'hog-fixed' }, ) ctx.check('with a 256 MB limit the same command finishes: "done"', /^done$/m.test(hog2.out), hog2.out) const hin2 = run("docker inspect -f 'status={{.State.Status}} exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}}' hog", { id: 'hog-inspect-fixed' }) ctx.check('inspect: exited 0, oom=false, restarts=0', /status=exited exit=0 oom=false restarts=0/.test(hin2.out), hin2.out) run('docker rm -f hog', { id: 'hog-rm2', show: { none: true } }) // ── 7. exit 0 under "always" ──────────────────────────────────────────────────────────── run('docker run -d --name hello --restart always alpine echo hello', { id: 'hello-start', show: { none: true } }) L('sleep 8') const hs = run('docker ps -a --filter name=hello', { id: 'hello-ps' }) ctx.check('a container that ends cleanly under "always" shows "Restarting (0)"', /\bRestarting \(0\) /.test(hs.out), hs.out) run('docker rm -f hello', { id: 'hello-rm', show: { none: true } }) // ── 8. on-failure:3 gives up ──────────────────────────────────────────────────────────── run("docker run -d --name limited --restart on-failure:3 alpine sh -c 'exit 1'", { id: 'limited-start', show: { none: true } }) L('sleep 8') const lim = run("docker inspect -f 'status={{.State.Status}} exit={{.State.ExitCode}} restarts={{.RestartCount}}' limited", { id: 'limited-inspect' }) ctx.check('on-failure:3 gave up: exited, exit=1, restarts=3', /status=exited exit=1 restarts=3/.test(lim.out), lim.out) run('docker rm -f limited', { id: 'limited-rm', show: { none: true } }) // ── end state (reader checks) ─────────────────────────────────────────────────────────── const rs = L("docker ps -a --filter status=restarting -q | wc -l") ctx.check('(no container is stuck restarting at the end of the steps)', rs.trim() === '0', rs, { reader: { cmd: 'docker ps -a --filter status=restarting -q | wc -l', expect: '^0$' }, }) const om = L("docker inspect -f '{{.State.OOMKilled}}' $(docker ps -aq) | grep -c true || true") ctx.check('(no container was killed for running out of memory at the end of the steps)', om.trim() === '0', om, { reader: { cmd: "docker inspect -f '{{.State.OOMKilled}}' $(docker ps -aq) | grep -c true", expect: '^0$' }, }) ctx.stepsDone() // the article's steps end here; the undo follows reportBox( ctx, L, `A fresh Ubuntu ${RELEASE} server from Ubuntu’s cloud image with Docker Engine installed from Docker’s apt repository, as the install article leaves it, reached over SSH as the sudo user deploy (in the docker group). The container that loops is the official postgres:17 image started without a password.`, ['docker-ce', 'docker-ce-cli', 'containerd.io'], ) // ── undo ──────────────────────────────────────────────────────────────────────────────── const rm = run('docker rm -f db', { id: 'undo' }) ctx.check('undo: docker rm -f removes the container', /^db$/m.test(rm.out), rm.out) }) export default check