<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: DeployLark</title>
    <description>The latest articles on DEV Community by DeployLark (@deploylark).</description>
    <link>https://dev.to/deploylark</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4188014%2Feff5e0ad-288f-4605-8094-070bce7d34ad.png</url>
      <title>DEV Community: DeployLark</title>
      <link>https://dev.to/deploylark</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deploylark"/>
    <language>en</language>
    <item>
      <title>Docker Build: Fix failed to compute cache key: not found</title>
      <dc:creator>DeployLark</dc:creator>
      <pubDate>Fri, 13 Nov 2026 03:54:39 +0000</pubDate>
      <link>https://dev.to/deploylark/docker-build-fix-failed-to-compute-cache-key-not-found-1jil</link>
      <guid>https://dev.to/deploylark/docker-build-fix-failed-to-compute-cache-key-not-found-1jil</guid>
      <description>&lt;p&gt;Your &lt;code&gt;docker build&lt;/code&gt; or &lt;code&gt;docker compose build&lt;/code&gt; stops at a &lt;code&gt;COPY&lt;/code&gt; line with &lt;code&gt;failed to compute cache key: failed to calculate checksum of ref ...: "/package.json": not found&lt;/code&gt;. The file is right there in your project, so the message looks wrong. It isn't. The builder never received that file. The &lt;a href="https://docs.docker.com/build/concepts/context/" rel="noopener noreferrer"&gt;Docker build context docs&lt;/a&gt; put it in one line: "The build context is the set of files that your build can access." Anything outside that set, or filtered out of it, is "not found".&lt;/p&gt;

&lt;p&gt;This guide reproduces every common cause on Docker Engine 29.9.3 with a small Node app and fixes each one. All output below comes from that lab run on November 13, 2026. The error is a classic. On that day the Stack Overflow question &lt;a href="https://stackoverflow.com/questions/66146088/docker-gets-error-failed-to-compute-cache-key-not-found-runs-fine-in-visual" rel="noopener noreferrer"&gt;Docker gets error "failed to compute cache key: not found"&lt;/a&gt; had 636,533 views, &lt;a href="https://stackoverflow.com/questions/27068596/how-to-include-files-outside-of-dockers-build-context" rel="noopener noreferrer"&gt;How to include files outside of Docker's build context?&lt;/a&gt; had 629,103 and &lt;a href="https://stackoverflow.com/questions/64221861/an-error-failed-to-solve-with-frontend-dockerfile-v0" rel="noopener noreferrer"&gt;An error, "failed to solve with frontend dockerfile.v0"&lt;/a&gt; had 764,565.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;COPY&lt;/code&gt; paths resolve from the root of the build context, the last argument of &lt;code&gt;docker build&lt;/code&gt;. Not from the Dockerfile's folder and not from your shell's folder.&lt;/li&gt;
&lt;li&gt;If your Dockerfile sits in a subfolder, build with that folder as the context: &lt;code&gt;docker build app&lt;/code&gt;, or set &lt;code&gt;context: ./app&lt;/code&gt; in Compose.&lt;/li&gt;
&lt;li&gt;If you use an allowlist &lt;code&gt;.dockerignore&lt;/code&gt; (&lt;code&gt;*&lt;/code&gt; then &lt;code&gt;!file&lt;/code&gt;), add every file you &lt;code&gt;COPY&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;COPY ../something&lt;/code&gt; can't leave the context. Pass the folder with &lt;code&gt;--build-context name=./path&lt;/code&gt; and use &lt;code&gt;COPY --from=name&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;COPY --from=&amp;lt;stage&amp;gt;&lt;/code&gt;, build that stage with &lt;code&gt;--target&lt;/code&gt; and look inside it to find the real path.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;h2&gt;
  
  
  What failed to compute cache key means
&lt;/h2&gt;

&lt;p&gt;BuildKit checksums every &lt;code&gt;COPY&lt;/code&gt; source so it can decide whether the cached layer is still valid. To checksum a file it needs the file. When the path doesn't exist in the build context, the checksum fails and the whole build stops with "failed to compute cache key". The error names the path BuildKit looked for, relative to the context root.&lt;/p&gt;

&lt;p&gt;This map shows the path a file takes to reach &lt;code&gt;COPY&lt;/code&gt; and the five ways it gets lost:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-build-failed-to-compute-cache-key%2F01-failed-to-compute-cache-key-causes-diagram.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-build-failed-to-compute-cache-key%2F01-failed-to-compute-cache-key-causes-diagram.webp" alt="Diagram of how files reach a Dockerfile COPY: docker build PATH sets the context root, .dockerignore removes files, named contexts add folders, then COPY reads from the result. Below it a table of five causes, wrong build context, .dockerignore excludes the file, path outside the context, wrong path in another stage and filename case, with how to spot and fix each" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What the error looks like
&lt;/h3&gt;

&lt;p&gt;The lab project has this layout. The Dockerfile, &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;server.js&lt;/code&gt; live in &lt;code&gt;app/&lt;/code&gt;, and a shared config file lives next to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shop/
├── app/
│   ├── Dockerfile
│   ├── package.json
│   └── server.js
└── shared/
    └── config.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Dockerfile is as plain as it gets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt; node:24.22.3-alpine3.24
&lt;span class="k"&gt;WORKDIR&lt;/span&gt; /app
&lt;span class="k"&gt;COPY&lt;/span&gt; package.json ./
&lt;span class="k"&gt;COPY&lt;/span&gt; server.js ./
&lt;span class="k"&gt;RUN&lt;/span&gt; npm run build
&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"npm"&lt;/span&gt;, &lt;span class="s2"&gt;"start"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running the build from &lt;code&gt;shop/&lt;/code&gt; with &lt;code&gt;-f app/Dockerfile .&lt;/code&gt; fails on both &lt;code&gt;COPY&lt;/code&gt; lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#6 [3/5] COPY package.json ./
#6 ERROR: failed to calculate checksum of ref u7ril5urrzm4gaxcl1f3cs3fw::mc3ahjnoy3ee1gcvane9cej4q: "/package.json": not found
#7 [4/5] COPY server.js ./
#7 ERROR: failed to calculate checksum of ref u7ril5urrzm4gaxcl1f3cs3fw::mc3ahjnoy3ee1gcvane9cej4q: "/server.js": not found
ERROR: failed to build: failed to solve: failed to compute cache key: failed to calculate checksum of ref u7ril5urrzm4gaxcl1f3cs3fw::mc3ahjnoy3ee1gcvane9cej4q: "/server.js": not found
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read the quoted path. &lt;code&gt;"/package.json"&lt;/code&gt; means BuildKit looked for &lt;code&gt;package.json&lt;/code&gt; at the root of the context, which was &lt;code&gt;shop/&lt;/code&gt;. The file is at &lt;code&gt;shop/app/package.json&lt;/code&gt;. The &lt;code&gt;ref&lt;/code&gt; IDs are random and differ on every machine, so ignore them.&lt;/p&gt;

&lt;h3&gt;
  
  
  The legacy builder says it differently
&lt;/h3&gt;

&lt;p&gt;With BuildKit turned off (&lt;code&gt;DOCKER_BUILDKIT=0&lt;/code&gt;), Docker 29.9.3 first prints a deprecation notice and then a clearer message for the same mistake:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Step 3/6 : COPY package.json ./
COPY failed: file not found in build context or excluded by .dockerignore: stat package.json: file does not exist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That message already lists the two main causes. Two neighbour errors look similar but happen before any &lt;code&gt;COPY&lt;/code&gt;. A wrong Dockerfile name gives &lt;code&gt;failed to read dockerfile: open dockerfile: no such file or directory&lt;/code&gt;, and a context folder that doesn't exist gives &lt;code&gt;unable to prepare context: path "app/shared" not found&lt;/code&gt;. Those mean the &lt;code&gt;-f&lt;/code&gt; path or the context path is wrong, not a file inside it. Also note that &lt;code&gt;docker build --check&lt;/code&gt; reported "Check complete, no warnings found." for a Dockerfile with a broken &lt;code&gt;COPY&lt;/code&gt;, so the build checks won't catch this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 1: build from the right context
&lt;/h2&gt;

&lt;p&gt;The most common cause by far is a context that doesn't match the paths in the Dockerfile. The &lt;code&gt;-f&lt;/code&gt; flag only tells Docker where the Dockerfile is. It doesn't move the context. The accepted answer on the Stack Overflow question explains why Visual Studio users hit this. &lt;a href="https://stackoverflow.com/a/67944726" rel="noopener noreferrer"&gt;GregHNZ wrote&lt;/a&gt; that Visual Studio does it this way: "Instead of launching docker build in the folder with the Dockerfile, it launches in the parent folder and specifies the Dockerfile with the -f option."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-build-failed-to-compute-cache-key%2F02-docker-build-wrong-context-not-found.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-build-failed-to-compute-cache-key%2F02-docker-build-wrong-context-not-found.webp" alt="Terminal capture: docker build with -f app/Dockerfile and context dot from the repo root transfers 2B of context and fails with package.json and server.js not found, then docker build app succeeds and the container answers shop-api ok" width="800" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  docker build with -f
&lt;/h3&gt;

&lt;p&gt;Pick one of two fixes and keep the Dockerfile and the command in agreement.&lt;/p&gt;

&lt;p&gt;Make the folder with the files the context. The Dockerfile in that folder is found automatically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build -t shop-api:1 app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or keep the repo root as the context and write every &lt;code&gt;COPY&lt;/code&gt; source relative to it, &lt;code&gt;COPY app/package.json ./&lt;/code&gt; and so on, then build with &lt;code&gt;docker build -f app/Dockerfile .&lt;/code&gt;. This is the layout Visual Studio generates for .NET projects, which is why its Dockerfiles have paths like &lt;code&gt;COPY ["WorkerService2/WorkerService2.csproj", ...]&lt;/code&gt;. A Dockerfile written for one context breaks with the other, so the fix is to match them, not to move files around.&lt;/p&gt;

&lt;p&gt;In the lab, the failing build's context step showed &lt;code&gt;transferring context: 2B&lt;/code&gt;. After the fix the image built and the container answered &lt;code&gt;shop-api ok&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Docker Compose context and dockerfile
&lt;/h3&gt;

&lt;p&gt;Compose has the same trap. In &lt;code&gt;build&lt;/code&gt;, the &lt;code&gt;dockerfile&lt;/code&gt; path is relative to &lt;code&gt;context&lt;/code&gt;, and &lt;code&gt;COPY&lt;/code&gt; paths are relative to &lt;code&gt;context&lt;/code&gt; too. This file fails exactly like the &lt;code&gt;-f&lt;/code&gt; command above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;shop-api:compose&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
      &lt;span class="na"&gt;dockerfile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app/Dockerfile&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;docker compose build api&lt;/code&gt; ends with &lt;code&gt;failed to solve: failed to compute cache key: failed to calculate checksum of ref ...: "/package.json": not found&lt;/code&gt;. Point the context at the app folder and the &lt;code&gt;dockerfile&lt;/code&gt; becomes a plain file name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;shop-api:compose&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./app&lt;/span&gt;
      &lt;span class="na"&gt;dockerfile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Dockerfile&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see &lt;code&gt;/app/package.json&lt;/code&gt; in the error, as in &lt;a href="https://stackoverflow.com/questions/68308209/docker-compose-failed-to-solve-rpc-error-code-unknown-desc-failed-to-comp" rel="noopener noreferrer"&gt;this Compose question&lt;/a&gt; with 56,651 views, the Dockerfile copies &lt;code&gt;app/package.json&lt;/code&gt; from a context that doesn't contain an &lt;code&gt;app&lt;/code&gt; folder. Same fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 2: check .dockerignore
&lt;/h2&gt;

&lt;p&gt;The most upvoted answer on the same Visual Studio question, with 532 votes, points here. &lt;a href="https://stackoverflow.com/a/67928771" rel="noopener noreferrer"&gt;Ilia Reshetnikov wrote&lt;/a&gt;: "Check your .dockerignore file. Possible it ignores needed files for copy command and you get failed to compute cache key error." The usual culprit is an allowlist, where &lt;code&gt;*&lt;/code&gt; ignores everything and &lt;code&gt;!&lt;/code&gt; lines add files back. Forget one file and &lt;code&gt;COPY&lt;/code&gt; can't see it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-build-failed-to-compute-cache-key%2F03-dockerignore-excludes-copy-file.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-build-failed-to-compute-cache-key%2F03-dockerignore-excludes-copy-file.webp" alt="Terminal capture: a .dockerignore with star and an exception only for package.json makes COPY server.js fail with not found, a busybox build that copies the context lists only package.json, and after adding an exception for server.js the context lists both files and the build succeeds" width="799" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  List what the builder receives
&lt;/h3&gt;

&lt;p&gt;You don't have to guess what survives &lt;code&gt;.dockerignore&lt;/code&gt;. Build a throwaway image that copies the whole context and lists it. Save this as &lt;code&gt;ctx.Dockerfile&lt;/code&gt; next to your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt; busybox:1.37.0
&lt;span class="k"&gt;COPY&lt;/span&gt; . /ctx
&lt;span class="k"&gt;RUN&lt;/span&gt; find /ctx -type f &lt;span class="o"&gt;|&lt;/span&gt; sort
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then build it with the same context you use for the real image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build --no-cache --progress=plain -f ctx.Dockerfile app 2&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;1 &lt;span class="o"&gt;|&lt;/span&gt; grep -E &lt;span class="s1"&gt;'^#[0-9]+ [0-9.]+ '&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the &lt;code&gt;.dockerignore&lt;/code&gt; set to &lt;code&gt;*&lt;/code&gt; and &lt;code&gt;!package.json&lt;/code&gt;, the lab printed a single line, &lt;code&gt;/ctx/package.json&lt;/code&gt;. That explains the &lt;code&gt;"/server.js": not found&lt;/code&gt;. Adding &lt;code&gt;!server.js&lt;/code&gt; to the file made the listing show both files, and the real build passed. Keep &lt;code&gt;--no-cache&lt;/code&gt;, otherwise the &lt;code&gt;RUN&lt;/code&gt; step can come from cache and print nothing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Per-Dockerfile ignore files
&lt;/h3&gt;

&lt;p&gt;The ignore file must sit at the root of the context. The docs say: "When you run a build command, the build client looks for a file named .dockerignore in the root directory of the context." A &lt;code&gt;.dockerignore&lt;/code&gt; inside &lt;code&gt;app/&lt;/code&gt; does nothing when the context is the repo root.&lt;/p&gt;

&lt;p&gt;If one repo has several Dockerfiles that need different rules, name the ignore file after the Dockerfile and put it next to it, for example &lt;code&gt;app/Dockerfile.ctx.dockerignore&lt;/code&gt;. The lab used that pattern for every variant. According to the &lt;a href="https://docs.docker.com/build/concepts/context/" rel="noopener noreferrer"&gt;same docs page&lt;/a&gt;, "A Dockerfile-specific ignore-file takes precedence over the .dockerignore file at the root of the build context if both exist." So when you debug, check both files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 3: files outside the build context
&lt;/h2&gt;

&lt;p&gt;Monorepos often have shared config or libraries one level up. &lt;code&gt;COPY ../shared/config.json ./config.json&lt;/code&gt; looks reasonable, but BuildKit doesn't let a build step leave its context.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-build-failed-to-compute-cache-key%2F04-copy-outside-build-context-named-context.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-build-failed-to-compute-cache-key%2F04-copy-outside-build-context-named-context.webp" alt="Terminal capture: COPY ../shared/config.json fails with /shared/config.json not found, a build with --build-context shared=./shared and COPY --from=shared succeeds and the container reports config=shared-prod, and the same Dockerfile without --build-context fails trying to pull docker.io/library/shared" width="800" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why ../ doesn't work
&lt;/h3&gt;

&lt;p&gt;Look at the path in the lab's error: &lt;code&gt;"/shared/config.json": not found&lt;/code&gt;. The &lt;code&gt;../&lt;/code&gt; is gone. BuildKit resolved the path inside the context, &lt;code&gt;app/&lt;/code&gt;, where no &lt;code&gt;shared&lt;/code&gt; folder exists. You get "not found", not a clear "outside the context" message, which is why this cause is easy to miss.&lt;/p&gt;

&lt;p&gt;The old workaround was to move the context up to the repo root and rewrite every &lt;code&gt;COPY&lt;/code&gt; path to match. That still works. Named contexts let you keep the small context instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  Named build contexts
&lt;/h3&gt;

&lt;p&gt;Buildx lets you pass extra folders as named contexts. Give the shared folder a name on the command line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build --build-context &lt;span class="nv"&gt;shared&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;./shared -f app/Dockerfile.ctx -t shop-api:2 app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then copy from it by name in the Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt; node:24.22.3-alpine3.24
&lt;span class="k"&gt;WORKDIR&lt;/span&gt; /app
&lt;span class="k"&gt;COPY&lt;/span&gt; package.json server.js ./
&lt;span class="k"&gt;COPY&lt;/span&gt; --from=shared config.json ./config.json
&lt;span class="k"&gt;RUN&lt;/span&gt; npm run build
&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"npm"&lt;/span&gt;, &lt;span class="s2"&gt;"start"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The image built and the app printed &lt;code&gt;config=shared-prod&lt;/code&gt;, read from the shared file. Run the same Dockerfile without &lt;code&gt;--build-context&lt;/code&gt; and BuildKit treats &lt;code&gt;shared&lt;/code&gt; as an image name. It failed with &lt;code&gt;failed to resolve source metadata for docker.io/library/shared:latest: pull access denied&lt;/code&gt;. If you see a pull error for a name that isn't an image, a named context is missing.&lt;/p&gt;

&lt;p&gt;Compose supports the same thing with &lt;code&gt;additional_contexts&lt;/code&gt;, described in the &lt;a href="https://docs.docker.com/reference/compose-file/build/" rel="noopener noreferrer"&gt;Compose build reference&lt;/a&gt;. This file built and ran in the lab:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;shop-api:compose&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./app&lt;/span&gt;
      &lt;span class="na"&gt;dockerfile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Dockerfile.ctx&lt;/span&gt;
      &lt;span class="na"&gt;additional_contexts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;shared&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./shared&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"3000:3000"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Fix 4: multi-stage paths and filename case
&lt;/h2&gt;

&lt;p&gt;The error isn't only about your local files. &lt;code&gt;COPY --from=&amp;lt;stage&amp;gt;&lt;/code&gt; in a multi-stage build checksums files inside that stage, and a wrong path there produces the same message.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-build-failed-to-compute-cache-key%2F05-copy-from-stage-wrong-path.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-build-failed-to-compute-cache-key%2F05-copy-from-stage-wrong-path.webp" alt="Terminal capture: a multi-stage build fails because COPY --from=build looks for /src/build/server.js, building only the build stage with --target and running find shows the file is in /src/dist, and after fixing the path the image builds and the container answers shop-api ok" width="800" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Build one stage and look inside
&lt;/h3&gt;

&lt;p&gt;In the lab, the build stage ran &lt;code&gt;npm run build&lt;/code&gt;, which writes &lt;code&gt;dist/server.js&lt;/code&gt;. The final stage asked for the wrong folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; --from=build /src/build/server.js ./server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The error read &lt;code&gt;"/src/build/server.js": not found&lt;/code&gt;. Note the absolute path. For &lt;code&gt;--from&lt;/code&gt;, the path is inside the stage's filesystem, not your context. To see what the stage really contains, build only that stage and list it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build --target build -t shop-api:build -f app/Dockerfile.multi app
docker run --rm shop-api:build find /src -type f
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The listing showed &lt;code&gt;/src/dist/server.js&lt;/code&gt;. Changing the &lt;code&gt;COPY&lt;/code&gt; to &lt;code&gt;/src/dist/server.js&lt;/code&gt; fixed the build. Check the stage's build script or tool config whenever you change its output folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  Filename case
&lt;/h3&gt;

&lt;p&gt;Linux file names are case sensitive, and BuildKit on Linux treats them that way. In the lab, &lt;code&gt;COPY Package.json server.js ./&lt;/code&gt; failed with &lt;code&gt;"/Package.json": not found&lt;/code&gt; even though &lt;code&gt;package.json&lt;/code&gt; was right there. A wrong case can go unnoticed on a machine whose file system ignores case and only break when the image builds on Linux or in CI. Match the case of the file on disk exactly, and check &lt;code&gt;.dockerignore&lt;/code&gt; patterns for the same problem.&lt;/p&gt;

&lt;p&gt;If none of the four fixes apply and your build stops later with a different error, the &lt;a href="https://deploylark.com/posts/docker-exec-format-error/" rel="noopener noreferrer"&gt;exec format error guide&lt;/a&gt; covers broken entrypoints and wrong architectures. If builds fail because the disk is full, see &lt;a href="https://deploylark.com/posts/docker-no-space-left-on-device/" rel="noopener noreferrer"&gt;Docker no space left on device&lt;/a&gt;. For &lt;code&gt;permission denied&lt;/code&gt; on &lt;code&gt;/var/run/docker.sock&lt;/code&gt; when you run the build at all, see &lt;a href="https://deploylark.com/posts/docker-sock-permission-denied/" rel="noopener noreferrer"&gt;the docker.sock guide&lt;/a&gt;. And if the image builds but Nginx in front of it can't find the container, read &lt;a href="https://deploylark.com/posts/nginx-docker-host-not-found-in-upstream/" rel="noopener noreferrer"&gt;Nginx host not found in upstream&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h4&gt;
  
  
  What does failed to compute cache key: not found mean?
&lt;/h4&gt;

&lt;p&gt;BuildKit tried to checksum a &lt;code&gt;COPY&lt;/code&gt; or &lt;code&gt;ADD&lt;/code&gt; source and the file wasn't in what the builder received. The quoted path is relative to the root of the build context, or to the stage's filesystem for &lt;code&gt;COPY --from&lt;/code&gt;. The file either sits outside the context, was excluded by &lt;code&gt;.dockerignore&lt;/code&gt;, or has a different name or case.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I fix COPY failed: file not found in build context?
&lt;/h4&gt;

&lt;p&gt;That is the legacy builder's wording for the same problem. In the lab it read &lt;code&gt;COPY failed: file not found in build context or excluded by .dockerignore&lt;/code&gt;. Check that the last argument of &lt;code&gt;docker build&lt;/code&gt; is the folder that contains the file, and that &lt;code&gt;.dockerignore&lt;/code&gt; doesn't exclude it. BuildKit is the default builder, so switch back to it instead of keeping &lt;code&gt;DOCKER_BUILDKIT=0&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Can Docker COPY files from a parent directory?
&lt;/h4&gt;

&lt;p&gt;Not with &lt;code&gt;COPY ../file&lt;/code&gt;. BuildKit resolves &lt;code&gt;../&lt;/code&gt; inside the context, so the lab got &lt;code&gt;"/shared/config.json": not found&lt;/code&gt;. Pass the folder as a named context with &lt;code&gt;--build-context shared=../shared&lt;/code&gt; and use &lt;code&gt;COPY --from=shared file ./&lt;/code&gt;, or &lt;code&gt;additional_contexts&lt;/code&gt; in Compose. The other option is to use the parent folder as the context.&lt;/p&gt;

&lt;h4&gt;
  
  
  Does .dockerignore affect COPY?
&lt;/h4&gt;

&lt;p&gt;Yes. Excluded files are removed from the context before the build starts, so &lt;code&gt;COPY&lt;/code&gt; can't see them. With &lt;code&gt;*&lt;/code&gt; and &lt;code&gt;!package.json&lt;/code&gt; in the lab, &lt;code&gt;COPY server.js&lt;/code&gt; failed until &lt;code&gt;!server.js&lt;/code&gt; was added. Remember that the ignore file must be at the context root or named after the Dockerfile.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why does the build work in Visual Studio but fail on the command line?
&lt;/h4&gt;

&lt;p&gt;Visual Studio runs &lt;code&gt;docker build&lt;/code&gt; from the solution folder with &lt;code&gt;-f&lt;/code&gt; pointing at the project's Dockerfile, and the generated &lt;code&gt;COPY&lt;/code&gt; paths include the project folder. Run the same command from the solution folder, &lt;code&gt;docker build -f Project/Dockerfile .&lt;/code&gt;, or the paths won't match.&lt;/p&gt;

&lt;h4&gt;
  
  
  Is Dockerfile COPY case sensitive?
&lt;/h4&gt;

&lt;p&gt;On a Linux builder, yes. &lt;code&gt;COPY Package.json&lt;/code&gt; failed in the lab when the file was &lt;code&gt;package.json&lt;/code&gt;. Use the exact case of the file name.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>dockerfile</category>
      <category>devops</category>
      <category>selfhosted</category>
    </item>
    <item>
      <title>Docker No Space Left on Device: Find and Free Disk Space</title>
      <dc:creator>DeployLark</dc:creator>
      <pubDate>Thu, 12 Nov 2026 04:05:06 +0000</pubDate>
      <link>https://dev.to/deploylark/docker-no-space-left-on-device-find-and-free-disk-space-322c</link>
      <guid>https://dev.to/deploylark/docker-no-space-left-on-device-find-and-free-disk-space-322c</guid>
      <description>&lt;p&gt;Your build stops with &lt;code&gt;No space left on device&lt;/code&gt;, &lt;code&gt;docker pull&lt;/code&gt; fails with &lt;code&gt;write /var/lib/docker/...: no space left on device&lt;/code&gt;, or &lt;code&gt;docker run&lt;/code&gt; won't even create a container. The disk that holds Docker's data is full. The &lt;a href="https://docs.docker.com/engine/manage-resources/pruning/" rel="noopener noreferrer"&gt;Docker pruning docs&lt;/a&gt; say unused images, containers, volumes and build cache "are generally not removed unless you explicitly ask Docker to do so." A host that builds and deploys every day fills up over weeks until something breaks.&lt;/p&gt;

&lt;p&gt;This guide fills a Docker 29.9.3 data disk on purpose, finds what used the space and frees it with the right command for each part. All output below comes from that lab run on November 12, 2026. You are not alone with this one. On that day the Stack Overflow question &lt;a href="https://stackoverflow.com/questions/30604846/docker-complains-about-no-space-left-on-device-how-to-clean-up" rel="noopener noreferrer"&gt;Docker complains about "no space left on device", how to clean up?&lt;/a&gt; had 770,358 views, &lt;a href="https://stackoverflow.com/questions/42510002/docker-how-to-clear-the-logs-properly-for-a-docker-container" rel="noopener noreferrer"&gt;How to clear the logs properly for a Docker container?&lt;/a&gt; had 961,251 and &lt;a href="https://stackoverflow.com/questions/32723111/how-can-i-remove-old-and-unused-docker-images" rel="noopener noreferrer"&gt;How can I remove old and unused Docker images?&lt;/a&gt; had 957,579.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;df -h /var/lib/docker&lt;/code&gt; and &lt;code&gt;docker system df&lt;/code&gt; to see how full the disk is and what Docker counts.&lt;/li&gt;
&lt;li&gt;In the lab, old builds sat in the build cache, not in images. &lt;code&gt;docker image prune&lt;/code&gt; freed 0B and &lt;code&gt;docker builder prune&lt;/code&gt; freed 839MB.&lt;/li&gt;
&lt;li&gt;Container logs don't show up in &lt;code&gt;docker system df&lt;/code&gt;. Find them with &lt;code&gt;du&lt;/code&gt;, and cap them with &lt;code&gt;max-size&lt;/code&gt; and &lt;code&gt;max-file&lt;/code&gt; in &lt;code&gt;daemon.json&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;df -h&lt;/code&gt; shows free space but Docker still says no space left, run &lt;code&gt;df -i&lt;/code&gt;. You are out of inodes.&lt;/li&gt;
&lt;li&gt;Never delete folders inside &lt;code&gt;/var/lib/docker&lt;/code&gt; by hand. Use the prune commands.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Why Docker runs out of disk space
&lt;/h2&gt;

&lt;p&gt;Docker keeps everything under one data directory, &lt;code&gt;/var/lib/docker&lt;/code&gt; by default. That covers images, the build cache, container writable layers, volumes and container logs. On a fresh Docker Engine 29 install with the containerd image store, the &lt;a href="https://docs.docker.com/engine/daemon/" rel="noopener noreferrer"&gt;daemon docs&lt;/a&gt; say image contents and container snapshots live in &lt;code&gt;/var/lib/containerd&lt;/code&gt; instead, so check that path too. The lab's Docker hosts run their own containerd, so everything landed under &lt;code&gt;/var/lib/docker/containerd&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This map shows each thing that fills the disk, how to see it and how to free it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-no-space-left-on-device%2F01-docker-disk-space-causes-fixes-diagram.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-no-space-left-on-device%2F01-docker-disk-space-causes-fixes-diagram.webp" alt="Table diagram of what fills Docker disk space: build cache, unused images, stopped containers, container logs, volumes and inodes, with the command to check each one, the command to free it and what to watch out for" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What the errors look like
&lt;/h3&gt;

&lt;p&gt;The lab built the same &lt;code&gt;myapp:latest&lt;/code&gt; tag four times. Each build wrote a 200 MB file, the kind of thing a real app does when it bundles assets or dependencies. The fourth build failed inside the &lt;code&gt;RUN&lt;/code&gt; step:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0.412 dd: error writing '/opt/assets-4.bin': No space left on device
ERROR: failed to build: failed to solve: process "/bin/sh -c dd if=/dev/urandom of=/opt/assets-$VERSION.bin bs=1M count=200 status=none" did not complete successfully: exit code: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the disk at 100%, a pull and a plain &lt;code&gt;docker run&lt;/code&gt; failed as well, each with a different path in the message:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-no-space-left-on-device%2F02-docker-no-space-left-on-device-build-pull-run.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-no-space-left-on-device%2F02-docker-no-space-left-on-device-build-pull-run.webp" alt="Terminal capture: docker build fails with dd error writing No space left on device, docker pull postgres 18.6-alpine fails writing to the containerd content ingest directory, docker run fails writing meta.db, and df -h shows the Docker disk at 100 percent with 1.6M free" width="800" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The path tells you where Docker was writing, but the fix is the same. Free space on the filesystem behind &lt;code&gt;/var/lib/docker&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check the disk before you delete anything
&lt;/h3&gt;

&lt;p&gt;Start with two commands on the Docker host:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;df -h /var/lib/docker
docker system df
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;df -h&lt;/code&gt; shows the filesystem, so it counts everything. &lt;code&gt;docker system df&lt;/code&gt; splits Docker's share into images, containers, local volumes and build cache, with a RECLAIMABLE column for what no running container uses. If the numbers don't add up to what &lt;code&gt;df&lt;/code&gt; shows, look at container logs next. Docker doesn't count them here.&lt;/p&gt;

&lt;h2&gt;
  
  
  Free space from the build cache
&lt;/h2&gt;

&lt;p&gt;This was the biggest surprise in the lab. After those four builds, &lt;code&gt;docker image ls -a&lt;/code&gt; listed only one &lt;code&gt;myapp&lt;/code&gt; image, and &lt;code&gt;docker image ls --filter dangling=true&lt;/code&gt; was empty. The old versions were not images anymore. They were build cache:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-no-space-left-on-device%2F03-docker-system-df-builder-prune.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-no-space-left-on-device%2F03-docker-system-df-builder-prune.webp" alt="Terminal capture: docker system df shows 1.271GB of build cache with 839MB reclaimable, docker system df -v lists three 419MB cache records, docker image prune reclaims 0B, docker builder prune reclaims 839MB and df -h drops to 48 percent used" width="800" height="316"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Prune the build cache
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;docker builder prune&lt;/code&gt; removes build cache that no image uses. In the lab it took the disk from 100% to 48%:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker builder prune -f
&lt;span class="c"&gt;# Total:    839MB&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add &lt;code&gt;-a&lt;/code&gt; to remove all build cache, including records an existing image still shares. Your next build then starts from scratch. To keep some cache for faster builds, Docker 29's prune has &lt;code&gt;--reserved-space&lt;/code&gt;, &lt;code&gt;--max-used-space&lt;/code&gt; and &lt;code&gt;--min-free-space&lt;/code&gt; flags. In the lab, &lt;code&gt;docker builder prune -f --max-used-space 100MB&lt;/code&gt; removed 419.5MB of unused cache and kept the records the current image still used.&lt;/p&gt;

&lt;p&gt;The classic overlay2 store behaves a little differently. On the box's own Docker Engine 26.1.5, two builds of the same tag left the old one as a &lt;code&gt;&amp;lt;none&amp;gt;&lt;/code&gt; image. &lt;code&gt;docker image prune&lt;/code&gt; removed that image but reported 0B, because its 209.7MB of layers moved into the build cache. &lt;code&gt;docker builder prune&lt;/code&gt; was still the command that gave the space back.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let BuildKit clean up on its own
&lt;/h3&gt;

&lt;p&gt;You can make the build cache trim itself. The &lt;a href="https://docs.docker.com/build/cache/garbage-collection/" rel="noopener noreferrer"&gt;Docker build garbage collection docs&lt;/a&gt; configure it in &lt;code&gt;daemon.json&lt;/code&gt; for the default builder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"builder"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"gc"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;"enabled"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"defaultKeepStorage"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"500MB"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A second lab host started with this setting built four versions of the same image. Right after the builds, the cache was 1.691GB and the disk was at 90%. Within two minutes, without any command, the cache dropped to 851.5MB and the disk to 51%. Garbage collection runs in the background, so it won't save a build that is already failing, but it stops the slow creep.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix container logs that fill the disk
&lt;/h2&gt;

&lt;p&gt;Logs are the item that &lt;code&gt;docker system df&lt;/code&gt; doesn't show. The lab ran a container that printed 2.5 million health check lines, a mild version of a chatty app running for weeks. &lt;code&gt;docker system df&lt;/code&gt; reported 157.3MB for containers, and all of that was the writable layer of a separate batch container. The chatty container's log file alone was 278.7M:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-no-space-left-on-device%2F04-docker-container-log-size-rotation.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-no-space-left-on-device%2F04-docker-container-log-size-rotation.webp" alt="Terminal capture: docker system df does not count logs, du shows a 278.7M json log file for the chatty container, truncate empties it to 0, and a container started with max-size 10m and max-file 3 keeps only three log files of 2.1M, 9.5M and 9.5M" width="800" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Find and empty a large log
&lt;/h3&gt;

&lt;p&gt;Each container's log path is in &lt;code&gt;docker inspect&lt;/code&gt;. To list the biggest logs on the host:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;du -h /var/lib/docker/containers/*/*-json.log &lt;span class="o"&gt;|&lt;/span&gt; sort -h &lt;span class="o"&gt;|&lt;/span&gt; tail -5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To empty one log right now, truncate it in place:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;truncate -s 0 &lt;span class="nv"&gt;$(&lt;/span&gt;docker inspect -f &lt;span class="s1"&gt;'&amp;amp;#123;&amp;amp;#123;.LogPath&amp;amp;#125;&amp;amp;#125;'&lt;/span&gt; chatty&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the lab that set the file to 0 bytes without restarting the container. Don't use &lt;code&gt;rm&lt;/code&gt; for this. When the lab deleted a 222.9M log with &lt;code&gt;rm&lt;/code&gt;, &lt;code&gt;df&lt;/code&gt; didn't change at all, because Docker still had the file open. The space only came back after &lt;code&gt;docker stop&lt;/code&gt;. BMitch, in the &lt;a href="https://stackoverflow.com/questions/42510002/docker-how-to-clear-the-logs-properly-for-a-docker-container" rel="noopener noreferrer"&gt;accepted answer&lt;/a&gt; on SO 42510002, warns against truncating too: "The external log deletion could happen while docker is writing json formatted data to the file, resulting in a partial line, and breaking the ability to read any logs from the docker logs cli." Truncate when you have to, then set up rotation so you don't have to again.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rotate logs with max-size and max-file
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://docs.docker.com/engine/logging/drivers/json-file/" rel="noopener noreferrer"&gt;json-file driver docs&lt;/a&gt; list &lt;code&gt;max-size&lt;/code&gt; as "Defaults to -1 (unlimited)", so logs grow forever unless you set it. Per container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run -d --log-opt max-size=10m --log-opt max-file=3 alpine:3.24.4 ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same 2.5 million lines in the lab left three files of 2.1M, 9.5M and 9.5M. In Compose, use the &lt;code&gt;logging&lt;/code&gt; key. The lab's service got &lt;code&gt;{json-file map[max-file:3 max-size:10m]}&lt;/code&gt; in &lt;code&gt;docker inspect&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;alpine:3.24.4&lt;/span&gt;
    &lt;span class="na"&gt;logging&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;json-file&lt;/span&gt;
      &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;max-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"10m"&lt;/span&gt;
        &lt;span class="na"&gt;max-file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"3"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make it the default for every new container, put it in &lt;code&gt;/etc/docker/daemon.json&lt;/code&gt; and restart Docker. The values must be strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"log-driver"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"json-file"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"log-opts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"max-size"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"10m"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"max-file"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"3"&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A lab host started with this file gave a new container the same 10m and 3 file limits without any flags. The docs are clear that existing containers keep their old settings, so recreate them with &lt;code&gt;docker compose up -d --force-recreate&lt;/code&gt; or &lt;code&gt;docker rm&lt;/code&gt; and &lt;code&gt;docker run&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;local&lt;/code&gt; log driver is the other option. Per the &lt;a href="https://docs.docker.com/engine/logging/drivers/local/" rel="noopener noreferrer"&gt;local driver docs&lt;/a&gt; it keeps 100MB per container by default and compresses rotated files. In the lab, &lt;code&gt;--log-driver local&lt;/code&gt; with no other options held the same output in 20.1M. Tools that read the JSON files directly, such as some log shippers, won't understand its format.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clean up images, containers and volumes safely
&lt;/h2&gt;

&lt;p&gt;Each of these has its own prune command. They differ in how much they delete by default, so it pays to know which flag removes what.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prune stopped containers and unused images
&lt;/h3&gt;

&lt;p&gt;Stopped containers keep their writable layer on disk. &lt;code&gt;docker ps -a -s&lt;/code&gt; shows the size of each one. In the lab, a batch job that wrote a 150 MB export to &lt;code&gt;/tmp&lt;/code&gt; and exited kept 157MB on disk until it was removed. &lt;code&gt;docker container prune&lt;/code&gt; removes all stopped containers. Anything a stopped container wrote outside a volume is gone after that, so copy it out with &lt;code&gt;docker cp&lt;/code&gt; first if you need it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker image prune&lt;/code&gt; without flags only removes dangling images, the untagged &lt;code&gt;&amp;lt;none&amp;gt;&lt;/code&gt; ones. To remove every image that no container uses, add &lt;code&gt;-a&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker image prune -a -f
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pruning docs describe &lt;code&gt;-a&lt;/code&gt; as removing "all images without at least one container associated to them", and a stopped container counts. Prune containers first, then images. Anything &lt;code&gt;-a&lt;/code&gt; removes comes back with &lt;code&gt;docker pull&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Volumes need extra care
&lt;/h3&gt;

&lt;p&gt;Volumes hold your data, so Docker is careful with them. The &lt;a href="https://docs.docker.com/reference/cli/docker/volume/prune/" rel="noopener noreferrer"&gt;docker volume prune reference&lt;/a&gt; says: "By default, it only removes anonymous volumes." The lab checked both kinds. &lt;code&gt;docker volume prune -f&lt;/code&gt; removed an anonymous volume of 41.94MB and left the named &lt;code&gt;pgdata&lt;/code&gt; volume alone. &lt;code&gt;docker volume prune -a -f&lt;/code&gt; deleted &lt;code&gt;pgdata&lt;/code&gt; as well.&lt;/p&gt;

&lt;p&gt;Before you run &lt;code&gt;-a&lt;/code&gt;, list what would go and make sure no database lives there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker volume ls --filter &lt;span class="nv"&gt;dangling&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;
docker system df -v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A volume counts as used while any container references it, running or stopped. Once &lt;code&gt;docker compose down&lt;/code&gt; removes the containers, nothing references the volume and &lt;code&gt;-a&lt;/code&gt; will delete it. If you're cleaning up after a Postgres upgrade, read &lt;a href="https://deploylark.com/posts/postgres-docker-upgrade-17-to-18/" rel="noopener noreferrer"&gt;upgrade PostgreSQL 17 to 18 in Docker&lt;/a&gt; first so you don't delete the old data before the new one works.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker system prune&lt;/code&gt; combines several of these commands. Its own warning lists what it removes: "all stopped containers", "all networks not used by at least one container", "all dangling images" and "unused build cache". In the lab it deleted two stopped containers and reclaimed 157.3MB. &lt;code&gt;docker system prune -a&lt;/code&gt; also removes every unused image and reclaimed another 759.7MB. Neither touched the named volume.&lt;/p&gt;

&lt;h3&gt;
  
  
  Don't delete overlay2 by hand
&lt;/h3&gt;

&lt;p&gt;It's tempting to &lt;code&gt;rm -rf&lt;/code&gt; the biggest folder you find. BMitch's accepted answer on &lt;a href="https://stackoverflow.com/questions/46672001/is-it-safe-to-clean-docker-overlay2" rel="noopener noreferrer"&gt;Is it safe to clean docker/overlay2/&lt;/a&gt; puts it plainly: "Deleting this can result in data loss and possibly stop the engine from running." The lab deleted the snapshot directories of the containerd store on a test host. &lt;code&gt;docker image ls&lt;/code&gt; still listed &lt;code&gt;alpine:3.24.4&lt;/code&gt;, but &lt;code&gt;docker run&lt;/code&gt; failed with &lt;code&gt;failed to stat parent: stat .../snapshots/1/fs: no such file or directory&lt;/code&gt;. Removing the image with &lt;code&gt;docker rmi&lt;/code&gt; and pulling it again fixed it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix no space left on device when df shows free space
&lt;/h2&gt;

&lt;p&gt;Sometimes &lt;code&gt;df -h&lt;/code&gt; reports plenty of free space and Docker still fails. Check inodes, the filesystem's count of how many files it can hold. A lab host with a 1 GB disk capped at 30,000 inodes ran a build that created 40,000 small files:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-no-space-left-on-device%2F05-docker-no-space-left-inodes-df-i.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-no-space-left-on-device%2F05-docker-no-space-left-inodes-df-i.webp" alt="Terminal capture: a build creating many files fails with can't create f28886: No space left on device, df -h shows only 13 percent used, df -i shows 29992 of 30000 inodes used at 100 percent, and after builder prune df -i drops to 2 percent" width="800" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Read df -i
&lt;/h3&gt;

&lt;p&gt;The build failed at file 28,886 with the same &lt;code&gt;No space left on device&lt;/code&gt; message. &lt;code&gt;df -h&lt;/code&gt; said 13% used. &lt;code&gt;df -i&lt;/code&gt; said 100%. Pruning the failed build's cache freed the inodes, and usage dropped to 2%. Images with huge &lt;code&gt;node_modules&lt;/code&gt; trees or package caches use inodes fast, so clean caches in the same &lt;code&gt;RUN&lt;/code&gt; step that creates them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Give Docker a bigger disk
&lt;/h3&gt;

&lt;p&gt;If the disk is simply too small for your workload, move Docker's data to a bigger one. Set &lt;code&gt;data-root&lt;/code&gt; in &lt;code&gt;daemon.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"data-root"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"/mnt/docker-data"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A fresh lab host started with that setting reported &lt;code&gt;/mnt/docker-data&lt;/code&gt; in &lt;code&gt;docker info --format '&amp;amp;#123;&amp;amp;#123;.DockerRootDir&amp;amp;#125;&amp;amp;#125;'&lt;/code&gt;. Docker starts with an empty data directory there, so stop Docker and copy the old directory across first if you want to keep your images and volumes. On a system using the containerd image store, the &lt;a href="https://docs.docker.com/engine/daemon/" rel="noopener noreferrer"&gt;daemon docs&lt;/a&gt; note that &lt;code&gt;data-root&lt;/code&gt; does not move &lt;code&gt;/var/lib/containerd&lt;/code&gt;. That location is set with &lt;code&gt;root&lt;/code&gt; in &lt;code&gt;/etc/containerd/config.toml&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For other errors that stop containers from starting, see &lt;a href="https://deploylark.com/posts/docker-exec-format-error/" rel="noopener noreferrer"&gt;Docker exec format error&lt;/a&gt;, &lt;a href="https://deploylark.com/posts/docker-port-already-allocated/" rel="noopener noreferrer"&gt;port is already allocated&lt;/a&gt; and &lt;a href="https://deploylark.com/posts/docker-sock-permission-denied/" rel="noopener noreferrer"&gt;permission denied on docker.sock&lt;/a&gt;. If you run Elasticsearch, &lt;a href="https://deploylark.com/posts/elasticsearch-docker-vm-max-map-count/" rel="noopener noreferrer"&gt;vm.max_map_count too low&lt;/a&gt; covers the host setting it needs before it starts.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h4&gt;
  
  
  How do I see what is using disk space in Docker?
&lt;/h4&gt;

&lt;p&gt;Run &lt;code&gt;docker system df&lt;/code&gt; for images, containers, volumes and build cache, and &lt;code&gt;docker system df -v&lt;/code&gt; for each item. Add &lt;code&gt;du -h /var/lib/docker/containers/*/*-json.log&lt;/code&gt; for logs, which &lt;code&gt;docker system df&lt;/code&gt; doesn't count. See &lt;a href="https://stackoverflow.com/questions/30604846/docker-complains-about-no-space-left-on-device-how-to-clean-up" rel="noopener noreferrer"&gt;SO 30604846&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why doesn't docker image prune free any space?
&lt;/h4&gt;

&lt;p&gt;Without &lt;code&gt;-a&lt;/code&gt; it only removes dangling images. In the lab, old builds of a tag sat in the build cache instead, so &lt;code&gt;docker image prune&lt;/code&gt; reclaimed 0B and &lt;code&gt;docker builder prune&lt;/code&gt; reclaimed 839MB.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I clear the logs of a Docker container?
&lt;/h4&gt;

&lt;p&gt;Run &lt;code&gt;truncate -s 0 $(docker inspect -f '&amp;amp;#123;&amp;amp;#123;.LogPath&amp;amp;#125;&amp;amp;#125;' NAME)&lt;/code&gt; for a quick fix, then set &lt;code&gt;max-size&lt;/code&gt; and &lt;code&gt;max-file&lt;/code&gt; so it doesn't happen again. Deleting the file with &lt;code&gt;rm&lt;/code&gt; doesn't free space until the container stops. See &lt;a href="https://stackoverflow.com/questions/42510002/docker-how-to-clear-the-logs-properly-for-a-docker-container" rel="noopener noreferrer"&gt;SO 42510002&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Is it safe to delete /var/lib/docker/overlay2?
&lt;/h4&gt;

&lt;p&gt;No. Deleting folders inside &lt;code&gt;/var/lib/docker&lt;/code&gt; breaks the images and containers that use them. In the lab &lt;code&gt;docker run&lt;/code&gt; failed until the image was pulled again. Use &lt;code&gt;docker system prune&lt;/code&gt; and the other prune commands instead. See &lt;a href="https://stackoverflow.com/questions/46672001/is-it-safe-to-clean-docker-overlay2" rel="noopener noreferrer"&gt;SO 46672001&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Does docker system prune delete volumes?
&lt;/h4&gt;

&lt;p&gt;Not by default. In the lab &lt;code&gt;docker system prune -a -f&lt;/code&gt; left the named &lt;code&gt;pgdata&lt;/code&gt; volume in place. &lt;code&gt;docker volume prune&lt;/code&gt; removes anonymous volumes, and only &lt;code&gt;docker volume prune -a&lt;/code&gt; removes named ones. The &lt;a href="https://docs.docker.com/engine/manage-resources/pruning/" rel="noopener noreferrer"&gt;pruning docs&lt;/a&gt; cover each command.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why does Docker say no space left on device when df shows free space?
&lt;/h4&gt;

&lt;p&gt;The filesystem ran out of inodes. Check with &lt;code&gt;df -i /var/lib/docker&lt;/code&gt;. In the lab &lt;code&gt;df -h&lt;/code&gt; showed 13% used while &lt;code&gt;df -i&lt;/code&gt; showed 100%, and pruning the build cache fixed it.&lt;/p&gt;

&lt;p&gt;Originally published on DeployLark: &lt;a href="https://deploylark.com/posts/docker-no-space-left-on-device/" rel="noopener noreferrer"&gt;Docker No Space Left on Device: Find and Free Disk Space&lt;/a&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>selfhosted</category>
      <category>linux</category>
    </item>
    <item>
      <title>Docker exec format error: Fix Entrypoint and Arch Mismatch</title>
      <dc:creator>DeployLark</dc:creator>
      <pubDate>Tue, 10 Nov 2026 05:46:16 +0000</pubDate>
      <link>https://dev.to/deploylark/docker-exec-format-error-fix-entrypoint-and-arch-mismatch-3ob3</link>
      <guid>https://dev.to/deploylark/docker-exec-format-error-fix-entrypoint-and-arch-mismatch-3ob3</guid>
      <description>&lt;p&gt;Your container dies the moment it starts, and &lt;code&gt;docker logs&lt;/code&gt; shows one line: &lt;code&gt;exec /entrypoint.sh: exec format error&lt;/code&gt;. Or the same path with &lt;code&gt;no such file or directory&lt;/code&gt;, even though &lt;code&gt;ls&lt;/code&gt; inside the image proves the file is there. Both messages come from the kernel refusing to start the container's first process. The cause is either the CPU architecture of the image or the first few bytes of your entrypoint script.&lt;/p&gt;

&lt;p&gt;This guide reproduces every common cause on Docker Engine 29.9.3 with &lt;code&gt;alpine:3.24.4&lt;/code&gt; on an x86_64 host, shows the one command that identifies each, and fixes it. All output below comes from that lab run on November 10, 2026. The problem is common. On that day the Stack Overflow question &lt;a href="https://stackoverflow.com/questions/42494853/standard-init-linux-go178-exec-user-process-caused-exec-format-error" rel="noopener noreferrer"&gt;exec user process caused "exec format error"&lt;/a&gt; had 423,211 views, &lt;a href="https://stackoverflow.com/questions/73285601/docker-exec-usr-bin-sh-exec-format-error" rel="noopener noreferrer"&gt;exec /usr/bin/sh: exec format error&lt;/a&gt; had 407,667 and &lt;a href="https://stackoverflow.com/questions/38905135/why-wont-my-docker-entrypoint-sh-execute" rel="noopener noreferrer"&gt;Why won't my docker-entrypoint.sh execute?&lt;/a&gt; had 287,087.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run &lt;code&gt;docker image inspect -f '&amp;amp;#123;&amp;amp;#123;.Architecture&amp;amp;#125;&amp;amp;#125;' IMAGE&lt;/code&gt; and &lt;code&gt;uname -m&lt;/code&gt;. If they differ (arm64 image, x86_64 host), rebuild with &lt;code&gt;--platform linux/amd64&lt;/code&gt; or build a multi-platform image.&lt;/li&gt;
&lt;li&gt;If the architecture matches, look at the first line of the script with &lt;code&gt;head -1 entrypoint.sh | od -c&lt;/code&gt;. It must start with &lt;code&gt;#!/bin/sh&lt;/code&gt; and end with &lt;code&gt;\n&lt;/code&gt;, not &lt;code&gt;\r\n&lt;/code&gt;, with no bytes before &lt;code&gt;#!&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;no such file or directory&lt;/code&gt; for a file that exists means the interpreter is missing: CRLF line endings, &lt;code&gt;#!/bin/bash&lt;/code&gt; on Alpine, or a glibc binary on musl.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;*.sh text eol=lf&lt;/code&gt; to &lt;code&gt;.gitattributes&lt;/code&gt; and use &lt;code&gt;COPY --chmod=755&lt;/code&gt; so the problem doesn't come back.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Why Docker says exec format error
&lt;/h2&gt;

&lt;p&gt;When a container starts, runc asks the Linux kernel to execute the entrypoint. The kernel runs two kinds of files: an ELF binary built for its own CPU, or a text file whose first two bytes are &lt;code&gt;#!&lt;/code&gt; followed by the path of an interpreter. Anything else gets &lt;code&gt;exec format error&lt;/code&gt;. If the interpreter path in &lt;code&gt;#!&lt;/code&gt; doesn't exist, it gets &lt;code&gt;no such file or directory&lt;/code&gt;, and the message names your script, not the missing interpreter. That is what makes this error confusing.&lt;/p&gt;

&lt;p&gt;Use this map to go from the message to the cause:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-exec-format-error%2F01-docker-exec-format-error-causes-fixes-diagram.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-exec-format-error%2F01-docker-exec-format-error-causes-fixes-diagram.webp" alt="Table diagram mapping Docker errors exec format error, no such file or directory and permission denied to causes such as arm64 image on amd64, missing shebang, UTF-8 BOM, CRLF line endings, bash on Alpine, glibc binary on musl and missing execute bit, with a check command and fix for each" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Old and new versions of the message
&lt;/h3&gt;

&lt;p&gt;Older Docker releases printed the error through runc's Go source file, for example &lt;code&gt;standard_init_linux.go:178: exec user process caused "exec format error"&lt;/code&gt;. Docker Engine 29.9.3 prints a shorter form with the path that failed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exec /entrypoint.sh: exec format error
exec /entrypoint.sh: no such file or directory
exec /bin/sh: exec format error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They mean the same thing. The path tells you which file the kernel rejected, and that is your first clue. A system path like &lt;code&gt;/bin/sh&lt;/code&gt; or &lt;code&gt;/bin/uname&lt;/code&gt; points at the architecture. Your own script points at the script.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check the architecture first
&lt;/h3&gt;

&lt;p&gt;Run these two commands on the machine where the container fails:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;uname -m
docker image inspect -f &lt;span class="s1"&gt;'&amp;amp;#123;&amp;amp;#123;.Os&amp;amp;#125;&amp;amp;#125;/&amp;amp;#123;&amp;amp;#123;.Architecture&amp;amp;#125;&amp;amp;#125;'&lt;/span&gt; myapp:arm64only
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the lab the host said &lt;code&gt;x86_64&lt;/code&gt; and the image said &lt;code&gt;linux/arm64&lt;/code&gt;. That mismatch alone explains the error, so fix it before you look at scripts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix an image built for the wrong CPU architecture
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://docs.docker.com/build/building/multi-platform/" rel="noopener noreferrer"&gt;Docker multi-platform docs&lt;/a&gt; put it plainly: "Containers share the host kernel, which means that the code that's running inside the container must be compatible with the host's architecture." &lt;code&gt;docker build&lt;/code&gt; targets the build machine's platform unless you pass &lt;code&gt;--platform&lt;/code&gt;, so an image built on an Apple Silicon Mac or a Raspberry Pi is &lt;code&gt;linux/arm64&lt;/code&gt;. Push it, pull it on an x86_64 server, and every binary inside it fails.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reproduce the mismatch
&lt;/h3&gt;

&lt;p&gt;The lab forced arm64 on the x86_64 host in three ways. Running an arm64 image, building an arm64 image with a &lt;code&gt;RUN&lt;/code&gt; step, and running an image that was built for arm64 only:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run --rm --platform linux/arm64 alpine:3.24.4 uname -m
docker build --platform linux/arm64 -t myapp:arm .
docker run --rm myapp:arm64only
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All three failed. The build stopped at the first &lt;code&gt;RUN&lt;/code&gt; with &lt;code&gt;exec /bin/sh: exec format error&lt;/code&gt;, which is the exact message from &lt;a href="https://stackoverflow.com/questions/73285601/docker-exec-usr-bin-sh-exec-format-error" rel="noopener noreferrer"&gt;SO 73285601&lt;/a&gt;. The plain &lt;code&gt;docker run&lt;/code&gt; printed a warning first: &lt;code&gt;The requested image's platform (linux/arm64) does not match the detected host platform (linux/amd64/v4) and no specific platform was requested&lt;/code&gt;. Look for that warning in your logs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-exec-format-error%2F02-docker-exec-format-error-arm64-image-on-amd64.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-exec-format-error%2F02-docker-exec-format-error-arm64-image-on-amd64.webp" alt="Terminal capture on an x86_64 host: running alpine 3.24.4 as linux/arm64 prints exec /bin/uname: exec format error, an arm64 build fails at RUN with exec /bin/sh: exec format error, docker image inspect shows linux/arm64, and an amd64 rebuild prints x86_64" width="800" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Build for the right platform
&lt;/h3&gt;

&lt;p&gt;If the image only needs to run on x86_64 servers, build for that platform. The lab rebuilt the same Dockerfile and it ran:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build --platform linux/amd64 -t myapp:amd64 .
docker run --rm myapp:amd64
&lt;span class="c"&gt;# x86_64&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setting &lt;code&gt;DOCKER_DEFAULT_PLATFORM=linux/amd64&lt;/code&gt; in your shell does the same for commands without the flag. A lab build with only that variable set came out as &lt;code&gt;amd64&lt;/code&gt;. In Compose, the &lt;code&gt;platform&lt;/code&gt; key does it per service. With &lt;code&gt;platform: linux/arm64&lt;/code&gt; the lab service printed &lt;code&gt;exec /bin/uname: exec format error&lt;/code&gt; and exited with code 255. With &lt;code&gt;platform: linux/amd64&lt;/code&gt; it printed &lt;code&gt;x86_64&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;alpine:3.24.4&lt;/span&gt;
    &lt;span class="na"&gt;platform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;linux/amd64&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"uname"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"-m"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To serve both Macs and servers, build a multi-platform image. Docker Engine 29 uses the containerd image store by default, so &lt;code&gt;docker build&lt;/code&gt; can produce one tag with several platforms. Cross-compiling in a stage pinned to &lt;code&gt;$BUILDPLATFORM&lt;/code&gt; avoids emulation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt; --platform=&lt;span class="nv"&gt;$BUILDPLATFORM&lt;/span&gt; golang:1.27.3-alpine3.24 AS build
&lt;span class="k"&gt;ARG&lt;/span&gt; TARGETOS TARGETARCH
&lt;span class="k"&gt;WORKDIR&lt;/span&gt; /src
&lt;span class="k"&gt;COPY&lt;/span&gt; . .
&lt;span class="k"&gt;RUN&lt;/span&gt; &lt;span class="nv"&gt;CGO_ENABLED&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0 &lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$TARGETOS&lt;/span&gt; &lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$TARGETARCH&lt;/span&gt; go build -o /out/hello .

&lt;span class="k"&gt;FROM&lt;/span&gt; alpine:3.24.4
&lt;span class="k"&gt;COPY&lt;/span&gt; --from=build /out/hello /usr/local/bin/hello
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"hello"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker build --platform linux/amd64,linux/arm64 -t hello:1.0 .
docker image ls --tree hello:1.0
docker run --rm hello:1.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The tree listed a &lt;code&gt;linux/amd64&lt;/code&gt; and a &lt;code&gt;linux/arm64&lt;/code&gt; variant, and the run printed &lt;code&gt;hello from linux/amd64&lt;/code&gt;. Docker picked the right variant for the host. To check what a published image supports before you pull it, use &lt;code&gt;docker buildx imagetools inspect alpine:3.24.4 | grep Platform:&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-exec-format-error%2F05-docker-multi-platform-build-amd64-arm64.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-exec-format-error%2F05-docker-multi-platform-build-amd64-arm64.webp" alt="Terminal capture: docker build for linux/amd64 and linux/arm64 creates hello 1.0, docker image ls --tree shows both platform variants, the container prints hello from linux/amd64, and imagetools inspect lists the platforms of alpine 3.24.4" width="799" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same docs describe running foreign images under QEMU emulation, registered with the &lt;code&gt;tonistiigi/binfmt&lt;/code&gt; image. That works for testing, but it is slower than a native image, so a matching build is the better fix for production.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix entrypoint scripts that fail with exec format error
&lt;/h2&gt;

&lt;p&gt;If the architecture matches and the error names your own script, the problem is inside the script. The lab built one Dockerfile six times with different versions of &lt;code&gt;entrypoint.sh&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt; alpine:3.24.4
&lt;span class="k"&gt;COPY&lt;/span&gt; entrypoint.sh /entrypoint.sh
&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"/entrypoint.sh"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"uname"&lt;/span&gt;, &lt;span class="s2"&gt;"-m"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two variants failed with &lt;code&gt;exec format error&lt;/code&gt;, two with &lt;code&gt;no such file or directory&lt;/code&gt; and one with &lt;code&gt;permission denied&lt;/code&gt;. Only the clean script ran:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-exec-format-error%2F03-docker-entrypoint-exec-format-error-no-such-file.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-exec-format-error%2F03-docker-entrypoint-exec-format-error-no-such-file.webp" alt="Terminal capture: the same image built with six entrypoint.sh versions, no shebang and BOM give exec format error, CRLF and bash on Alpine give no such file or directory, a non-executable file gives permission denied, and the good script prints hello from entrypoint and x86_64" width="799" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Missing shebang line
&lt;/h3&gt;

&lt;p&gt;A script without &lt;code&gt;#!&lt;/code&gt; is just text to the kernel. Bash runs such a file as a shell script when you call &lt;code&gt;./entrypoint.sh&lt;/code&gt;. In the lab it printed &lt;code&gt;hello from entrypoint&lt;/code&gt; without complaint, which hides the bug on your laptop. Docker uses the exec form and has no shell to fall back on. The top answer on SO 42494853, from Marcus Ruddick with 503 votes, is one line: "I forgot to put #!/bin/bash at the top of the sh file, problem solved."&lt;/p&gt;

&lt;p&gt;Make &lt;code&gt;#!/bin/sh&lt;/code&gt; the first line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/sh&lt;/span&gt;
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"hello from entrypoint"&lt;/span&gt;
&lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="s2"&gt;"$@"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same image ran fine when started with &lt;code&gt;--entrypoint sh&lt;/code&gt; and the script as an argument. &lt;code&gt;ENTRYPOINT ["sh", "/entrypoint.sh"]&lt;/code&gt; works as a quick workaround, but the shebang is the real fix.&lt;/p&gt;

&lt;h3&gt;
  
  
  A byte order mark before the shebang
&lt;/h3&gt;

&lt;p&gt;Some Windows editors save UTF-8 files with a byte order mark, three invisible bytes before the text. The kernel then sees &lt;code&gt;\357\273\277#!&lt;/code&gt; instead of &lt;code&gt;#!&lt;/code&gt; and treats the file like it has no shebang. &lt;code&gt;od -c&lt;/code&gt; shows it at once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run --rm --entrypoint sh ep:bom -c &lt;span class="s2"&gt;"head -1 /entrypoint.sh | od -c"&lt;/span&gt;
&lt;span class="c"&gt;# 0000000 357 273 277   #   !   /   b   i   n   /   s   h  \n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the file as UTF-8 without BOM in your editor, or strip it with GNU sed on your machine before you build:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sed -i &lt;span class="s1"&gt;'1s/^\xEF\xBB\xBF//'&lt;/span&gt; entrypoint.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't rely on running that sed inside an Alpine build stage. In the lab, the same command with BusyBox sed in &lt;code&gt;alpine:3.24.4&lt;/code&gt; left the BOM in place, and the image still failed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix exec /entrypoint.sh: no such file or directory
&lt;/h2&gt;

&lt;p&gt;This message names your script, but the missing file is the interpreter in the shebang or, for binaries, the dynamic loader. The &lt;code&gt;file&lt;/code&gt; command on your machine identifies all three cases:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-exec-format-error%2F04-docker-entrypoint-crlf-bom-file-od-diagnose.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-exec-format-error%2F04-docker-entrypoint-crlf-bom-file-od-diagnose.webp" alt="Terminal capture: file reports CRLF line terminators and UTF-8 with BOM for the broken scripts, od -c shows a carriage return after /bin/sh and BOM bytes before the shebang, /bin/bash is missing in Alpine, and a Debian uname binary in Alpine needs /lib64/ld-linux-x86-64.so.2" width="800" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Windows CRLF line endings
&lt;/h3&gt;

&lt;p&gt;A script saved with Windows line endings has a carriage return at the end of every line, including the shebang. The kernel looks for an interpreter called &lt;code&gt;/bin/sh\r&lt;/code&gt;, which doesn't exist. &lt;code&gt;file&lt;/code&gt; reports &lt;code&gt;with CRLF line terminators&lt;/code&gt; and &lt;code&gt;od -c&lt;/code&gt; shows the &lt;code&gt;\r&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0000000   #   !   /   b   i   n   /   s   h  \r  \n
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fix the copy in the image during the build. This worked in the lab with BusyBox sed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; --chmod=755 entrypoint.sh /entrypoint.sh
&lt;span class="k"&gt;RUN&lt;/span&gt; sed -i &lt;span class="s1"&gt;'s/\r$//'&lt;/span&gt; /entrypoint.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Better, stop Git from creating CRLF files. In the lab, cloning with &lt;code&gt;core.autocrlf=true&lt;/code&gt; (the usual Windows setting) turned an LF script into a CRLF script. After adding one line to &lt;code&gt;.gitattributes&lt;/code&gt;, a fresh clone with the same setting kept LF:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*.sh text eol=lf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://git-scm.com/docs/gitattributes" rel="noopener noreferrer"&gt;gitattributes docs&lt;/a&gt; use the same &lt;code&gt;*.sh text eol=lf&lt;/code&gt; line as their example. Run &lt;code&gt;git add --renormalize .&lt;/code&gt; after you add it so files already in the repo get fixed too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bash on Alpine and glibc binaries on musl
&lt;/h3&gt;

&lt;p&gt;Alpine ships BusyBox &lt;code&gt;sh&lt;/code&gt;, not bash. A script that starts with &lt;code&gt;#!/bin/bash&lt;/code&gt; fails with &lt;code&gt;no such file or directory&lt;/code&gt;, and &lt;code&gt;ls -l /bin/bash&lt;/code&gt; inside the image confirms bash is missing. Change the shebang to &lt;code&gt;#!/bin/sh&lt;/code&gt; if the script uses no bash features, or install bash in the image:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; apk add --no-cache bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Binaries hit the same wall. The lab copied &lt;code&gt;/usr/bin/uname&lt;/code&gt; from &lt;code&gt;debian:13.7-slim&lt;/code&gt; into &lt;code&gt;alpine:3.24.4&lt;/code&gt;, and it failed with &lt;code&gt;exec /app/uname: no such file or directory&lt;/code&gt;. &lt;code&gt;file&lt;/code&gt; showed why: &lt;code&gt;dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2&lt;/code&gt;. Alpine uses musl and has no glibc loader. Three fixes all worked in the lab: run the binary on a Debian base, install the compatibility layer with &lt;code&gt;apk add --no-cache gcompat&lt;/code&gt;, or build a static binary as in the Go example above with &lt;code&gt;CGO_ENABLED=0&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix permission denied and stop it coming back
&lt;/h2&gt;

&lt;p&gt;A script without the execute bit fails with a different, longer message from the daemon:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: exec: "/entrypoint.sh": permission denied
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Set the execute bit in the Dockerfile
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;COPY --chmod=755&lt;/code&gt; sets the mode as the file lands in the image. The lab's broken image ran after that one change. &lt;code&gt;RUN chmod +x /entrypoint.sh&lt;/code&gt; works as well. To store the bit in Git, so a checkout from Windows doesn't lose it, run &lt;code&gt;git update-index --chmod=+x entrypoint.sh&lt;/code&gt; and commit. In the lab &lt;code&gt;git ls-files -s&lt;/code&gt; changed from &lt;code&gt;100644&lt;/code&gt; to &lt;code&gt;100755&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  A checklist before you push
&lt;/h3&gt;

&lt;p&gt;Run these on any image that will run on another machine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker image inspect -f &lt;span class="s1"&gt;'&amp;amp;#123;&amp;amp;#123;.Os&amp;amp;#125;&amp;amp;#125;/&amp;amp;#123;&amp;amp;#123;.Architecture&amp;amp;#125;&amp;amp;#125;'&lt;/span&gt; IMAGE
file entrypoint.sh
head -1 entrypoint.sh &lt;span class="o"&gt;|&lt;/span&gt; od -c
docker run --rm IMAGE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first line must match the server's &lt;code&gt;uname -m&lt;/code&gt;. &lt;code&gt;file&lt;/code&gt; must not mention CRLF or BOM. The first bytes must be &lt;code&gt;#!&lt;/code&gt; and an interpreter that exists in the image. If the container runs locally and fails only on the server, check the architecture first.&lt;/p&gt;

&lt;p&gt;Other Docker errors have their own guides: &lt;a href="https://deploylark.com/posts/docker-sock-permission-denied/" rel="noopener noreferrer"&gt;Docker permission denied on docker.sock&lt;/a&gt;, &lt;a href="https://deploylark.com/posts/docker-port-already-allocated/" rel="noopener noreferrer"&gt;port is already allocated&lt;/a&gt;, &lt;a href="https://deploylark.com/posts/nginx-docker-host-not-found-in-upstream/" rel="noopener noreferrer"&gt;nginx host not found in upstream&lt;/a&gt; and &lt;a href="https://deploylark.com/posts/docker-registry-http-response-https-client/" rel="noopener noreferrer"&gt;server gave HTTP response to HTTPS client&lt;/a&gt; for private registries. If your entrypoint initializes a database, see &lt;a href="https://deploylark.com/posts/postgres-docker-password-authentication-failed/" rel="noopener noreferrer"&gt;PostgreSQL in Docker: password authentication failed&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h4&gt;
  
  
  What does exec format error mean in Docker?
&lt;/h4&gt;

&lt;p&gt;The kernel could not run the container's first process. Either the binary was built for another CPU architecture, or a script lacks a valid &lt;code&gt;#!&lt;/code&gt; line at the very start of the file. The path in the message tells you which file failed. See &lt;a href="https://stackoverflow.com/questions/42494853/standard-init-linux-go178-exec-user-process-caused-exec-format-error" rel="noopener noreferrer"&gt;SO 42494853&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why does an image built on my M1 Mac fail on my server?
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;docker build&lt;/code&gt; targets the platform of the machine it runs on, so an Apple Silicon Mac produces &lt;code&gt;linux/arm64&lt;/code&gt; images. An x86_64 server can't run arm64 binaries without emulation. Build with &lt;code&gt;--platform linux/amd64&lt;/code&gt; or with &lt;code&gt;--platform linux/amd64,linux/arm64&lt;/code&gt;. See &lt;a href="https://stackoverflow.com/questions/73398714/docker-fails-when-building-on-m1-macs-exec-usr-local-bin-docker-entrypoint-sh" rel="noopener noreferrer"&gt;SO 73398714&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why does Docker say no such file or directory when the file exists?
&lt;/h4&gt;

&lt;p&gt;The missing file is the interpreter named in the shebang, not your script. CRLF line endings turn &lt;code&gt;/bin/sh&lt;/code&gt; into &lt;code&gt;/bin/sh\r&lt;/code&gt;, and &lt;code&gt;#!/bin/bash&lt;/code&gt; fails on Alpine because bash isn't installed. See &lt;a href="https://stackoverflow.com/questions/51508150/standard-init-linux-go190-exec-user-process-caused-no-such-file-or-directory" rel="noopener noreferrer"&gt;SO 51508150&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why won't my docker-entrypoint.sh execute?
&lt;/h4&gt;

&lt;p&gt;Check four things in order: the image architecture, the shebang line, the line endings and the execute bit. &lt;code&gt;file entrypoint.sh&lt;/code&gt; and &lt;code&gt;ls -l&lt;/code&gt; cover the last three in seconds. See &lt;a href="https://stackoverflow.com/questions/38905135/why-wont-my-docker-entrypoint-sh-execute" rel="noopener noreferrer"&gt;SO 38905135&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  What does $'\r': command not found mean?
&lt;/h4&gt;

&lt;p&gt;The script has CRLF line endings and you ran it through a shell, for example &lt;code&gt;sh entrypoint.sh&lt;/code&gt; or &lt;code&gt;bash entrypoint.sh&lt;/code&gt;. The shell starts, but every command ends in a stray carriage return. In the lab bash printed &lt;code&gt;cd: $'/tmp\r': No such file or directory&lt;/code&gt;. Convert the file to LF with &lt;code&gt;sed -i 's/\r$//'&lt;/code&gt; or &lt;code&gt;dos2unix&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Can I run arm64 images on an amd64 host?
&lt;/h4&gt;

&lt;p&gt;Only through emulation. Docker's multi-platform docs describe registering QEMU emulators on the host with the privileged &lt;code&gt;tonistiigi/binfmt&lt;/code&gt; image (tag &lt;code&gt;qemu-v10.2.3&lt;/code&gt; at the time of writing). Without it, the lab's arm64 containers failed with &lt;code&gt;exec format error&lt;/code&gt;. For production, a native amd64 or multi-platform image is faster.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>dockerfile</category>
      <category>devops</category>
      <category>selfhosted</category>
    </item>
    <item>
      <title>Nginx in Docker: Fix host not found in upstream</title>
      <dc:creator>DeployLark</dc:creator>
      <pubDate>Mon, 09 Nov 2026 03:00:11 +0000</pubDate>
      <link>https://dev.to/deploylark/nginx-in-docker-fix-host-not-found-in-upstream-3b53</link>
      <guid>https://dev.to/deploylark/nginx-in-docker-fix-host-not-found-in-upstream-3b53</guid>
      <description>&lt;p&gt;Your nginx container exits a second after it starts, and &lt;code&gt;docker logs&lt;/code&gt; shows &lt;code&gt;nginx: [emerg] host not found in upstream "app" in /etc/nginx/conf.d/default.conf:4&lt;/code&gt;. nginx looks up every hostname in &lt;code&gt;proxy_pass&lt;/code&gt; once, at startup. If Docker's DNS can't answer for &lt;code&gt;app&lt;/code&gt; at that moment, nginx refuses to run at all.&lt;/p&gt;

&lt;p&gt;This guide reproduces the error with &lt;code&gt;nginx:1.30.6-alpine&lt;/code&gt; in front of &lt;code&gt;traefik/whoami:v1.12.0&lt;/code&gt; on Docker Engine 29.9.2, then fixes each cause. Every message below comes from that lab run on November 9, 2026. Plenty of people hit this. On that day the Stack Overflow question &lt;a href="https://stackoverflow.com/questions/33639138/docker-networking-nginx-emerg-host-not-found-in-upstream" rel="noopener noreferrer"&gt;Docker Networking - nginx: [emerg] host not found in upstream&lt;/a&gt; had 302,677 views and &lt;a href="https://stackoverflow.com/questions/42720618/nginx-how-to-resolve-stopped-emerg-11-host-not-found-in-upstream" rel="noopener noreferrer"&gt;stopped: [emerg] 1#1: host not found in upstream&lt;/a&gt; had 177,100.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The app container must be running and attached to the same user-defined network as nginx when nginx starts.&lt;/li&gt;
&lt;li&gt;The default bridge network has no container DNS. Create a network with &lt;code&gt;docker network create&lt;/code&gt; and attach both containers.&lt;/li&gt;
&lt;li&gt;Two Compose projects get two separate networks. Join both to one external network.&lt;/li&gt;
&lt;li&gt;Use the Compose service name, a network alias or &lt;code&gt;container_name&lt;/code&gt;. The generated name like &lt;code&gt;projc-app-1&lt;/code&gt; does not resolve once &lt;code&gt;container_name&lt;/code&gt; is set.&lt;/li&gt;
&lt;li&gt;To let nginx start without the app and follow IP changes, add &lt;code&gt;resolver 127.0.0.11 valid=10s&lt;/code&gt; and either a variable in &lt;code&gt;proxy_pass&lt;/code&gt; or &lt;code&gt;server app:3000 resolve&lt;/code&gt; in an &lt;code&gt;upstream&lt;/code&gt; block.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Why nginx says host not found in upstream
&lt;/h2&gt;

&lt;p&gt;Start with the map. Match your setup to a cause, check it, then apply the fix:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fnginx-docker-host-not-found-in-upstream%2F01-nginx-host-not-found-in-upstream-causes-fixes-diagram.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fnginx-docker-host-not-found-in-upstream%2F01-nginx-host-not-found-in-upstream-causes-fixes-diagram.webp" alt="Table diagram of nginx host not found in upstream causes in Docker: app not running, default bridge, different Compose networks and wrong name, with the command to check and the fix for each" width="800" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Reproduce it with a missing app
&lt;/h3&gt;

&lt;p&gt;This is the whole nginx config used in the lab:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="k"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kn"&gt;proxy_pass&lt;/span&gt; &lt;span class="s"&gt;http://app:3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start nginx on a user-defined network before any container called &lt;code&gt;app&lt;/code&gt; exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker network create labnet
docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; proxy &lt;span class="nt"&gt;--network&lt;/span&gt; labnet &lt;span class="nt"&gt;-p&lt;/span&gt; 8080:80 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PWD&lt;/span&gt;&lt;span class="s2"&gt;/static.conf:/etc/nginx/conf.d/default.conf:ro"&lt;/span&gt; nginx:1.30.6-alpine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The container exited with code 1 two seconds later:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nginx: [emerg] host not found in upstream "app" in /etc/nginx/conf.d/default.conf:4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The line number points at the &lt;code&gt;proxy_pass&lt;/code&gt; line. If you define the backend in an &lt;code&gt;upstream&lt;/code&gt; block instead, the message includes the port, &lt;code&gt;host not found in upstream "app:3000"&lt;/code&gt;, and points at the &lt;code&gt;server&lt;/code&gt; line. &lt;code&gt;nginx -t&lt;/code&gt; fails with the same message, so you can test a config before you deploy it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fnginx-docker-host-not-found-in-upstream%2F02-nginx-emerg-host-not-found-app-not-running.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fnginx-docker-host-not-found-in-upstream%2F02-nginx-emerg-host-not-found-app-not-running.webp" alt="Terminal capture: nginx 1.30.6 container started on labnet shows Exited (1) and logs nginx emerg host not found in upstream app in default.conf line 4" width="800" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Start the app on the same network
&lt;/h3&gt;

&lt;p&gt;Containers on a user-defined network find each other through Docker's embedded DNS server at &lt;code&gt;127.0.0.11&lt;/code&gt;. Start the app with the name nginx expects, then start nginx again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; app &lt;span class="nt"&gt;--network&lt;/span&gt; labnet &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;WHOAMI_PORT_NUMBER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3000 traefik/whoami:v1.12.0
docker start proxy
curl &lt;span class="nt"&gt;-s&lt;/span&gt; localhost:8080 | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-1&lt;/span&gt;
docker &lt;span class="nb"&gt;exec &lt;/span&gt;proxy getent hosts app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The proxy stayed up, &lt;code&gt;curl&lt;/code&gt; returned the whoami page, and &lt;code&gt;getent&lt;/code&gt; showed &lt;code&gt;172.19.0.2 app&lt;/code&gt;. Inside the container, &lt;code&gt;/etc/resolv.conf&lt;/code&gt; listed &lt;code&gt;nameserver 127.0.0.11&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix the default bridge network
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why plain docker run fails
&lt;/h3&gt;

&lt;p&gt;Run both containers without &lt;code&gt;--network&lt;/code&gt; and they land on the default &lt;code&gt;bridge&lt;/code&gt; network:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; app &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="nv"&gt;WHOAMI_PORT_NUMBER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3000 traefik/whoami:v1.12.0
docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; proxy &lt;span class="nt"&gt;-p&lt;/span&gt; 8080:80 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$PWD&lt;/span&gt;&lt;span class="s2"&gt;/static.conf:/etc/nginx/conf.d/default.conf:ro"&lt;/span&gt; nginx:1.30.6-alpine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The app was up, both containers showed &lt;code&gt;bridge&lt;/code&gt; in &lt;code&gt;docker ps&lt;/code&gt;, and nginx still exited with the same &lt;code&gt;[emerg]&lt;/code&gt; line. A &lt;code&gt;busybox:1.37.0&lt;/code&gt; lookup of &lt;code&gt;app&lt;/code&gt; on that network returned &lt;code&gt;NXDOMAIN&lt;/code&gt;. The &lt;a href="https://docs.docker.com/engine/network/drivers/bridge/" rel="noopener noreferrer"&gt;Docker bridge network docs&lt;/a&gt; say it plainly: "Containers on the default bridge network can only access each other by IP addresses, unless you use the &lt;code&gt;--link&lt;/code&gt; option, which is considered legacy."&lt;/p&gt;

&lt;p&gt;Check which network a container uses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker inspect &lt;span class="nt"&gt;-f&lt;/span&gt; &lt;span class="s1"&gt;'&amp;amp;#123;&amp;amp;#123;.HostConfig.NetworkMode&amp;amp;#125;&amp;amp;#125;'&lt;/span&gt; app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It printed &lt;code&gt;bridge&lt;/code&gt; for the plain &lt;code&gt;docker run&lt;/code&gt; container.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connect both containers to a user-defined network
&lt;/h3&gt;

&lt;p&gt;You don't need to recreate anything. Create a network, connect both containers and start nginx:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker network create web
docker network connect web app
docker network connect web proxy
docker start proxy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;curl -s -o /dev/null -w '%{http_code}\n' localhost:8080&lt;/code&gt; returned &lt;code&gt;200&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix separate Docker Compose projects
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Each project gets its own network
&lt;/h3&gt;

&lt;p&gt;A common layout keeps the reverse proxy in one directory and each app in another. The &lt;a href="https://docs.docker.com/compose/how-tos/networking/" rel="noopener noreferrer"&gt;Compose networking docs&lt;/a&gt; explain the catch: "Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by its service name." That default network belongs to one project only.&lt;/p&gt;

&lt;p&gt;In the lab, &lt;code&gt;projA&lt;/code&gt; ran the app service and &lt;code&gt;projB&lt;/code&gt; ran nginx with the same config. &lt;code&gt;docker compose ls -a&lt;/code&gt; showed &lt;code&gt;proja&lt;/code&gt; as &lt;code&gt;running(1)&lt;/code&gt; and &lt;code&gt;projb&lt;/code&gt; as &lt;code&gt;exited(1)&lt;/code&gt;. The proxy logged &lt;code&gt;host not found in upstream "app"&lt;/code&gt;, and &lt;code&gt;docker network ls&lt;/code&gt; listed two networks, &lt;code&gt;proja_default&lt;/code&gt; and &lt;code&gt;projb_default&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Join both projects to an external network
&lt;/h3&gt;

&lt;p&gt;Create the shared network once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker network create edge
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add it to the app project. Keep &lt;code&gt;default&lt;/code&gt; too if the app talks to its own database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;traefik/whoami:v1.12.0&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;WHOAMI_PORT_NUMBER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;3000"&lt;/span&gt;
    &lt;span class="na"&gt;networks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;default&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;edge&lt;/span&gt;

&lt;span class="na"&gt;networks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;edge&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;external&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And to the proxy project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;proxy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx:1.30.6-alpine&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;8080:80"&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;../conf/static.conf:/etc/nginx/conf.d/default.conf:ro&lt;/span&gt;
    &lt;span class="na"&gt;networks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;edge&lt;/span&gt;

&lt;span class="na"&gt;networks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;edge&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;external&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;docker compose up -d&lt;/code&gt; in each directory. The proxy returned the whoami page, and &lt;code&gt;docker network inspect edge&lt;/code&gt; listed &lt;code&gt;proja-app-1&lt;/code&gt; and &lt;code&gt;projb-proxy-1&lt;/code&gt;. If you forget the &lt;code&gt;docker network create&lt;/code&gt; step, Compose stops with &lt;code&gt;network edge declared as external, but could not be found&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fnginx-docker-host-not-found-in-upstream%2F03-docker-compose-two-projects-external-network-fix.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fnginx-docker-host-not-found-in-upstream%2F03-docker-compose-two-projects-external-network-fix.webp" alt="Terminal capture: docker compose ls shows proja running and projb exited, proxy logs host not found in upstream app, two default networks, then after creating the edge network curl returns the whoami hostname" width="800" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Which names resolve
&lt;/h3&gt;

&lt;p&gt;In one Compose project with service &lt;code&gt;app&lt;/code&gt;, &lt;code&gt;container_name: myapp&lt;/code&gt; and a network alias &lt;code&gt;backend&lt;/code&gt;, the lab tested names from inside the nginx container with &lt;code&gt;getent hosts&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;app&lt;/code&gt; (service name)&lt;/td&gt;
&lt;td&gt;resolves&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;myapp&lt;/code&gt; (container_name)&lt;/td&gt;
&lt;td&gt;resolves&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;backend&lt;/code&gt; (network alias)&lt;/td&gt;
&lt;td&gt;resolves&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;App&lt;/code&gt; (different case)&lt;/td&gt;
&lt;td&gt;resolves&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;projc-app-1&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;not found&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ap&lt;/code&gt; (typo)&lt;/td&gt;
&lt;td&gt;not found&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Copy the service name from &lt;code&gt;compose.yaml&lt;/code&gt; into &lt;code&gt;proxy_pass&lt;/code&gt;. A typo produces the same &lt;code&gt;[emerg]&lt;/code&gt; error as a missing container.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 502 errors and startup order
&lt;/h2&gt;

&lt;h3&gt;
  
  
  nginx keeps the old IP address
&lt;/h3&gt;

&lt;p&gt;The static config has a second problem. nginx resolved &lt;code&gt;app&lt;/code&gt; once, so it keeps that IP until it restarts. In the lab, the app had &lt;code&gt;172.22.0.2&lt;/code&gt;. After &lt;code&gt;docker compose stop app&lt;/code&gt;, another container took that address, and &lt;code&gt;docker compose up -d app&lt;/code&gt; gave the app &lt;code&gt;172.22.0.4&lt;/code&gt;. nginx still sent traffic to the old one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[error] 23#23: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 172.22.0.1, upstream: "http://172.22.0.2:3000/"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;curl&lt;/code&gt; returned &lt;code&gt;502&lt;/code&gt;. Restarting nginx fixes it until the next redeploy. When the app is down at that moment, a restart brings back the &lt;code&gt;[emerg]&lt;/code&gt; exit. The lab stopped the app, ran &lt;code&gt;docker compose restart proxy&lt;/code&gt;, and the proxy exited with code 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fnginx-docker-host-not-found-in-upstream%2F04-nginx-502-stale-ip-after-container-recreate.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fnginx-docker-host-not-found-in-upstream%2F04-nginx-502-stale-ip-after-container-recreate.webp" alt="Terminal capture: app container moves from 172.22.0.2 to 172.22.0.4 after recreate, curl through nginx returns 502 and the log shows connect failed to the old upstream 172.22.0.2 port 3000" width="799" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  depends_on helps only at first start
&lt;/h3&gt;

&lt;p&gt;A popular answer by &lt;a href="https://stackoverflow.com/a/39039830" rel="noopener noreferrer"&gt;czerasz&lt;/a&gt; on the original question says: "This can be solved with the mentioned depends_on directive since it's implemented now (2016)". In the lab, &lt;code&gt;depends_on: [app]&lt;/code&gt; in the same project did make the first &lt;code&gt;docker compose up -d&lt;/code&gt; work. It did not help when the app was recreated or stopped later, as shown above. It also does nothing across two projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resolve the upstream at run time
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use resolver 127.0.0.11 with a variable
&lt;/h3&gt;

&lt;p&gt;When &lt;code&gt;proxy_pass&lt;/code&gt; contains a variable, nginx resolves the name per request through the &lt;code&gt;resolver&lt;/code&gt; you set, not at startup. The &lt;a href="https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass" rel="noopener noreferrer"&gt;proxy_pass docs&lt;/a&gt; describe this. Point the resolver at Docker's DNS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="k"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;resolver&lt;/span&gt; &lt;span class="mf"&gt;127.0&lt;/span&gt;&lt;span class="s"&gt;.0.11&lt;/span&gt; &lt;span class="s"&gt;valid=10s&lt;/span&gt; &lt;span class="s"&gt;ipv6=off&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kn"&gt;set&lt;/span&gt; &lt;span class="nv"&gt;$upstream&lt;/span&gt; &lt;span class="s"&gt;http://app:3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kn"&gt;proxy_pass&lt;/span&gt; &lt;span class="nv"&gt;$upstream&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this config, nginx started with no app container. Requests returned &lt;code&gt;502&lt;/code&gt; and the log showed &lt;code&gt;app could not be resolved (3: Host not found)&lt;/code&gt;. One second after &lt;code&gt;docker compose up -d app&lt;/code&gt;, the same URL returned &lt;code&gt;200&lt;/code&gt;. After the stale IP test from the previous section, it returned &lt;code&gt;200&lt;/code&gt; within 11 seconds without a restart. Drop the &lt;code&gt;resolver&lt;/code&gt; line and requests fail with &lt;code&gt;no resolver defined to resolve app&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One catch. If the variable includes a path, such as &lt;code&gt;http://app:3000/api/&lt;/code&gt;, the proxy_pass docs say nginx passes that URI as is and replaces the original request URI. Prefix rewriting from a &lt;code&gt;location&lt;/code&gt; no longer happens, so test your paths after the change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fnginx-docker-host-not-found-in-upstream%2F05-nginx-resolver-127-0-0-11-runtime-dns.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fnginx-docker-host-not-found-in-upstream%2F05-nginx-resolver-127-0-0-11-runtime-dns.webp" alt="Terminal capture: nginx with resolver 127.0.0.11 starts without the app, returns 502 with app could not be resolved Host not found, then 200 after the app container starts" width="800" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Use the resolve parameter in an upstream block
&lt;/h3&gt;

&lt;p&gt;Open source nginx can also re-resolve servers inside an &lt;code&gt;upstream&lt;/code&gt; block. The &lt;a href="https://nginx.org/en/docs/http/ngx_http_upstream_module.html#resolve" rel="noopener noreferrer"&gt;upstream module docs&lt;/a&gt; note: "In order for this parameter to work, the resolver directive must be specified in the http block or in the corresponding upstream block. Prior to version 1.27.3, this parameter was available only as part of our commercial subscription." The 1.30.6 image includes it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="k"&gt;resolver&lt;/span&gt; &lt;span class="mf"&gt;127.0&lt;/span&gt;&lt;span class="s"&gt;.0.11&lt;/span&gt; &lt;span class="s"&gt;valid=10s&lt;/span&gt; &lt;span class="s"&gt;ipv6=off&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;upstream&lt;/span&gt; &lt;span class="s"&gt;app_backend&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;zone&lt;/span&gt; &lt;span class="s"&gt;app_backend&lt;/span&gt; &lt;span class="mi"&gt;64k&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;server&lt;/span&gt; &lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt; &lt;span class="s"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kn"&gt;proxy_pass&lt;/span&gt; &lt;span class="s"&gt;http://app_backend&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;nginx started without the app and returned &lt;code&gt;502&lt;/code&gt; with &lt;code&gt;no live upstreams&lt;/code&gt;. Within 11 seconds of starting the app it returned &lt;code&gt;200&lt;/code&gt;, and it followed the app to a new IP the same way. Without the &lt;code&gt;resolver&lt;/code&gt; line, &lt;code&gt;nginx -t&lt;/code&gt; failed with &lt;code&gt;no resolver defined to resolve names at run time in upstream "app_backend"&lt;/code&gt;. This form keeps normal &lt;code&gt;proxy_pass&lt;/code&gt; URI handling, so it is the cleaner choice on nginx 1.27.3 or later.&lt;/p&gt;

&lt;p&gt;For other reverse proxies, see &lt;a href="https://deploylark.com/posts/traefik-docker-labels-404-502/" rel="noopener noreferrer"&gt;Traefik Docker labels: fix 404, 502 and gateway timeout&lt;/a&gt; and &lt;a href="https://deploylark.com/posts/nginx-proxy-manager-docker-ssl/" rel="noopener noreferrer"&gt;Nginx Proxy Manager with SSL&lt;/a&gt;. If nginx starts but the host port clashes, read &lt;a href="https://deploylark.com/posts/docker-port-already-allocated/" rel="noopener noreferrer"&gt;Docker port is already allocated&lt;/a&gt;. Apps behind nginx often need proxy headers too, as in &lt;a href="https://deploylark.com/posts/keycloak-reverse-proxy-https-required/" rel="noopener noreferrer"&gt;Keycloak behind a reverse proxy&lt;/a&gt; and &lt;a href="https://deploylark.com/posts/home-assistant-reverse-proxy-400-bad-request/" rel="noopener noreferrer"&gt;Home Assistant 400 Bad Request&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h4&gt;
  
  
  What does "host not found in upstream" mean in nginx?
&lt;/h4&gt;

&lt;p&gt;nginx could not resolve a hostname from &lt;code&gt;proxy_pass&lt;/code&gt; or an &lt;code&gt;upstream&lt;/code&gt; server while loading its config, so it stopped. In Docker the name is usually a container that is not running or not on nginx's network. See &lt;a href="https://stackoverflow.com/questions/42720618/nginx-how-to-resolve-stopped-emerg-11-host-not-found-in-upstream" rel="noopener noreferrer"&gt;SO 42720618&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I reach another container from a Dockerized nginx?
&lt;/h4&gt;

&lt;p&gt;Put both containers on the same user-defined network and use the other container's service name or container name with its internal port, for example &lt;code&gt;http://app:3000&lt;/code&gt;. Don't use &lt;code&gt;localhost&lt;/code&gt;, which points at the nginx container itself. See &lt;a href="https://stackoverflow.com/questions/39202964/how-to-reach-another-container-from-a-dockerised-nginx" rel="noopener noreferrer"&gt;SO 39202964&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Does depends_on fix host not found in upstream?
&lt;/h4&gt;

&lt;p&gt;Only for the first start of a single Compose project. In the lab, a recreated or stopped app still broke nginx, and &lt;code&gt;depends_on&lt;/code&gt; cannot point at a service in another project. Run-time resolution covers those cases.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why does nginx return 502 after I recreate a container?
&lt;/h4&gt;

&lt;p&gt;nginx cached the container's old IP at startup. The recreated container got a new address, and nginx kept connecting to the old one. Restart nginx, or add &lt;code&gt;resolver 127.0.0.11 valid=10s&lt;/code&gt; with a variable or &lt;code&gt;resolve&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is 127.0.0.11 in Docker?
&lt;/h4&gt;

&lt;p&gt;It is Docker's embedded DNS server. Containers on user-defined networks get &lt;code&gt;nameserver 127.0.0.11&lt;/code&gt; in &lt;code&gt;/etc/resolv.conf&lt;/code&gt;, and it answers service names, container names and aliases. Containers on the default bridge use the daemon's normal DNS servers instead and can't look up container names.&lt;/p&gt;

&lt;h4&gt;
  
  
  Can nginx start when the upstream container is down?
&lt;/h4&gt;

&lt;p&gt;Yes, with a &lt;code&gt;resolver&lt;/code&gt; and either a variable in &lt;code&gt;proxy_pass&lt;/code&gt; or &lt;code&gt;server ... resolve&lt;/code&gt;. nginx then starts, returns 502 until the app is up, and recovers without a restart. With a plain hostname in &lt;code&gt;proxy_pass&lt;/code&gt;, it exits with &lt;code&gt;[emerg]&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>nginx</category>
      <category>docker</category>
      <category>devops</category>
      <category>selfhosted</category>
    </item>
    <item>
      <title>Docker Port Is Already Allocated: Fix Address Already in Use</title>
      <dc:creator>DeployLark</dc:creator>
      <pubDate>Sun, 08 Nov 2026 06:19:56 +0000</pubDate>
      <link>https://dev.to/deploylark/docker-port-is-already-allocated-fix-address-already-in-use-2fln</link>
      <guid>https://dev.to/deploylark/docker-port-is-already-allocated-fix-address-already-in-use-2fln</guid>
      <description>&lt;p&gt;You run &lt;code&gt;docker run -p 8080:80&lt;/code&gt; or &lt;code&gt;docker compose up&lt;/code&gt; and Docker refuses to start the container. The message is either &lt;code&gt;Bind for 0.0.0.0:8080 failed: port is already allocated&lt;/code&gt; or &lt;code&gt;Error starting userland proxy: listen tcp4 0.0.0.0:8080: bind: address already in use&lt;/code&gt;. The two look alike, but they point at different owners of the port, and the fix depends on which one you got.&lt;/p&gt;

&lt;p&gt;This guide reproduces both errors with &lt;code&gt;nginx:1.30.6-alpine&lt;/code&gt; on Docker Engine 26.1.5, then repeats them on Docker Engine 29.9.2 to show the newer wording. Every message below comes from that lab run on November 8, 2026. Demand for this fix is huge. On that day the Stack Overflow question &lt;a href="https://stackoverflow.com/questions/37971961/docker-error-bind-address-already-in-use" rel="noopener noreferrer"&gt;Docker Error bind: address already in use&lt;/a&gt; had 1,095,795 views and &lt;a href="https://stackoverflow.com/questions/46176584/docker-bind-for-0-0-0-04000-failed-port-is-already-allocated" rel="noopener noreferrer"&gt;Bind for 0.0.0.0:4000 failed: port is already allocated&lt;/a&gt; had 610,285.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;port is already allocated&lt;/code&gt; means another container already publishes that host port. Find it with &lt;code&gt;docker ps --filter publish=8080&lt;/code&gt;, then stop it or change your host port.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;bind: address already in use&lt;/code&gt; means something outside Docker's port table holds the port, usually a host service. Find it with &lt;code&gt;sudo ss -ltnp 'sport = :8080'&lt;/code&gt; and stop that service.&lt;/li&gt;
&lt;li&gt;With Compose, the owner is often a project in another directory. Read its labels, then run &lt;code&gt;docker compose -p NAME down&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A failed &lt;code&gt;docker run&lt;/code&gt; leaves a container in the &lt;code&gt;Created&lt;/code&gt; state. Remove it before you retry with the same name.&lt;/li&gt;
&lt;li&gt;Stopping a container frees its port. Removing it is not required.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Fix port is already allocated
&lt;/h2&gt;

&lt;p&gt;Start with the map. Match the message you see to the cause and the fix:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-port-already-allocated%2F01-docker-port-conflict-error-cause-fix-diagram.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-port-already-allocated%2F01-docker-port-conflict-error-cause-fix-diagram.webp" alt="Table diagram mapping Docker port errors to causes and fixes: port is already allocated, bind address already in use, and the container name conflict after a failed run" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Reproduce it with two containers
&lt;/h3&gt;

&lt;p&gt;The first container takes host port 8080. The second asks for the same host port:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; web1 &lt;span class="nt"&gt;-p&lt;/span&gt; 8080:80 nginx:1.30.6-alpine
docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; web2 &lt;span class="nt"&gt;-p&lt;/span&gt; 8080:80 nginx:1.30.6-alpine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The second command failed with exit code 125:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker: Error response from daemon: driver failed programming external connectivity on endpoint web2 (494c5c95...): Bind for 0.0.0.0:8080 failed: port is already allocated.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Docker tracks every published host port. When a running container already holds &lt;code&gt;0.0.0.0:8080&lt;/code&gt;, the daemon rejects the new mapping from its own records. The container port (80 in both cases) does not matter. Two containers can both listen on port 80 inside their own network namespaces. Only the host side collides. As the accepted Stack Overflow answer by &lt;a href="https://stackoverflow.com/a/46177519" rel="noopener noreferrer"&gt;yamenk&lt;/a&gt; puts it: "You need to make sure that the previous container you launched is killed, before launching a new one that uses the same port."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-port-already-allocated%2F02-docker-port-already-allocated-two-containers.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-port-already-allocated%2F02-docker-port-already-allocated-two-containers.webp" alt="Terminal capture: second nginx container fails with Bind for 0.0.0.0:8080 failed port is already allocated, docker ps filter publish=8080 shows web1, and ss shows docker-proxy listening" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Find the container that owns the port
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;docker ps&lt;/code&gt; has a filter for published ports, so you don't have to scan the whole list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker ps &lt;span class="nt"&gt;--filter&lt;/span&gt; &lt;span class="nv"&gt;publish&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;8080 &lt;span class="nt"&gt;--format&lt;/span&gt; &lt;span class="s2"&gt;"table &amp;amp;#123;&amp;amp;#123;.Names&amp;amp;#125;&amp;amp;#125;&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;#123;&amp;amp;#123;.Status&amp;amp;#125;&amp;amp;#125;&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;#123;&amp;amp;#123;.Ports&amp;amp;#125;&amp;amp;#125;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAMES     STATUS         PORTS
web1      Up 5 seconds   0.0.0.0:8080-&amp;gt;80/tcp, :::8080-&amp;gt;80/tcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the host, &lt;code&gt;ss&lt;/code&gt; showed the listener as &lt;code&gt;docker-proxy&lt;/code&gt;, one process for IPv4 and one for IPv6:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LISTEN 0      4096         0.0.0.0:8080      0.0.0.0:*    users:(("docker-proxy",pid=751939,fd=4))
LISTEN 0      4096            [::]:8080         [::]:*    users:(("docker-proxy",pid=751946,fd=4))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you see &lt;code&gt;docker-proxy&lt;/code&gt; there, the owner is a container. Don't kill the proxy process. Stop the container instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  Free the port or pick another one
&lt;/h3&gt;

&lt;p&gt;You have two clean options. Stop the old container (&lt;code&gt;docker stop web1&lt;/code&gt;), or move the new one to a free host port such as &lt;code&gt;-p 8081:80&lt;/code&gt;. In the lab, &lt;code&gt;docker stop web1&lt;/code&gt; released 8080 at once. &lt;code&gt;ss&lt;/code&gt; showed no listener and a new container took 8080 without errors. You don't need &lt;code&gt;docker rm&lt;/code&gt; to free a port.&lt;/p&gt;

&lt;p&gt;One trap waits on the retry. The failed run still created &lt;code&gt;web2&lt;/code&gt;, and &lt;code&gt;docker ps -a&lt;/code&gt; listed it as &lt;code&gt;Created&lt;/code&gt;. Running again with the same name, even on a new port, failed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker: Error response from daemon: Conflict. The container name "/web2" is already in use by container "dfb7802a...". You have to remove (or rename) that container to be able to reuse that name.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;docker rm web2&lt;/code&gt; first. After that, &lt;code&gt;docker run -d --name web2 -p 8081:80 nginx:1.30.6-alpine&lt;/code&gt; started, and both &lt;code&gt;http://127.0.0.1:8080/&lt;/code&gt; and &lt;code&gt;http://127.0.0.1:8081/&lt;/code&gt; returned 200.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix bind: address already in use
&lt;/h2&gt;

&lt;h3&gt;
  
  
  When no container owns the port
&lt;/h3&gt;

&lt;p&gt;For this case the lab started a plain host process on port 8082 with &lt;code&gt;python3 -m http.server 8082&lt;/code&gt;. Then it asked Docker for the same port:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker: Error response from daemon: driver failed programming external connectivity on endpoint web3 (3f7ab854...): Error starting userland proxy: listen tcp4 0.0.0.0:8082: bind: address already in use.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;docker ps --filter publish=8082&lt;/code&gt; printed nothing. Docker's own table was clean, so the daemon tried to open the socket and the kernel said no. That is the difference between the two messages. "Already allocated" comes from Docker's records. "Address already in use" comes from the operating system.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-port-already-allocated%2F03-docker-bind-address-already-in-use-host-process.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-port-already-allocated%2F03-docker-bind-address-already-in-use-host-process.webp" alt="Terminal capture: Docker fails with listen tcp4 0.0.0.0:8082 bind address already in use, ss shows python3 owns the port, and docker start succeeds after the process stops" width="799" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Find the process with ss and stop it
&lt;/h3&gt;

&lt;p&gt;Ask the kernel who is listening:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;ss &lt;span class="nt"&gt;-ltnp&lt;/span&gt; &lt;span class="s1"&gt;'sport = :8082'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LISTEN 0      5            0.0.0.0:8082      0.0.0.0:*    users:(("python3",pid=752469,fd=5))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run it with &lt;code&gt;sudo&lt;/code&gt;. In the lab, plain &lt;code&gt;ss&lt;/code&gt; without root listed the docker-proxy socket on 8080 with an empty process column. This tip comes from a highly voted answer by &lt;a href="https://stackoverflow.com/a/58604402" rel="noopener noreferrer"&gt;Jamie&lt;/a&gt; on the Postgres port question: "If lsof -i :5432 doesn't show you any output, you can use sudo ss -lptn 'sport = :5432' to see what process is bound to the port."&lt;/p&gt;

&lt;p&gt;After &lt;code&gt;kill 752469&lt;/code&gt;, the listener was gone. The failed container already existed, so &lt;code&gt;docker start web3&lt;/code&gt; was enough, and port 8082 returned 200. On a real server, the owner is usually a packaged service: nginx or apache2 on 80 and 443, postgresql on 5432, mysql on 3306. Stop and disable it with &lt;code&gt;sudo systemctl disable --now nginx&lt;/code&gt;, or move your container to another host port. If a database on the host is the reason you can't connect at all, the &lt;a href="https://deploylark.com/posts/postgres-docker-password-authentication-failed/" rel="noopener noreferrer"&gt;PostgreSQL Docker auth guide&lt;/a&gt; and the &lt;a href="https://deploylark.com/posts/mysql-docker-access-denied-root/" rel="noopener noreferrer"&gt;MySQL Docker access denied guide&lt;/a&gt; cover the login errors that follow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix port conflicts in Docker Compose
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Another Compose project holds the port
&lt;/h3&gt;

&lt;p&gt;Compose names projects after their folder. Two folders with the same service and the same &lt;code&gt;ports:&lt;/code&gt; line collide:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;web&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx:1.30.6-alpine&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;8090:80"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;docker compose up -d&lt;/code&gt; in &lt;code&gt;projA&lt;/code&gt; started. The same command in &lt;code&gt;projB&lt;/code&gt; failed with &lt;code&gt;Bind for 0.0.0.0:8090 failed: port is already allocated&lt;/code&gt;. Running &lt;code&gt;docker compose down&lt;/code&gt; in &lt;code&gt;projB&lt;/code&gt; would not help, because &lt;code&gt;projB&lt;/code&gt; doesn't own the port.&lt;/p&gt;

&lt;p&gt;Compose writes the project name and its folder into container labels. Ask for them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker ps &lt;span class="nt"&gt;--filter&lt;/span&gt; &lt;span class="nv"&gt;publish&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;8090 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--format&lt;/span&gt; &lt;span class="s2"&gt;"table &amp;amp;#123;&amp;amp;#123;.Names&amp;amp;#125;&amp;amp;#125;&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;#123;&amp;amp;#123;.Label &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;com.docker.compose.project&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;#125;&amp;amp;#125;&lt;/span&gt;&lt;span class="se"&gt;\t&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;#123;&amp;amp;#123;.Label &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;com.docker.compose.project.working_dir&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;#125;&amp;amp;#125;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output named &lt;code&gt;proja&lt;/code&gt; and its full path. &lt;code&gt;docker compose ls&lt;/code&gt; listed the same project with its compose file. &lt;code&gt;docker compose -p proja down&lt;/code&gt; removed it from any directory, and &lt;code&gt;projB&lt;/code&gt; then started and served 200 on 8090.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-port-already-allocated%2F04-docker-compose-port-conflict-other-project.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-port-already-allocated%2F04-docker-compose-port-conflict-other-project.webp" alt="Terminal capture: second Compose project fails with port 8090 already allocated, docker ps labels reveal project proja and its folder, and docker compose -p proja down frees the port" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;restart: unless-stopped&lt;/code&gt; line matters here. A container with a restart policy comes back with the Docker daemon. In the 29.9.2 lab, a container started with &lt;code&gt;--restart unless-stopped&lt;/code&gt; was up again on its port right after dockerd restarted, so an old stack you forgot about can hold a port after every reboot. Take it down with &lt;code&gt;docker compose down&lt;/code&gt;, not just &lt;code&gt;docker stop&lt;/code&gt;. A UI like &lt;a href="https://deploylark.com/posts/portainer-ce-install-stacks/" rel="noopener noreferrer"&gt;Portainer&lt;/a&gt; also shows each stack and its published ports in one place.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scaling a service with a fixed host port
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;docker compose up -d --scale web=2&lt;/code&gt; on the same file started &lt;code&gt;web-1&lt;/code&gt; and then failed on &lt;code&gt;web-2&lt;/code&gt; with &lt;code&gt;Bind for 0.0.0.0:8090 failed: port is already allocated&lt;/code&gt;. Every replica asks for the same host port. Give Compose a range instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;web&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx:1.30.6-alpine&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;8091-8092:80"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the range, both replicas started. &lt;code&gt;docker compose ps&lt;/code&gt; showed one on 8091 and one on 8092. If you put a reverse proxy in front, you can drop the host port entirely and let the proxy reach the containers over the Compose network. The &lt;a href="https://deploylark.com/posts/traefik-docker-labels-404-502/" rel="noopener noreferrer"&gt;Traefik labels guide&lt;/a&gt; and the &lt;a href="https://deploylark.com/posts/nginx-proxy-manager-docker-ssl/" rel="noopener noreferrer"&gt;Nginx Proxy Manager guide&lt;/a&gt; both work that way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bind to one IP or let Docker pick a port
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Same port on different host IPs
&lt;/h3&gt;

&lt;p&gt;A host port is only taken on the address it is bound to. In the lab, &lt;code&gt;-p 127.0.0.1:8095:80&lt;/code&gt; and &lt;code&gt;-p 127.0.0.2:8095:80&lt;/code&gt; ran side by side, and both addresses returned 200. A third container with plain &lt;code&gt;-p 8095:80&lt;/code&gt; then failed with &lt;code&gt;listen tcp4 0.0.0.0:8095: bind: address already in use&lt;/code&gt;. Note that this case produces the kernel message, not the Docker one, even though only containers were involved.&lt;/p&gt;

&lt;p&gt;Binding to &lt;code&gt;127.0.0.1&lt;/code&gt; is a good habit anyway. The Docker &lt;a href="https://docs.docker.com/engine/network/port-publishing/" rel="noopener noreferrer"&gt;port publishing docs&lt;/a&gt; warn: "Publishing container ports is insecure by default. Meaning, when you publish a container's ports it becomes available not only to the Docker host, but to the outside world as well."&lt;/p&gt;

&lt;h3&gt;
  
  
  Let Docker choose a free host port
&lt;/h3&gt;

&lt;p&gt;Publish only the container port and Docker picks a free host port:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker run &lt;span class="nt"&gt;-d&lt;/span&gt; &lt;span class="nt"&gt;--name&lt;/span&gt; rnd &lt;span class="nt"&gt;-p&lt;/span&gt; 80 nginx:1.30.6-alpine
docker port rnd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;80/tcp -&amp;gt; 0.0.0.0:32768
80/tcp -&amp;gt; [::]:32768
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Compose file reference says the same about &lt;code&gt;ports:&lt;/code&gt;. You can give both sides "or just the container port. In the latter case, the container runtime automatically allocates any unassigned port of the host" (&lt;a href="https://docs.docker.com/reference/compose-file/services/#ports" rel="noopener noreferrer"&gt;Compose services reference&lt;/a&gt;). This suits test stacks and CI runs where nobody types the URL by hand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Newer Docker error wording
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Docker Engine 29 messages
&lt;/h3&gt;

&lt;p&gt;The same two tests inside &lt;code&gt;docker:29.9.2-dind&lt;/code&gt; produced longer messages with a new prefix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker: Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint web2 (e1f0829e...): Bind for :::8080 failed: port is already allocated
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker: Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint web3 (d9beba20...): failed to bind host port 0.0.0.0:8082/tcp: address already in use
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Search for the tail of the line, not the prefix. &lt;code&gt;Bind for :::8080&lt;/code&gt; is the IPv6 side of the same conflict, and &lt;code&gt;failed to bind host port ... address already in use&lt;/code&gt; replaces the older &lt;code&gt;Error starting userland proxy&lt;/code&gt; text. The causes and fixes are the same as above. &lt;code&gt;docker ps --filter publish=8080&lt;/code&gt; worked unchanged on 29.9.2, and &lt;code&gt;netstat -ltnp&lt;/code&gt; inside the dind host still showed &lt;code&gt;docker-proxy&lt;/code&gt; as the listener.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-port-already-allocated%2F05-docker-29-port-error-wording.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fdocker-port-already-allocated%2F05-docker-29-port-error-wording.webp" alt="Terminal capture on Docker Engine 29.9.2: failed to set up container networking with Bind for :::8080 failed port is already allocated, and failed to bind host port 0.0.0.0:8082/tcp address already in use" width="800" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you hit permission errors before you even get to the port problem, fix access to the daemon first with the &lt;a href="https://deploylark.com/posts/docker-sock-permission-denied/" rel="noopener noreferrer"&gt;docker.sock permission guide&lt;/a&gt;. The &lt;a href="https://deploylark.com/posts/uptime-kuma-docker-setup/" rel="noopener noreferrer"&gt;Uptime Kuma guide&lt;/a&gt; shows this same port error for a second copy of an app on 3001.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h4&gt;
  
  
  What does "port is already allocated" mean in Docker?
&lt;/h4&gt;

&lt;p&gt;Another container already publishes that host port. Docker checks its own records and refuses the mapping. Run &lt;code&gt;docker ps --filter publish=PORT&lt;/code&gt; to see which one, as in &lt;a href="https://stackoverflow.com/questions/46176584/docker-bind-for-0-0-0-04000-failed-port-is-already-allocated" rel="noopener noreferrer"&gt;SO 46176584&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I find which process is using a port on Linux?
&lt;/h4&gt;

&lt;p&gt;Run &lt;code&gt;sudo ss -ltnp 'sport = :PORT'&lt;/code&gt;. The &lt;code&gt;users:&lt;/code&gt; column shows the process name and PID. If it says &lt;code&gt;docker-proxy&lt;/code&gt;, a container owns the port. See &lt;a href="https://stackoverflow.com/questions/38249434/docker-postgres-failed-to-bind-tcp-0-0-0-05432-address-already-in-use" rel="noopener noreferrer"&gt;SO 38249434&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why does docker ps show nothing but the port is still in use?
&lt;/h4&gt;

&lt;p&gt;A process outside Docker holds the port, such as a host nginx, apache2 or postgresql service, or another container bound to a specific host IP. In the lab, &lt;code&gt;docker ps --filter publish=8082&lt;/code&gt; was empty while &lt;code&gt;ss&lt;/code&gt; showed &lt;code&gt;python3&lt;/code&gt;. Threads like &lt;a href="https://stackoverflow.com/questions/37971961/docker-error-bind-address-already-in-use" rel="noopener noreferrer"&gt;SO 37971961&lt;/a&gt; end with stopping the host service.&lt;/p&gt;

&lt;h4&gt;
  
  
  Can two Docker containers use the same port?
&lt;/h4&gt;

&lt;p&gt;Yes on the container side, and on the host side if they use different host IPs. Both test containers listened on 80 inside. On the host, 127.0.0.1:8095 and 127.0.0.2:8095 worked together, but a third on 0.0.0.0:8095 failed.&lt;/p&gt;

&lt;h4&gt;
  
  
  Does stopping a container free its port?
&lt;/h4&gt;

&lt;p&gt;Yes. After &lt;code&gt;docker stop web1&lt;/code&gt;, &lt;code&gt;ss&lt;/code&gt; showed nothing on 8080 and a new container took it. A restart policy can bring the old one back with the daemon, so use &lt;code&gt;docker compose down&lt;/code&gt; or &lt;code&gt;docker rm&lt;/code&gt; for stacks you no longer want.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I let Docker pick a free port automatically?
&lt;/h4&gt;

&lt;p&gt;Publish only the container port, for example &lt;code&gt;-p 80&lt;/code&gt; or &lt;code&gt;ports: ["80"]&lt;/code&gt;, then read the result with &lt;code&gt;docker port NAME&lt;/code&gt;. The lab got 32768. The &lt;a href="https://docs.docker.com/reference/compose-file/services/#ports" rel="noopener noreferrer"&gt;Compose ports reference&lt;/a&gt; documents the same behavior.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>networking</category>
      <category>selfhosted</category>
      <category>devops</category>
    </item>
    <item>
      <title>Elasticsearch in Docker: Fix vm.max_map_count Too Low</title>
      <dc:creator>DeployLark</dc:creator>
      <pubDate>Sat, 07 Nov 2026 02:46:33 +0000</pubDate>
      <link>https://dev.to/deploylark/elasticsearch-in-docker-fix-vmmaxmapcount-too-low-25aa</link>
      <guid>https://dev.to/deploylark/elasticsearch-in-docker-fix-vmmaxmapcount-too-low-25aa</guid>
      <description>&lt;p&gt;You paste an Elasticsearch service into Compose, run &lt;code&gt;docker compose up&lt;/code&gt;, and the container exits with code 78. The log says &lt;code&gt;max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]&lt;/code&gt;. Or the node starts, your app still can't talk to it because &lt;code&gt;localhost:9200&lt;/code&gt; is the app's own loopback, or security returns &lt;code&gt;401&lt;/code&gt; until you set &lt;code&gt;ELASTIC_PASSWORD&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This guide reproduces those failures with &lt;code&gt;docker.elastic.co/elasticsearch/elasticsearch:9.1.5&lt;/code&gt; and a &lt;code&gt;curlimages/curl:8.16.0&lt;/code&gt; client on the same Compose network, then fixes them. Every error string below comes from that lab run on November 7, 2026. The Stack Overflow question &lt;a href="https://stackoverflow.com/questions/51445846/elasticsearch-max-virtual-memory-areas-vm-max-map-count-65530-is-too-low-inc" rel="noopener noreferrer"&gt;Elasticsearch: Max virtual memory areas vm.max_map_count [65530] is too low&lt;/a&gt; had 402,986 views that day, &lt;a href="https://stackoverflow.com/questions/42889241/how-to-increase-vm-max-map-count" rel="noopener noreferrer"&gt;How to increase vm.max_map_count?&lt;/a&gt; had 201,093, and &lt;a href="https://stackoverflow.com/questions/41064572/docker-elk-vm-max-map-count" rel="noopener noreferrer"&gt;Docker - ELK - vm.max_map_count&lt;/a&gt; had 72,355.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Raise &lt;code&gt;vm.max_map_count&lt;/code&gt; on the Docker host, not inside the container. Elastic's current docs recommend &lt;code&gt;1048576&lt;/code&gt;. The bootstrap check text still says at least &lt;code&gt;262144&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Production-style settings (&lt;code&gt;network.host&lt;/code&gt; plus &lt;code&gt;cluster.initial_master_nodes&lt;/code&gt;) enforce the check and exit 78. &lt;code&gt;discovery.type=single-node&lt;/code&gt; may only WARN and still start.&lt;/li&gt;
&lt;li&gt;From another container, use the Compose service name (&lt;code&gt;http://elasticsearch:9200&lt;/code&gt;), not &lt;code&gt;localhost&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;With security enabled (the 8.x and 9.x default), set &lt;code&gt;ELASTIC_PASSWORD&lt;/code&gt; and send Basic auth.&lt;/li&gt;
&lt;li&gt;Persist the sysctl in &lt;code&gt;/etc/sysctl.d/&lt;/code&gt; so a reboot does not bring the failure back.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Fix vm.max_map_count bootstrap exit 78
&lt;/h2&gt;

&lt;p&gt;Start with the map. Match the message you see to the cause and the fix:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Felasticsearch-docker-vm-max-map-count%2F01-elasticsearch-docker-error-cause-fix-diagram.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Felasticsearch-docker-vm-max-map-count%2F01-elasticsearch-docker-error-cause-fix-diagram.webp" alt="Table diagram mapping Elasticsearch Docker errors to causes and fixes: vm.max_map_count bootstrap exit 78, localhost connection refused on 9200, and security_exception 401" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Confirm the host value and the exact log line
&lt;/h3&gt;

&lt;p&gt;The lab host started at the classic low default:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;65530
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With a production-style Compose file that binds a non-loopback address and sets &lt;code&gt;cluster.initial_master_nodes&lt;/code&gt;, Elasticsearch enforced bootstrap checks and died:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;elasticsearch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;docker.elastic.co/elasticsearch/elasticsearch:9.1.5&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;cluster.name=eslab&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;node.name=eslab-node&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;cluster.initial_master_nodes=eslab-node&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;xpack.security.enabled=false&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;ES_JAVA_OPTS=-Xms512m -Xmx512m&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;network.host=0.0.0.0&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1:9200:9200"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The container status was &lt;code&gt;Exited (78)&lt;/code&gt;. The log named the check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bound or publishing to a non-loopback address, enforcing bootstrap checks
ERROR node validation exception
[1] bootstrap checks failed. You must address the points described in the following [1] lines before starting Elasticsearch.
bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
ERROR: Elasticsearch died while starting up, with exit code 78
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Felasticsearch-docker-vm-max-map-count%2F02-elasticsearch-docker-vm-max-map-count-bootstrap-fail.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Felasticsearch-docker-vm-max-map-count%2F02-elasticsearch-docker-vm-max-map-count-bootstrap-fail.webp" alt="Terminal capture: Elasticsearch container exits with code 78 and logs that vm.max_map_count 65530 is too low while enforcing bootstrap checks" width="800" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Elasticsearch uses memory-mapped files for indices by default. The kernel limit &lt;code&gt;vm.max_map_count&lt;/code&gt; caps how many mappings a process may hold. The &lt;a href="https://www.elastic.co/docs/deploy-manage/deploy/self-managed/vm-max-map-count" rel="noopener noreferrer"&gt;Increase virtual memory&lt;/a&gt; docs say: "If the operating system's default &lt;code&gt;vm.max_map_count&lt;/code&gt; value is &lt;code&gt;1048576&lt;/code&gt; or higher, no configuration change is necessary. If the default value is lower than &lt;code&gt;1048576&lt;/code&gt;, configure the &lt;code&gt;vm.max_map_count&lt;/code&gt; parameter to &lt;code&gt;1048576&lt;/code&gt;." The &lt;a href="https://www.elastic.co/docs/deploy-manage/deploy/self-managed/install-elasticsearch-docker-prod" rel="noopener noreferrer"&gt;Docker production guide&lt;/a&gt; repeats that the setting "must be set to &lt;code&gt;1048576&lt;/code&gt;." The check message still quotes the older floor of &lt;code&gt;262144&lt;/code&gt;. Raise the host to &lt;code&gt;1048576&lt;/code&gt; so you clear both the check and the current docs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Raise it on the Docker host, then persist it
&lt;/h3&gt;

&lt;p&gt;You cannot fix this from inside the container. The value lives on the host kernel. On Linux:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;sysctl &lt;span class="nt"&gt;-w&lt;/span&gt; vm.max_map_count&lt;span class="o"&gt;=&lt;/span&gt;1048576
&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'vm.max_map_count=1048576'&lt;/span&gt; | &lt;span class="nb"&gt;sudo tee&lt;/span&gt; /etc/sysctl.d/99-vm-max-map-count.conf
&lt;span class="nb"&gt;sudo &lt;/span&gt;sysctl &lt;span class="nt"&gt;--system&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify with &lt;code&gt;cat /proc/sys/vm/max_map_count&lt;/code&gt; or &lt;code&gt;sysctl vm.max_map_count&lt;/code&gt;. After the lab raised the host to &lt;code&gt;1048576&lt;/code&gt; and restarted the same Compose file, &lt;code&gt;curl http://127.0.0.1:9200&lt;/code&gt; returned the cluster root JSON with &lt;code&gt;"number" : "9.1.5"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Felasticsearch-docker-vm-max-map-count%2F03-elasticsearch-docker-sysctl-vm-max-map-count-fix.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Felasticsearch-docker-vm-max-map-count%2F03-elasticsearch-docker-sysctl-vm-max-map-count-fix.webp" alt="Terminal capture: sysctl raises vm.max_map_count to 1048576, then curl to port 9200 returns Elasticsearch 9.1.5 cluster JSON" width="800" height="203"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On Docker Desktop for Windows or Mac the knob sits in the Linux VM behind Docker, not in your Windows or macOS shell. Elastic's Docker production page documents &lt;code&gt;wsl -d docker-desktop&lt;/code&gt; and &lt;code&gt;.wslconfig&lt;/code&gt; paths for WSL2. Change it there, then restart the container.&lt;/p&gt;

&lt;h3&gt;
  
  
  single-node can WARN and still start
&lt;/h3&gt;

&lt;p&gt;With &lt;code&gt;discovery.type=single-node&lt;/code&gt; and the same low host value of &lt;code&gt;65530&lt;/code&gt;, the lab only logged a WARN and the node stayed up with HTTP 200:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WARN max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Felasticsearch-docker-vm-max-map-count%2F06-elasticsearch-docker-single-node-warn-only.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Felasticsearch-docker-vm-max-map-count%2F06-elasticsearch-docker-single-node-warn-only.webp" alt="Terminal capture: discovery.type single-node only warns that vm.max_map_count is too low while curl still returns HTTP 200" width="800" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That soft path is why some Compose snippets "work" on a laptop and then explode when you switch to production-style networking. Fix the host either way. Do not treat &lt;code&gt;single-node&lt;/code&gt; as a substitute for &lt;code&gt;sysctl&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix Connection refused to localhost:9200
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Don't connect to localhost from the app container
&lt;/h3&gt;

&lt;p&gt;Once Elasticsearch was healthy, a curl client on the same Compose network played the app. With &lt;code&gt;http://127.0.0.1:9200/&lt;/code&gt; it never reached Elasticsearch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;curl: (7) Failed to connect to 127.0.0.1 port 9200 after 0 ms: Could not connect to server
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same client with &lt;code&gt;http://elasticsearch:9200/&lt;/code&gt; got the cluster root JSON. Each container has its own loopback interface, so &lt;code&gt;localhost&lt;/code&gt; inside the app is the app. Elasticsearch listens in the &lt;code&gt;elasticsearch&lt;/code&gt; service. The &lt;a href="https://deploylark.com/posts/redis-docker-connection-refused-noauth-misconf/" rel="noopener noreferrer"&gt;Redis guide&lt;/a&gt; and the &lt;a href="https://deploylark.com/posts/rabbitmq-docker-access-refused/" rel="noopener noreferrer"&gt;RabbitMQ guide&lt;/a&gt; show the same mistake for those brokers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Felasticsearch-docker-vm-max-map-count%2F04-elasticsearch-docker-localhost-connection-refused.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Felasticsearch-docker-vm-max-map-count%2F04-elasticsearch-docker-localhost-connection-refused.webp" alt="Terminal capture: curl from an app container fails on 127.0.0.1:9200 then succeeds on http://elasticsearch:9200" width="798" height="165"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the Docker host itself you also need a published port. Without &lt;code&gt;ports&lt;/code&gt;, a host tool cannot reach 9200. Add &lt;code&gt;ports: ["127.0.0.1:9200:9200"]&lt;/code&gt; if a process on the host must talk HTTP. Other Compose services do not need that mapping.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wait until the HTTP port is ready
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;depends_on: [elasticsearch]&lt;/code&gt; only waits for the container process to start. Right after a restart in the lab, an immediate &lt;code&gt;curl http://elasticsearch:9200/&lt;/code&gt; still failed to connect. After the healthcheck flipped to healthy, the same request returned 200. Use a healthcheck and &lt;code&gt;condition: service_healthy&lt;/code&gt;, the pattern also used in the &lt;a href="https://deploylark.com/posts/self-host-umami-docker-postgresql/" rel="noopener noreferrer"&gt;Umami guide&lt;/a&gt; with PostgreSQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;elasticsearch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;docker.elastic.co/elasticsearch/elasticsearch:9.1.5&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;discovery.type=single-node&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;xpack.security.enabled=false&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;ES_JAVA_OPTS=-Xms512m -Xmx512m&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD-SHELL"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;curl&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;-fsS&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;http://127.0.0.1:9200&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;gt;/dev/null&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;||&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;exit&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;1"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;24&lt;/span&gt;
      &lt;span class="na"&gt;start_period&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;25s&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;your-app:tag&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;elasticsearch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;service_healthy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Fix 401 missing authentication credentials
&lt;/h2&gt;

&lt;p&gt;Elasticsearch 8.x and 9.x enable security by default. The lab turned it on with &lt;code&gt;xpack.security.enabled=true&lt;/code&gt;, set &lt;code&gt;ELASTIC_PASSWORD=LabPassw0rd!&lt;/code&gt;, and disabled HTTP TLS for a local Compose network with &lt;code&gt;xpack.security.http.ssl.enabled=false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Without credentials, curl got:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{"error":{"type":"security_exception","reason":"missing authentication credentials for REST request [/]"},"status":401}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With a wrong password it got &lt;code&gt;unable to authenticate user [elastic]&lt;/code&gt;. With &lt;code&gt;-u elastic:LabPassw0rd!&lt;/code&gt; the root endpoint returned 200, a document indexed under &lt;code&gt;lab-notes&lt;/code&gt;, and a search hit after &lt;code&gt;_refresh&lt;/code&gt;. The same password pattern shows up for other databases in the &lt;a href="https://deploylark.com/posts/postgres-docker-password-authentication-failed/" rel="noopener noreferrer"&gt;PostgreSQL Docker auth guide&lt;/a&gt; and the &lt;a href="https://deploylark.com/posts/mongodb-docker-authentication-failed/" rel="noopener noreferrer"&gt;MongoDB Docker auth guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Felasticsearch-docker-vm-max-map-count%2F05-elasticsearch-docker-elastic-password-401.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Felasticsearch-docker-vm-max-map-count%2F05-elasticsearch-docker-elastic-password-401.webp" alt="Terminal capture: Elasticsearch returns 401 without credentials or with a wrong password, then succeeds with elastic user and ELASTIC_PASSWORD" width="798" height="165"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Minimal secure Compose shape used in the lab:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;elasticsearch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;docker.elastic.co/elasticsearch/elasticsearch:9.1.5&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;discovery.type=single-node&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;xpack.security.enabled=true&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;ELASTIC_PASSWORD=LabPassw0rd!&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;xpack.security.http.ssl.enabled=false&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;ES_JAVA_OPTS=-Xms512m -Xmx512m&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1:9200:9200"&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD-SHELL"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;curl&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;-fsS&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;-u&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;elastic:LabPassw0rd!&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;http://127.0.0.1:9200&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;gt;/dev/null&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;||&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;exit&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;1"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;
      &lt;span class="na"&gt;start_period&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;40s&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Put the password in an env file or a secret, not in a public git repo. For production TLS and enrollment tokens, follow Elastic's current Docker docs rather than disabling HTTP SSL.&lt;/p&gt;

&lt;h2&gt;
  
  
  Working Compose checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Set host &lt;code&gt;vm.max_map_count=1048576&lt;/code&gt; and persist it under &lt;code&gt;/etc/sysctl.d/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Pin the image tag (&lt;code&gt;9.1.5&lt;/code&gt; in this lab, never &lt;code&gt;latest&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Give the node enough heap for your box (&lt;code&gt;ES_JAVA_OPTS=-Xms512m -Xmx512m&lt;/code&gt; was enough here).&lt;/li&gt;
&lt;li&gt;Bind a data volume to &lt;code&gt;/usr/share/elasticsearch/data&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Point app containers at &lt;code&gt;http://elasticsearch:9200&lt;/code&gt; (or your service name) with credentials when security is on.&lt;/li&gt;
&lt;li&gt;Gate app start on &lt;code&gt;condition: service_healthy&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Manage the stack from &lt;a href="https://deploylark.com/posts/portainer-ce-install-stacks/" rel="noopener noreferrer"&gt;Portainer&lt;/a&gt; if you prefer a UI over the CLI.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;a href="https://deploylark.com/posts/grafana-prometheus-docker-permission-denied/" rel="noopener noreferrer"&gt;Grafana and Prometheus permission guide&lt;/a&gt; is another case where the Docker host, not the Compose YAML alone, owns the fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h4&gt;
  
  
  What does vm.max_map_count control?
&lt;/h4&gt;

&lt;p&gt;It is the Linux kernel limit on memory-mapped areas per process. Elasticsearch maps index files with &lt;code&gt;mmap&lt;/code&gt;, so a low value fails bootstrap or risks mapping errors. See &lt;a href="https://stackoverflow.com/questions/42889241/how-to-increase-vm-max-map-count" rel="noopener noreferrer"&gt;SO 42889241&lt;/a&gt; and Elastic's virtual memory docs.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why does the container exit with code 78?
&lt;/h4&gt;

&lt;p&gt;Exit 78 is the lab result when bootstrap checks fail under enforced mode. The log line names &lt;code&gt;vm.max_map_count&lt;/code&gt;. Fix the host and start again.&lt;/p&gt;

&lt;h4&gt;
  
  
  Does discovery.type=single-node remove the need for sysctl?
&lt;/h4&gt;

&lt;p&gt;No. In the lab it only logged a WARN and still started. Production-style networking enforced the check and exited 78. Raise the host either way.&lt;/p&gt;

&lt;h4&gt;
  
  
  Can I set vm.max_map_count inside the Elasticsearch container?
&lt;/h4&gt;

&lt;p&gt;No. Threads such as &lt;a href="https://stackoverflow.com/questions/41064572/docker-elk-vm-max-map-count" rel="noopener noreferrer"&gt;SO 41064572&lt;/a&gt; hit a read-only &lt;code&gt;/proc&lt;/code&gt; from inside the container. Change the Docker host (or the Docker Desktop VM).&lt;/p&gt;

&lt;h4&gt;
  
  
  Why does my app get connection refused on localhost:9200?
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;localhost&lt;/code&gt; inside the app container is that container. Use the Compose service name and wait for the healthcheck. Same pattern as Redis and RabbitMQ on this site.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I set the elastic user password in Compose?
&lt;/h4&gt;

&lt;p&gt;Set &lt;code&gt;ELASTIC_PASSWORD&lt;/code&gt; (and keep security enabled). Call the API with &lt;code&gt;-u elastic:$ELASTIC_PASSWORD&lt;/code&gt;. Missing or wrong credentials return &lt;code&gt;security_exception&lt;/code&gt; with status 401, as captured in the lab.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>elasticsearch</category>
      <category>selfhosted</category>
      <category>devops</category>
    </item>
    <item>
      <title>Redis in Docker: Fix Connection Refused, NOAUTH and MISCONF</title>
      <dc:creator>DeployLark</dc:creator>
      <pubDate>Fri, 06 Nov 2026 10:38:55 +0000</pubDate>
      <link>https://dev.to/deploylark/redis-in-docker-fix-connection-refused-noauth-and-misconf-40be</link>
      <guid>https://dev.to/deploylark/redis-in-docker-fix-connection-refused-noauth-and-misconf-40be</guid>
      <description>&lt;p&gt;You add the official &lt;code&gt;redis&lt;/code&gt; image to a Compose file, and your app logs &lt;code&gt;Could not connect to Redis at 127.0.0.1:6379: Connection refused&lt;/code&gt;. Or it gets &lt;code&gt;DENIED Redis is running in protected mode&lt;/code&gt;, or &lt;code&gt;NOAUTH Authentication required&lt;/code&gt;. Or Redis works until its first snapshot, and then every write fails with &lt;code&gt;MISCONF Redis is configured to save RDB snapshots, but it's currently unable to persist to disk&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This guide reproduces each of those errors with &lt;code&gt;redis:8.10.3&lt;/code&gt; in Docker Compose and fixes them. Every error message and log line below comes from that lab run on October 31, 2026, when &lt;code&gt;redis:latest&lt;/code&gt; pulled the same 8.10.3 image. These errors are common. The Stack Overflow question &lt;a href="https://stackoverflow.com/questions/19581059/misconf-redis-is-configured-to-save-rdb-snapshots" rel="noopener noreferrer"&gt;MISCONF Redis is configured to save RDB snapshots&lt;/a&gt; had 646,097 views that day, &lt;a href="https://stackoverflow.com/questions/41371402/connecting-to-redis-running-in-docker-container-from-host-machine" rel="noopener noreferrer"&gt;Connecting to Redis running in Docker Container from Host machine&lt;/a&gt; had 306,329, and the image repo issue &lt;a href="https://github.com/redis/docker-library-redis/issues/46" rel="noopener noreferrer"&gt;Set password and other options via ENV variable&lt;/a&gt; has 53 comments.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From another container, &lt;code&gt;localhost&lt;/code&gt; is that container. Connect to the Compose service name, for example &lt;code&gt;redis&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A mounted stock &lt;code&gt;redis.conf&lt;/code&gt; listens on &lt;code&gt;127.0.0.1&lt;/code&gt; only and turns protected mode on. Change &lt;code&gt;bind&lt;/code&gt; and set a password.&lt;/li&gt;
&lt;li&gt;The official image ignores &lt;code&gt;REDIS_PASSWORD&lt;/code&gt; and &lt;code&gt;REDIS_ARGS&lt;/code&gt;. Pass &lt;code&gt;--requirepass&lt;/code&gt; on the command line or use a config or ACL file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;redis-cli ping&lt;/code&gt; exits 0 on &lt;code&gt;NOAUTH&lt;/code&gt;, so a plain ping healthcheck stays healthy. Set &lt;code&gt;REDISCLI_AUTH&lt;/code&gt; and check for &lt;code&gt;PONG&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MISCONF&lt;/code&gt; and &lt;code&gt;appendonlydir: Permission denied&lt;/code&gt; mean the Redis user (uid 999) can't write &lt;code&gt;/data&lt;/code&gt;. Fix the ownership instead of turning the check off.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Fix &lt;code&gt;Connection refused&lt;/code&gt; from another container
&lt;/h2&gt;

&lt;p&gt;Start with the map. Find the message you see in the left column:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fredis-docker-connection-refused-noauth-misconf%2F01-redis-docker-error-cause-fix-diagram.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fredis-docker-connection-refused-noauth-misconf%2F01-redis-docker-error-cause-fix-diagram.webp" alt="Table diagram mapping five Redis Docker errors to their cause and fix: Connection refused on localhost, Connection refused with a mounted redis.conf, DENIED protected mode, NOAUTH or no password asked, and MISCONF or appendonlydir Permission denied" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The lab started with the smallest possible service, no config file and no published port:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;redis&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;redis:8.10.3&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;redis-data:/data&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;redis-data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Don't connect to localhost from the app container
&lt;/h3&gt;

&lt;p&gt;A one-off &lt;code&gt;redis:8.10.3&lt;/code&gt; container on the same Compose network played the app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ redis-cli -h localhost PING
Could not connect to Redis at localhost:6379: Connection refused
$ redis-cli -h 127.0.0.1 -p 6379 PING
Could not connect to Redis at 127.0.0.1:6379: Connection refused
$ redis-cli -h redis PING
PONG
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each container has its own loopback interface, so &lt;code&gt;localhost&lt;/code&gt; inside the app container is the app container. Redis listens in its own container, which Compose names after the service. Set your app's Redis host to &lt;code&gt;redis&lt;/code&gt; (or whatever you called the service), for example &lt;code&gt;redis://redis:6379/0&lt;/code&gt;. The container name &lt;code&gt;redislab-redis-1&lt;/code&gt; also answered. The &lt;a href="https://deploylark.com/posts/mysql-docker-access-denied-root/" rel="noopener noreferrer"&gt;MySQL guide&lt;/a&gt; shows the same mistake with ERROR 2002 and 2003.&lt;/p&gt;

&lt;p&gt;From the Docker host itself, the port was closed too. &lt;code&gt;docker compose ps&lt;/code&gt; listed &lt;code&gt;6379/tcp&lt;/code&gt; with no host mapping, and a TCP connect to &lt;code&gt;127.0.0.1:6379&lt;/code&gt; on the host was refused. Add &lt;code&gt;ports: ["127.0.0.1:6379:6379"]&lt;/code&gt; if a tool on the host needs Redis. Keep the &lt;code&gt;127.0.0.1&lt;/code&gt; part. Other containers on the network don't need &lt;code&gt;ports&lt;/code&gt; at all.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check &lt;code&gt;bind&lt;/code&gt; in a mounted redis.conf
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://hub.docker.com/_/redis" rel="noopener noreferrer"&gt;image docs&lt;/a&gt; show how to start Redis with your own &lt;code&gt;redis.conf&lt;/code&gt;. If you start from the upstream file, note that the 8.10.3 version from the &lt;a href="https://github.com/redis/redis/blob/8.10.3/redis.conf" rel="noopener noreferrer"&gt;redis/redis repository&lt;/a&gt; contains &lt;code&gt;bind 127.0.0.1 -::1&lt;/code&gt; and &lt;code&gt;protected-mode yes&lt;/code&gt;. Mounted as-is, Redis answers inside its own container and refuses everyone else:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fredis-docker-connection-refused-noauth-misconf%2F02-redis-docker-connection-refused-localhost-bind.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fredis-docker-connection-refused-noauth-misconf%2F02-redis-docker-connection-refused-localhost-bind.webp" alt="Terminal output from an app container: redis-cli to localhost and 127.0.0.1 is refused, the service name redis returns PONG, and after mounting the stock redis.conf the service name is refused too while redis-cli inside the Redis container still returns PONG" width="800" height="209"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;CONFIG GET bind&lt;/code&gt; inside the container returned &lt;code&gt;127.0.0.1 -::1&lt;/code&gt;. A published port didn't help either: from the host, &lt;code&gt;redis-cli&lt;/code&gt; got &lt;code&gt;Error: Connection reset by peer&lt;/code&gt;. This is the accepted answer on the 306,329-view question above: the server only listens on loopback. Change the line to &lt;code&gt;bind * -::*&lt;/code&gt;, which the same file describes as "like the default, all available interfaces". That leads straight to the next error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix &lt;code&gt;DENIED Redis is running in protected mode&lt;/code&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why the plain image has no protected mode
&lt;/h3&gt;

&lt;p&gt;Without a config file, the image turns protected mode off. In the lab, &lt;code&gt;CONFIG GET protected-mode&lt;/code&gt; returned &lt;code&gt;no&lt;/code&gt;, &lt;code&gt;bind&lt;/code&gt; was &lt;code&gt;* -::*&lt;/code&gt; and &lt;code&gt;requirepass&lt;/code&gt; was empty. Redis logged this on startup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WARNING: Redis does not require authentication and is not protected by network restrictions. Redis will accept connections from any IP address on any network interface.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="https://hub.docker.com/_/redis" rel="noopener noreferrer"&gt;Docker Official Image docs&lt;/a&gt; explain the choice: "For the ease of accessing Redis from other containers via Docker networking, the "Protected mode" is turned off by default. This means that if you expose the port outside of your host (e.g., via &lt;code&gt;-p&lt;/code&gt; on &lt;code&gt;docker run&lt;/code&gt;), it will be open without a password to anyone."&lt;/p&gt;

&lt;h3&gt;
  
  
  Set a password instead of turning protected mode off
&lt;/h3&gt;

&lt;p&gt;With a mounted &lt;code&gt;redis.conf&lt;/code&gt; you get the upstream default, &lt;code&gt;protected-mode yes&lt;/code&gt;. After the &lt;code&gt;bind * -::*&lt;/code&gt; change and no password, every client from another container got the long &lt;code&gt;DENIED&lt;/code&gt; reply:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fredis-docker-connection-refused-noauth-misconf%2F03-redis-denied-protected-mode-docker.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fredis-docker-connection-refused-noauth-misconf%2F03-redis-denied-protected-mode-docker.webp" alt="Terminal output showing the full DENIED Redis is running in protected mode message from another container, then NOAUTH Authentication required after adding requirepass, and PONG once REDISCLI_AUTH supplies the password" width="800" height="251"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The message offers four ways out. Three of them switch protected mode off. Option 4, a password for the default user, is the one to pick. Adding &lt;code&gt;requirepass Lab-Pass-4821&lt;/code&gt; to &lt;code&gt;redis.conf&lt;/code&gt; and restarting turned &lt;code&gt;DENIED&lt;/code&gt; into &lt;code&gt;NOAUTH Authentication required.&lt;/code&gt;, and the same client with the password got &lt;code&gt;PONG&lt;/code&gt;. The &lt;a href="https://redis.io/docs/latest/operate/oss_and_stack/management/security/" rel="noopener noreferrer"&gt;Redis security docs&lt;/a&gt; state the design plainly: "Redis is designed to be accessed by trusted clients inside trusted environments." A password plus a private Compose network keeps it that way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix NOAUTH and a password that never applied
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Pass the password with &lt;code&gt;--requirepass&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The first lab file had &lt;code&gt;REDIS_PASSWORD: "ChangeMe123"&lt;/code&gt; in &lt;code&gt;environment&lt;/code&gt;. Redis started without a password anyway: &lt;code&gt;CONFIG GET requirepass&lt;/code&gt; came back empty, and a client from another container ran &lt;code&gt;PING&lt;/code&gt; without any auth. The entrypoint script in &lt;code&gt;redis:8.10.3&lt;/code&gt; never reads &lt;code&gt;REDIS_PASSWORD&lt;/code&gt;, and it contains no &lt;code&gt;REDIS_ARGS&lt;/code&gt; either. A test with &lt;code&gt;REDIS_ARGS: "--requirepass Lab-Pass-4821"&lt;/code&gt; also left Redis open, and &lt;code&gt;SET&lt;/code&gt; worked without a password. A &lt;a href="https://stackoverflow.com/questions/70807705/how-to-use-redis-username-with-password-in-docker-compose" rel="noopener noreferrer"&gt;Stack Overflow answer&lt;/a&gt; notes that &lt;code&gt;REDIS_ARGS&lt;/code&gt; works with the &lt;code&gt;redis/redis-stack&lt;/code&gt; images. The official &lt;code&gt;redis&lt;/code&gt; image doesn't support it.&lt;/p&gt;

&lt;p&gt;Instead, put the password in &lt;code&gt;.env&lt;/code&gt; and let Compose interpolate it into the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;redis&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;redis:8.10.3&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;redis-server"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--requirepass"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;${REDIS_PASSWORD:?set&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;REDIS_PASSWORD&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;in&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;.env}"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;REDIS_PASSWORD=Lab-Pass-4821&lt;/code&gt; in &lt;code&gt;.env&lt;/code&gt;, this produced the errors your app shows when its password is missing or wrong:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fredis-docker-connection-refused-noauth-misconf%2F04-redis-noauth-healthcheck-docker-compose.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fredis-docker-connection-refused-noauth-misconf%2F04-redis-noauth-healthcheck-docker-compose.webp" alt="Terminal output: redis-cli PING without a password prints NOAUTH Authentication required with exit code 0, a wrong password prints WRONGPASS invalid username-password pair, REDISCLI_AUTH returns PONG, and docker compose ps shows a naive ping healthcheck as healthy while a wrong-password healthcheck is unhealthy" width="800" height="251"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;NOAUTH Authentication required.&lt;/code&gt; means the client sent no password. &lt;code&gt;WRONGPASS invalid username-password pair or user is disabled.&lt;/code&gt; means it sent the wrong one. With &lt;code&gt;-a&lt;/code&gt;, &lt;code&gt;redis-cli&lt;/code&gt; also prints a warning that a password on the command line may not be safe. Set &lt;code&gt;REDISCLI_AUTH&lt;/code&gt; instead, or add &lt;code&gt;--no-auth-warning&lt;/code&gt;. Without &lt;code&gt;.env&lt;/code&gt;, the &lt;code&gt;:?&lt;/code&gt; form stops Compose with &lt;code&gt;required variable REDIS_PASSWORD is missing a value&lt;/code&gt;, so Redis never starts open by accident.&lt;/p&gt;

&lt;p&gt;Two details from the lab. &lt;code&gt;docker inspect&lt;/code&gt; shows the password in the container's &lt;code&gt;Args&lt;/code&gt;. And the shell form from a &lt;a href="https://stackoverflow.com/questions/68461172/docker-compose-redis-password-via-environment-variable" rel="noopener noreferrer"&gt;41-vote answer&lt;/a&gt; also worked, with &lt;code&gt;redis-server --requirepass "$${REDIS_PASSWORD:?REDIS_PASSWORD variable is not set}"&lt;/code&gt; after &lt;code&gt;/bin/sh -c&lt;/code&gt; and the password in &lt;code&gt;environment&lt;/code&gt;. The &lt;code&gt;$$&lt;/code&gt; stops Compose from replacing the variable itself. That keeps the value out of &lt;code&gt;Args&lt;/code&gt;, but it still sits in the container's environment.&lt;/p&gt;

&lt;p&gt;The healthcheck has the same blind spot, as the bottom of the screenshot above shows. A healthcheck of &lt;code&gt;["CMD", "redis-cli", "ping"]&lt;/code&gt; against a password-protected Redis stayed &lt;code&gt;healthy&lt;/code&gt;. Its last probe printed &lt;code&gt;NOAUTH Authentication required.&lt;/code&gt; and exited 0. So &lt;code&gt;depends_on: condition: service_healthy&lt;/code&gt; lets your app start against a Redis it can't log in to.&lt;/p&gt;

&lt;p&gt;This check reports the truth:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;REDISCLI_AUTH&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${REDIS_PASSWORD}&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD-SHELL"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;redis-cli&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;ping&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;|&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;grep&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;-q&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;PONG"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the right password the service was &lt;code&gt;healthy&lt;/code&gt;. With a wrong &lt;code&gt;REDISCLI_AUTH&lt;/code&gt; it turned &lt;code&gt;unhealthy&lt;/code&gt;, and the probe log showed the &lt;code&gt;WRONGPASS&lt;/code&gt; error.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use an ACL user when your app wants a username
&lt;/h3&gt;

&lt;p&gt;Some apps ask for a Redis username and password. Define the user in an ACL file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user default off
user app on &amp;gt;App-Pass-7730 ~app:* +@all -@dangerous
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mount it read-only and start Redis with &lt;code&gt;command: ["redis-server", "--aclfile", "/usr/local/etc/redis/users.acl"]&lt;/code&gt;. In the lab, &lt;code&gt;redis-cli --user app --pass App-Pass-7730 PING&lt;/code&gt; returned &lt;code&gt;PONG&lt;/code&gt;, and a URL like &lt;code&gt;redis://app:App-Pass-7730@redis:6379&lt;/code&gt; worked too. A password without the username failed with &lt;code&gt;WRONGPASS&lt;/code&gt;, because the default user is off. The &lt;code&gt;~app:*&lt;/code&gt; key pattern did its job: &lt;code&gt;SET other 1&lt;/code&gt; returned &lt;code&gt;NOPERM No permissions to access a key&lt;/code&gt;, and &lt;code&gt;FLUSHALL&lt;/code&gt; was refused. Putting the same rules on the command line went wrong in the lab. Written as one list item after &lt;code&gt;--user&lt;/code&gt;, Redis exited with &lt;code&gt;Spaces not allowed in ACL usernames&lt;/code&gt;. The file avoids that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix MISCONF and append-only Permission denied
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why saves to /data fail
&lt;/h3&gt;

&lt;p&gt;The Redis server runs as uid 999 (&lt;code&gt;redis&lt;/code&gt;) in this image. When the container starts as root, the entrypoint fixes the ownership of &lt;code&gt;/data&lt;/code&gt; itself. The &lt;a href="https://hub.docker.com/_/redis" rel="noopener noreferrer"&gt;image docs&lt;/a&gt; add a limit: "This adjustment is only performed in basic, default scenarios to avoid interfering with custom or user-specific configurations."&lt;/p&gt;

&lt;p&gt;The lab hit that limit with a bind mount, &lt;code&gt;./data:/data&lt;/code&gt;, owned by root and holding a &lt;code&gt;.gitkeep&lt;/code&gt; file. The entrypoint printed &lt;code&gt;Notice: Unknown file './.gitkeep' found in data dir. Permissions will not be modified.&lt;/code&gt; and left the directory alone. Redis started, and reads and writes worked until the first snapshot:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fredis-docker-connection-refused-noauth-misconf%2F05-redis-misconf-permission-denied-docker-volume.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fredis-docker-connection-refused-noauth-misconf%2F05-redis-misconf-permission-denied-docker-volume.webp" alt="Terminal output: the entrypoint notice about an unknown file in the data dir, BGSAVE failing with Permission denied on the temp RDB file, SET returning the MISCONF error, the appendonlydir Permission denied log line, then chown to 999 fixing writes and rdb_last_bgsave_status ok" width="800" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The default save rule was &lt;code&gt;3600 1 300 100 60 10000&lt;/code&gt;. After a failed &lt;code&gt;BGSAVE&lt;/code&gt;, the log said &lt;code&gt;Failed opening the temp RDB file temp-51.rdb (in server root dir /data) for saving: Permission denied&lt;/code&gt;, &lt;code&gt;INFO persistence&lt;/code&gt; showed &lt;code&gt;rdb_last_bgsave_status:err&lt;/code&gt;, and the next &lt;code&gt;SET&lt;/code&gt; returned &lt;code&gt;MISCONF&lt;/code&gt;. Reads still worked, as &lt;code&gt;GET a&lt;/code&gt; returned &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;--appendonly yes&lt;/code&gt; the same mount fails at once. The container exited with code 1 after &lt;code&gt;Can't open or create append-only dir appendonlydir: Permission denied&lt;/code&gt;. A &lt;code&gt;user: "1000:1000"&lt;/code&gt; line on a fresh named volume, which the image creates owned by 999, failed with the same message.&lt;/p&gt;

&lt;p&gt;Every start in the lab logged &lt;code&gt;WARNING Memory overcommit must be enabled!&lt;/code&gt;, because the host had &lt;code&gt;vm.overcommit_memory&lt;/code&gt; set to 0. The warning says it can make a background save fail under low memory, and it names the fix: "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." This is a host setting. &lt;code&gt;docker run --sysctl vm.overcommit_memory=1&lt;/code&gt; failed with &lt;code&gt;sysctl 'vm.overcommit_memory=1' is not allowed&lt;/code&gt;, and the container read the host value of 0. Set it on the Docker host. A permission error, not this warning, caused every &lt;code&gt;MISCONF&lt;/code&gt; in this lab.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fix ownership, don't switch off the check
&lt;/h3&gt;

&lt;p&gt;The 533-vote answer on the MISCONF question runs &lt;code&gt;config set stop-writes-on-bgsave-error no&lt;/code&gt; and adds that you should check why the save failed. The lab shows why that matters. After that command, &lt;code&gt;SET&lt;/code&gt; worked again, but the next &lt;code&gt;BGSAVE&lt;/code&gt; still ended with &lt;code&gt;rdb_last_bgsave_status:err&lt;/code&gt;. Nothing reached the disk.&lt;/p&gt;

&lt;p&gt;Fix the directory instead. Any of these worked in the lab:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Change the owner to the Redis user with &lt;code&gt;sudo chown -R 999:999 ./data&lt;/code&gt; and restart. Writes worked, &lt;code&gt;BGSAVE&lt;/code&gt; returned &lt;code&gt;rdb_last_bgsave_status:ok&lt;/code&gt; and &lt;code&gt;dump.rdb&lt;/code&gt; appeared with owner 999.&lt;/li&gt;
&lt;li&gt;Run the command from the image docs, &lt;code&gt;docker run --rm -v /your/host/path:/data redis:8.10.3 chown -R redis:redis /data&lt;/code&gt;. It set the same root-owned folder to 999:999.&lt;/li&gt;
&lt;li&gt;Remove the unknown files, so the entrypoint fixes the folder on its own. With &lt;code&gt;.gitkeep&lt;/code&gt; gone it changed the owner to 999 and created &lt;code&gt;appendonlydir&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If you run Redis with &lt;code&gt;user: "1000:1000"&lt;/code&gt;, make the volume belong to that user, for example &lt;code&gt;docker run --rm -v &amp;lt;volume&amp;gt;:/data alpine:3.22 chown 1000:1000 /data&lt;/code&gt;. After that the AOF service started and &lt;code&gt;aof_last_write_status:ok&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;a href="https://deploylark.com/posts/grafana-prometheus-docker-permission-denied/" rel="noopener noreferrer"&gt;Grafana and Prometheus guide&lt;/a&gt; walks through the same uid mismatch with two other images.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Redis Compose file that avoids these errors
&lt;/h2&gt;

&lt;h3&gt;
  
  
  compose.yaml and .env
&lt;/h3&gt;

&lt;p&gt;This file ran verbatim in the lab:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;redis&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;redis:8.10.3&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;redis-server&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;--requirepass&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;${REDIS_PASSWORD:?set REDIS_PASSWORD in .env}&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;--appendonly&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;yes"&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;REDISCLI_AUTH&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${REDIS_PASSWORD}&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1:6379:6379"&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;redis-data:/data&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD-SHELL"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;redis-cli&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;ping&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;|&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;grep&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;-q&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;PONG"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;10s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;3s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;
      &lt;span class="na"&gt;start_period&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;

  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;redis:8.10.3&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;redis&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;service_healthy&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;REDISCLI_AUTH&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${REDIS_PASSWORD}&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;redis-cli"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-h"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;redis"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;INCR"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;app:visits"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;redis-data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REDIS_PASSWORD=f3Kq9-Lab-Only-2Vx7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;app&lt;/code&gt; with your real service and give it the host &lt;code&gt;redis&lt;/code&gt; and the same password. The named volume belongs to the Redis user from the start, AOF is on, and the healthcheck only passes on a real &lt;code&gt;PONG&lt;/code&gt;. Drop the &lt;code&gt;ports&lt;/code&gt; block if nothing on the host needs Redis. If your password contains &lt;code&gt;$&lt;/code&gt;, read the dollar sign section of the &lt;a href="https://deploylark.com/posts/mysql-docker-access-denied-root/" rel="noopener noreferrer"&gt;MySQL guide&lt;/a&gt; first, because Compose interpolates &lt;code&gt;.env&lt;/code&gt; the same way. The &lt;a href="https://deploylark.com/posts/self-host-umami-docker-postgresql/" rel="noopener noreferrer"&gt;Umami guide&lt;/a&gt; uses the same &lt;code&gt;service_healthy&lt;/code&gt; pattern with PostgreSQL.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check it
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker compose up -d
 Container redisfinal-redis-1  Waiting
 Container redisfinal-redis-1  Healthy
 Container redisfinal-app-1  Started
$ docker compose logs --no-log-prefix app
1
$ docker compose run --rm app
2
$ docker compose exec redis redis-cli INFO persistence | grep -E 'aof_enabled|aof_last_write_status'
aof_enabled:1
aof_last_write_status:ok
$ docker compose restart redis
$ docker compose exec redis redis-cli GET app:visits
2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the host, &lt;code&gt;redis-cli -h 127.0.0.1 PING&lt;/code&gt; without a password got &lt;code&gt;NOAUTH Authentication required.&lt;/code&gt;, and with &lt;code&gt;REDISCLI_AUTH&lt;/code&gt; set it read the counter. The value survived the restart. Paperless-ngx uses a Redis-compatible broker in the same way, as the &lt;a href="https://deploylark.com/posts/paperless-ngx-docker-compose-ocr/" rel="noopener noreferrer"&gt;Paperless-ngx guide&lt;/a&gt; shows with Valkey.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Connection errors
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Why does Redis refuse connections between Docker Compose containers?
&lt;/h4&gt;

&lt;p&gt;Usually the app connects to &lt;code&gt;localhost&lt;/code&gt; or &lt;code&gt;127.0.0.1&lt;/code&gt;, which is the app's own container. Use the service name, such as &lt;code&gt;redis&lt;/code&gt;. If you mounted your own &lt;code&gt;redis.conf&lt;/code&gt;, check its &lt;code&gt;bind&lt;/code&gt; line. The stock file has &lt;code&gt;bind 127.0.0.1 -::1&lt;/code&gt;, so other containers get &lt;code&gt;Connection refused&lt;/code&gt; until you change it.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I connect to Redis in Docker from the host machine?
&lt;/h4&gt;

&lt;p&gt;Publish the port, for example &lt;code&gt;ports: ["127.0.0.1:6379:6379"]&lt;/code&gt;, and connect to &lt;code&gt;127.0.0.1:6379&lt;/code&gt;. Without &lt;code&gt;ports&lt;/code&gt;, the host connection is refused. If Redis uses the stock &lt;code&gt;redis.conf&lt;/code&gt; with &lt;code&gt;bind 127.0.0.1 -::1&lt;/code&gt;, the published port still fails. In the lab, &lt;code&gt;redis-cli&lt;/code&gt; on the host got &lt;code&gt;Error: Connection reset by peer&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I fix &lt;code&gt;DENIED Redis is running in protected mode&lt;/code&gt; in Docker?
&lt;/h4&gt;

&lt;p&gt;Set a password with &lt;code&gt;requirepass&lt;/code&gt; or define an ACL user. Protected mode only blocks remote clients when the default user has no password. The plain image starts with protected mode off, so this error usually comes from a mounted &lt;code&gt;redis.conf&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Password and persistence errors
&lt;/h3&gt;

&lt;h4&gt;
  
  
  How do I set a Redis password with an environment variable in docker-compose?
&lt;/h4&gt;

&lt;p&gt;The official image doesn't read &lt;code&gt;REDIS_PASSWORD&lt;/code&gt;. Put the value in &lt;code&gt;.env&lt;/code&gt; and pass it in &lt;code&gt;command&lt;/code&gt;, as in &lt;code&gt;["redis-server", "--requirepass", "${REDIS_PASSWORD}"]&lt;/code&gt;. Check with &lt;code&gt;CONFIG GET requirepass&lt;/code&gt; or a &lt;code&gt;PING&lt;/code&gt; without a password, which should return &lt;code&gt;NOAUTH&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I use a Redis username with a password in docker-compose?
&lt;/h4&gt;

&lt;p&gt;Create an ACL file with &lt;code&gt;user default off&lt;/code&gt; and a line such as &lt;code&gt;user app on &amp;gt;password ~app:* +@all&lt;/code&gt;, mount it, and start Redis with &lt;code&gt;--aclfile&lt;/code&gt;. Clients then log in with &lt;code&gt;--user&lt;/code&gt; and &lt;code&gt;--pass&lt;/code&gt;, or a URL like &lt;code&gt;redis://app:password@redis:6379&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I fix &lt;code&gt;MISCONF Redis is configured to save RDB snapshots&lt;/code&gt; in Docker?
&lt;/h4&gt;

&lt;p&gt;Read the Redis log for the reason. In Docker it is often &lt;code&gt;Permission denied&lt;/code&gt; on &lt;code&gt;/data&lt;/code&gt;. Change the data directory's owner to uid 999, or to the user you set with &lt;code&gt;user:&lt;/code&gt;, then restart. Setting &lt;code&gt;stop-writes-on-bgsave-error no&lt;/code&gt; hides the error but doesn't save anything.&lt;/p&gt;

</description>
      <category>redis</category>
      <category>docker</category>
      <category>selfhosted</category>
      <category>devops</category>
    </item>
    <item>
      <title>MongoDB in Docker: Fix Authentication Failed and Init Scripts</title>
      <dc:creator>DeployLark</dc:creator>
      <pubDate>Fri, 06 Nov 2026 10:35:54 +0000</pubDate>
      <link>https://dev.to/deploylark/mongodb-in-docker-fix-authentication-failed-and-init-scripts-47nm</link>
      <guid>https://dev.to/deploylark/mongodb-in-docker-fix-authentication-failed-and-init-scripts-47nm</guid>
      <description>&lt;p&gt;You set &lt;code&gt;MONGO_INITDB_ROOT_USERNAME&lt;/code&gt; and &lt;code&gt;MONGO_INITDB_ROOT_PASSWORD&lt;/code&gt; in Compose, start the official &lt;code&gt;mongo&lt;/code&gt; image, and your app logs &lt;code&gt;MongoServerError: Authentication failed.&lt;/code&gt; The password is right. You can see it in the container's environment. Or you changed the password in &lt;code&gt;.env&lt;/code&gt;, recreated the container, and now nothing can log in. Or the user your &lt;code&gt;docker-entrypoint-initdb.d&lt;/code&gt; script should create never shows up.&lt;/p&gt;

&lt;p&gt;This guide reproduces each of those failures with &lt;code&gt;mongo:9.0.4&lt;/code&gt; in Docker Compose and fixes them. Every error and log line below comes from that lab run on November 1, 2026, when &lt;code&gt;mongo:latest&lt;/code&gt; pointed to the same 9.0.4 image digest. The questions behind these errors get a lot of traffic. On Stack Overflow, &lt;a href="https://stackoverflow.com/questions/42912755/how-can-i-create-a-database-for-a-mongodb-container-on-startup" rel="noopener noreferrer"&gt;How can I create a database for a MongoDB container on startup?&lt;/a&gt; had 325,066 views that day and &lt;a href="https://stackoverflow.com/questions/34559557/how-to-enable-authentication-on-mongodb-through-docker" rel="noopener noreferrer"&gt;How to enable authentication on MongoDB through Docker?&lt;/a&gt; had 316,972. The image repo issue &lt;a href="https://github.com/docker-library/mongo/issues/329" rel="noopener noreferrer"&gt;Initialization of a user defined database, username, and password using environment variables&lt;/a&gt; has 29 comments.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From another container, &lt;code&gt;localhost&lt;/code&gt; is that container. Connect to the Compose service name, for example &lt;code&gt;mongodb://mongo:27017&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The root user from &lt;code&gt;MONGO_INITDB_ROOT_USERNAME&lt;/code&gt; lives in the &lt;code&gt;admin&lt;/code&gt; database. If your connection string names another database, add &lt;code&gt;?authSource=admin&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;MONGO_INITDB_*&lt;/code&gt; variables and the &lt;code&gt;/docker-entrypoint-initdb.d&lt;/code&gt; scripts only run when &lt;code&gt;/data/db&lt;/code&gt; is empty. Changing them later does nothing. Change the password with &lt;code&gt;changeUserPassword()&lt;/code&gt; or run the script by hand.&lt;/li&gt;
&lt;li&gt;Percent-encode &lt;code&gt;@ : / ? # [ ] $&lt;/code&gt; in the password part of the connection string.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;docker logs&lt;/code&gt; on the MongoDB container tells you why a login failed. Look for &lt;code&gt;Failed to authenticate&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Fix Authentication failed with MONGO_INITDB_ROOT_USERNAME
&lt;/h2&gt;

&lt;p&gt;Start with the map. Find the message you see in the left column:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fmongodb-docker-authentication-failed%2F01-mongodb-docker-error-cause-fix-diagram.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fmongodb-docker-authentication-failed%2F01-mongodb-docker-error-cause-fix-diagram.webp" alt="Table diagram mapping five MongoDB Docker errors to their cause and fix: ECONNREFUSED on localhost, Authentication failed because of the wrong authSource, Authentication failed after a password change, initdb.d scripts that did not run, and unescaped characters in the password" width="799" height="453"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The lab started with the smallest Compose file that turns on authentication:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;mongo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mongo:9.0.4&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;MONGO_INITDB_ROOT_USERNAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;root&lt;/span&gt;
      &lt;span class="na"&gt;MONGO_INITDB_ROOT_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${MONGO_ROOT_PASSWORD}&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;mongo-data:/data/db&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;mongo-data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MONGO_ROOT_PASSWORD=Lab-Pass-4821
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the first start, the entrypoint ran a temporary &lt;code&gt;mongod&lt;/code&gt; bound to &lt;code&gt;127.0.0.1&lt;/code&gt;, created the root user, shut it down and then started the real server as &lt;code&gt;mongod --auth --bind_ip_all&lt;/code&gt;. A one-off &lt;code&gt;mongo:9.0.4&lt;/code&gt; container on the same Compose network played the app and ran &lt;code&gt;mongosh&lt;/code&gt; 2.14.0. &lt;code&gt;mongosh&lt;/code&gt; is built on the Node.js driver (the server log named driver 7.5.0), so Node.js apps get the same error names.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connect to the service name, not localhost
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;client$ mongosh 'mongodb://localhost:27017' --eval 'db.runCommand({ping:1})'
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
client$ mongosh 'mongodb://mongo:27017' --eval 'db.runCommand({ping:1})'
{ ok: 1 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the app container, &lt;code&gt;localhost&lt;/code&gt; points back at the app itself. Nothing listens on port 27017 there, so the connection is refused. Use the service name from your Compose file. The same rule applies to MySQL and Redis, as the &lt;a href="https://deploylark.com/posts/mysql-docker-access-denied-root/" rel="noopener noreferrer"&gt;MySQL guide&lt;/a&gt; and the &lt;a href="https://deploylark.com/posts/redis-docker-connection-refused-noauth-misconf/" rel="noopener noreferrer"&gt;Redis guide&lt;/a&gt; show.&lt;/p&gt;

&lt;p&gt;Note that &lt;code&gt;ping&lt;/code&gt; worked without a password. MongoDB lets unauthenticated clients run &lt;code&gt;ping&lt;/code&gt;, but a real query failed with &lt;code&gt;MongoServerError: Command find requires authentication&lt;/code&gt;. That message means you connected without credentials. &lt;code&gt;Authentication failed.&lt;/code&gt; means you sent credentials and the server rejected them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add authSource=admin for the root user
&lt;/h3&gt;

&lt;p&gt;When the password is right and the login still fails, check the database in the connection string first. Here it named the app database, and the right password failed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;client$ mongosh 'mongodb://root:Lab-Pass-4821@mongo:27017/appdb' --eval 'db.items.insertOne({a:1})'
MongoServerError: Authentication failed.
client$ mongosh 'mongodb://root:Lab-Pass-4821@mongo:27017/appdb?authSource=admin' --eval 'db.items.insertOne({a:1}).acknowledged'
true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A MongoDB user belongs to the database where it was created. The &lt;a href="https://hub.docker.com/_/mongo" rel="noopener noreferrer"&gt;Docker Hub page for the image&lt;/a&gt; says &lt;code&gt;MONGO_INITDB_ROOT_USERNAME&lt;/code&gt; and &lt;code&gt;MONGO_INITDB_ROOT_PASSWORD&lt;/code&gt; are for "creating a simple user with the role &lt;code&gt;root&lt;/code&gt; in the &lt;code&gt;admin&lt;/code&gt; authentication database". The &lt;a href="https://www.mongodb.com/docs/manual/reference/connection-string-options/" rel="noopener noreferrer"&gt;MongoDB connection string docs&lt;/a&gt; describe &lt;code&gt;authSource&lt;/code&gt; as the "Database name associated with the user's credentials. Defaults to defaultauthdb if specified, otherwise defaults to admin." So &lt;code&gt;/appdb&lt;/code&gt; in the path made the driver look for &lt;code&gt;root&lt;/code&gt; in &lt;code&gt;appdb&lt;/code&gt;. Without a database in the path, as in &lt;code&gt;mongodb://root:Lab-Pass-4821@mongo:27017/&lt;/code&gt;, the login worked because the default was &lt;code&gt;admin&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The server log shows the difference between a wrong database and a wrong password. The client gets the same message for both:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fmongodb-docker-authentication-failed%2F02-mongodb-docker-econnrefused-authsource.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fmongodb-docker-authentication-failed%2F02-mongodb-docker-econnrefused-authsource.webp" alt="Terminal capture: mongosh to localhost gets ECONNREFUSED, a query without credentials gets Command find requires authentication, root with database appdb gets Authentication failed, adding authSource=admin works, and the server log shows UserNotFound for db appdb and storedKey mismatch for a wrong password" width="800" height="223"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker logs mongolab-mongo-1 2&amp;gt;&amp;amp;1 | grep '"Failed to authenticate"'
Failed to authenticate user=root db=appdb error=UserNotFound: Could not find user "root" for db "appdb"
Failed to authenticate user=root db=admin error=AuthenticationFailed: SCRAM authentication failed, storedKey mismatch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The raw lines are JSON. The lab filtered them with &lt;code&gt;jq&lt;/code&gt; to show &lt;code&gt;attr.user&lt;/code&gt;, &lt;code&gt;attr.db&lt;/code&gt; and &lt;code&gt;attr.error&lt;/code&gt;. &lt;code&gt;UserNotFound&lt;/code&gt; means fix the &lt;code&gt;authSource&lt;/code&gt; or the user name. &lt;code&gt;storedKey mismatch&lt;/code&gt; means the user exists and the password is wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix a password that changed in .env but not in MongoDB
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why MONGO_INITDB_ROOT_PASSWORD is ignored on restart
&lt;/h3&gt;

&lt;p&gt;The lab changed &lt;code&gt;MONGO_ROOT_PASSWORD&lt;/code&gt; in &lt;code&gt;.env&lt;/code&gt; from &lt;code&gt;Lab-Pass-4821&lt;/code&gt; to &lt;code&gt;New-Pass-7310&lt;/code&gt; and ran &lt;code&gt;docker compose up -d --force-recreate&lt;/code&gt;. The new container had the new value in its environment, but the new password failed:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fmongodb-docker-authentication-failed%2F03-mongo-initdb-root-password-ignored-existing-volume.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fmongodb-docker-authentication-failed%2F03-mongo-initdb-root-password-ignored-existing-volume.webp" alt="Terminal capture: after changing the password in .env the container environment shows New-Pass-7310 but the init process did not run and the new password fails, changeUserPassword with the old password fixes it, and a volume first started without auth runs mongod with auth on but no root user" width="800" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The init step never ran. The entrypoint checks &lt;code&gt;/data/db&lt;/code&gt; for files such as &lt;code&gt;WiredTiger&lt;/code&gt;, &lt;code&gt;journal&lt;/code&gt; and &lt;code&gt;storage.bson&lt;/code&gt;, and skips initialization when any of them exist. The Docker Hub page says it directly: "Do note that none of the variables below will have any effect if you start the container with a data directory that already contains a database: any pre-existing database will always be left untouched on container startup."&lt;/p&gt;

&lt;p&gt;So the old password stays valid until you change it inside MongoDB.&lt;/p&gt;

&lt;h3&gt;
  
  
  Change the password with mongosh
&lt;/h3&gt;

&lt;p&gt;Log in with the old password and change it in place. Then the &lt;code&gt;.env&lt;/code&gt; value and the database agree again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;client$ mongosh 'mongodb://root:Lab-Pass-4821@mongo:27017/?authSource=admin' \
          --eval 'db.getSiblingDB("admin").changeUserPassword("root", "New-Pass-7310")'
{ ok: 1 }
client$ mongosh 'mongodb://root:New-Pass-7310@mongo:27017/?authSource=admin' \
          --eval 'db.runCommand({connectionStatus:1}).authInfo.authenticatedUsers'
[ { user: 'root', db: 'admin' } ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you lost the old password, or the volume only holds test data, &lt;code&gt;docker compose down -v&lt;/code&gt; deletes the volume. In the lab, the next start ran the init step again and the new &lt;code&gt;.env&lt;/code&gt; password worked. That also deletes every database in the volume.&lt;/p&gt;

&lt;p&gt;A related trap: the lab started a volume with no auth variables, wrote data, then added &lt;code&gt;MONGO_INITDB_ROOT_USERNAME&lt;/code&gt; and &lt;code&gt;MONGO_INITDB_ROOT_PASSWORD&lt;/code&gt;. The entrypoint still added &lt;code&gt;--auth&lt;/code&gt; (the process ran as &lt;code&gt;mongod --auth --bind_ip_all&lt;/code&gt;), but it did not create the user, because the volume was not empty. Every login failed with &lt;code&gt;UserNotFound: Could not find user "root" for db "admin"&lt;/code&gt;, and queries without credentials failed with &lt;code&gt;Command aggregate requires authentication&lt;/code&gt;. MongoDB's &lt;a href="https://www.mongodb.com/docs/manual/core/localhost-exception/" rel="noopener noreferrer"&gt;localhost exception&lt;/a&gt; "allows you to create the first user or role in the system after enabling access control." It only works from inside the container, so the fix ran there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;exec &lt;/span&gt;mongolate-mongo-1 sh &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s1"&gt;'mongosh --quiet admin --eval "db.createUser({user: process.env.MONGO_INITDB_ROOT_USERNAME, pwd: process.env.MONGO_INITDB_ROOT_PASSWORD, roles: [\"root\"]}).ok"'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It printed &lt;code&gt;1&lt;/code&gt;, and the root login worked. The old data was still there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix docker-entrypoint-initdb.d scripts that don't run
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Create the app user in the right database
&lt;/h3&gt;

&lt;p&gt;Most apps should not log in as root. The usual pattern is a script in &lt;code&gt;/docker-entrypoint-initdb.d&lt;/code&gt; that creates an app user. The lab mounted this file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Runs once, against MONGO_INITDB_DATABASE, only when /data/db is empty&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;pwd&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;APP_DB_PASSWORD&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;roles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[{&lt;/span&gt; &lt;span class="na"&gt;role&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;readWrite&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;appdb&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertOne&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;seeded&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;MONGO_INITDB_DATABASE: appdb&lt;/code&gt; and &lt;code&gt;APP_DB_PASSWORD&lt;/code&gt; passed into the &lt;code&gt;mongo&lt;/code&gt; service, the log showed &lt;code&gt;running /docker-entrypoint-initdb.d/01-create-app-user.js&lt;/code&gt; and &lt;code&gt;MongoDB init process complete; ready for start up.&lt;/code&gt; The &lt;code&gt;.js&lt;/code&gt; file read the password with &lt;code&gt;process.env&lt;/code&gt;, so it never sat in the script.&lt;/p&gt;

&lt;p&gt;Now the &lt;code&gt;authSource&lt;/code&gt; rule flips. The script ran in &lt;code&gt;appdb&lt;/code&gt;, so the &lt;code&gt;app&lt;/code&gt; user lives in &lt;code&gt;appdb&lt;/code&gt;, not in &lt;code&gt;admin&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;client$ mongosh 'mongodb://app:App-Pass-5532@mongo:27017/appdb?authSource=admin' --eval 'db.items.countDocuments()'
MongoServerError: Authentication failed.
client$ mongosh 'mongodb://app:App-Pass-5532@mongo:27017/appdb' --eval 'db.items.countDocuments()'
1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copying &lt;code&gt;?authSource=admin&lt;/code&gt; from a root example into an app user's connection string breaks it. Set &lt;code&gt;authSource&lt;/code&gt; to the database the script ran in, or leave it out when that database is already in the path. The &lt;code&gt;readWrite&lt;/code&gt; role on &lt;code&gt;appdb&lt;/code&gt; also limits the user. A write to another database failed with &lt;code&gt;MongoServerError: not authorized on other to execute command&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;MONGO_INITDB_DATABASE&lt;/code&gt; only picks the database the &lt;code&gt;.js&lt;/code&gt; scripts run against. The Docker Hub page notes that "MongoDB is fundamentally designed for "create on first use", so if you do not insert data with your JavaScript files, then no database is created." The &lt;code&gt;insertOne&lt;/code&gt; line in the script creates &lt;code&gt;appdb&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Run a new script on an existing volume
&lt;/h3&gt;

&lt;p&gt;The lab then added &lt;code&gt;02-create-report-user.js&lt;/code&gt; and recreated the container on the same volume. The script did not run, and the user did not exist:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fmongodb-docker-authentication-failed%2F04-docker-entrypoint-initdb-not-running-mongo.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fmongodb-docker-authentication-failed%2F04-docker-entrypoint-initdb-not-running-mongo.webp" alt="Terminal capture: on a fresh volume the init script runs and the app user works only without authSource=admin, then a second init script added later does not run on the existing volume until it is run by hand with mongosh as root" width="800" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Init scripts follow the same rule as the root variables. They run once, on the first start of an empty &lt;code&gt;/data/db&lt;/code&gt;. You don't need to delete the volume to apply a new one. Run it with &lt;code&gt;mongosh&lt;/code&gt; inside the container, logged in as root, against the same database the entrypoint would use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker &lt;span class="nb"&gt;exec &lt;/span&gt;mongoinit-mongo-1 sh &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s1"&gt;'mongosh --quiet -u root -p "$MONGO_INITDB_ROOT_PASSWORD" \
  --authenticationDatabase admin appdb /docker-entrypoint-initdb.d/02-create-report-user.js'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, the &lt;code&gt;report&lt;/code&gt; user logged in to &lt;code&gt;appdb&lt;/code&gt;. The entrypoint also runs &lt;code&gt;.sh&lt;/code&gt; files from the same folder, and ignores any other extension. On a fresh volume with no scripts, the log shows &lt;code&gt;ignoring /docker-entrypoint-initdb.d/*&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix MongoParseError for passwords with special characters
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Percent-encode the password in the connection string
&lt;/h3&gt;

&lt;p&gt;The lab set the root password to &lt;code&gt;p@ss:w/rd#1&lt;/code&gt;. The Compose value worked, because the entrypoint passes it to &lt;code&gt;createUser&lt;/code&gt; and never builds a URL. The connection string was the problem:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fmongodb-docker-authentication-failed%2F05-mongodb-password-unescaped-characters-docker.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fmongodb-docker-authentication-failed%2F05-mongodb-password-unescaped-characters-docker.webp" alt="Terminal capture: passwords with @, colon and slash in a MongoDB connection string fail with MongoRuntimeError Unable to parse and MongoParseError Password contains unescaped characters, and the percent-encoded password p%40ss%3Aw%2Frd%231 works from another container and from the host through a published port" width="800" height="237"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;client$ mongosh 'mongodb://root:p@ss:w/rd#1@mongo:27017/?authSource=admin' --eval 1
MongoRuntimeError: Unable to parse ss:w with URL
client$ mongosh 'mongodb://root:pa:ss@mongo:27017/?authSource=admin' --eval 1
MongoParseError: Password contains unescaped characters
client$ mongosh 'mongodb://root:pa@ss@mongo:27017/?authSource=admin' --eval 1
MongoParseError: Invalid connection string
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The MongoDB docs say that if the username or password includes &lt;code&gt;$ : / ? # [ ] @&lt;/code&gt;, "those characters must be converted using percent encoding". Python's &lt;code&gt;urllib.parse.quote("p@ss:w/rd#1", safe="")&lt;/code&gt; printed &lt;code&gt;p%40ss%3Aw%2Frd%231&lt;/code&gt;, and that version logged in. Put the encoded form only in the connection string. The &lt;code&gt;MONGO_INITDB_ROOT_PASSWORD&lt;/code&gt; value stays as typed. If the password contains &lt;code&gt;$&lt;/code&gt;, Compose also treats it as a variable, which the &lt;a href="https://deploylark.com/posts/mysql-docker-access-denied-root/" rel="noopener noreferrer"&gt;MySQL guide&lt;/a&gt; covers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connect from the host through a published port
&lt;/h3&gt;

&lt;p&gt;Without a &lt;code&gt;ports&lt;/code&gt; entry, nothing on the host can reach MongoDB. The lab published &lt;code&gt;127.0.0.1:27018:27017&lt;/code&gt;, and &lt;code&gt;mongosh&lt;/code&gt; with host networking connected to &lt;code&gt;127.0.0.1:27018&lt;/code&gt; and printed &lt;code&gt;9.0.4&lt;/code&gt;. Port 27017 on the host was refused, because only 27018 was published. Binding to &lt;code&gt;127.0.0.1&lt;/code&gt; keeps the port off the network, so only the host itself can connect.&lt;/p&gt;

&lt;h2&gt;
  
  
  A MongoDB Compose file that avoids these errors
&lt;/h2&gt;

&lt;h3&gt;
  
  
  compose.yaml, .env and the secret file
&lt;/h3&gt;

&lt;p&gt;This file ran verbatim in the lab:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;mongo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mongo:9.0.4&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;MONGO_INITDB_ROOT_USERNAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;root&lt;/span&gt;
      &lt;span class="na"&gt;MONGO_INITDB_ROOT_PASSWORD_FILE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/run/secrets/mongo_root_password&lt;/span&gt;
      &lt;span class="na"&gt;MONGO_INITDB_DATABASE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;appdb&lt;/span&gt;
      &lt;span class="na"&gt;APP_DB_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${APP_DB_PASSWORD}&lt;/span&gt;
    &lt;span class="na"&gt;secrets&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;mongo_root_password&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;mongo-data:/data/db&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./init:/docker-entrypoint-initdb.d:ro&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;mongosh"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--quiet"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--eval"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;db.adminCommand('ping').ok"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;10s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;
      &lt;span class="na"&gt;start_period&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;20s&lt;/span&gt;

  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mongo:9.0.4&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;mongo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;service_healthy&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;MONGODB_URI&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mongodb://app:${APP_DB_PASSWORD}@mongo:27017/appdb?authSource=appdb&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sh"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-c"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;mongosh&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;--quiet&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s"&gt;$$MONGODB_URI&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;--eval&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;'db.items.countDocuments()'"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;

&lt;span class="na"&gt;secrets&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;mongo_root_password&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;file&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./secrets/mongo_root_password&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;mongo-data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;APP_DB_PASSWORD=App-Pass-5532
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;./secrets/mongo_root_password&lt;/code&gt; holds the root password, and &lt;code&gt;./init/01-create-app-user.js&lt;/code&gt; is the script from above. The &lt;code&gt;_FILE&lt;/code&gt; suffix is documented on Docker Hub for both root variables. With it, &lt;code&gt;docker inspect&lt;/code&gt; on the container did not show the root password. Replace &lt;code&gt;app&lt;/code&gt; with your real service and give it the &lt;code&gt;MONGODB_URI&lt;/code&gt;. Keep &lt;code&gt;APP_DB_PASSWORD&lt;/code&gt; URL-safe or percent-encode it in the URI.&lt;/p&gt;

&lt;p&gt;The healthcheck only proves that &lt;code&gt;mongod&lt;/code&gt; answers, since &lt;code&gt;ping&lt;/code&gt; needs no login. It doesn't test the app's password. That is enough for &lt;code&gt;depends_on&lt;/code&gt;. A wrong app password still shows up in the app's own log as &lt;code&gt;Authentication failed.&lt;/code&gt; The &lt;a href="https://deploylark.com/posts/self-host-umami-docker-postgresql/" rel="noopener noreferrer"&gt;Umami guide&lt;/a&gt; uses the same &lt;code&gt;service_healthy&lt;/code&gt; pattern with PostgreSQL.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check it
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;code&amp;gt;text&lt;br&gt;
$ docker compose up -d&lt;br&gt;
 Container mongofinal-mongo-1  Started&lt;br&gt;
 Container mongofinal-mongo-1  Healthy&lt;br&gt;
 Container mongofinal-app-1  Started&lt;br&gt;
$ docker compose ps -a --format '&amp;amp;#123;&amp;amp;#123;.Service&amp;amp;#125;&amp;amp;#125; &amp;amp;#123;&amp;amp;#123;.Status&amp;amp;#125;&amp;amp;#125;'&lt;br&gt;
app Exited (0) 7 seconds ago&lt;br&gt;
mongo Up 18 seconds (healthy)&lt;br&gt;
$ docker compose logs --no-log-prefix app&lt;br&gt;
1&lt;br&gt;
$ docker compose restart mongo&lt;br&gt;
$ docker compose run --rm app&lt;br&gt;
1&lt;br&gt;
&amp;lt;/code&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The log showed &lt;code&gt;running /docker-entrypoint-initdb.d/01-create-app-user.js&lt;/code&gt; once, on the first start. Root logged in with the password from the secret file and &lt;code&gt;?authSource=admin&lt;/code&gt;. After the restart, the seeded document was still there. Keep the image pinned to an exact version such as &lt;code&gt;mongo:9.0.4&lt;/code&gt;, so a restart never pulls a new major version onto your data volume. The &lt;a href="https://deploylark.com/posts/postgres-docker-upgrade-17-to-18/" rel="noopener noreferrer"&gt;PostgreSQL 17 to 18 guide&lt;/a&gt; shows what a planned major upgrade of a database volume looks like.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Login errors
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Why does MongoDB in Docker say Authentication failed when the password is right?
&lt;/h4&gt;

&lt;p&gt;Check which database the user lives in. The root user from &lt;code&gt;MONGO_INITDB_ROOT_USERNAME&lt;/code&gt; is in &lt;code&gt;admin&lt;/code&gt;, so a URI like &lt;code&gt;mongodb://root:pass@mongo:27017/appdb&lt;/code&gt; needs &lt;code&gt;?authSource=admin&lt;/code&gt;. Run &lt;code&gt;docker logs&lt;/code&gt; on the MongoDB container. &lt;code&gt;UserNotFound&lt;/code&gt; points to the database, &lt;code&gt;storedKey mismatch&lt;/code&gt; to the password.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is the default username and password for the MongoDB Docker image?
&lt;/h4&gt;

&lt;p&gt;There is none. Without &lt;code&gt;MONGO_INITDB_ROOT_USERNAME&lt;/code&gt; and &lt;code&gt;MONGO_INITDB_ROOT_PASSWORD&lt;/code&gt;, the lab's server ran without &lt;code&gt;--auth&lt;/code&gt;, logged "Access control is not enabled for the database" and accepted writes without a login. If you set only one of the two, the container exits with &lt;code&gt;error: missing 'MONGO_INITDB_ROOT_USERNAME' or 'MONGO_INITDB_ROOT_PASSWORD'&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I connect to MongoDB in Docker from the host?
&lt;/h4&gt;

&lt;p&gt;Publish the port, for example &lt;code&gt;ports: ["127.0.0.1:27017:27017"]&lt;/code&gt;, and connect to &lt;code&gt;127.0.0.1&lt;/code&gt; with &lt;code&gt;?authSource=admin&lt;/code&gt; for the root user. Without a published port, the host can't reach the container.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup and init scripts
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Why are my docker-entrypoint-initdb.d scripts not running?
&lt;/h4&gt;

&lt;p&gt;The volume already holds a database. The entrypoint runs the scripts only when &lt;code&gt;/data/db&lt;/code&gt; has no data files. Run the script with &lt;code&gt;mongosh&lt;/code&gt; inside the container, or delete the volume if the data doesn't matter. Only &lt;code&gt;.js&lt;/code&gt; and &lt;code&gt;.sh&lt;/code&gt; files run.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I create a database and user for a MongoDB container on startup?
&lt;/h4&gt;

&lt;p&gt;Set &lt;code&gt;MONGO_INITDB_DATABASE&lt;/code&gt; and mount a &lt;code&gt;.js&lt;/code&gt; file into &lt;code&gt;/docker-entrypoint-initdb.d&lt;/code&gt; that calls &lt;code&gt;db.createUser()&lt;/code&gt; and inserts one document. MongoDB creates a database on the first write. The app then connects to that database without &lt;code&gt;authSource=admin&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I change the MongoDB root password in Docker?
&lt;/h4&gt;

&lt;p&gt;Changing &lt;code&gt;MONGO_INITDB_ROOT_PASSWORD&lt;/code&gt; has no effect on an existing volume. Log in with the old password and run &lt;code&gt;db.getSiblingDB("admin").changeUserPassword("root", "new-password")&lt;/code&gt;, then update &lt;code&gt;.env&lt;/code&gt; to match.&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>docker</category>
      <category>selfhosted</category>
      <category>devops</category>
    </item>
    <item>
      <title>PostgreSQL in Docker: Fix Password Authentication Failed</title>
      <dc:creator>DeployLark</dc:creator>
      <pubDate>Fri, 06 Nov 2026 10:32:53 +0000</pubDate>
      <link>https://dev.to/deploylark/postgresql-in-docker-fix-password-authentication-failed-57j5</link>
      <guid>https://dev.to/deploylark/postgresql-in-docker-fix-password-authentication-failed-57j5</guid>
      <description>&lt;p&gt;Your Postgres container runs, but every login fails with &lt;code&gt;password authentication failed for user "postgres"&lt;/code&gt;. Or &lt;code&gt;psql&lt;/code&gt; inside the container says &lt;code&gt;role "root" does not exist&lt;/code&gt;. Or the container exits right away with &lt;code&gt;Database is uninitialized and superuser password is not specified&lt;/code&gt;. The password in your &lt;code&gt;.env&lt;/code&gt; file looks right, and that is the trap.&lt;/p&gt;

&lt;p&gt;This guide reproduces each of these errors with the official &lt;code&gt;postgres:18.6&lt;/code&gt; image in Docker Compose and fixes them. Every error message below comes from that lab run on November 3, 2026, with a small Node.js client (&lt;code&gt;pg&lt;/code&gt; 8.23.1) standing in for your app. Most of them have one root cause: the image reads its &lt;code&gt;POSTGRES_*&lt;/code&gt; variables only once, the first time it creates the database.&lt;/p&gt;

&lt;p&gt;These questions get a lot of traffic. On Stack Overflow, &lt;a href="https://stackoverflow.com/questions/37694987/connecting-to-postgresql-in-a-docker-container-from-outside" rel="noopener noreferrer"&gt;Connecting to Postgresql in a docker container from outside&lt;/a&gt; had 986,699 views on November 3, 2026, and &lt;a href="https://stackoverflow.com/questions/60193781/postgres-with-docker-compose-gives-fatal-role-root-does-not-exist-error" rel="noopener noreferrer"&gt;postgres with docker compose gives FATAL: role "root" does not exist error&lt;/a&gt; had 188,086.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Changing &lt;code&gt;POSTGRES_PASSWORD&lt;/code&gt; after the first start does nothing. The log says &lt;code&gt;Skipping initialization&lt;/code&gt;. Change the password with &lt;code&gt;ALTER ROLE ... PASSWORD&lt;/code&gt; through &lt;code&gt;docker compose exec&lt;/code&gt;, or delete the volume if the data is disposable.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;role "root" does not exist&lt;/code&gt; means &lt;code&gt;psql&lt;/code&gt; or &lt;code&gt;pg_isready&lt;/code&gt; ran without &lt;code&gt;-U&lt;/code&gt;. Pass &lt;code&gt;-U&lt;/code&gt; with the user from &lt;code&gt;POSTGRES_USER&lt;/code&gt;. If you set &lt;code&gt;POSTGRES_USER&lt;/code&gt;, there is no &lt;code&gt;postgres&lt;/code&gt; role at all.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;superuser password is not specified&lt;/code&gt; means the password reached the container empty. Check for an unset &lt;code&gt;.env&lt;/code&gt; variable or a &lt;code&gt;$&lt;/code&gt; in the password, and use &lt;code&gt;${DB_PASSWORD:?}&lt;/code&gt; so Compose stops before it starts anything.&lt;/li&gt;
&lt;li&gt;From another container, connect to the service name, not &lt;code&gt;localhost&lt;/code&gt;, and wait for a healthcheck with &lt;code&gt;condition: service_healthy&lt;/code&gt;. URL-encode special characters in connection strings.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Fix &lt;code&gt;password authentication failed&lt;/code&gt; after changing POSTGRES_PASSWORD
&lt;/h2&gt;

&lt;p&gt;You started the stack once, then changed the password in &lt;code&gt;.env&lt;/code&gt; or &lt;code&gt;compose.yaml&lt;/code&gt;, recreated the container, and now nothing can log in with the new password.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why the new password never applied
&lt;/h3&gt;

&lt;p&gt;The official image runs &lt;code&gt;initdb&lt;/code&gt; only when the data directory is empty. The &lt;a href="https://github.com/docker-library/docs/blob/master/postgres/content.md" rel="noopener noreferrer"&gt;image documentation&lt;/a&gt; says it plainly: "the Docker specific variables will only have an effect if you start the container with a data directory that is empty." An existing database is left as it is.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fpostgres-docker-password-authentication-failed%2F01-postgres-docker-init-only-on-empty-volume-diagram.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fpostgres-docker-password-authentication-failed%2F01-postgres-docker-init-only-on-empty-volume-diagram.webp" alt="Diagram: when the postgres container starts, the entrypoint checks if the data directory is empty. If yes, initdb creates POSTGRES_USER with its password, creates POSTGRES_DB and runs the initdb.d scripts. If no, it prints Skipping initialization and ignores the POSTGRES variables and scripts" width="799" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the lab, the volume was first created with &lt;code&gt;DB_PASSWORD=first-secret&lt;/code&gt;. After changing &lt;code&gt;.env&lt;/code&gt; to &lt;code&gt;second-secret&lt;/code&gt; and running &lt;code&gt;docker compose up -d --force-recreate&lt;/code&gt;, the log showed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PostgreSQL Database directory appears to contain a database; Skipping initialization
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The container's environment had the new value, &lt;code&gt;POSTGRES_PASSWORD=second-secret&lt;/code&gt;. The database still had the old one. A login with the new password failed with &lt;code&gt;password authentication failed for user "postgres"&lt;/code&gt; (SQLSTATE &lt;code&gt;28P01&lt;/code&gt;), and the server log named the &lt;code&gt;pg_hba.conf&lt;/code&gt; rule the connection matched:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FATAL:  password authentication failed for user "postgres"
DETAIL:  Connection matched file "/var/lib/postgresql/18/docker/pg_hba.conf" line 128: "host all all all scram-sha-256"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same rule explains why init scripts seem to be ignored. A file mounted into &lt;code&gt;/docker-entrypoint-initdb.d&lt;/code&gt; after the first start never ran, and &lt;code&gt;select count(*) from notes&lt;/code&gt; returned &lt;code&gt;relation "notes" does not exist&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Change the password inside the database
&lt;/h3&gt;

&lt;p&gt;The image's &lt;code&gt;pg_hba.conf&lt;/code&gt; trusts connections over the local Unix socket, so &lt;code&gt;docker compose exec&lt;/code&gt; gets you in without the password. Set the new password with SQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose &lt;span class="nb"&gt;exec &lt;/span&gt;db psql &lt;span class="nt"&gt;-U&lt;/span&gt; postgres &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"ALTER ROLE postgres WITH PASSWORD 'second-secret'"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It printed &lt;code&gt;ALTER ROLE&lt;/code&gt;. The app logged in with &lt;code&gt;second-secret&lt;/code&gt; right away, without a restart, and &lt;code&gt;first-secret&lt;/code&gt; stopped working. Keep &lt;code&gt;.env&lt;/code&gt; and the database in sync from now on, because the app reads the new value from &lt;code&gt;.env&lt;/code&gt; while the database only changes through SQL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fpostgres-docker-password-authentication-failed%2F02-postgres-password-authentication-failed-after-change.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fpostgres-docker-password-authentication-failed%2F02-postgres-password-authentication-failed-after-change.webp" alt="Terminal capture: after changing DB_PASSWORD in .env, the postgres log says Skipping initialization, the container environment shows the new password, the login fails with password authentication failed for user postgres and a pg_hba.conf scram-sha-256 detail line, and ALTER ROLE fixes it" width="800" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the data is disposable, delete the volume instead. &lt;code&gt;docker compose down -v&lt;/code&gt; removes the project's volumes, and with them all data. On the next &lt;code&gt;up&lt;/code&gt;, the log showed &lt;code&gt;running /docker-entrypoint-initdb.d/01-notes.sql&lt;/code&gt;, the &lt;code&gt;notes&lt;/code&gt; table existed, and the new password worked. The &lt;a href="https://deploylark.com/posts/mysql-docker-access-denied-root/" rel="noopener noreferrer"&gt;MySQL&lt;/a&gt; and &lt;a href="https://deploylark.com/posts/mongodb-docker-authentication-failed/" rel="noopener noreferrer"&gt;MongoDB&lt;/a&gt; guides cover the same first-start rule for those images.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix &lt;code&gt;role "root" does not exist&lt;/code&gt; and &lt;code&gt;role "postgres" does not exist&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The second family of errors comes from the user name, not the password.&lt;/p&gt;

&lt;h3&gt;
  
  
  psql and pg_isready inside the container
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;docker compose exec db psql&lt;/code&gt; runs as root inside the container. Without &lt;code&gt;-U&lt;/code&gt;, psql uses that operating system user as the database user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  role "root" does not exist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The top answer on the Stack Overflow question, by richyen, puts it this way: "If you don't specify the PGUSER environment variable, then psql will assume you want to use the current OS user as your database user name." Pass the user, as in &lt;code&gt;psql -U postgres&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The same thing happens in healthchecks. With &lt;code&gt;test: ["CMD", "pg_isready"]&lt;/code&gt;, the container still showed &lt;code&gt;(healthy)&lt;/code&gt;, because &lt;code&gt;pg_isready&lt;/code&gt; reported the server as up even though the role was rejected. But the server log got a new &lt;code&gt;FATAL:  role "root" does not exist&lt;/code&gt; line every 5 seconds, one per check. With &lt;code&gt;["CMD", "pg_isready", "-U", "app", "-d", "app"]&lt;/code&gt;, the log had 0 FATAL lines over the next 20 seconds.&lt;/p&gt;

&lt;h3&gt;
  
  
  POSTGRES_USER replaces the postgres role
&lt;/h3&gt;

&lt;p&gt;When you set &lt;code&gt;POSTGRES_USER&lt;/code&gt;, the image creates that user as the superuser and does not create &lt;code&gt;postgres&lt;/code&gt;. With &lt;code&gt;POSTGRES_USER: app&lt;/code&gt; and no &lt;code&gt;POSTGRES_DB&lt;/code&gt;, the lab cluster had one role, &lt;code&gt;app&lt;/code&gt; (Superuser, Create role, Create DB, Replication, Bypass RLS), and a database also named &lt;code&gt;app&lt;/code&gt;, matching the docs: the variable creates "the specified user with superuser power and a database with the same name."&lt;/p&gt;

&lt;p&gt;So these failed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker compose exec db psql -U postgres
... FATAL:  role "postgres" does not exist
$ docker compose exec db psql -U app -d appdb
... FATAL:  database "appdb" does not exist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Over TCP, the error is less helpful. Logging in as &lt;code&gt;postgres&lt;/code&gt; from the app with the &lt;code&gt;app&lt;/code&gt; password returned &lt;code&gt;password authentication failed for user "postgres"&lt;/code&gt;, which sends you after the wrong problem. Only the server log told the truth: &lt;code&gt;DETAIL:  Role "postgres" does not exist.&lt;/code&gt; Check the server log before you reset any password.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fpostgres-docker-password-authentication-failed%2F03-postgres-role-root-does-not-exist.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fpostgres-docker-password-authentication-failed%2F03-postgres-role-root-does-not-exist.webp" alt="Terminal capture: with POSTGRES_USER set to app, psql without -U fails with role root does not exist, psql -U postgres fails with role postgres does not exist, a TCP login as postgres reports password authentication failed with a server detail that the role does not exist, and a pg_isready healthcheck without -U logs FATAL lines until -U app -d app is added" width="800" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Use the user and database you set, in &lt;code&gt;psql&lt;/code&gt;, in the healthcheck and in the app's connection string. If an app insists on a &lt;code&gt;postgres&lt;/code&gt; role or another database, create them with SQL: &lt;code&gt;CREATE ROLE postgres WITH LOGIN SUPERUSER PASSWORD '...'&lt;/code&gt; and &lt;code&gt;createdb -U app appdb&lt;/code&gt; both worked in the lab, and the app then logged in to &lt;code&gt;appdb&lt;/code&gt; as &lt;code&gt;postgres&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix &lt;code&gt;Database is uninitialized and superuser password is not specified&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This one stops the container on its first start. It exited with code 1 and printed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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".
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The message goes on to offer &lt;code&gt;POSTGRES_HOST_AUTH_METHOD=trust&lt;/code&gt;, which lets anyone connect without a password. Don't use that to get past this error. If you did set a password, check how it gets into the container.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unset .env variables and dollar signs
&lt;/h3&gt;

&lt;p&gt;In the lab, the compose file had &lt;code&gt;POSTGRES_PASSWORD: ${DB_PASSWORD}&lt;/code&gt; and there was no &lt;code&gt;.env&lt;/code&gt; file next to it. Compose printed a warning and started the container anyway, with an empty password:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;level=warning msg="The \"DB_PASSWORD\" variable is not set. Defaulting to a blank string."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;$&lt;/code&gt; inside a password does something similar. Compose treats it as the start of a variable. With &lt;code&gt;POSTGRES_PASSWORD: pa$word42&lt;/code&gt; written straight into &lt;code&gt;compose.yaml&lt;/code&gt;, Compose warned that &lt;code&gt;word42&lt;/code&gt; was not set, and the container got &lt;code&gt;POSTGRES_PASSWORD=pa&lt;/code&gt;. The cluster was created with the password &lt;code&gt;pa&lt;/code&gt;, so &lt;code&gt;pa$word42&lt;/code&gt; failed with &lt;code&gt;password authentication failed&lt;/code&gt; and &lt;code&gt;pa&lt;/code&gt; worked. The &lt;a href="https://docs.docker.com/reference/compose-file/interpolation/" rel="noopener noreferrer"&gt;Compose interpolation docs&lt;/a&gt; give the fix: "You can use a $$ (double-dollar sign) when your configuration needs a literal dollar sign." With &lt;code&gt;pa$$word42&lt;/code&gt;, the container got &lt;code&gt;pa$word42&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fpostgres-docker-password-authentication-failed%2F04-postgres-superuser-password-not-specified.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fpostgres-docker-password-authentication-failed%2F04-postgres-superuser-password-not-specified.webp" alt="Terminal capture: with DB_PASSWORD unset Compose warns and the postgres container exits with Database is uninitialized and superuser password is not specified, a dollar sign in the password is cut to pa until it is written as two dollar signs, and the colon question mark syntax makes Compose stop with an error before starting anything" width="800" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Make Compose fail before it starts anything
&lt;/h3&gt;

&lt;p&gt;Replace &lt;code&gt;${DB_PASSWORD}&lt;/code&gt; with &lt;code&gt;${DB_PASSWORD:?set DB_PASSWORD in .env}&lt;/code&gt;. With no &lt;code&gt;.env&lt;/code&gt; file, &lt;code&gt;docker compose up -d&lt;/code&gt; stopped right away and started nothing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error while interpolating services.db.environment.POSTGRES_PASSWORD: required variable DB_PASSWORD is missing a value: set DB_PASSWORD in .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This matters more than it looks. An empty password stops Postgres on the first start, but the &lt;code&gt;pa&lt;/code&gt; case above did not stop anything. It created a database with the wrong password, and the variable rule from the first section then kept that password for good.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix connection refused and pg_hba errors from other containers
&lt;/h2&gt;

&lt;p&gt;If the password and user are right and the app still can't connect, look at the address, the timing and the connection string.&lt;/p&gt;

&lt;h3&gt;
  
  
  localhost, published ports and startup order
&lt;/h3&gt;

&lt;p&gt;Inside a container, &lt;code&gt;localhost&lt;/code&gt; is that container. An app with &lt;code&gt;@localhost:5432&lt;/code&gt; in its URL got &lt;code&gt;connect ECONNREFUSED ::1:5432, connect ECONNREFUSED 127.0.0.1:5432&lt;/code&gt;. Use the Compose service name, such as &lt;code&gt;db&lt;/code&gt;, as the host. &lt;a href="https://node-postgres.com/features/connecting" rel="noopener noreferrer"&gt;node-postgres&lt;/a&gt; defaults to &lt;code&gt;PGHOST=localhost&lt;/code&gt; when you set no host, so always set it.&lt;/p&gt;

&lt;p&gt;From the host machine, the container is only reachable if you publish the port. Without &lt;code&gt;ports&lt;/code&gt;, &lt;code&gt;psql -h 127.0.0.1 -p 5432&lt;/code&gt; on the host got &lt;code&gt;Connection refused&lt;/code&gt;. With &lt;code&gt;ports: - "127.0.0.1:5433:5432"&lt;/code&gt;, the host connected on port 5433 and read the data. Binding to &lt;code&gt;127.0.0.1&lt;/code&gt; keeps the port off the host's other network interfaces.&lt;/p&gt;

&lt;p&gt;Timing is the other trap. With &lt;code&gt;depends_on: - db&lt;/code&gt; and no condition, the app started as soon as the database container did. On a fresh volume with an init script, it failed with &lt;code&gt;connect ECONNREFUSED 172.28.0.2:5432&lt;/code&gt; and exited. During the first start, the entrypoint runs a temporary server with &lt;code&gt;listen_addresses=''&lt;/code&gt;, so nothing listens on TCP until the init scripts are done. Starting the app again a few seconds later worked. The fix is a healthcheck plus &lt;code&gt;condition: service_healthy&lt;/code&gt;, shown in the Compose file below. The &lt;a href="https://deploylark.com/posts/grafana-prometheus-docker-permission-denied/" rel="noopener noreferrer"&gt;Grafana and Prometheus guide&lt;/a&gt; and the &lt;a href="https://deploylark.com/posts/redis-docker-connection-refused-noauth-misconf/" rel="noopener noreferrer"&gt;Redis guide&lt;/a&gt; run into the same &lt;code&gt;localhost&lt;/code&gt; mistake.&lt;/p&gt;

&lt;h3&gt;
  
  
  Passwords in URLs and pg_hba.conf entries
&lt;/h3&gt;

&lt;p&gt;Connection strings are URLs, so &lt;code&gt;@&lt;/code&gt;, &lt;code&gt;#&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt; and &lt;code&gt;:&lt;/code&gt; in a password break them. With the password &lt;code&gt;p@ss#word/1&lt;/code&gt; pasted into &lt;code&gt;postgres://app:p@ss#word/1@db:5432/app&lt;/code&gt;, the parser read the password as &lt;code&gt;p&lt;/code&gt;, the host as &lt;code&gt;ss&lt;/code&gt; and no database at all. Encode the password first. &lt;code&gt;node -p 'encodeURIComponent("p@ss#word/1")'&lt;/code&gt; printed &lt;code&gt;p%40ss%23word%2F1&lt;/code&gt;, and the URL with that value logged in. Or skip the URL and pass &lt;code&gt;PGHOST&lt;/code&gt;, &lt;code&gt;PGUSER&lt;/code&gt;, &lt;code&gt;PGPASSWORD&lt;/code&gt; and &lt;code&gt;PGDATABASE&lt;/code&gt; as separate variables, as the Compose file below does.&lt;/p&gt;

&lt;p&gt;You only see &lt;code&gt;no pg_hba.conf entry&lt;/code&gt; if you replaced the image's default &lt;code&gt;pg_hba.conf&lt;/code&gt;, which ends with &lt;code&gt;host all all all scram-sha-256&lt;/code&gt; and allows every address with a password. A custom file that only allowed &lt;code&gt;127.0.0.1/32&lt;/code&gt; over TCP rejected the app container:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;no pg_hba.conf entry for host "172.28.0.4", user "postgres", database "postgres", no encryption
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add a line for the Docker network. The &lt;a href="https://www.postgresql.org/docs/18/auth-pg-hba-conf.html" rel="noopener noreferrer"&gt;pg_hba.conf docs&lt;/a&gt; describe &lt;code&gt;samenet&lt;/code&gt; as "any address in any subnet that the server is directly connected to", which covers the Compose network. After adding &lt;code&gt;host  all all samenet scram-sha-256&lt;/code&gt; and running &lt;code&gt;SELECT pg_reload_conf()&lt;/code&gt;, the same login worked without a restart. If you carried a custom file over from an upgrade, the &lt;a href="https://deploylark.com/posts/postgres-docker-upgrade-17-to-18/" rel="noopener noreferrer"&gt;PostgreSQL 17 to 18 upgrade guide&lt;/a&gt; covers the other places it can break.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fpostgres-docker-password-authentication-failed%2F05-postgres-connection-refused-pg-hba-docker.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fpostgres-docker-password-authentication-failed%2F05-postgres-connection-refused-pg-hba-docker.webp" alt="Terminal capture: the app gets ECONNREFUSED while the database runs its init script, localhost inside the app container is refused, an unencoded password is parsed as host ss, the encodeURIComponent value logs in, and a custom pg_hba.conf returns no pg_hba.conf entry for host until a samenet line is added" width="800" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A PostgreSQL Compose file that avoids these errors
&lt;/h2&gt;

&lt;p&gt;This file puts the fixes together. It ran verbatim in the lab. The &lt;code&gt;app&lt;/code&gt; service is the small Node.js test client from the lab. Replace its image, command and volume with your own app and keep the environment and &lt;code&gt;depends_on&lt;/code&gt; parts.&lt;/p&gt;

&lt;h3&gt;
  
  
  The compose file
&lt;/h3&gt;

&lt;p&gt;Create &lt;code&gt;.env&lt;/code&gt; next to it with a random password. Hex characters need no escaping in Compose or in URLs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'DB_PASSWORD=%s\n'&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;openssl rand &lt;span class="nt"&gt;-hex&lt;/span&gt; 24&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres:18.6&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_DB&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_PASSWORD:?set DB_PASSWORD in .env}&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;pgdata:/var/lib/postgresql&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./initdb:/docker-entrypoint-initdb.d:ro&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pg_isready"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-U"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;app"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-d"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;app"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;3s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
      &lt;span class="na"&gt;start_period&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;30s&lt;/span&gt;

  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;node:24.22.2-alpine&lt;/span&gt;
    &lt;span class="na"&gt;working_dir&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/app&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;node pgtest.mjs&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;PGHOST&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;db&lt;/span&gt;
      &lt;span class="na"&gt;PGUSER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app&lt;/span&gt;
      &lt;span class="na"&gt;PGDATABASE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;app&lt;/span&gt;
      &lt;span class="na"&gt;PGPASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DB_PASSWORD:?set DB_PASSWORD in .env}&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./app:/app:ro&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;service_healthy&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;pgdata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The volume is mounted at &lt;code&gt;/var/lib/postgresql&lt;/code&gt;, the path that the 18 images expect. Both services read the same &lt;code&gt;DB_PASSWORD&lt;/code&gt;, so there is one place to change it. If you change it later, also run the &lt;code&gt;ALTER ROLE&lt;/code&gt; command from the first section, or the app gets &lt;code&gt;28P01&lt;/code&gt; again.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check that it works
&lt;/h3&gt;

&lt;p&gt;Run &lt;code&gt;docker compose up -d&lt;/code&gt;. Compose waited for the database before it started the app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Container pgfinal-db-1  Waiting
 Container pgfinal-db-1  Healthy
 Container pgfinal-app-1  Starting
 Container pgfinal-app-1  Started
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The app printed &lt;code&gt;OK app app PostgreSQL 18.6 (Debian 18.6-1.pgdg13+2)&lt;/code&gt;, and the log showed &lt;code&gt;running /docker-entrypoint-initdb.d/01-schema.sql&lt;/code&gt;. After &lt;code&gt;docker compose restart db&lt;/code&gt; and &lt;code&gt;docker compose run --rm app&lt;/code&gt;, the app connected again, and the database log had 0 FATAL lines for the whole run. For a full app stack on this pattern, see the &lt;a href="https://deploylark.com/posts/self-host-umami-docker-postgresql/" rel="noopener noreferrer"&gt;Umami with PostgreSQL guide&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Passwords and users
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Why does Postgres in Docker say password authentication failed when the password is right?
&lt;/h4&gt;

&lt;p&gt;Either the database still has the password from its first start, or the user doesn't exist. Look at the server log with &lt;code&gt;docker compose logs db&lt;/code&gt;. &lt;code&gt;Skipping initialization&lt;/code&gt; at startup means your new &lt;code&gt;POSTGRES_PASSWORD&lt;/code&gt; was ignored. &lt;code&gt;DETAIL:  Role "..." does not exist.&lt;/code&gt; means the user name is wrong.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I use a new password from .env without removing the volume?
&lt;/h4&gt;

&lt;p&gt;Run &lt;code&gt;docker compose exec db psql -U &amp;lt;user&amp;gt; -c "ALTER ROLE &amp;lt;user&amp;gt; WITH PASSWORD '&amp;lt;new&amp;gt;'"&lt;/code&gt;. It works over the local socket without the old password and takes effect right away. Then keep the same value in &lt;code&gt;.env&lt;/code&gt; for the app.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why do I get role "root" does not exist?
&lt;/h4&gt;

&lt;p&gt;Something ran &lt;code&gt;psql&lt;/code&gt; or &lt;code&gt;pg_isready&lt;/code&gt; inside the container without &lt;code&gt;-U&lt;/code&gt;, so it used the operating system user &lt;code&gt;root&lt;/code&gt;. Add &lt;code&gt;-U&lt;/code&gt; with your &lt;code&gt;POSTGRES_USER&lt;/code&gt; (and &lt;code&gt;-d&lt;/code&gt; with your database) to the command or the healthcheck.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup and connections
&lt;/h3&gt;

&lt;h4&gt;
  
  
  How do I connect to Postgres in Docker from the host?
&lt;/h4&gt;

&lt;p&gt;Publish the port, for example &lt;code&gt;ports: - "127.0.0.1:5433:5432"&lt;/code&gt;, then connect to &lt;code&gt;127.0.0.1&lt;/code&gt; on port 5433 with the user and password from your Compose file. Other containers on the same network use the service name and port 5432 and need no published port.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why didn't my docker-entrypoint-initdb.d script run?
&lt;/h4&gt;

&lt;p&gt;Init scripts run only when the data directory is empty, the same as the &lt;code&gt;POSTGRES_*&lt;/code&gt; variables. If the volume already has a database, the scripts are skipped. Run the SQL by hand with &lt;code&gt;docker compose exec&lt;/code&gt;, or delete the volume if the data is disposable.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I fix Database is uninitialized and superuser password is not specified?
&lt;/h4&gt;

&lt;p&gt;Give &lt;code&gt;POSTGRES_PASSWORD&lt;/code&gt; a non-empty value. Check that your &lt;code&gt;.env&lt;/code&gt; file sits next to &lt;code&gt;compose.yaml&lt;/code&gt; and defines the variable, and run &lt;code&gt;docker compose config&lt;/code&gt; to see the value Compose actually passes. Use &lt;code&gt;${DB_PASSWORD:?}&lt;/code&gt; so a missing value stops Compose with an error.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>docker</category>
      <category>selfhosted</category>
      <category>devops</category>
    </item>
    <item>
      <title>RabbitMQ in Docker: Fix ACCESS_REFUSED and Connection Refused</title>
      <dc:creator>DeployLark</dc:creator>
      <pubDate>Fri, 06 Nov 2026 10:32:09 +0000</pubDate>
      <link>https://dev.to/deploylark/rabbitmq-in-docker-fix-accessrefused-and-connection-refused-41ea</link>
      <guid>https://dev.to/deploylark/rabbitmq-in-docker-fix-accessrefused-and-connection-refused-41ea</guid>
      <description>&lt;p&gt;You add &lt;code&gt;rabbitmq&lt;/code&gt; to a Compose file, point Celery or Spring AMQP at &lt;code&gt;amqp://guest:guest@localhost:5672/&lt;/code&gt;, and the client dies with &lt;code&gt;Connection refused&lt;/code&gt;. Or the TCP path works and you still get &lt;code&gt;ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN&lt;/code&gt;. The broker log may say &lt;code&gt;user 'guest' can only connect via localhost&lt;/code&gt;, or &lt;code&gt;invalid credentials&lt;/code&gt;, or nothing useful while your new &lt;code&gt;RABBITMQ_DEFAULT_PASS&lt;/code&gt; is ignored.&lt;/p&gt;

&lt;p&gt;This guide reproduces those failures with &lt;code&gt;rabbitmq:4.4.0-management-alpine&lt;/code&gt; and a small Pika 1.3.2 client on the same Compose network, then fixes them. Every error string and log line below comes from that lab run on November 6, 2026. The Stack Overflow question &lt;a href="https://stackoverflow.com/questions/26811924/spring-amqp-rabbitmq-3-3-5-access-refused-login-was-refused-using-authentica" rel="noopener noreferrer"&gt;ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN&lt;/a&gt; had 370,994 views that day, &lt;a href="https://stackoverflow.com/questions/53031439/connecting-to-rabbitmq-container-with-docker-compose" rel="noopener noreferrer"&gt;Connecting to RabbitMQ container with docker-compose&lt;/a&gt; had 50,479, and &lt;a href="https://stackoverflow.com/questions/55086050/rabbitmq-docker-connection-refused-on-port-5672" rel="noopener noreferrer"&gt;Rabbitmq - Docker connection refused on port 5672&lt;/a&gt; had 35,539.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;From another container, &lt;code&gt;localhost&lt;/code&gt; is that container. Use the Compose service name, for example &lt;code&gt;rabbitmq&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ACCESS_REFUSED&lt;/code&gt; with PLAIN is the same client text for wrong passwords and for the guest loopback rule. Read the broker log line under it.&lt;/li&gt;
&lt;li&gt;The official Docker image sets &lt;code&gt;loopback_users.guest = false&lt;/code&gt;, so guest from a peer container often works. Bare-metal RabbitMQ and a restored loopback config refuse guest from peers.&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;RABBITMQ_DEFAULT_USER&lt;/code&gt; and &lt;code&gt;RABBITMQ_DEFAULT_PASS&lt;/code&gt; on first boot. Changing them later does nothing if the data volume already exists.&lt;/li&gt;
&lt;li&gt;Wait for a real healthcheck before starting the app. &lt;code&gt;depends_on&lt;/code&gt; alone is not enough.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Table of contents
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Fix Connection refused to port 5672
&lt;/h2&gt;

&lt;p&gt;Start with the map. Match the message you see to the cause and the fix:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Frabbitmq-docker-access-refused%2F01-rabbitmq-docker-error-cause-fix-diagram.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Frabbitmq-docker-access-refused%2F01-rabbitmq-docker-error-cause-fix-diagram.webp" alt="Table diagram mapping RabbitMQ Docker errors to causes and fixes: Connection refused on localhost or before ready, ACCESS_REFUSED guest can only connect via localhost, and ACCESS_REFUSED invalid credentials with a stale volume" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Don't connect to localhost from the app container
&lt;/h3&gt;

&lt;p&gt;The lab started with stock RabbitMQ and no custom user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;rabbitmq&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rabbitmq:4.4.0-management-alpine&lt;/span&gt;
    &lt;span class="na"&gt;hostname&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rabbitmq&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A Pika client on the same Compose network played the app. With &lt;code&gt;RMQ_HOST=localhost&lt;/code&gt; it never reached RabbitMQ:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;client hostname='b1ce59b7a70e'
connecting host='localhost' port=5672 user='guest' vhost='/'
tcp connect to localhost:5672 failed: [Errno 111] Connection refused
FAIL AMQPConnectionError: ConnectionRefusedError(111, 'Connection refused')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Frabbitmq-docker-access-refused%2F02-rabbitmq-docker-connection-refused-localhost.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Frabbitmq-docker-access-refused%2F02-rabbitmq-docker-connection-refused-localhost.webp" alt="Terminal capture: Pika client in an app container gets Connection refused on localhost:5672, and the same refused error when it connects to rabbitmq before the listener is ready" width="798" height="165"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each container has its own loopback interface, so &lt;code&gt;localhost&lt;/code&gt; inside the app is the app. RabbitMQ listens in the &lt;code&gt;rabbitmq&lt;/code&gt; service. Set the host to &lt;code&gt;rabbitmq&lt;/code&gt; (or whatever you named the service), for example &lt;code&gt;amqp://labuser:secret@rabbitmq:5672/&lt;/code&gt;. The &lt;a href="https://deploylark.comhttps://deploylark.com/posts/redis-docker-connection-refused-noauth-misconf/" rel="noopener noreferrer"&gt;Redis guide&lt;/a&gt; and the &lt;a href="https://deploylark.comhttps://deploylark.com/posts/mysql-docker-access-denied-root/" rel="noopener noreferrer"&gt;MySQL guide&lt;/a&gt; show the same mistake for those databases.&lt;/p&gt;

&lt;p&gt;From the Docker host itself you also need a published port. Without &lt;code&gt;ports&lt;/code&gt;, a host tool cannot reach 5672. Add &lt;code&gt;ports: ["127.0.0.1:5672:5672"]&lt;/code&gt; if a process on the host must talk AMQP. Other Compose services do not need that mapping.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wait until the listener is ready
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;depends_on: [rabbitmq]&lt;/code&gt; only waits for the container process to start. In the lab, an immediate connect right after &lt;code&gt;docker compose up -d&lt;/code&gt; still got &lt;code&gt;Connection refused&lt;/code&gt; on &lt;code&gt;rabbitmq:5672&lt;/code&gt;. After the healthcheck flipped to healthy, the same client opened AMQP and published a message. Use a healthcheck and &lt;code&gt;condition: service_healthy&lt;/code&gt;, the pattern also used in the &lt;a href="https://deploylark.comhttps://deploylark.com/posts/self-host-umami-docker-postgresql/" rel="noopener noreferrer"&gt;Umami guide&lt;/a&gt; with PostgreSQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;rabbitmq&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rabbitmq:4.4.0-management-alpine&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;rabbitmq-diagnostics"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-q"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ping"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;10s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;12&lt;/span&gt;
      &lt;span class="na"&gt;start_period&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;15s&lt;/span&gt;
  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;your-app:tag&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;rabbitmq&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;service_healthy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Fix ACCESS_REFUSED for the guest user
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Read the broker log, not only the client text
&lt;/h3&gt;

&lt;p&gt;The client message is almost always:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That one string covers several causes. The broker log names the real one. With a wrong password the lab logged &lt;code&gt;PLAIN login refused: user 'guest' - invalid credentials&lt;/code&gt;. With the guest loopback rule restored it logged &lt;code&gt;PLAIN login refused: user 'guest' can only connect via localhost&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Frabbitmq-docker-access-refused%2F04-rabbitmq-docker-access-refused-invalid-credentials.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Frabbitmq-docker-access-refused%2F04-rabbitmq-docker-access-refused-invalid-credentials.webp" alt="Terminal capture: wrong guest password returns ACCESS_REFUSED PLAIN on the client and PLAIN login refused invalid credentials in the RabbitMQ broker log" width="798" height="127"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Guest loopback on bare metal vs the Docker image
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://www.rabbitmq.com/docs/access-control#loopback-users" rel="noopener noreferrer"&gt;RabbitMQ access control docs&lt;/a&gt; state the product default: "By default, the &lt;code&gt;guest&lt;/code&gt; user is prohibited from connecting from remote hosts. It can only connect over a loopback interface (&lt;code&gt;localhost&lt;/code&gt;)."&lt;/p&gt;

&lt;p&gt;The official Docker image changes that for Compose peers. In &lt;code&gt;rabbitmq:4.4.0-management-alpine&lt;/code&gt;, &lt;code&gt;/etc/rabbitmq/conf.d/10-defaults.conf&lt;/code&gt; contains this setting (the image file comments it with &lt;code&gt;##&lt;/code&gt;, shown here with &lt;code&gt;#&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# allow access to the guest user from anywhere on the network
loopback_users.guest = false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So a stock image often accepts &lt;code&gt;guest&lt;/code&gt; from another container on the Compose network. That surprises people who paste bare-metal advice into a Docker setup, and the reverse also happens: if you restore the product default, guest from a peer fails again.&lt;/p&gt;

&lt;p&gt;The lab copied &lt;code&gt;loopback_users.guest = true&lt;/code&gt; into &lt;code&gt;conf.d/20-loopback.conf&lt;/code&gt; and rebuilt a thin image on top of &lt;code&gt;rabbitmq:4.4.0-management-alpine&lt;/code&gt;. From a peer container, guest then failed with ACCESS_REFUSED. The broker said:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PLAIN login refused: user 'guest' can only connect via localhost
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same guest credentials from &lt;code&gt;127.0.0.1&lt;/code&gt; inside the RabbitMQ network namespace opened AMQP successfully.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Frabbitmq-docker-access-refused%2F03-rabbitmq-docker-access-refused-guest-loopback.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Frabbitmq-docker-access-refused%2F03-rabbitmq-docker-access-refused-guest-loopback.webp" alt="Terminal capture: guest from the rabbitmq service name gets ACCESS_REFUSED with broker log user guest can only connect via localhost, then the same guest credentials succeed on 127.0.0.1 inside the broker container" width="800" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do not "fix" this by setting &lt;code&gt;loopback_users = none&lt;/code&gt; in production. Create a non-guest user instead. The docs call remote guest access a danger zone for a reason.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix DEFAULT_USER that never changes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Seed a user on first boot
&lt;/h3&gt;

&lt;p&gt;On an empty data directory, the image reads &lt;code&gt;RABBITMQ_DEFAULT_USER&lt;/code&gt; and &lt;code&gt;RABBITMQ_DEFAULT_PASS&lt;/code&gt; and creates that administrator. The lab used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;rabbitmq&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rabbitmq:4.4.0-management-alpine&lt;/span&gt;
    &lt;span class="na"&gt;hostname&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rabbitmq&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;RABBITMQ_DEFAULT_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;labuser&lt;/span&gt;
      &lt;span class="na"&gt;RABBITMQ_DEFAULT_PASS&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Lab-Pass-4821&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1:5672:5672"&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1:15672:15672"&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;rabbitmq-diagnostics"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-q"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ping"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;10s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;12&lt;/span&gt;
      &lt;span class="na"&gt;start_period&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;15s&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;rmq-data:/var/lib/rabbitmq&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;rmq-data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the container became healthy, &lt;code&gt;rabbitmqctl list_users&lt;/code&gt; showed only &lt;code&gt;labuser&lt;/code&gt; with the administrator tag. The Pika client with those credentials published and consumed a durable queue message. &lt;code&gt;guest&lt;/code&gt;/&lt;code&gt;guest&lt;/code&gt; then returned ACCESS_REFUSED with &lt;code&gt;invalid credentials&lt;/code&gt;. The management HTTP API on &lt;code&gt;127.0.0.1:15672&lt;/code&gt; answered 200 for &lt;code&gt;labuser&lt;/code&gt; and reported &lt;code&gt;rabbitmq_version&lt;/code&gt; &lt;code&gt;4.4.0&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  A named volume keeps the first password
&lt;/h3&gt;

&lt;p&gt;The image stores users in &lt;code&gt;/var/lib/rabbitmq&lt;/code&gt;. If you change &lt;code&gt;RABBITMQ_DEFAULT_PASS&lt;/code&gt; in Compose and recreate the container with the same named volume, the env vars do not rewrite the existing user. That is the core of &lt;a href="https://github.com/docker-library/rabbitmq/issues/150" rel="noopener noreferrer"&gt;docker-library/rabbitmq#150&lt;/a&gt; (29 comments): people edit the password in &lt;code&gt;.env&lt;/code&gt;, restart, and still get ACCESS_REFUSED.&lt;/p&gt;

&lt;p&gt;In the lab, the volume kept &lt;code&gt;Lab-Pass-4821&lt;/code&gt;. After switching the env to &lt;code&gt;New-Pass-9999&lt;/code&gt;, the new password failed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FAIL ProbableAuthenticationError: ConnectionClosedByBroker: (403) 'ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The broker logged &lt;code&gt;PLAIN login refused: user 'labuser' - invalid credentials&lt;/code&gt;. The old password still opened AMQP.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Frabbitmq-docker-access-refused%2F05-rabbitmq-docker-default-user-stale-volume.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Frabbitmq-docker-access-refused%2F05-rabbitmq-docker-default-user-stale-volume.webp" alt="Terminal capture: labuser with RABBITMQ_DEFAULT_USER works on first boot, then after changing RABBITMQ_DEFAULT_PASS with the same volume the new password gets ACCESS_REFUSED while the original password still connects" width="799" height="253"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To rotate credentials on purpose, change the password with &lt;code&gt;rabbitmqctl change_password&lt;/code&gt;, or remove the volume and accept a wipe of queues and users, then boot once with the new env. The &lt;a href="https://deploylark.comhttps://deploylark.com/posts/postgres-docker-password-authentication-failed/" rel="noopener noreferrer"&gt;PostgreSQL password guide&lt;/a&gt; and the &lt;a href="https://deploylark.comhttps://deploylark.com/posts/mongodb-docker-authentication-failed/" rel="noopener noreferrer"&gt;MongoDB auth guide&lt;/a&gt; hit the same first-boot-vs-volume pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Compose file that works
&lt;/h2&gt;

&lt;p&gt;Put the service name, a non-guest user, a healthcheck, and a volume together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;rabbitmq&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rabbitmq:4.4.0-management-alpine&lt;/span&gt;
    &lt;span class="na"&gt;hostname&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rabbitmq&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;RABBITMQ_DEFAULT_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${RABBITMQ_DEFAULT_USER:?set RABBITMQ_DEFAULT_USER in .env}&lt;/span&gt;
      &lt;span class="na"&gt;RABBITMQ_DEFAULT_PASS&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${RABBITMQ_DEFAULT_PASS:?set RABBITMQ_DEFAULT_PASS in .env}&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1:5672:5672"&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1:15672:15672"&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;rabbitmq-diagnostics"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-q"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ping"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;10s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;12&lt;/span&gt;
      &lt;span class="na"&gt;start_period&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;15s&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;rmq-data:/var/lib/rabbitmq&lt;/span&gt;

  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;your-app:tag&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;AMQP_URL&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;amqp://${RABBITMQ_DEFAULT_USER}:${RABBITMQ_DEFAULT_PASS}@rabbitmq:5672/&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;rabbitmq&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;service_healthy&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;rmq-data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RABBITMQ_DEFAULT_USER=labuser
RABBITMQ_DEFAULT_PASS=replace-me-with-a-long-secret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Point every client at host &lt;code&gt;rabbitmq&lt;/code&gt;, not &lt;code&gt;localhost&lt;/code&gt;. Drop the &lt;code&gt;ports&lt;/code&gt; block if nothing on the host needs AMQP or the management UI. Keep &lt;code&gt;127.0.0.1&lt;/code&gt; when you do publish, the same habit as in the &lt;a href="https://deploylark.comhttps://deploylark.com/posts/portainer-ce-install-stacks/" rel="noopener noreferrer"&gt;Portainer guide&lt;/a&gt; when you bind admin ports to the host loopback only.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check it
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ docker compose up -d
$ docker compose ps
# rabbitmq health: healthy
$ docker compose exec rabbitmq rabbitmqctl list_users
Listing users ...
user    tags
labuser [administrator]
$ curl -sS -u labuser:replace-me-with-a-long-secret http://127.0.0.1:15672/api/overview | head -c 80
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the app still fails, read &lt;code&gt;docker compose logs rabbitmq&lt;/code&gt; for the &lt;code&gt;PLAIN login refused&lt;/code&gt; line before you change more config.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Connection refused
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Why does RabbitMQ refuse connections between Docker Compose containers?
&lt;/h4&gt;

&lt;p&gt;Usually the app uses &lt;code&gt;localhost&lt;/code&gt; or &lt;code&gt;127.0.0.1&lt;/code&gt;, which is its own container. Use the Compose service name, such as &lt;code&gt;rabbitmq&lt;/code&gt;. If the host is already correct, the broker may not be listening yet. Add a &lt;code&gt;rabbitmq-diagnostics -q ping&lt;/code&gt; healthcheck and &lt;code&gt;depends_on&lt;/code&gt; with &lt;code&gt;condition: service_healthy&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I connect to RabbitMQ in Docker from the host machine?
&lt;/h4&gt;

&lt;p&gt;Publish the port, for example &lt;code&gt;ports: ["127.0.0.1:5672:5672"]&lt;/code&gt;, and connect to &lt;code&gt;127.0.0.1:5672&lt;/code&gt; with a user that exists in the broker. Without &lt;code&gt;ports&lt;/code&gt;, the host gets Connection refused. Other containers on the Compose network do not need a published port.&lt;/p&gt;

&lt;h3&gt;
  
  
  ACCESS_REFUSED and guest
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Why do I get ACCESS_REFUSED with guest and PLAIN from another container?
&lt;/h4&gt;

&lt;p&gt;Check the broker log. If it says &lt;code&gt;user 'guest' can only connect via localhost&lt;/code&gt;, the loopback rule is on. Create a non-guest user with &lt;code&gt;RABBITMQ_DEFAULT_USER&lt;/code&gt; and &lt;code&gt;RABBITMQ_DEFAULT_PASS&lt;/code&gt; instead of reopening guest. If the log says &lt;code&gt;invalid credentials&lt;/code&gt;, the password or username is wrong for the data in the volume.&lt;/p&gt;

&lt;h4&gt;
  
  
  Does the official RabbitMQ Docker image allow guest from other containers?
&lt;/h4&gt;

&lt;p&gt;Yes, by default. &lt;code&gt;rabbitmq:4.4.0-management-alpine&lt;/code&gt; ships &lt;code&gt;loopback_users.guest = false&lt;/code&gt; in &lt;code&gt;/etc/rabbitmq/conf.d/10-defaults.conf&lt;/code&gt; so Compose peers can use guest. That differs from bare-metal RabbitMQ, where guest is localhost-only. Still create a dedicated user for anything beyond a throwaway lab.&lt;/p&gt;

&lt;h3&gt;
  
  
  Users and volumes
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Why does RABBITMQ_DEFAULT_PASS not change my password after a restart?
&lt;/h4&gt;

&lt;p&gt;Those variables seed the default user only when the data directory is empty. A named volume keeps the first user and password. Change the password with &lt;code&gt;rabbitmqctl change_password&lt;/code&gt;, or remove the volume and boot once with the new env if you accept wiping broker data.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I create a RabbitMQ user for Docker Compose the right way?
&lt;/h4&gt;

&lt;p&gt;Set &lt;code&gt;RABBITMQ_DEFAULT_USER&lt;/code&gt; and &lt;code&gt;RABBITMQ_DEFAULT_PASS&lt;/code&gt; before the first start with an empty volume, or run &lt;code&gt;rabbitmqctl add_user&lt;/code&gt;, &lt;code&gt;set_user_tags&lt;/code&gt;, and &lt;code&gt;set_permissions&lt;/code&gt; against a running node. Point clients at that user and the service name &lt;code&gt;rabbitmq&lt;/code&gt; on port 5672.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>rabbitmq</category>
      <category>selfhosted</category>
      <category>devops</category>
    </item>
    <item>
      <title>S3 NoSuchBucket in Docker: Fix Bucket Not Found Errors</title>
      <dc:creator>DeployLark</dc:creator>
      <pubDate>Fri, 06 Nov 2026 03:23:45 +0000</pubDate>
      <link>https://dev.to/deploylark/s3-nosuchbucket-in-docker-fix-bucket-not-found-errors-582</link>
      <guid>https://dev.to/deploylark/s3-nosuchbucket-in-docker-fix-bucket-not-found-errors-582</guid>
      <description>&lt;p&gt;Your app talks to self-hosted S3 storage in Docker, and the upload fails with &lt;code&gt;NoSuchBucket&lt;/code&gt;. The message says &lt;code&gt;Bucket not found&lt;/code&gt; or &lt;code&gt;The specified bucket does not exist&lt;/code&gt;. But the bucket is right there in the admin UI. Or the same call works from the AWS CLI and fails from your Node app.&lt;/p&gt;

&lt;p&gt;This guide reproduces those failures in Docker Compose with Garage v2.5.0, a maintained open-source S3 server, and fixes each one. Every Garage and RustFS error below comes from that lab run on November 2, 2026. In several cases the bucket existed the whole time. The server read a different bucket name than the one you meant, because of a typo, the URL style, the endpoint or the access key.&lt;/p&gt;

&lt;p&gt;The questions get steady traffic. On Stack Overflow, &lt;a href="https://stackoverflow.com/questions/21335580/code-nosuchbucket" rel="noopener noreferrer"&gt;Code: NoSuchBucket&lt;/a&gt; had 78,316 views on November 2, 2026, and &lt;a href="https://stackoverflow.com/questions/56627446/docker-compose-how-to-use-minio-in-and-outside-of-the-docker-network" rel="noopener noreferrer"&gt;docker-compose: how to use minio in- and outside of the docker network&lt;/a&gt; had 57,838.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read the bucket name inside the error. Garage prints it, as in &lt;code&gt;Bucket not found: cat.txt&lt;/code&gt;. If that isn't your bucket, the request was built wrong.&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;forcePathStyle: true&lt;/code&gt; (or the path-style option of your SDK) for self-hosted endpoints. The AWS SDK for JavaScript v3 puts the bucket in the hostname by default.&lt;/li&gt;
&lt;li&gt;Set the region to the one the server expects. Garage uses &lt;code&gt;garage&lt;/code&gt; unless you change &lt;code&gt;s3_region&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Point the endpoint at the S3 API port (3900 for Garage) with no bucket in the URL.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;AccessDenied&lt;/code&gt; means the bucket exists but your key can't use it. &lt;code&gt;NoSuchBucket&lt;/code&gt; means the server found no bucket under that name for that key.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Fix NoSuchBucket: find the bucket name the server saw
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Read the name in the error
&lt;/h3&gt;

&lt;p&gt;The lab created one bucket, &lt;code&gt;photos&lt;/code&gt;, and one access key with read and write rights on it. Then it ran &lt;code&gt;aws&lt;/code&gt; 2.39.3 in a container on the same Compose network with &lt;code&gt;AWS_ENDPOINT_URL=http://garage:3900&lt;/code&gt; and &lt;code&gt;AWS_DEFAULT_REGION=garage&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fs3-nosuchbucket-bucket-not-found-docker%2F02-s3-nosuchbucket-bucket-not-found-garage.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fs3-nosuchbucket-bucket-not-found-docker%2F02-s3-nosuchbucket-bucket-not-found-garage.webp" alt="Terminal capture: garage bucket list shows photos and invoices, aws s3 ls s3://Photos and a copy to s3://pictures fail with NoSuchBucket Bucket not found, head-bucket returns 404 Not Found, the raw XML error body names the bucket, and aws s3 ls s3://photos lists cat.txt" width="800" height="265"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bucket names are case sensitive. &lt;code&gt;Photos&lt;/code&gt; is not &lt;code&gt;photos&lt;/code&gt;, so Garage answered &lt;code&gt;NoSuchBucket&lt;/code&gt; with &lt;code&gt;Bucket not found: Photos&lt;/code&gt;. The raw XML body carries the same name in &lt;code&gt;&amp;lt;Message&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;Resource&amp;gt;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Error&amp;gt;&amp;lt;Code&amp;gt;&lt;/span&gt;NoSuchBucket&lt;span class="nt"&gt;&amp;lt;/Code&amp;gt;&amp;lt;Message&amp;gt;&lt;/span&gt;Bucket not found: pictures&lt;span class="nt"&gt;&amp;lt;/Message&amp;gt;&amp;lt;Resource&amp;gt;&lt;/span&gt;/pictures&lt;span class="nt"&gt;&amp;lt;/Resource&amp;gt;&amp;lt;Region&amp;gt;&lt;/span&gt;garage&lt;span class="nt"&gt;&amp;lt;/Region&amp;gt;&amp;lt;/Error&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Garage log shows the same thing with the method and path, for example &lt;code&gt;error 404 Not Found, Bucket not found: pictures ... HEAD /pictures&lt;/code&gt;. Run &lt;code&gt;docker compose logs garage&lt;/code&gt; and compare the path with the bucket you meant.&lt;/p&gt;

&lt;p&gt;Other servers word it differently. RustFS 1.0.4 answered a missing bucket with &lt;code&gt;NoSuchBucket: The specified bucket does not exist&lt;/code&gt;, the same text MinIO used, and rejected &lt;code&gt;Photos&lt;/code&gt; with &lt;code&gt;InvalidBucketName: The specified bucket is not valid.&lt;/code&gt; Supabase Storage wraps the error in JSON, &lt;code&gt;{"statusCode":"404","error":"Bucket not found","message":"Bucket not found","code":"NoSuchBucket"}&lt;/code&gt;, which the &lt;a href="https://deploylark.com/posts/supabase-self-host-docker/" rel="noopener noreferrer"&gt;Supabase self-hosting guide&lt;/a&gt; ran into before the bucket was created.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check the bucket with head-bucket
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;aws s3api head-bucket --bucket pictures&lt;/code&gt; returned &lt;code&gt;An error occurred (404) when calling the HeadBucket operation: Not Found&lt;/code&gt; and exit code 254. A HEAD response has no body, so you get a bare 404 instead of &lt;code&gt;NoSuchBucket&lt;/code&gt;. That makes it a quick existence check in a script. On Garage, &lt;code&gt;docker compose exec garage /garage bucket list&lt;/code&gt; shows every bucket name, called a global alias, and every per-key name, called a local alias. More on those below.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix Bucket not found caused by virtual-hosted URLs
&lt;/h2&gt;

&lt;p&gt;Start with how the request is built. S3 clients can address a bucket in two ways, and the &lt;a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html" rel="noopener noreferrer"&gt;AWS docs on virtual hosting&lt;/a&gt; describe both. Path-style puts the bucket in the path. Virtual-hosted style puts it in the hostname.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fs3-nosuchbucket-bucket-not-found-docker%2F01-s3-path-style-vs-virtual-hosted-diagram.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fs3-nosuchbucket-bucket-not-found-docker%2F01-s3-path-style-vs-virtual-hosted-diagram.webp" alt="Diagram: the same GetObject call for bucket photos and key cat.txt. With forcePathStyle true the request goes to http://garage:3900/photos/cat.txt and works. With the SDK default it goes to http://photos.garage:3900/cat.txt and fails with getaddrinfo ENOTFOUND photos.garage, or with NoSuchBucket Bucket not found cat.txt when that name resolves to Garage" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Set forcePathStyle for self-hosted endpoints
&lt;/h3&gt;

&lt;p&gt;The lab used a small Node.js script with &lt;code&gt;@aws-sdk/client-s3&lt;/code&gt; 3.1161.0 on &lt;code&gt;node:24.22.2-alpine&lt;/code&gt;. It set &lt;code&gt;endpoint: "http://garage:3900"&lt;/code&gt; and &lt;code&gt;region: "garage"&lt;/code&gt; and nothing else. The SDK built &lt;code&gt;http://photos.garage:3900/cat.txt&lt;/code&gt;. No DNS record exists for &lt;code&gt;photos.garage&lt;/code&gt;, so the call failed before it reached Garage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Error: getaddrinfo ENOTFOUND photos.garage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The box this ran on resolves unknown names to a proxy, so the lab pointed the client at a &lt;a href="https://coredns.io/" rel="noopener noreferrer"&gt;CoreDNS&lt;/a&gt; 1.15.0 container that answers "no such name" for everything, the way a normal network does. With &lt;code&gt;forcePathStyle: true&lt;/code&gt;, the same script fetched &lt;code&gt;http://garage:3900/photos/cat.txt&lt;/code&gt; and printed &lt;code&gt;OK hello garage&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The worse case is when &lt;code&gt;photos.garage&lt;/code&gt; does resolve, for example through a wildcard DNS record or a proxy that sends every subdomain to Garage. The lab simulated that with a Compose network alias. Garage did not recognize the hostname, so it treated the request as path-style and read the object key as the bucket name:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fs3-nosuchbucket-bucket-not-found-docker%2F03-s3-forcepathstyle-enotfound-nosuchbucket.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fs3-nosuchbucket-bucket-not-found-docker%2F03-s3-forcepathstyle-enotfound-nosuchbucket.webp" alt="Terminal capture: the AWS SDK for JavaScript v3 with its default URL style fails with getaddrinfo ENOTFOUND photos.garage, then with NoSuchBucket Bucket not found cat.txt once the name resolves, the Garage log shows GET /cat.txt, and forcePathStyle true or a matching root_domain endpoint works" width="800" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;NoSuchBucket: Bucket not found: cat.txt&lt;/code&gt; is the giveaway. &lt;code&gt;cat.txt&lt;/code&gt; is a file, not a bucket. An upload failed in a stranger way. Garage read &lt;code&gt;PUT /from-node.txt&lt;/code&gt; as a request to create a bucket called &lt;code&gt;from-node.txt&lt;/code&gt; and returned &lt;code&gt;InvalidRequest: Bad request: Invalid create bucket XML query&lt;/code&gt;. Here is the client config that fixed both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;S3Client&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@aws-sdk/client-s3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;s3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;S3Client&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;endpoint&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://garage:3900&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;region&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;garage&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;forcePathStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// http://garage:3900/&amp;lt;bucket&amp;gt;/&amp;lt;key&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Other SDKs have their own path-style option, so check their docs. The AWS CLI 2.39.3 in the lab used path-style on its own with a custom endpoint. Its debug log showed &lt;code&gt;http://garage:3900/photos?list-type=2...&lt;/code&gt;. That is why the CLI worked while the Node app failed with the same keys.&lt;/p&gt;

&lt;h3&gt;
  
  
  Or configure root_domain and wildcard DNS
&lt;/h3&gt;

&lt;p&gt;If an app can't do path-style, give Garage a domain for virtual-hosted requests. The lab config had &lt;code&gt;root_domain = ".s3.garage.localhost"&lt;/code&gt; under &lt;code&gt;[s3_api]&lt;/code&gt;. With the endpoint set to &lt;code&gt;http://s3.garage.localhost:3900&lt;/code&gt; and a network alias for &lt;code&gt;photos.s3.garage.localhost&lt;/code&gt;, the script worked without &lt;code&gt;forcePathStyle&lt;/code&gt;. In production that alias is a wildcard DNS record such as &lt;code&gt;*.s3.example.com&lt;/code&gt;, plus a wildcard TLS certificate if you serve HTTPS. The &lt;a href="https://garagehq.deuxfleurs.fr/documentation/reference-manual/configuration/" rel="noopener noreferrer"&gt;Garage configuration reference&lt;/a&gt; notes: "Note path-style requests are always enabled, whether or not vhost-style is configured." So path-style keeps working after you add the domain. Garage picks the bucket from the &lt;code&gt;Host&lt;/code&gt; header, so a reverse proxy in front has to pass that header through unchanged. The &lt;a href="https://deploylark.com/posts/traefik-docker-labels-404-502/" rel="noopener noreferrer"&gt;Traefik labels guide&lt;/a&gt; covers routing by host name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix the region and the endpoint URL
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Match the region to s3_region
&lt;/h3&gt;

&lt;p&gt;Garage only accepts requests signed for its own region. The Garage configuration reference says: "Garage will accept S3 API calls that are targeted to the S3 region defined here. API calls targeted to other regions will fail with a AuthorizationHeaderMalformed error message that redirects the client to the correct region."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fs3-nosuchbucket-bucket-not-found-docker%2F04-s3-wrong-region-endpoint-authorizationheadermalformed.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fs3-nosuchbucket-bucket-not-found-docker%2F04-s3-wrong-region-endpoint-authorizationheadermalformed.webp" alt="Terminal capture: the SDK with region us-east-1 fails with AuthorizationHeaderMalformed unexpected scope, the AWS CLI debug log shows a us-east-1 request followed by a retry signed for garage, port 3902 returns 404, port 3903 returns 400, and an endpoint that includes the bucket makes Garage look up /photos/photos/cat.txt" width="800" height="265"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;region: "us-east-1"&lt;/code&gt;, the Node SDK failed with &lt;code&gt;AuthorizationHeaderMalformed: Authorization header malformed, unexpected scope: '20261102/us-east-1/s3/aws4_request', expected: '20261102/garage/s3/aws4_request'&lt;/code&gt;. The AWS CLI hid the same mistake. With &lt;code&gt;--region us-east-1&lt;/code&gt; it listed the bucket fine, and its debug log showed why: one request signed for &lt;code&gt;us-east-1&lt;/code&gt;, the &lt;code&gt;AuthorizationHeaderMalformed&lt;/code&gt; answer, then a retry signed for &lt;code&gt;garage&lt;/code&gt;. So set the region in your app to the &lt;code&gt;s3_region&lt;/code&gt; value from &lt;code&gt;garage.toml&lt;/code&gt;, which is &lt;code&gt;garage&lt;/code&gt; in the Garage quick start. RustFS accepted &lt;code&gt;us-east-1&lt;/code&gt; in the lab.&lt;/p&gt;

&lt;h3&gt;
  
  
  Point the endpoint at the S3 port, without the bucket
&lt;/h3&gt;

&lt;p&gt;Garage listens on several ports. Only &lt;code&gt;3900&lt;/code&gt; speaks the S3 API. The lab tried the others:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;http://garage:3902&lt;/code&gt; (the static website port) returned &lt;code&gt;(404) ... Not Found&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;http://garage:3903&lt;/code&gt; (the admin API) returned &lt;code&gt;(400) ... Bad Request&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;http://garage:3900/photos&lt;/code&gt;, with the bucket inside the endpoint, made the client add the bucket a second time. Garage looked up &lt;code&gt;/photos/photos/cat.txt&lt;/code&gt;, and &lt;code&gt;aws s3 ls&lt;/code&gt; failed with &lt;code&gt;NoSuchKey: Key not found&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A wrong secret key returned &lt;code&gt;AccessDenied: Forbidden: Invalid signature&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The endpoint is the server address only, such as &lt;code&gt;http://garage:3900&lt;/code&gt;. The bucket goes in the bucket parameter. From another container, use the Compose service name. From the host, use the published port. In the lab, &lt;code&gt;--endpoint-url http://127.0.0.1:3900&lt;/code&gt; listed the bucket from the host. &lt;code&gt;localhost&lt;/code&gt; inside a container is that container, the same trap the &lt;a href="https://deploylark.com/posts/redis-docker-connection-refused-noauth-misconf/" rel="noopener noreferrer"&gt;Redis in Docker guide&lt;/a&gt; shows. And if you put a TLS proxy in front, match &lt;code&gt;http&lt;/code&gt; and &lt;code&gt;https&lt;/code&gt; with what the proxy serves. The &lt;a href="https://deploylark.com/posts/docker-registry-http-response-https-client/" rel="noopener noreferrer"&gt;private registry guide&lt;/a&gt; shows what a scheme mismatch looks like.&lt;/p&gt;

&lt;h2&gt;
  
  
  AccessDenied or NoSuchBucket: check the access key
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Grant the key access to the bucket
&lt;/h3&gt;

&lt;p&gt;In Garage, a key sees nothing until you run &lt;code&gt;garage bucket allow&lt;/code&gt; for it. The lab created &lt;code&gt;report-key&lt;/code&gt; with read access to &lt;code&gt;photos&lt;/code&gt; only:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fs3-nosuchbucket-bucket-not-found-docker%2F05-garage-accessdenied-vs-nosuchbucket-keys.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fs3-nosuchbucket-bucket-not-found-docker%2F05-garage-accessdenied-vs-nosuchbucket-keys.webp" alt="Terminal capture: report-key lists only photos, gets AccessDenied on invoices, NoSuchBucket on a missing bucket and on another key's local alias media, AccessDenied when creating a bucket, and RustFS returns The specified bucket does not exist and InvalidBucketName for the same mistakes" width="800" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The difference matters. &lt;code&gt;invoices&lt;/code&gt; exists, so the key got &lt;code&gt;AccessDenied: Forbidden: Operation is not allowed for this key.&lt;/code&gt; A bucket that didn't exist got &lt;code&gt;NoSuchBucket&lt;/code&gt;. &lt;code&gt;aws s3 ls&lt;/code&gt; only listed &lt;code&gt;photos&lt;/code&gt;, the one bucket the key could use. Creating a bucket failed with &lt;code&gt;AccessDenied: Forbidden: Access key GK8a04... is not allowed to create buckets&lt;/code&gt;, because the key had no create right. Grant access with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose &lt;span class="nb"&gt;exec &lt;/span&gt;garage /garage bucket allow &lt;span class="nt"&gt;--read&lt;/span&gt; &lt;span class="nt"&gt;--write&lt;/span&gt; photos &lt;span class="nt"&gt;--key&lt;/span&gt; report-key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Garage quick start also adds &lt;code&gt;--owner&lt;/code&gt;. Run &lt;code&gt;garage key info &amp;lt;key&amp;gt;&lt;/code&gt; to see which buckets a key can use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Local aliases are per key
&lt;/h3&gt;

&lt;p&gt;Garage lets a key give a bucket its own private name, a local alias. The lab ran &lt;code&gt;garage bucket alias --local app-key photos media&lt;/code&gt;. For &lt;code&gt;app-key&lt;/code&gt;, &lt;code&gt;s3://media&lt;/code&gt; listed the files in &lt;code&gt;photos&lt;/code&gt;. For &lt;code&gt;report-key&lt;/code&gt;, which had read access to the same bucket, &lt;code&gt;s3://media&lt;/code&gt; returned &lt;code&gt;NoSuchBucket: Bucket not found: media&lt;/code&gt;. If two services share a bucket, use the global alias, or create the same local alias for each key.&lt;/p&gt;

&lt;h2&gt;
  
  
  MinIO images are gone: a Garage Compose file that works
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What happened to minio/minio
&lt;/h3&gt;

&lt;p&gt;Many older tutorials pin &lt;code&gt;minio/minio&lt;/code&gt;. On November 2, 2026, the Docker Hub API answered &lt;code&gt;object not found&lt;/code&gt; for that repository. &lt;code&gt;docker pull minio/minio:RELEASE.2025-09-07T16-13-09Z&lt;/code&gt; failed with &lt;code&gt;pull access denied for minio/minio, repository does not exist&lt;/code&gt;, and the same tag on &lt;code&gt;quay.io/minio/minio&lt;/code&gt; returned &lt;code&gt;unauthorized: access to the requested resource is not authorized&lt;/code&gt;. The &lt;a href="https://github.com/minio/minio" rel="noopener noreferrer"&gt;MinIO GitHub repository&lt;/a&gt; was archived on April 25, 2026, and its README says "The MinIO community edition is now distributed as source code only." &lt;a href="https://www.stablebuild.com/blog/minio-images-disappeared-from-docker-hub" rel="noopener noreferrer"&gt;StableBuild's write-up&lt;/a&gt; dates the Docker Hub removal to around September 12, 2026.&lt;/p&gt;

&lt;p&gt;So a fresh &lt;code&gt;docker compose up&lt;/code&gt; with a MinIO image now fails before any bucket error can happen. The lab moved to Garage. RustFS 1.0.4 also ran fine, and its error messages are closer to MinIO's. The fixes above apply to both, since they come from the S3 protocol and the client.&lt;/p&gt;

&lt;h3&gt;
  
  
  compose.yaml, garage.toml and the app
&lt;/h3&gt;

&lt;p&gt;This setup ran verbatim in the lab. It uses the &lt;code&gt;--single-node&lt;/code&gt; and &lt;code&gt;--default-bucket&lt;/code&gt; flags from the &lt;a href="https://garagehq.deuxfleurs.fr/documentation/quick-start/" rel="noopener noreferrer"&gt;Garage quick start&lt;/a&gt;, which create the layout, one access key and one bucket from environment variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="n"&gt;metadata_dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"/var/lib/garage/meta"&lt;/span&gt;
&lt;span class="n"&gt;data_dir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"/var/lib/garage/data"&lt;/span&gt;
&lt;span class="n"&gt;db_engine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"sqlite"&lt;/span&gt;
&lt;span class="n"&gt;replication_factor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="n"&gt;rpc_bind_addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"[::]:3901"&lt;/span&gt;
&lt;span class="n"&gt;rpc_public_addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"127.0.0.1:3901"&lt;/span&gt;

&lt;span class="k"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s3_api&lt;/span&gt;&lt;span class="k"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;s3_region&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"garage"&lt;/span&gt;
&lt;span class="n"&gt;api_bind_addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"[::]:3900"&lt;/span&gt;

&lt;span class="k"&gt;[&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="k"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;api_bind_addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;"[::]:3903"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;garage&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dxflrs/garage:v2.5.0&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/garage"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;server"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--single-node"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--default-bucket"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;GARAGE_RPC_SECRET&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${GARAGE_RPC_SECRET}&lt;/span&gt;
      &lt;span class="na"&gt;GARAGE_ADMIN_TOKEN&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${GARAGE_ADMIN_TOKEN}&lt;/span&gt;
      &lt;span class="na"&gt;GARAGE_DEFAULT_ACCESS_KEY&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${S3_ACCESS_KEY}&lt;/span&gt;
      &lt;span class="na"&gt;GARAGE_DEFAULT_SECRET_KEY&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${S3_SECRET_KEY}&lt;/span&gt;
      &lt;span class="na"&gt;GARAGE_DEFAULT_BUCKET&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${S3_BUCKET}&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./garage.toml:/etc/garage.toml:ro&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;garage-meta:/var/lib/garage/meta&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;garage-data:/var/lib/garage/data&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1:3900:3900"&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/garage"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;status"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;10s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;

  &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;node:24.22.2-alpine&lt;/span&gt;
    &lt;span class="na"&gt;working_dir&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/app&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;node"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;app.mjs"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;garage&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;service_healthy&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;S3_ENDPOINT&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;http://garage:3900&lt;/span&gt;
      &lt;span class="na"&gt;S3_REGION&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;garage&lt;/span&gt;
      &lt;span class="na"&gt;S3_BUCKET&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${S3_BUCKET}&lt;/span&gt;
      &lt;span class="na"&gt;AWS_ACCESS_KEY_ID&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${S3_ACCESS_KEY}&lt;/span&gt;
      &lt;span class="na"&gt;AWS_SECRET_ACCESS_KEY&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${S3_SECRET_KEY}&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./app:/app:ro&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;garage-meta&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;garage-data&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fill &lt;code&gt;.env&lt;/code&gt; with your own random values. The access key ID starts with &lt;code&gt;GK&lt;/code&gt;, as in the quick start:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cat&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; .env &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;EOF&lt;/span&gt;&lt;span class="sh"&gt;
GARAGE_RPC_SECRET=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;openssl rand &lt;span class="nt"&gt;-hex&lt;/span&gt; 32&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;
GARAGE_ADMIN_TOKEN=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;openssl rand &lt;span class="nt"&gt;-base64&lt;/span&gt; 32&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;
S3_ACCESS_KEY=GK&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;openssl rand &lt;span class="nt"&gt;-hex&lt;/span&gt; 16&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;
S3_SECRET_KEY=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;openssl rand &lt;span class="nt"&gt;-hex&lt;/span&gt; 32&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="sh"&gt;
S3_BUCKET=uploads
&lt;/span&gt;&lt;span class="no"&gt;EOF
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;./app/app.mjs&lt;/code&gt; stands in for your real service. It needs &lt;code&gt;npm install @aws-sdk/client-s3@3.1161.0&lt;/code&gt; in &lt;code&gt;./app&lt;/code&gt; first:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;S3Client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PutObjectCommand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ListObjectsV2Command&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@aws-sdk/client-s3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;s3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;S3Client&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;endpoint&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;S3_ENDPOINT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;// http://garage:3900&lt;/span&gt;
  &lt;span class="na"&gt;region&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;S3_REGION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;       &lt;span class="c1"&gt;// must match s3_region in garage.toml&lt;/span&gt;
  &lt;span class="na"&gt;forcePathStyle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                &lt;span class="c1"&gt;// http://garage:3900/&amp;lt;bucket&amp;gt;/&amp;lt;key&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Bucket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;S3_BUCKET&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;s3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PutObjectCommand&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Contents&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;s3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ListObjectsV2Command&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;Bucket&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;Contents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;o&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Key&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;, &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Check it
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;code&amp;gt;text&lt;br&gt;
$ docker compose up -d&lt;br&gt;
 Container s3final-garage-1  Healthy&lt;br&gt;
 Container s3final-app-1  Started&lt;br&gt;
$ docker compose ps -a --format '&amp;amp;#123;&amp;amp;#123;.Service&amp;amp;#125;&amp;amp;#125; &amp;amp;#123;&amp;amp;#123;.Status&amp;amp;#125;&amp;amp;#125;'&lt;br&gt;
app Exited (0) 1 second ago&lt;br&gt;
garage Up 12 seconds (healthy)&lt;br&gt;
$ docker compose logs --no-log-prefix app&lt;br&gt;
uploads: hello.txt&lt;br&gt;
$ docker compose restart garage&lt;br&gt;
$ docker compose run --rm app&lt;br&gt;
uploads: hello.txt&lt;br&gt;
&amp;lt;/code&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;On the first start, Garage logged &lt;code&gt;Created initial layout for single-node configuration&lt;/code&gt;, &lt;code&gt;Creating default access key&lt;/code&gt; and &lt;code&gt;Creating default bucket uploads&lt;/code&gt;. The SDK reads the key from &lt;code&gt;AWS_ACCESS_KEY_ID&lt;/code&gt; and &lt;code&gt;AWS_SECRET_ACCESS_KEY&lt;/code&gt; on its own. Changing &lt;code&gt;S3_BUCKET&lt;/code&gt; later behaves differently from database images. With &lt;code&gt;S3_BUCKET=assets&lt;/code&gt;, Garage created a second bucket, &lt;code&gt;assets&lt;/code&gt;, on the next start and kept &lt;code&gt;uploads&lt;/code&gt; with its files. The default key kept its rights on &lt;code&gt;uploads&lt;/code&gt;, and the app still read it after switching back. Keep the image pinned, here &lt;code&gt;dxflrs/garage:v2.5.0&lt;/code&gt;, so a restart never pulls a new release onto your data.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Errors
&lt;/h3&gt;

&lt;h4&gt;
  
  
  What does NoSuchBucket mean?
&lt;/h4&gt;

&lt;p&gt;The server found no bucket with the name in the request, for the key you used. Check the name in the error message first. If it shows a file name or a word you never used as a bucket, fix the URL style or the endpoint. If it shows your bucket, check the spelling, the case, and whether the bucket was created on this server.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why does the AWS CLI work but my app gets NoSuchBucket or ENOTFOUND?
&lt;/h4&gt;

&lt;p&gt;The AWS CLI 2.39.3 used path-style URLs with a custom endpoint, and it retried a wrong region on its own. The AWS SDK for JavaScript v3 did neither. Set &lt;code&gt;forcePathStyle: true&lt;/code&gt; and the server's region in the app.&lt;/p&gt;

&lt;h4&gt;
  
  
  What does "The authorization header is malformed" mean with self-hosted S3?
&lt;/h4&gt;

&lt;p&gt;The client signed the request for a different region than the server uses. Garage answered &lt;code&gt;AuthorizationHeaderMalformed&lt;/code&gt; with the expected scope in the message. Set the client region to the server's &lt;code&gt;s3_region&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup
&lt;/h3&gt;

&lt;h4&gt;
  
  
  How do I check if a bucket exists?
&lt;/h4&gt;

&lt;p&gt;Run &lt;code&gt;aws s3api head-bucket --bucket &amp;lt;name&amp;gt;&lt;/code&gt;. In the lab, exit code 0 meant the bucket existed and the key could use it. A missing bucket returned &lt;code&gt;(404) ... Not Found&lt;/code&gt; and a bucket the key had no rights on returned &lt;code&gt;(403) ... Forbidden&lt;/code&gt;, both with exit code 254.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I connect to S3 storage inside and outside the Docker network?
&lt;/h4&gt;

&lt;p&gt;Inside the Compose network, use the service name, such as &lt;code&gt;http://garage:3900&lt;/code&gt;. From the host, publish the port and use &lt;code&gt;http://127.0.0.1:3900&lt;/code&gt;. Use path-style in both cases, so the bucket never ends up in a hostname.&lt;/p&gt;

&lt;h4&gt;
  
  
  Can I still pull the MinIO Docker image?
&lt;/h4&gt;

&lt;p&gt;Not from Docker Hub. On November 2, 2026, &lt;code&gt;minio/minio&lt;/code&gt; no longer existed there, and the Quay copy returned &lt;code&gt;unauthorized&lt;/code&gt;. The community edition is source only. Use a maintained server such as Garage or RustFS, or build MinIO from source at your own risk.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>aws</category>
      <category>selfhosted</category>
      <category>devops</category>
    </item>
    <item>
      <title>Keycloak Behind a Reverse Proxy: Fix HTTPS Required</title>
      <dc:creator>DeployLark</dc:creator>
      <pubDate>Thu, 05 Nov 2026 23:28:48 +0000</pubDate>
      <link>https://dev.to/deploylark/keycloak-behind-a-reverse-proxy-fix-https-required-4fdg</link>
      <guid>https://dev.to/deploylark/keycloak-behind-a-reverse-proxy-fix-https-required-4fdg</guid>
      <description>&lt;p&gt;You open Keycloak in the browser and get a grey page that says &lt;code&gt;We are sorry... HTTPS required&lt;/code&gt;. Or you put it behind nginx, and the admin console stays blank, or the login page says &lt;code&gt;Invalid parameter: redirect_uri&lt;/code&gt;. Your app then rejects every token with &lt;code&gt;Invalid token issuer&lt;/code&gt;. All of these come from the same question: what URL does Keycloak think it lives at?&lt;/p&gt;

&lt;p&gt;This guide reproduces each error with &lt;code&gt;quay.io/keycloak/keycloak:26.8.1&lt;/code&gt;, &lt;code&gt;nginx:1.31.8&lt;/code&gt; and &lt;code&gt;postgres:18.6&lt;/code&gt; in Docker Compose and fixes it. Every error message below comes from that lab run on November 4, 2026. The lab used the name &lt;code&gt;auth.example.test&lt;/code&gt; with a self-signed certificate in place of your real domain.&lt;/p&gt;

&lt;p&gt;These errors are common. On Stack Overflow, &lt;a href="https://stackoverflow.com/questions/45352880/keycloak-invalid-parameter-redirect-uri" rel="noopener noreferrer"&gt;keycloak Invalid parameter: redirect_uri&lt;/a&gt; had 505,136 views on November 4, 2026, and &lt;a href="https://stackoverflow.com/questions/30622599/https-required-while-logging-in-to-keycloak-as-admin" rel="noopener noreferrer"&gt;"HTTPS required" while logging in to Keycloak as admin&lt;/a&gt; had 136,879.&lt;/p&gt;

&lt;p&gt;The short version:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;HTTPS required&lt;/code&gt; means a client with a public IP address reached Keycloak over plain HTTP. Put a TLS proxy in front. Don't switch the realm to &lt;code&gt;Require SSL: None&lt;/code&gt; outside a lab.&lt;/li&gt;
&lt;li&gt;Behind a proxy that terminates TLS, set &lt;code&gt;KC_PROXY_HEADERS=xforwarded&lt;/code&gt; and make the proxy send &lt;code&gt;X-Forwarded-Proto&lt;/code&gt;, &lt;code&gt;X-Forwarded-Host&lt;/code&gt; and &lt;code&gt;X-Forwarded-Port&lt;/code&gt;. Without both, Keycloak builds &lt;code&gt;http://&lt;/code&gt; URLs, and you get a blank admin console and &lt;code&gt;Invalid parameter: redirect_uri&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;KC_HOSTNAME&lt;/code&gt; to the full public URL, such as &lt;code&gt;https://auth.example.test&lt;/code&gt;. It fixes the token issuer, so apps that talk to &lt;code&gt;http://keycloak:8080&lt;/code&gt; stop failing with &lt;code&gt;Invalid token issuer&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For your own apps, the redirect URI must match the client's Valid redirect URIs exactly, including &lt;code&gt;https&lt;/code&gt;, the host and the port.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Fix Keycloak startup errors in production mode
&lt;/h2&gt;

&lt;p&gt;Most Compose examples on the internet use &lt;code&gt;start-dev&lt;/code&gt;. When you switch to &lt;code&gt;start&lt;/code&gt;, the production mode, the container exits before it serves a single page.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key material not provided to setup HTTPS
&lt;/h3&gt;

&lt;p&gt;With &lt;code&gt;command: start&lt;/code&gt; and only the admin variables set, the container stopped with exit code 2 and this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Key material not provided to setup HTTPS. Please configure your keys/certificates, or if HTTPS access is not needed see the `http-enabled` option. If you meant to start the server in development mode, see the `start-dev` command.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Production mode expects its own TLS certificate. When a reverse proxy handles TLS, as in this guide, set &lt;code&gt;KC_HTTP_ENABLED: "true"&lt;/code&gt; so Keycloak listens on plain HTTP port 8080 inside the Docker network. The &lt;a href="https://www.keycloak.org/server/reverseproxy#_configure_the_reverse_proxy_headers" rel="noopener noreferrer"&gt;reverse proxy guide&lt;/a&gt; requires it for this setup: "If the TLS connection is terminated at the reverse proxy (edge termination), enabling HTTP through the &lt;code&gt;http-enabled&lt;/code&gt; setting is required."&lt;/p&gt;

&lt;h3&gt;
  
  
  hostname is not configured
&lt;/h3&gt;

&lt;p&gt;With HTTP enabled, the next start failed with exit code 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERROR: hostname is not configured; either configure hostname, or set hostname-strict to false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The quick fix you see in many answers is &lt;code&gt;KC_HOSTNAME_STRICT: "false"&lt;/code&gt;. It works, but it tells Keycloak to build every URL from the request, which causes the next two problems. Set &lt;code&gt;KC_HOSTNAME&lt;/code&gt; instead, as shown in the last section. Keycloak also logged a warning that "the server is running in an insecure context" while HTTPS was off, &lt;code&gt;proxy-headers&lt;/code&gt; was unset and &lt;code&gt;hostname-strict&lt;/code&gt; was false.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix "HTTPS required" in Keycloak
&lt;/h2&gt;

&lt;p&gt;This is the error on the grey Keycloak page. In the lab it came from the master realm login page, where the admin console sends you to sign in.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fkeycloak-reverse-proxy-https-required%2F02-keycloak-https-required-error-page.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fkeycloak-reverse-proxy-https-required%2F02-keycloak-https-required-error-page.webp" alt="Keycloak error page in Chrome with the Keycloak logo and a white box that says We are sorry... HTTPS required, shown when the admin console is opened over plain HTTP from a public IP address" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Keycloak asks for HTTPS
&lt;/h3&gt;

&lt;p&gt;Every realm has a Require SSL setting. Its default, &lt;code&gt;external&lt;/code&gt;, allows plain HTTP only from private addresses. The &lt;a href="https://www.keycloak.org/docs/latest/server_admin/index.html#_ssl_modes" rel="noopener noreferrer"&gt;Keycloak server admin guide&lt;/a&gt; describes it: "If you try to access Keycloak without SSL from a non-private IP address, you will get an error."&lt;/p&gt;

&lt;p&gt;That explains why it works on your laptop and fails on a VPS. In the lab, Keycloak first ran on the default Compose network, &lt;code&gt;192.168.0.0/20&lt;/code&gt;, and the login page loaded over &lt;code&gt;http://127.0.0.1:8080&lt;/code&gt;. Then Keycloak moved to a Docker network in &lt;code&gt;203.0.113.0/24&lt;/code&gt;, a documentation range that counts as public, so it saw requests from &lt;code&gt;203.0.113.1&lt;/code&gt;. The same URL returned HTTP 400 with &lt;code&gt;HTTPS required&lt;/code&gt;, and the log named the reason:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type="LOGIN_ERROR", realmName="master", clientId="null", userId="null", ipAddress="203.0.113.1", error="ssl_required"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A VPS reached from the internet is in the same position. Your browser's address is public, so plain HTTP to port 8080 fails.&lt;/p&gt;

&lt;h3&gt;
  
  
  Serve Keycloak over HTTPS, not Require SSL: None
&lt;/h3&gt;

&lt;p&gt;The fix is HTTPS in front of Keycloak, which the rest of this guide sets up with nginx. If you already use &lt;a href="https://deploylark.com/posts/nginx-proxy-manager-docker-ssl/" rel="noopener noreferrer"&gt;Nginx Proxy Manager&lt;/a&gt; or &lt;a href="https://deploylark.com/posts/traefik-docker-labels-404-502/" rel="noopener noreferrer"&gt;Traefik&lt;/a&gt;, use that proxy and copy the header settings below.&lt;/p&gt;

&lt;p&gt;Many answers suggest switching the master realm to &lt;code&gt;Require SSL: None&lt;/code&gt;. It does work. From inside the container, where &lt;code&gt;localhost&lt;/code&gt; is a private address, &lt;code&gt;kcadm.sh&lt;/code&gt; changed the setting:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose &lt;span class="nb"&gt;exec &lt;/span&gt;keycloak /opt/keycloak/bin/kcadm.sh config credentials &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--server&lt;/span&gt; http://localhost:8080 &lt;span class="nt"&gt;--realm&lt;/span&gt; master &lt;span class="nt"&gt;--user&lt;/span&gt; admin &lt;span class="nt"&gt;--password&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;admin password&amp;gt;'&lt;/span&gt;
docker compose &lt;span class="nb"&gt;exec &lt;/span&gt;keycloak /opt/keycloak/bin/kcadm.sh update realms/master &lt;span class="nt"&gt;-s&lt;/span&gt; &lt;span class="nv"&gt;sslRequired&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;NONE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The login page loaded over plain HTTP from the public range right after. But the admin password and tokens then cross the internet unencrypted. The same guide says &lt;code&gt;None&lt;/code&gt; "applies only in development". The lab set it back with &lt;code&gt;-s sslRequired=EXTERNAL&lt;/code&gt;, and &lt;code&gt;HTTPS required&lt;/code&gt; returned.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix a blank admin console and Invalid parameter: redirect_uri behind nginx
&lt;/h2&gt;

&lt;p&gt;Once nginx terminates TLS and forwards plain HTTP to Keycloak, a new set of errors appears. Keycloak no longer sees HTTPS, so it generates &lt;code&gt;http://&lt;/code&gt; links for a site that you reach over &lt;code&gt;https://&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fkeycloak-reverse-proxy-https-required%2F01-keycloak-reverse-proxy-headers-diagram.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fkeycloak-reverse-proxy-https-required%2F01-keycloak-reverse-proxy-headers-diagram.webp" alt="Diagram: the browser connects over HTTPS port 443 to nginx, which ends TLS and adds the Host, X-Forwarded-Proto, X-Forwarded-Host, X-Forwarded-Port and X-Forwarded-For headers, then forwards plain HTTP to Keycloak on port 8080 with KC_PROXY_HEADERS and KC_HOSTNAME set. An app container uses the http://keycloak:8080 backchannel, and port 9000 serves health checks without being proxied" width="800" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens without the forwarded headers
&lt;/h3&gt;

&lt;p&gt;The first nginx config in the lab only passed &lt;code&gt;proxy_set_header Host $host&lt;/code&gt;, and Keycloak had &lt;code&gt;KC_HOSTNAME_STRICT=false&lt;/code&gt; with no proxy settings. Opening &lt;code&gt;https://auth.example.test/admin/&lt;/code&gt; returned:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Location: http://auth.example.test/admin/master/console/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The OpenID discovery document listed &lt;code&gt;http://auth.example.test/realms/master&lt;/code&gt; as the issuer. In Chrome, the admin console stayed blank with a spinner, and the browser console showed why:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Mixed Content: The page at 'https://auth.example.test/admin/master/console/' was loaded over HTTPS, but requested an insecure resource 'http://auth.example.test/resources/master/admin/en'. This request has been blocked; the content must be served over HTTPS.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The admin console's own sign-in failed too. A request with &lt;code&gt;redirect_uri=https://auth.example.test/admin/master/console/&lt;/code&gt; got HTTP 400 and &lt;code&gt;Invalid parameter: redirect_uri&lt;/code&gt;, because Keycloak expected the &lt;code&gt;http://&lt;/code&gt; version.&lt;/p&gt;

&lt;p&gt;Adding the headers to nginx alone changed nothing. With &lt;code&gt;X-Forwarded-Proto&lt;/code&gt; and the others set, the redirect still went to &lt;code&gt;http://&lt;/code&gt;, because Keycloak ignores those headers until you enable them. The reverse also failed: with &lt;code&gt;KC_PROXY_HEADERS=xforwarded&lt;/code&gt; but the old nginx config, the redirect still pointed to &lt;code&gt;http://&lt;/code&gt;. You need both.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fkeycloak-reverse-proxy-https-required%2F03-keycloak-proxy-headers-mixed-content-redirect-uri.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fkeycloak-reverse-proxy-https-required%2F03-keycloak-proxy-headers-mixed-content-redirect-uri.webp" alt="Terminal capture: without KC_PROXY_HEADERS, Keycloak behind nginx redirects to http://auth.example.test, reports an http issuer, rejects the admin console redirect_uri with HTTP 400 and Chrome blocks Mixed Content. After adding the X-Forwarded headers and KC_PROXY_HEADERS=xforwarded, the redirect, issuer and login form all use https" width="800" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Forward the headers and set KC_PROXY_HEADERS
&lt;/h3&gt;

&lt;p&gt;This nginx server block fixed it, together with &lt;code&gt;KC_PROXY_HEADERS: xforwarded&lt;/code&gt; on the Keycloak service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="k"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;server_name&lt;/span&gt; &lt;span class="s"&gt;auth.example.test&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;301&lt;/span&gt; &lt;span class="s"&gt;https://&lt;/span&gt;&lt;span class="nv"&gt;$host$request_uri&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;server&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kn"&gt;listen&lt;/span&gt; &lt;span class="mi"&gt;443&lt;/span&gt; &lt;span class="s"&gt;ssl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;server_name&lt;/span&gt; &lt;span class="s"&gt;auth.example.test&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;ssl_certificate&lt;/span&gt;     &lt;span class="n"&gt;/etc/nginx/certs/auth.crt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kn"&gt;ssl_certificate_key&lt;/span&gt; &lt;span class="n"&gt;/etc/nginx/certs/auth.key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kn"&gt;location&lt;/span&gt; &lt;span class="n"&gt;/&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kn"&gt;proxy_pass&lt;/span&gt; &lt;span class="s"&gt;http://keycloak:8080&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;Host&lt;/span&gt;              &lt;span class="nv"&gt;$host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;X-Forwarded-For&lt;/span&gt;   &lt;span class="nv"&gt;$proxy_add_x_forwarded_for&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;X-Forwarded-Proto&lt;/span&gt; &lt;span class="nv"&gt;$scheme&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;X-Forwarded-Host&lt;/span&gt;  &lt;span class="nv"&gt;$host&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kn"&gt;proxy_set_header&lt;/span&gt; &lt;span class="s"&gt;X-Forwarded-Port&lt;/span&gt;  &lt;span class="nv"&gt;$server_port&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the change, &lt;code&gt;/admin/&lt;/code&gt; redirected to &lt;code&gt;https://auth.example.test/admin/master/console/&lt;/code&gt;, the issuer started with &lt;code&gt;https://&lt;/code&gt;, and the admin console loaded with 0 console errors. &lt;code&gt;$server_port&lt;/code&gt; is the port nginx listens on inside its container. If you publish nginx on another host port, such as 8443, put that public port in &lt;code&gt;X-Forwarded-Port&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;Never publish Keycloak's port 8080 next to the proxy once headers are trusted. In the lab, a container on the same network sent &lt;code&gt;X-Forwarded-Host: evil.example&lt;/code&gt; straight to &lt;code&gt;keycloak:8080&lt;/code&gt;, and the discovery document answered with the issuer &lt;code&gt;https://evil.example/realms/master&lt;/code&gt;. The &lt;a href="https://www.keycloak.org/server/reverseproxy" rel="noopener noreferrer"&gt;reverse proxy guide&lt;/a&gt; warns that clients could "send forged forwarded headers directly to Keycloak" without network restrictions. The next section closes this hole for the host name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Invalid parameter: redirect_uri in your own apps
&lt;/h3&gt;

&lt;p&gt;The same error appears for your own clients when the redirect URI an app sends doesn't match the client's Valid redirect URIs. The lab client &lt;code&gt;app&lt;/code&gt; allowed &lt;code&gt;https://app.example.test/*&lt;/code&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;redirect_uri sent&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;https://app.example.test/callback&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;login form&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;https://app.example.test&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;login form&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;http://app.example.test/callback&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Invalid parameter: redirect_uri&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;https://app.example.test:8443/callback&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Invalid parameter: redirect_uri&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;https://www.app.example.test/callback&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;Invalid parameter: redirect_uri&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The scheme, the host and the port all have to match. The Keycloak log shows the URI that was rejected, which is the fastest way to see what your app sent:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type="LOGIN_ERROR", realmName="demo", clientId="app", ... error="invalid_redirect_uri", redirect_uri="https://www.app.example.test/callback"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An &lt;code&gt;http://&lt;/code&gt; URI usually means your app sits behind its own proxy and builds its callback URL from the plain HTTP request. Fix the app's proxy settings or set its public URL, the same way you fixed Keycloak. The &lt;a href="https://deploylark.com/posts/home-assistant-reverse-proxy-400-bad-request/" rel="noopener noreferrer"&gt;Home Assistant&lt;/a&gt; and &lt;a href="https://deploylark.com/posts/nextcloud-docker-reverse-proxy-untrusted-domain/" rel="noopener noreferrer"&gt;Nextcloud&lt;/a&gt; guides show this for two common apps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix "Invalid token issuer" with KC_HOSTNAME
&lt;/h2&gt;

&lt;p&gt;Even with the proxy headers fixed, &lt;code&gt;KC_HOSTNAME_STRICT=false&lt;/code&gt; makes Keycloak build every URL from the request, including the &lt;code&gt;iss&lt;/code&gt; claim in tokens. Your browser and your app's backend reach it at different addresses, so they disagree.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why the issuer changes
&lt;/h3&gt;

&lt;p&gt;In the lab, a user logged in through &lt;code&gt;https://auth.example.test&lt;/code&gt;, so the access token had &lt;code&gt;iss: https://auth.example.test/realms/demo&lt;/code&gt;. An app container then checked that token at &lt;code&gt;http://keycloak:8080&lt;/code&gt;, the address it uses inside the Docker network. The userinfo endpoint answered &lt;code&gt;401 Unauthorized&lt;/code&gt; with &lt;code&gt;error="invalid_token", error_description="Token verification failed"&lt;/code&gt;, and the Keycloak log gave the real reason:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;reason="Invalid token issuer. Expected 'http://keycloak:8080/realms/demo'"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same token worked through the public URL. Keycloak expected the issuer of the address that the request came in on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fkeycloak-reverse-proxy-https-required%2F04-keycloak-invalid-token-issuer-docker.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fkeycloak-reverse-proxy-https-required%2F04-keycloak-invalid-token-issuer-docker.webp" alt="Terminal capture: a token issued through https://auth.example.test is rejected at http://keycloak:8080 with 401 and the log reason Invalid token issuer. Expected http://keycloak:8080/realms/demo. With KC_HOSTNAME and KC_HOSTNAME_BACKCHANNEL_DYNAMIC set, the internal discovery document keeps the https issuer, uses internal token and certs endpoints, userinfo returns 200 and a forged X-Forwarded-Host no longer changes the issuer" width="800" height="307"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Set the full public URL
&lt;/h3&gt;

&lt;p&gt;Set &lt;code&gt;KC_HOSTNAME: https://auth.example.test&lt;/code&gt;, the full URL with the scheme. With that in place, the discovery document at &lt;code&gt;http://keycloak:8080&lt;/code&gt; listed &lt;code&gt;https://auth.example.test/realms/demo&lt;/code&gt; as the issuer, and the internal userinfo call returned HTTP 200. The forged &lt;code&gt;X-Forwarded-Host: evil.example&lt;/code&gt; request from the previous section now got the correct issuer back. The &lt;a href="https://www.keycloak.org/server/hostname" rel="noopener noreferrer"&gt;hostname guide&lt;/a&gt; gives this as the reason the option exists: "By explicitly setting the hostname option, we avoid a situation where tokens could be issued by a fraudulent issuer."&lt;/p&gt;

&lt;p&gt;One catch remains. With only &lt;code&gt;KC_HOSTNAME&lt;/code&gt; set, the internal discovery document pointed the token and certs endpoints at &lt;code&gt;https://auth.example.test&lt;/code&gt; too, so a backend inside Docker has to reach the public name. Add &lt;code&gt;KC_HOSTNAME_BACKCHANNEL_DYNAMIC: "true"&lt;/code&gt;. In the lab, the internal document then listed &lt;code&gt;http://keycloak:8080/realms/demo/protocol/openid-connect/token&lt;/code&gt; and &lt;code&gt;.../certs&lt;/code&gt;, kept the &lt;code&gt;https://&lt;/code&gt; issuer and the &lt;code&gt;https://&lt;/code&gt; login URL, and a token requested internally still carried &lt;code&gt;iss: https://auth.example.test/realms/demo&lt;/code&gt;. Browsers that use the public URL saw only &lt;code&gt;https://&lt;/code&gt; endpoints.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Keycloak Compose file with nginx that avoids these errors
&lt;/h2&gt;

&lt;p&gt;This file puts the fixes together. It ran verbatim in the lab with the nginx config from above saved as &lt;code&gt;nginx/keycloak.conf&lt;/code&gt;, and the certificate and key in &lt;code&gt;certs/&lt;/code&gt;. Use a real certificate for your domain, for example from your existing proxy.&lt;/p&gt;

&lt;h3&gt;
  
  
  The compose file
&lt;/h3&gt;

&lt;p&gt;Create &lt;code&gt;.env&lt;/code&gt; with two random passwords:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;printf&lt;/span&gt; &lt;span class="s1"&gt;'KC_DB_PASSWORD=%s\nKC_ADMIN_PASSWORD=%s\n'&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;openssl rand &lt;span class="nt"&gt;-hex&lt;/span&gt; 24&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;openssl rand &lt;span class="nt"&gt;-hex&lt;/span&gt; 16&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;postgres&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres:18.6&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;keycloak&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_DB&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;keycloak&lt;/span&gt;
      &lt;span class="na"&gt;POSTGRES_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${KC_DB_PASSWORD:?set KC_DB_PASSWORD in .env}&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;pgdata:/var/lib/postgresql&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;pg_isready"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-U"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;keycloak"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;-d"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;keycloak"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;3s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;

  &lt;span class="na"&gt;keycloak&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;quay.io/keycloak/keycloak:26.8.1&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;
    &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;start&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;KC_DB&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;postgres&lt;/span&gt;
      &lt;span class="na"&gt;KC_DB_URL&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;jdbc:postgresql://postgres:5432/keycloak&lt;/span&gt;
      &lt;span class="na"&gt;KC_DB_USERNAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;keycloak&lt;/span&gt;
      &lt;span class="na"&gt;KC_DB_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${KC_DB_PASSWORD:?set KC_DB_PASSWORD in .env}&lt;/span&gt;
      &lt;span class="na"&gt;KC_HOSTNAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://auth.example.test&lt;/span&gt;
      &lt;span class="na"&gt;KC_HOSTNAME_BACKCHANNEL_DYNAMIC&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;true"&lt;/span&gt;
      &lt;span class="na"&gt;KC_PROXY_HEADERS&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;xforwarded&lt;/span&gt;
      &lt;span class="na"&gt;KC_HTTP_ENABLED&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;true"&lt;/span&gt;
      &lt;span class="na"&gt;KC_HEALTH_ENABLED&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;true"&lt;/span&gt;
      &lt;span class="na"&gt;KC_BOOTSTRAP_ADMIN_USERNAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;admin&lt;/span&gt;
      &lt;span class="na"&gt;KC_BOOTSTRAP_ADMIN_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${KC_ADMIN_PASSWORD:?set KC_ADMIN_PASSWORD in .env}&lt;/span&gt;
    &lt;span class="na"&gt;healthcheck&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;CMD-SHELL"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;exec&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;3&amp;lt;&amp;gt;/dev/tcp/127.0.0.1/9000&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;printf&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;'GET&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;/health/ready&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;HTTP/1.1&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;nHost:&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;localhost&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;nConnection:&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;close&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;n&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;r&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;n'&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;gt;&amp;amp;3&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;grep&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;-q&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;UP&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;&amp;lt;&amp;amp;3"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
      &lt;span class="na"&gt;interval&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;10s&lt;/span&gt;
      &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5s&lt;/span&gt;
      &lt;span class="na"&gt;retries&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;12&lt;/span&gt;
      &lt;span class="na"&gt;start_period&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;30s&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;postgres&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;service_healthy&lt;/span&gt;

  &lt;span class="na"&gt;nginx&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;nginx:1.31.8&lt;/span&gt;
    &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;unless-stopped&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;80:80"&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;443:443"&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./nginx/keycloak.conf:/etc/nginx/conf.d/default.conf:ro&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./certs:/etc/nginx/certs:ro&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;keycloak&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;service_healthy&lt;/span&gt;

&lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;pgdata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keycloak has no published port, so only nginx can reach it. The Keycloak image has no &lt;code&gt;curl&lt;/code&gt;, so the healthcheck talks to the management port 9000 with bash's &lt;code&gt;/dev/tcp&lt;/code&gt;. Port 9000 stays off the proxy, as the reverse proxy guide recommends. If Postgres refuses the password on a rerun, the &lt;a href="https://deploylark.com/posts/postgres-docker-password-authentication-failed/" rel="noopener noreferrer"&gt;Postgres password guide&lt;/a&gt; explains why the variables only apply on the first start.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check that it works
&lt;/h3&gt;

&lt;p&gt;Run &lt;code&gt;docker compose up -d&lt;/code&gt;. Compose waited for each service in turn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; Container kcfinal-postgres-1  Healthy
 Container kcfinal-keycloak-1  Starting
 Container kcfinal-keycloak-1  Started
 Container kcfinal-keycloak-1  Waiting
 Container kcfinal-keycloak-1  Healthy
 Container kcfinal-nginx-1  Starting
 Container kcfinal-nginx-1  Started
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keycloak logged &lt;code&gt;Keycloak 26.8.1 on JVM (powered by Quarkus 3.40.1) started in 2.962s&lt;/code&gt;, with no &lt;code&gt;insecure context&lt;/code&gt; warning and no errors. &lt;code&gt;http://auth.example.test/admin/&lt;/code&gt; returned a 301 to &lt;code&gt;https://&lt;/code&gt;, &lt;code&gt;https://auth.example.test/admin/&lt;/code&gt; redirected to the &lt;code&gt;https://&lt;/code&gt; console, and the admin login through Chrome worked.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fkeycloak-reverse-proxy-https-required%2F05-keycloak-admin-console-behind-nginx.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fdeploylark.com%2Fimages%2Fkeycloak-reverse-proxy-https-required%2F05-keycloak-admin-console-behind-nginx.webp" alt="Keycloak 26.8.1 admin console loaded through nginx at https://auth.example.test, showing the master realm settings with Require SSL set to External requests and a banner about the temporary admin user" width="800" height="467"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The banner at the top is Keycloak's reminder that the bootstrap admin is temporary. Create a permanent admin account and delete the bootstrap one. For an internet-facing server, the reverse proxy guide also recommends that you don't expose &lt;code&gt;/admin/&lt;/code&gt; and &lt;code&gt;/realms/master/&lt;/code&gt; publicly, because "Exposed admin paths lead to an unnecessary attack vector." Your apps only need &lt;code&gt;/realms/&lt;/code&gt;, &lt;code&gt;/resources/&lt;/code&gt; and &lt;code&gt;/.well-known/&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Errors on the login page
&lt;/h3&gt;

&lt;h4&gt;
  
  
  How do I fix "HTTPS required" in Keycloak?
&lt;/h4&gt;

&lt;p&gt;Serve Keycloak over HTTPS, usually through a reverse proxy, and set &lt;code&gt;KC_PROXY_HEADERS&lt;/code&gt; and &lt;code&gt;KC_HOSTNAME&lt;/code&gt; so Keycloak knows the public URL is &lt;code&gt;https://&lt;/code&gt;. The error appears when a client with a public IP address uses plain HTTP while the realm's Require SSL is &lt;code&gt;external&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Should I set Require SSL to None in the master realm?
&lt;/h4&gt;

&lt;p&gt;Only in a throwaway lab. It removes the error, but logins and tokens then travel unencrypted. Keycloak's server admin guide limits &lt;code&gt;None&lt;/code&gt; to development. Fix HTTPS instead.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why does the Keycloak admin console load forever behind a reverse proxy?
&lt;/h4&gt;

&lt;p&gt;Keycloak builds &lt;code&gt;http://&lt;/code&gt; URLs because it doesn't know the proxy uses HTTPS, and the browser blocks them as Mixed Content. Send &lt;code&gt;X-Forwarded-Proto&lt;/code&gt;, &lt;code&gt;X-Forwarded-Host&lt;/code&gt; and &lt;code&gt;X-Forwarded-Port&lt;/code&gt; from the proxy and set &lt;code&gt;KC_PROXY_HEADERS=xforwarded&lt;/code&gt;. The browser console lists the blocked &lt;code&gt;http://&lt;/code&gt; request.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuration
&lt;/h3&gt;

&lt;h4&gt;
  
  
  What causes "Invalid parameter: redirect_uri" in Keycloak?
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;redirect_uri&lt;/code&gt; sent by the app doesn't match any Valid redirect URI of the client. Check the scheme, host and port. The Keycloak log shows the rejected value in the &lt;code&gt;invalid_redirect_uri&lt;/code&gt; event. For the admin console itself, it means Keycloak computed an &lt;code&gt;http://&lt;/code&gt; URL behind the proxy.&lt;/p&gt;

&lt;h4&gt;
  
  
  How do I fix "Invalid token issuer" when Keycloak runs in Docker?
&lt;/h4&gt;

&lt;p&gt;Set &lt;code&gt;KC_HOSTNAME&lt;/code&gt; to the full public URL so the issuer is the same no matter which address an app uses. Add &lt;code&gt;KC_HOSTNAME_BACKCHANNEL_DYNAMIC=true&lt;/code&gt; if your backend should call Keycloak at &lt;code&gt;http://keycloak:8080&lt;/code&gt; inside the Docker network.&lt;/p&gt;

&lt;h4&gt;
  
  
  Can Keycloak run in production mode with HTTP only?
&lt;/h4&gt;

&lt;p&gt;Yes, behind a proxy that handles HTTPS. Set &lt;code&gt;KC_HTTP_ENABLED=true&lt;/code&gt;, &lt;code&gt;KC_PROXY_HEADERS&lt;/code&gt; and &lt;code&gt;KC_HOSTNAME&lt;/code&gt;, and don't publish port 8080. Without &lt;code&gt;http-enabled&lt;/code&gt;, &lt;code&gt;start&lt;/code&gt; exits with &lt;code&gt;Key material not provided to setup HTTPS&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>selfhosted</category>
      <category>docker</category>
      <category>keycloak</category>
      <category>nginx</category>
    </item>
  </channel>
</rss>
