---
title: "How to containerize a Next.js app and deploy it to Sevalla"
description: "Your image ships only the server and the dependencies it uses, instead of your whole project and its node_modules folder."
url: https://sevalla.com/blog/containerize-next-js/
---

[Blog](https://sevalla.com/blog/)

# How to containerize a Next.js app and deploy it to Sevalla

Your image ships only the server and the dependencies it uses, instead of your whole project and its node\_modules folder.

Updated: Nov 30, 2026·by Marcia Ramos

This article explains how to containerize a [Next.js](https://sevalla.com/blog/next-js/) application with a **Dockerfile**, run it locally with Docker, and deploy it to Sevalla. If you don't need a custom image, you can also deploy a Next.js app without a Dockerfile, as shown in our guide to [building and deploying a Next.js portfolio](https://sevalla.com/blog/next-js-portfolio/).

With [Docker](https://www.docker.com/), you package an application, its runtime, and its dependencies into an image. A container is a running instance of that image, isolated from other processes on the same machine. You describe how to build the image in a **Dockerfile**, and Docker runs its instructions in order.

All the code in this article was tested with Next.js 16.4 and Docker.

## Advantages of containerizing your application

Containerizing an application gives you a few benefits:

- **Portability:** [Docker](https://kinsta.com/blog/what-is-docker/) packages everything your app needs to run, so it runs the same way on your computer, a teammate's computer, and in production.
- **Consistency:** The image contains exact versions of your runtime and dependencies, so you avoid bugs caused by different versions in development and production.
- **Scalability:** You can run several containers from the same image to handle more traffic.
- **Isolation and security:** Each container runs separately from other applications on the same server, and you control exactly what goes into the image. Learn more in [9 security best practices for Docker containers](https://kinsta.com/blog/docker-security/).
- **Control:** Automatic builders choose the base image and build steps for you. With a Dockerfile, you decide the Node.js version, the system packages, and every layer of the image.

## Requirements

To follow along, you need:

- [Node.js](https://nodejs.org/) 20.9 or later, which Next.js 16 requires, and npm.
- [Docker Desktop](https://www.docker.com/products/docker-desktop/), or [Docker Engine](https://docs.docker.com/engine/) on Linux, installed and running.

This tutorial assumes you have a basic understanding of Next.js and Docker.

## Start with a Next.js application

If you already have a Next.js application, you can skip this step. Otherwise, create one:

1. Create a new Next.js application in its own directory, with the default options:  
```  
npx create-next-app@latest new-app --yes  
```  
The `--yes` flag accepts the defaults: TypeScript, Tailwind CSS, ESLint, and the App Router.
2. Go to the **new-app** directory and start the development server to preview it:  
```  
cd new-app  
npm run dev  
```

## Enable standalone output

Before you write the Dockerfile, set Next.js to build a standalone server. Open **next.config.ts** and add `output: "standalone"`:

```
import type { NextConfig } from "next";
 
const nextConfig: NextConfig = {
  output: "standalone",
  // ...the rest of your config
};
 
export default nextConfig;
```

With this option, `next build` traces which files your server needs and copies them, with only the dependencies it uses, to **.next/standalone**, along with a minimal **server.js** that starts the app. Your image ships only the server and the dependencies it uses, instead of your whole project and its node\_modules folder. The standalone output includes `sharp`, so image optimization with `next/image` works in the container.

The standalone folder doesn't include your **public** folder or the static files in **.next/static**, which are meant to be served from a CDN. You'll copy them into the image yourself.

## Containerize a Next.js app with a Dockerfile

Create a file called **Dockerfile** in your app's root directory. It has two stages: a `build` stage that installs dependencies and builds the app, and a `runtime` stage that becomes the final image.

### Build stage

1. Use the official Node.js image as the base image for the `build` stage, and set the working directory:  
```  
FROM node:24-slim AS build  
WORKDIR /app  
```  
This uses Node.js 24, a long-term support (LTS) release, on a slim Debian base. Update the tag when you move to a newer LTS release.
2. Copy **package.json** and **package-lock.json**, then install the dependencies:  
```  
COPY package.json package-lock.json ./  
RUN npm ci  
```  
`npm ci` installs exactly the versions in **package-lock.json**. Because these files are copied before the rest of your code, Docker reuses this layer from its cache until your dependencies change, so a code change doesn't reinstall everything.
3. Copy the rest of the application code and build the app:  
```  
COPY . .  
RUN npm run build  
```

### Runtime stage

1. Start the `runtime` stage from the same base image, and set the working directory and environment variables:  
```  
FROM node:24-slim AS runtime  
WORKDIR /app  
ENV NODE_ENV=production  
ENV HOSTNAME=0.0.0.0  
ENV PORT=3000  
```  
The standalone server listens on the address in `HOSTNAME` and the port in `PORT`. `0.0.0.0` makes it listen on all network interfaces, which it must do to receive traffic from outside the container.
2. Copy the standalone server, the **public** folder, and the static files from the `build` stage:  
```  
COPY --from=build /app/public ./public  
COPY --from=build /app/.next/standalone ./  
COPY --from=build /app/.next/static ./.next/static  
```
3. Create the folders the server writes to, and give the `node` user ownership of them:  
```  
RUN mkdir -p .next/cache .next/server/route-cache \  
    && chown node:node .next/cache .next/server/route-cache  
```  
Next.js stores optimized images and cached data in **.next/cache**. Without this step, the server can't write there when it runs as `node`, and image optimization logs `EACCES: permission denied` errors.
4. Run the container as the unprivileged `node` user that the Node.js image includes, expose the port, and start the server:  
```  
USER node  
EXPOSE 3000  
CMD ["node", "server.js"]  
```  
Using exec form for `CMD` makes Node.js the container's main process, so it receives the stop signal when the container stops and shuts down cleanly.

Here's the complete **Dockerfile**:

```
FROM node:24-slim AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
 
FROM node:24-slim AS runtime
WORKDIR /app
ENV NODE_ENV=production
ENV HOSTNAME=0.0.0.0
ENV PORT=3000
COPY --from=build /app/public ./public
COPY --from=build /app/.next/standalone ./
COPY --from=build /app/.next/static ./.next/static
RUN mkdir -p .next/cache .next/server/route-cache \
    && chown node:node .next/cache .next/server/route-cache
USER node
EXPOSE 3000
CMD ["node", "server.js"]
```

The final image contains only the `runtime` stage. The build tools, your source code, and the development dependencies stay in the `build` stage.

### Add a .dockerignore file

Create a **.dockerignore** file next to the Dockerfile, so your local build output, installed packages, and environment files aren't sent to the build:

```
.git
.next
node_modules
.env*
*.log
Dockerfile
.dockerignore
```

Without it, `COPY . .` would copy your local **node\_modules** and **.next** folders over the ones built in the container, and could put secrets from **.env** files into the image.

### Environment variables at build time

Next.js reads server-side environment variables when the server runs, so you can pass them to the container at runtime. Variables that start with `NEXT_PUBLIC_` are different: Next.js writes them into the JavaScript bundle during `next build`, so they must be available in the `build` stage. Declare each one with `ARG` before the build step:

```
ARG NEXT_PUBLIC_API_URL
RUN npm run build
```

Then pass the value when you build the image, with `docker build --build-arg NEXT_PUBLIC_API_URL=https://api.example.com`.

## Run the app locally with Docker

You can still preview your app with `npm run dev`, but running it with Docker lets you test the production image and any changes to your Dockerfile.

1. Build the image with `docker build`:  
```  
docker build -t next-docker .  
```
2. Run a container from the image:  
```  
docker run -p 3000:3000 next-docker  
```  
The `-p 3000:3000` option maps port 3000 on your computer to port 3000 in the container. If port 3000 is already in use, map a different port, for example `-p 3001:3000`.
3. Open `http://localhost:3000` in your browser.

To stop the container, press `Ctrl+C`, or run `docker stop` with the container's ID from `docker ps`.

## Deploy the containerized Next.js app to Sevalla

Sevalla can build your app straight from its Dockerfile. Follow these steps:

1. Push your app's code, including the Dockerfile and **.dockerignore**, to a Git repository on GitHub, GitLab, or Bitbucket.
2. Log in to the [Sevalla dashboard](https://app.sevalla.com/), click **Applications** in the sidebar, then click **Create** \> **Application**.
3. Under **Source**, click **Private repository**, then choose your Git provider, repository, and branch.
4. Under **Location**, choose the data center closest to your users, choose a size, and click **Create application**.
5. On the application's **Settings** tab, in the **Build strategy** section, click **Update build strategy**. Click **Dockerfile**, keep the **Dockerfile path** as `Dockerfile` and the **Docker context** as `.`, then click **Update build strategy**.
6. On the **Overview** tab, click **Deploy now**, check the branch, and click **Deploy**.

Sevalla builds the image from your Dockerfile and starts the web process with its `CMD`. You don't need to change the port: Sevalla sets the `PORT` environment variable to the web process's port, `8080` by default, and the standalone server reads it instead of the `3000` in your Dockerfile.

When the deployment's status changes to **Deployed**, click the domain under the application's name to open your app. To deploy every push to the branch automatically, turn on **Auto-deploy** in the **Settings** tab.

If your app uses `NEXT_PUBLIC_` variables, add them on the application's **Environment variables** tab with **Available during buildtime** selected. Sevalla passes them to the build as build arguments, so the `ARG` lines in your Dockerfile receive them. For more details, see the docs on [deploying Next.js](https://docs.sevalla.com/guides/javascript/nextjs) and [building from a Dockerfile](https://docs.sevalla.com/applications/builds/dockerfile).

## Summary

In this article, you learned how to containerize a Next.js app with a multi-stage Dockerfile that runs the standalone server as an unprivileged user, run it locally with Docker, and deploy it to Sevalla. Standalone output keeps the image small, and the `.dockerignore` file keeps local files and secrets out of it.

With Sevalla's [Application Hosting](https://sevalla.com/application-hosting/), you can deploy your Dockerfile from Git, turn on the CDN and edge caching to serve static assets and pages from Cloudflare's network, and add a managed database when your app needs one.

Share on:[Tweet](https://x.com/intent/post?text=I've just read this article "How to containerize a Next.js app and deploy it to Sevalla" on @sevalla%5Fhosting 's blog: https://sevalla.com/blog/containerize-next-js/ "x.com")[Reddit](https://www.reddit.com/submit?url=https://sevalla.com/blog/containerize-next-js&title=How to containerize a Next.js app and deploy it to Sevalla "Reddit")[HackerNews](https://news.ycombinator.com/submitlink?u=https://sevalla.com/blog/containerize-next-js&t=How to containerize a Next.js app and deploy it to Sevalla "HackerNews")

## Deep dive into the cloud!

Deploy your application, database, or static site in minutes.

[Get started](https://sevalla.com/signup/)
