How to Dockerize and Deploy a Next.js Application on Koyeb
5 minKoyeb provides developers the fastest way to deploy full stack applications and APIs globally. Want to deploy your Next.js application globally in minutes? Sign up today and deploy 2 services for free forever!
Introduction
Next.js is a popular React framework offering a wide range of features such as hybrid and server rendering, file-system routing, route pre-fetching, smart bundling, and more with near-zero configuration.
In this guide, we will showcase how to Dockerize a Next.js application and deploy it to Koyeb.
Requirements
To successfully follow and complete this tutorial, you need:
- Docker installed on your machine
- A GitHub account to store our Docker image on the GitHub container registry
- Have configured Docker for use with GitHub Packages
- A Koyeb account to deploy and run our dockerized Next.js application
Steps
To dockerize and deploy a Next.js application on Koyeb, you need to follow these steps:
- Create a Hello World Next.js app or use an existing one
- Write the Dockerfile and build the Docker image
- Push the Docker image to the GitHub container registry
- Deploy the dockerized Next.js app on Koyeb
Create a Hello World Next.js app or use an existing one
If you already have an existing Next.js application you want to dockerize, you can jump to the next step.
Let's get started by creating a new Next.js application. In your terminal run the following command:
This command initializes all the files and configurations required to run a new Next.js application. During the installation process, you are asked to give your project a name.
This name is used to create the folder your project will be located and to seed the package.json
name key.
Once the initialization complete, you can launch the Next.js application by running yarn dev
on your project folder and opening your browser to http://localhost:3000
.
You should land on the Welcome to Next.js
page.
Write the Dockerfile and build the Docker image
To Dockerize a Next.js app, you need to create a Dockerfile
in your project folder containing the content below. In this guide, we use Docker multi-stage build to keep the image layers size as small as possible and to ensure our image contains only what is needed to run.
The Dockerfile is composed of three different stages:
- The first one is used to install dependencies
- The second is used to build the Next.js app
- The last one is used to configure the runtime environment of the Next.js app
In this guide, we use the Node.js LTS version as the base image, if you need to use a specific version of Node, you can refer to the available tags on the Docker Hub.
To build the Docker image execute the following command:
This command will build the Docker image with the name ghcr.io/<YOUR_GITHUB_USERNAME>/my-project
. Once the build is over, you can run a container using the image locally to validate everything is working as expected running:
Open your browser and navigate to http://localhost:3000
to view your project landing page.
Push the Docker image to the GitHub container registry
Since our Docker image is built and functional in our test, we can now upload it to the GitHub container registry. In your terminal run the command below to push the image.
Within a few minutes, you will see your Docker image available on the GitHub container registry: https://github.com/<YOUR_GITHUB_USERNAME>?tab=packages
.
Deploy the dockerized Next.js app on Koyeb
It's now time to deploy our freshly dockerized Next.js application on Koyeb. On the Overview tab of the Koyeb control panel, click Create Web Service to begin.
- Choose Docker as the deployment method.
- In the Docker image field, enter the name of the image you created.
- Click the Private image toggle and, in the select field, click Create secret. In the form that appears, fill in a name for the Secret which will be created, for instance
gh-registry-secret
, the registry provider, GitHub in our case, and your GitHub username and a valid GitHub token with registry read/write permissions as the password. Once you've filled all the fields, click the Create button. - In the Exposed ports section, change the export port to 3000.
- Choose a name for your App and Service, for example
my-next-js-project
, and click Deploy.
You can add more regions to deploy your applications, set environment variables, and define the horizontal scaling according to your needs.
You will automatically be redirected to the Koyeb App page where you can follow the progress of your Next.js application deployment. In a few seconds, once your app is deployed, click on the Public URL ending with koyeb.app
.
Et voilà, your Next.js project is running on Koyeb!
Your Next.js app is now secured with native TLS encryption and benefits from all the Koyeb Serverless features including autoscaling, auto-healing, and a High-Performance Edge Network.