Language and Framework Guides

Run a NextJS Container App

24min

Back4App Containers is a powerful platform for hosting Next.js applications. With its ability to automatically deploy Dockerized Next.js apps, you can launch your project in a scalable and flexible environment with ease.

In this guide, we will walk you through the process of preparing and deploying your Next.js application on Back4App Containers, covering everything from simple projects to more complex setups. We will begin with the necessary preparations, then move on to dockerizing the application, testing it locally, pushing the project to GitHub, setting up the project on Back4App Containers, monitoring deployment, and troubleshooting common issues.

If you have any questions or comments, feel free to join the conversation in the #Containers channel on the Back4app Community on Slack. Anytime you can also contact us at [email protected].

At any time, if you want to check a sample working Next.js project on Back4app containers, go to: https://github.com/templates-back4app/containers-nextjs-sample

1-Prepare your Next.js application:

a. Project Structure: Verify that your Next.js application follows a proper directory structure, with all necessary files and folders, such as pages, public, components, and styles, organized appropriately. The main entry point for your application should be the pages/index.js or pages/index.jsx file.

b. Dependencies: Check if all required dependencies are listed in the package.json file, including their correct versions. Ensure that you have installed all dependencies using npm install or yarn install to generate a package-lock.json or yarn.lock file.

c. Build Process: For more complex projects that involve a build process, ensure that your build scripts and configurations (e.g., Webpack, Babel, or Next.js) are properly set up. You should be able to run the build process locally without any issues.

d. Environment Variables: If your application relies on environment variables, ensure that you have a .env file with the necessary variables defined. When deploying to Back4App Containers, you will need to configure these environment variables in the deployment settings.

e. Server Setup (if applicable): If your Next.js application includes a custom server (e.g., Express), make sure it is correctly set up and configured to serve your Next.js application. Test your server locally to ensure it works as expected.

f. Application Optimization: Optimize your Next.js application by minimizing bundle sizes, using code-splitting, and implementing performance best practices. Use tools like Lighthouse to audit your application and address any performance or accessibility issues.

g. Cross-Browser Compatibility: Test your application across multiple browsers and devices to ensure proper rendering and functionality.

Once you have thoroughly reviewed and prepared your Next.js application, you can proceed to the next step, which is creating a Dockerfile for your project.

2-Dockerization:

Dockerizing a Next.js application involves creating a Dockerfile in the root directory of your project. The Dockerfile contains instructions to build a Docker image of your application, which can then be deployed to Back4App Containers. Here's a detailed explanation of how to create a Dockerfile for a typical Next.js application:

1-Create a new file named Dockerfile (without any file extension) in the root directory of your Next.js application.

2-Define the base image: Start the Dockerfile by specifying a base image using the FROM command. For a typical Next.js application, the base image should be a Node.js image, e.g., node:14 or node:16.

Dockerfile


3-Set the working directory: Use the WORKDIR command to set the working directory for your application within the Docker container. This is where your application files will be stored and executed.

Dockerfile


4-Copy package.json and package-lock.json: Copy the package.json and package-lock.json files from your local machine to the Docker container using the COPY command. These files are required to install your application's dependencies.

Dockerfile


5-Install dependencies: Use the RUN command to install your application's dependencies from the package.json file. This is typically done using npm ci, which installs the exact dependency versions specified in the package-lock.json file.

Dockerfile


6-Copy the rest of the project files: Use the COPY command again to copy the remaining files and folders from your local machine to the Docker container.

Dockerfile


7-Build the Next.js application: Add a RUN command to build your Next.js application using your build script, typically npm run build. This generates a production-ready version of your Next.js application in the .next folder.

Dockerfile


8-Configure the server: You need a server to serve your built Next.js application. One common choice is the built-in Next.js server. First, install it globally in the Docker container using the RUN command:

Dockerfile


9-Expose the server port: Use the EXPOSE command to specify the port on which your server will run inside the Docker container. For example, you can use port 3000.

Dockerfile


10-Start the server: Use the CMD command to specify the command that starts the server to serve your built Next.js application. With the built-in Next.js server, you can specify the start script and the port number.

Dockerfile


The complete Dockerfile for a typical Next.js application should look like this:

Dockerfile


Another Dockerfiles examples you can use as a reference:

Example 1: Using an Nginx server

If you prefer to use an Nginx server to serve your Next.js application, you can create a multi-stage Dockerfile. In this case, you will first build the Next.js application and then copy the generated files to the Nginx server.

Dockerfile


Make sure to create an appropriate nginx.conf file to configure the Nginx server properly.

Example 2: Using a custom server

If you have a custom server set up for your Next.js application, you can modify the Dockerfile accordingly. Here's an example using a custom server.js file:

Dockerfile


In this example, the custom server is started with the node server.js command instead of using the Next.js server.

3-Build and run the Docker container

After creating the Dockerfile, you can build the Docker image and run a container from it. Use the following commands to build and run the container:

Text


Replace "your-image-name" with a name for your Docker image.

Once the container is running, you can access your Next.js application at http://localhost:3000 in your web browser.

4-Test your Project Locally:

Before deploying your Next.js application on Back4App Containers, it's important to test it locally using Docker. This helps ensure that your application runs as expected and helps you identify and fix any issues before deployment.

Build the Docker image for your Next.js application:

In your terminal, navigate to your project's root directory and run the following command, replacing your-app-name with the name of your application:

docker build -t your-app-name .

Run the Docker container locally:

Next, run the following command to start the Docker container locally. This command maps the container's exposed port (e.g., 3000) to a port on your local machine (e.g., 3000):

docker run -p 3000:3000 your-app-name

Test your application:

Open a web browser and navigate to http://localhost:3000 to view your Next.js application. Make sure everything works as expected. If you encounter any issues, address them before moving on to the next step.

5-Push your project to GitHub:

Create a .gitignore file in your project's root directory to exclude unnecessary or sensitive files from your repository (e.g., node_modules, .env, build, etc.). Initialize a Git repository, commit your project files, and push them to a remote repository (e.g., on GitHub).

Text


6-Deploy your application on Back4app Containers

After creating your Back4app account you can follow the steps listed on the Docs:

In summary containers will follow the instructions detailed on your Dockerfile and start to create your App.

7-Monitor deployment and address possible errors:

Keep an eye on the deployment logs and status on the Back4App Containers dashboard. Address any errors or issues that arise during deployment. In case of more complex projects, ensure that all necessary services (such as databases or external APIs) are correctly configured and accessible.

8-Troubleshooting common problems

Common deployments errors when running a Next.js App on Back4App Containers are listed here. Other possible common errors when deploying a Next.js Application are:

Incorrect port configuration

Next.js applications should be configured to run on a specified port when deployed on Back4App Containers. If the application is still not accessible, check the Dockerfile to make sure the correct port is exposed (e.g., EXPOSE 3000 for port 3000).

Incompatible or missing dependencies

Ensure that all required dependencies are listed in the package.json file and that their versions are compatible with each other and your application code. Missing or incompatible dependencies can lead to runtime errors.

Invalid environment variables or configuration

Check if your application relies on specific environment variables or configuration files, and ensure they are correctly set up in the Back4App Containers environment. Set any necessary environment variables in your Dockerfile using the ENV command.

Application crashes or unhandled exceptions

Unhandled exceptions or crashes in your Next.js application code can cause deployment failures or unexpected behavior. Examine your application logs for any error messages, and address any issues in your code. Check the container logs by running docker logs your-app-name to see if there are any errors or exceptions being thrown. Use a tool like Sentry to track and monitor errors in your application.

Server-side rendering configuration

If your Next.js application uses server-side rendering (SSR), ensure that your SSR setup is correctly configured in your Dockerfile and that it starts your application using the right entry point.

9-Scale your application

For more complex projects that require additional resources or horizontal/vertical scaling, consider upgrading your Back4app Containers plan to handle increased traffic and load.

10-Sample Application

For a sample Next.js application project you can go to : https://github.com/templates-back4app/containers-nextjs-sample