How To Containerize a Next.js App and Deploy to Kinsta

Next prompts you to specify a number of configuration options for your new app. For this tutorial, you can simply accept the suggested defaults.

  1. To preview your new application, navigate to the new-app directory and run:

Features such as containerized apps on GCP infrastructure running on C2 machines with 35 data centers available, premium integration with Cloudflare for a high-performance CDN that serves your site from 260+ Points of Presence, enterprise-level firewall DDoS protection, Edge Caching, and uptime monitoring (with 99% uptime guarantee), ensure your app runs fast, secure, and is reliably available to the internet.

With Kinsta, you can deploy your applications automatically by using Buildpacks and Nixpacks. But when the build process is automatically triggered based on your app’s codebase, you don’t have a lot of room for customizations. If you deploy to Kinsta with a Dockerfile, you can configure precisely how you want to build and deploy your application.

Start With a Next.js Application

To containerize an application, we use a declarative method through a Dockerfile. Docker reads and executes the scripts defined in this file to build and deploy your application.

Advantages of Containerizing Your Application

docker run -p 3000:3000 next-docker
  1. On your browser, open http://localhost:3000.

Deploy the Containerized Next.js App to Kinsta

npm run dev

Create the runtime stage to deploy your application:

  1. Use the official latest stable Node.js alpine image as the base image for the runtime stage:
USER node
  1. Start the Next.js app:
RUN npm ci --only=production
  1. Copy the built app from the build stage to the runtime stage:

With Docker, you can run several instances of your container on different servers. Container orchestrators handle increased traffic without affecting your app’s performance.


docker build -t next-docker .
  1. Run the container to preview your app:
RUN npm ci
  1. Copy the rest of the application code to the container with:
COPY package*.json ./
  1. Install only production dependencies:

Follow the steps below to deploy your application to Kinsta using its Dockerfile.

  1. Host your app’s codebase on a Git repository (on GitHub, GitLab, or Bitbucket).
  2. Log into your MyKinsta account or create a new one to access your dashboard.
  3. Click Add service and select Application.
  4. Select your Git provider, repository, and the branch you want to deploy from.
  5. Check the Automatic deployment on commit checkbox if you’d like to deploy your app at every push to your repo.
  6. Select the data center closest to your audience and click Continue.
  7. Choose your build environment and select Use Dockerfile to set up container image.
  8. If your Dockerfile isn’t in your repo’s root, use Context to indicate its path and click Continue.
  9. You can leave the Start command entry empty. Kinsta uses npm start to start your application.
  10. Select the pod size and number of instances best suited for your app and click Continue.
  11. Fill out your credit card information and click Create application.

If you are starting from an existing application, you can skip this step. If you are starting fresh, create a new Next.js application:

  1. Open your terminal and install create-next-app:

Get started with a free trial of our Application Hosting or Database Hosting. Explore our plans or talk to sales to find your best fit.

Amin Choroomi

Although you can still preview your application with run npm dev, run it locally with Docker to mimic the production environment and to test and preview any changes you make to your app’s Dockerfile.

Docker encapsulates everything an application needs to run, allowing them to be commuted easily between environments. Whether you are running it locally, on a computer with a different operating system, or in staging and production environments, Docker builds the application with the same components, making it easier to code, test, and deploy.


COPY . .
  1. Build the application: