Published on

Running a Next.js App with Docker

Authors

In the world of web development, efficiency and scalability are paramount. Docker has become a go-to tool for developers looking to streamline the deployment of their applications. In this blog, we'll walk you through the process of running a Next.js app using Docker and exposing it on port 4000. This will allow you to containerize your Next.js application, making it easy to deploy and manage, and exposing it on a specific port for external access.

Prerequisites

Before we dive into the steps, make sure you have the following prerequisites installed on your system:

  1. Docker: You can download and install Docker from the official website: Docker

  2. Node.js and npm: You'll need Node.js and npm installed to work with your Next.js app. You can install them from nodejs.org.

  3. A Next.js App: Have a Next.js application ready to containerize. If you don't have one, you can create a new Next.js app with the following command:

    npx create-next-app my-nextjs-app
    

Step 1: Dockerize Your Next.js App

The first step is to create a Dockerfile for your Next.js application. A Dockerfile is a script that specifies how your application's container should be built. Let's break down the key components of this file:

# Use an official Node.js runtime as the base image
FROM node:16-alpine
# Set the working directory in the container
WORKDIR /frontend
# Copy the package.json and package-lock.json to the container
COPY package*.json ./
# Install the app's dependencies
RUN npm install
# Copy the rest of the application code to the container
COPY . .
# Expose the desired port
EXPOSE 3000
# Run next app (you can also build and start the app)
CMD npm run dev

In this Dockerfile

  1. We use the official Node.js image as our base.
  2. We set the working directory inside the container to /frontend.
  3. We copy the package.json and package-lock.json files to the container to install dependencies.
  4. We run npm install to install the app's dependencies.
  5. We copy the entire application code to the container.
  6. We expose port 3000 to allow external access. Finally, we start the application using the npm run dev command.

Step 2: Create a .dockerignore file

To enhance your Docker setup, it's a good practice to create a .dockerignore file to specify which files and directories should be excluded when building the Docker image. This helps reduce the image size and minimize unnecessary files. Here's how you can create a .dockerignore file:

  1. In your Next.js app's root directory, create a file named .dockerignore.

  2. Add the following content to the .dockerignore file:

    node_modules
    npm-debug.log
    yarn-error.log
    .DS_Store
    .git
    .idea
    *.md
    *.log
    

These patterns will exclude common development-related files and directories. Adjust the .dockerignore file to include or exclude specific files or directories relevant to your project.

After creating or modifying the .dockerignore file, you can proceed with building and running your Docker container. This will help ensure that only essential files are included in your Docker image, making it more efficient and smaller in size.

Step 3: Build the Docker Image

docker build -t my-nextjs-app .

Replace my-nextjs-app with a suitable name for your Docker image.

Step 4: Run the Docker Container

After the Docker image is built, you can run a Docker container based on it. Use the following command to run your Next.js app and expose it on port 4000:

docker run -d -p 4000:3000 --name next-container-name my-nextjs-app

This command runs a detached (background) Docker container, mapping port 4000 from your host to port 3000 in the container.

Step 5: Access Your Next.js App

Your Next.js app is now up and running inside a Docker container, and it's exposed on port 4000. You can access it by opening your web browser and navigating to:

http://localhost:4000

Your Next.js app should be accessible at this URL. Any changes you make to the app code will require rebuilding and rerunning the Docker container to see the updates.

That's it! You've successfully containerized your Next.js app using Docker and exposed it on port 4000. This approach allows you to easily deploy and manage your Next.js application while providing external access through a specific port. Docker containers are versatile and offer a scalable solution for modern web development projects. Happy coding