<?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: Gharelu Remedy</title>
    <description>The latest articles on DEV Community by Gharelu Remedy (@gharelu_remedy_07192d2301).</description>
    <link>https://dev.to/gharelu_remedy_07192d2301</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%2F4249902%2Fd56123b1-3739-4e24-8b11-d5855d8c841c.png</url>
      <title>DEV Community: Gharelu Remedy</title>
      <link>https://dev.to/gharelu_remedy_07192d2301</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gharelu_remedy_07192d2301"/>
    <language>en</language>
    <item>
      <title>How I Deploy Next.js with Docker, Caddy HTTPS and GitHub Actions</title>
      <dc:creator>Gharelu Remedy</dc:creator>
      <pubDate>Tue, 17 Nov 2026 12:59:44 +0000</pubDate>
      <link>https://dev.to/gharelu_remedy_07192d2301/how-i-deploy-nextjs-with-docker-caddy-https-and-github-actions-1e4p</link>
      <guid>https://dev.to/gharelu_remedy_07192d2301/how-i-deploy-nextjs-with-docker-caddy-https-and-github-actions-1e4p</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fybltm4q7em6c580e5ily.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fybltm4q7em6c580e5ily.png" alt=" " width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every new Next.js project, I ended up rebuilding the same Docker setup: a dev container with hot reload, a lean production image, CI that builds it, and a way to deploy with HTTPS. So I put it together once and tested it. Here is how the pieces fit, and what I would check before using it on a real server.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. One Dockerfile, three targets
&lt;/h2&gt;

&lt;p&gt;A multi-stage Dockerfile keeps dev and production separate, so the production image stays small.&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="c"&gt;# syntax=docker/dockerfile:1&lt;/span&gt;
&lt;span class="k"&gt;ARG&lt;/span&gt; &lt;span class="nv"&gt;NODE_VERSION&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;22

&lt;span class="k"&gt;FROM&lt;/span&gt; node:&lt;span class="nv"&gt;${NODE_VERSION}&lt;/span&gt;-alpine AS base
&lt;span class="k"&gt;WORKDIR&lt;/span&gt; /app
&lt;span class="k"&gt;RUN&lt;/span&gt; apk add --no-cache libc6-compat

&lt;span class="k"&gt;FROM&lt;/span&gt; base AS deps
&lt;span class="k"&gt;COPY&lt;/span&gt; package.json package-lock.json* ./
&lt;span class="k"&gt;RUN&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; -f package-lock.json &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; npm ci&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; npm install&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;fi&lt;/span&gt;

&lt;span class="c"&gt;# used by the dev compose file&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; base AS dev
&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;NODE_ENV&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;development
&lt;span class="k"&gt;COPY&lt;/span&gt; --from=deps /app/node_modules ./node_modules
&lt;span class="k"&gt;COPY&lt;/span&gt; . .
&lt;span class="k"&gt;EXPOSE&lt;/span&gt; 3000
&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;"run"&lt;/span&gt;, &lt;span class="s2"&gt;"dev"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;FROM&lt;/span&gt; base AS builder
&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;NEXT_TELEMETRY_DISABLED&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1
&lt;span class="k"&gt;COPY&lt;/span&gt; --from=deps /app/node_modules ./node_modules
&lt;span class="k"&gt;COPY&lt;/span&gt; . .
&lt;span class="k"&gt;RUN&lt;/span&gt; npm run build

&lt;span class="c"&gt;# used by production&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; base AS prod
&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;NODE_ENV&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;production &lt;span class="nv"&gt;NEXT_TELEMETRY_DISABLED&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1 &lt;span class="nv"&gt;PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3000 &lt;span class="nv"&gt;HOSTNAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0.0.0.0
&lt;span class="k"&gt;RUN&lt;/span&gt; addgroup -S nodejs -g 1001 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; adduser -S nextjs -u 1001 -G nodejs
&lt;span class="k"&gt;COPY&lt;/span&gt; --from=builder /app/public ./public
&lt;span class="k"&gt;COPY&lt;/span&gt; --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
&lt;span class="k"&gt;COPY&lt;/span&gt; --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
&lt;span class="k"&gt;USER&lt;/span&gt; nextjs
&lt;span class="k"&gt;EXPOSE&lt;/span&gt; 3000
&lt;span class="k"&gt;CMD&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"node"&lt;/span&gt;, &lt;span class="s2"&gt;"server.js"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details matter here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npm ci&lt;/code&gt; uses your &lt;code&gt;package-lock.json&lt;/code&gt;, so builds are reproducible. Commit the lock file.&lt;/li&gt;
&lt;li&gt;The production stage copies only the standalone output and runs as a non-root user.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the standalone output, enable it in &lt;code&gt;next.config.mjs&lt;/code&gt;:&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="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;nextConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"standalone"&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;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="n"&gt;nextConfig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Dev stack with hot reload
&lt;/h2&gt;

&lt;p&gt;The dev compose file mounts your source into the container and waits for the database to be healthy before the app starts.&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;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;target&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dev&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;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s"&gt;.:/app&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s"&gt;/app/node_modules&lt;/span&gt;   &lt;span class="c1"&gt;# keep the container's node_modules
&lt;/span&gt;      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s"&gt;/app/.next&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;postgres&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;service_healthy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Windows and macOS, bind mounts do not always trigger file watching. Enabling polling in the Next.js webpack config for dev fixes that.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Production behind Caddy
&lt;/h2&gt;

&lt;p&gt;On the server I run the prebuilt image behind Caddy. Caddy gets and renews a free HTTPS certificate on its own, so the config is tiny:&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="p"&gt;{&lt;/span&gt;$&lt;span class="no"&gt;DOMAIN&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;encode&lt;/span&gt; &lt;span class="n"&gt;gzip&lt;/span&gt; &lt;span class="n"&gt;zstd&lt;/span&gt;
    &lt;span class="n"&gt;reverse_proxy&lt;/span&gt; &lt;span class="n"&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server compose file starts the app, Postgres, Redis and Caddy together. Caddy only starts once the app reports healthy:&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;caddy&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;caddy:2&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;"80:80"&lt;/span&gt;
    &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s2"&gt;"443:443"&lt;/span&gt;
  &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;DOMAIN&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${DOMAIN:?set DOMAIN in .env}&lt;/span&gt;
  &lt;span class="na"&gt;depends_on&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;condition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;service_healthy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For this to work, the DNS record for your domain must point to the server, and ports 80 and 443 must be open.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. GitHub Actions: test, build, push, deploy
&lt;/h2&gt;

&lt;p&gt;The workflow does four things on every push to &lt;code&gt;main&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Starts the production stack and calls &lt;code&gt;/api/health&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Builds the production image and pushes it to GitHub Container Registry (GHCR).&lt;/li&gt;
&lt;li&gt;Copies the deploy files to your server over SSH.&lt;/li&gt;
&lt;li&gt;Runs the deploy script.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If the SSH secrets are not set yet, the deploy job skips itself instead of failing, so you still get the test and build results.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. A deploy script that rolls back
&lt;/h2&gt;

&lt;p&gt;The server script pulls the new image, restarts the stack and waits until it is healthy. If it is not, it restores the previous image:&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="k"&gt;if&lt;/span&gt; &lt;span class="nv"&gt;$COMPOSE&lt;/span&gt; up -d --remove-orphans --wait --wait-timeout 180&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Deployed $IMAGE"&lt;/span&gt;
&lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Deploy FAILED for $IMAGE"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;2
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; -n &lt;span class="s2"&gt;"$PREV"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"$PREV"&lt;/span&gt; != &lt;span class="s2"&gt;"$IMAGE"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Rolling back to $PREV"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;2
    set_image &lt;span class="s2"&gt;"$PREV"&lt;/span&gt;
    &lt;span class="nv"&gt;$COMPOSE&lt;/span&gt; up -d --remove-orphans --wait --wait-timeout 180 &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="k"&gt;fi&lt;/span&gt;
  &lt;span class="nb"&gt;exit&lt;/span&gt; 1
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It stores the current image name in &lt;code&gt;.env&lt;/code&gt;, which is how it knows what to go back to.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I tested, and what I did not
&lt;/h2&gt;

&lt;p&gt;I want to be upfront about this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tested: the dev and production stacks on Docker Desktop (Windows 11, WSL2), the server stack with Caddy locally, and the GitHub Actions test and image build.&lt;/li&gt;
&lt;li&gt;Not tested: a deploy to a real VPS over SSH with a public domain. If you try it, I would like to hear what breaks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Before you use this in production
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Set strong passwords in &lt;code&gt;.env&lt;/code&gt; and never commit it.&lt;/li&gt;
&lt;li&gt;Do not expose port 3000 without HTTPS.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;npm audit&lt;/code&gt; and decide how to handle the findings for your project.&lt;/li&gt;
&lt;li&gt;Test the rollback on a staging server before you rely on it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The kit
&lt;/h2&gt;

&lt;p&gt;I packaged all of this as a starter kit with a Postgres version and a MySQL version (the MySQL one also adds a BullMQ worker, a scheduler, Mailpit and phpMyAdmin): &lt;a href="https://remedygharelu.gumroad.com/l/nextjs-docker-starter" rel="noopener noreferrer"&gt;Next.js Docker Starter Kit&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What would you add to a setup like this? I would like to hear what your deploys always need.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>docker</category>
      <category>devops</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
