Deploy Nextjs Project from Scratch with Docker and GitHub

Author

Reads 716

HTML and CSS code on a computer monitor, highlighting web development and programming.
Credit: pexels.com, HTML and CSS code on a computer monitor, highlighting web development and programming.

Let's get started with deploying a Next.js project from scratch. To begin, we'll need to create a new Next.js project using the `npx create-next-app` command.

This command will create a basic Next.js project structure, including a `pages` directory and a `public` directory. We'll also need to install Docker and GitHub to deploy our project.

Next, we'll need to create a Dockerfile to containerize our Next.js project. This will allow us to build and deploy our project using Docker.

Curious to learn more? Check out: Next.js

Deploying to Cloud Platforms

Deploying to Cloud Platforms is a crucial step in making your Next.js project accessible to the world.

You can deploy your Next.js project to Vercel, a platform specifically designed for Next.js applications, with a single command.

To deploy to Vercel, you'll need to create a Vercel account and link it to your GitHub repository.

Vercel provides a free plan, making it an accessible option for small projects or personal websites.

Once you've linked your repository, you can deploy your project with the command "vercel deploy".

This command will automatically build and deploy your Next.js project to Vercel.

Building and Packaging

Credit: youtube.com, Next.js 15 Crash Course | Build and Deploy a Production-Ready Full Stack App

You can generate a Dockerfile for your Next.js project using the Dockerfile generator, which can be installed and run using npx dockerfile for Node applications or bunx dockerfile for Bun applications.

Using the standalone setting in your Next config can reduce your total image size by ~400mb by only including files necessary for a production environment.

To use the standalone setting, you can manually update your Dockerfile after running the build script (npm run build), replacing the final COPY instruction with the recommended one.

Using the standalone setting and updating your Dockerfile can reduce your total image size by ~400mb, so we highly recommend this approach!

Standalone builds are recommended when running fly launch or using the Dockerfile generator.

By setting the "standalone" value in your Next config, your Dockerfile will be updated to only include files necessary for a production environment.

This can lead to a significant reduction in image size, making it a great option for developers looking to optimize their build process.

Related reading: Next Js Image Component

Create Next.js Dockerfile

Credit: youtube.com, Dockerize Next.js & Deploy to VPS (EASY!)

Creating a Next.js Dockerfile is a breeze with the Dockerfile generator. You can use it to generate a Dockerfile for your Node applications by running npx dockerfile.

This tool is a game-changer for developers, as it saves time and effort in setting up a Dockerfile. With a simple command, you can have a well-structured Dockerfile that's ready to use.

The Dockerfile generator can also be used for Bun applications by running bunx dockerfile. This shows how versatile the tool is, supporting multiple frameworks and applications.

By using the Dockerfile generator, you can focus on building and packaging your Next.js application, rather than worrying about the intricacies of Dockerfile creation.

Additional reading: Why Use Next Js

Static Site Generation

Static site generation is a crucial aspect of deploying a Next.js project. You can address the need for static site generation with databases through two approaches: build time secrets and deferring the static site generation until after deployment.

If you prefer to use the Dockerfile generator, you can do so by running a specific command. This approach allows your build to have full access to all of your deployment secrets and environment variables.

Curious to learn more? Check out: Openshift Deployment

Credit: youtube.com, Next.js Static Site Generation (SSG) Tutorial

One of the benefits of deferring static site generation is that your deployment machines will only need enough memory to run a build. You can achieve this by following the instructions on fly scale memory and swap_size_mb.

However, keep in mind that you may need to adjust the grace period for any HTTP service checks. This is especially important if you're running multiple machines, as the static site generation will be run on each machine, increasing the total time before any changes are fully deployed.

Here are some key considerations to keep in mind:

  • Your deployment Machines will need enough memory to run a build.
  • You may need to adjust the grace period for any HTTP service checks.
  • If you are only running one Machine there will be a period of time where your server is inaccessible while the site is being statically generated.
  • If you run multiple Machines, the static site generation will be run on each increasing the total time before any changes are fully deployed.

Environment and Configuration

To deploy a Next.js project, you need to configure your environment and application settings correctly. In the 'Main' tab of Porter, you can define the start command to run your app, which is required if you're using a buildpack.

You can also specify the resources your app needs, such as CPU and RAM, in the 'Resources' tab. This is where you can define the number of replicas you'd like to run for your app and any autoscaling rules.

To expose environment variables to the browser, you can add them as ARG variables in your Dockerfile. This is especially useful if you're using Next.js, as it supports exposing environment variables using variables with names starting with NEXT_PUBLIC_.

Expose EnvVars to Browser

Credit: youtube.com, What are environment variables How do they work

If you're a Next.js user, you might know that it supports exposing environment variables to the browser using variables with names starting with NEXT_PUBLIC_.

These variables are fixed at build time, and unlike runtime environment variables, they can't be changed unless the application is built again.

To make these NEXT_PUBLIC_ variables available, add them as ARG variables in your Dockerfile.

This should come after any actual build steps, which means it will be after the line FROM base as build if you've used the Dockerfile generator or the Dockerfile created from fly launch.

For your interest: Nextjs Dockerfile

Build Settings Configuration

Porter can automatically detect your app's language and select a suitable buildpack for packaging your Next.js app for deployment.

You can further tune your build settings in Porter by selecting the branch you want to use, which will then display a screen where you can make adjustments.

Porter recommends using the newer 'heroku/builder:22' buildpack for Next.js apps.

Credit: youtube.com, App Configuration, where does it go? Config files, env vars, external service?

Using a standalone build is recommended, as it can reduce your total image size by ~400mb.

To enable standalone builds, set the "standalone" value in your Next config, which will update your Dockerfile to only include necessary files for a production environment.

You can also manually update your Dockerfile after running the build script (npm run build) by replacing the final COPY instruction with the following.

Related reading: Nextjs Build Output

Configure Your Service

To configure your service on Porter, you need to specify what command to run for this service, which can be done in the 'Main' tab. This is required if your app's being built using a buildpack.

Porter Cloud imposes a limit on the resources that can be used by a single app, so you can define how much CPU and RAM your app's allowed to access in the 'Resources' tab. You can allocate up to the limit, but if your app needs more scalability, you might need to look at Porter Standard and using your own AWS account instead.

Credit: youtube.com, How to Use an Environment Variable for Application Settings in Azure App Service

You can define the number of replicas you'd like to run for this app and any autoscaling rules in the 'Resources' tab, which allows you to instruct Porter to add more replicas if resource utilization crosses a certain threshold.

In the 'Networking' tab, you specify what port your app listens on - since we've kept NextJS's default settings, you're going to set the port to 8000. Porter automatically generates a public URL for you to use when you deploy a web app, but you can also opt to bring your own domain by adding an A record to your DNS records.

Make sure your app is configured to listen on '0.0.0.0' instead of 'localhost' or '127.0.0.1', as Porter won't be able to forward incoming connections and requests to your app if it's listening on the latter.

GitHub and Version Control

GitHub is a web-based platform for version control and collaboration. It allows developers to store and manage their code in a centralized repository.

Credit: youtube.com, How to host next js app on GitHub pages using GitHub Actions | CI/CD

GitHub offers a free plan for open-source projects, which is ideal for small projects or projects in the early stages of development. This plan includes unlimited private repositories.

To deploy a Next.js project, you'll need to create a new repository on GitHub and connect it to your local project. This involves initializing a Git repository, adding files, and committing changes.

GitHub provides a user-friendly interface for managing your repository, including features like file versioning and collaboration tools.

Access and Exploration

After deploying your Next.js project, you can access it through a unique URL or domain name visible on the Porter Cloud dashboard under your app's name.

Once your app is live, you can test it by visiting the URL or domain name. Porter Cloud also provides logging and monitoring in the form of app logs and resource consumption metrics on the dashboard, allowing you to see how your web application is faring.

You can view the following metrics on the dashboard: App logsResource consumption metrics

Accessing Your App

Hand Holding Smartphone with Internet Access to YouTube
Credit: pexels.com, Hand Holding Smartphone with Internet Access to YouTube

Now that your app is live on Porter Cloud, you can access it with a unique URL or domain name visible on the dashboard under your app's name.

Your app is now live and accessible to the world. You can test it and see how it's working.

Porter Cloud provides logging and monitoring on the dashboard, so you can see how your web application is faring. This includes app logs and resource consumption metrics.

You can check your app's performance and troubleshoot any issues using the logging and monitoring features.

Here are the key features you can access on the dashboard:

  • App logs
  • Resource consumption metrics

Exploring Further

As you get more comfortable with deploying Next.JS projects on Porter, you'll likely want to explore further customization options. Let's dive into a few key areas to help you optimize your application.

You can add your own domain to your Next.js application, giving you more control over your online presence.

If you're hosting on Porter Standard, you can also create environment variables and groups, making it easier to manage different settings for your app.

Scaling your Next.js applications is also a breeze, allowing you to adapt to changing traffic and demands.

To ensure your app never goes offline, Porter will automatically renew and manage the SSL certificate for you.

Nancy Rath

Copy Editor

Nancy Rath is a meticulous and detail-oriented Copy Editor with a passion for refining written content. With a keen eye for grammar, syntax, and style, she has honed her skills in ensuring that articles are polished and engaging. Her expertise spans a range of categories, including digital presentation design, where she has a particular interest in the intersection of visual and written communication.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.