Deploying with Docker

Docker is a popular platform for building, shipping, and running applications in containers. With Docker, you can package your application and its dependencies into a container image, which can then be deployed to any environment that supports Docker.

In this guide, you will learn how to build your staarter.devarrow-up-right project into a Docker container.

Prerequisites

Before you begin, make sure you have the following prerequisites:

Set output in next.config.js

Next.js can automatically create a standalone folder that copies only the necessary files for a production deployment including select files in node_modules.

To do this, add the following configuration to your next.config.js file:

/** @type {import('next').NextConfig} */
module.exports = withNextIntl({
  output: 'standalone',
  // ... other configurations
})

Create a Dockerfile

Create a new file named Dockerfile in the root of your web app directory with the content from this examplearrow-up-right.

Create a .dockerignore File

Create a new file named .dockerignore in the root of your web app directory with content from this examplearrow-up-right.

Build the Docker Image

To build the Docker image, run the following command in your terminal from the root of your web app directory:

This command builds the Docker image with the tag my-next-app. You can replace my-next-app with any name you prefer.

Run the Docker Container

To run the Docker container, use the following command:

This command runs the Docker container with the tag my-next-app and maps port 3000 on your local machine to port 3000 in the container. It also loads environment variables from the .env.local file.

Last updated