|Blogs|Deploying Next.js using Docker Compose
ringring
DevOps
Deploying Next.js using Docker Compose
Published by:Anuj Poudel
Published on:18 Feb, 2024
blogimage
Share
 

In our previous guide on Dockerizing Next.js projects, we explored the process of containerizing Next.js applications. While the process worked well, there is one problem, running projects using lengthy Docker commands can be cumbersome, especially when dealing with multiple flags or environment variables. To streamline this process and facilitate deployment, we'll now use Docker Compose.

 

What is Docker Compose?

Docker Compose is a tool designed to simplify the management of multi-container Docker applications. It allows you to define and run multi-container Docker applications using YAML configuration files. With Docker Compose, you can manage complex applications with ease, specifying dependencies, networks, volumes, and other configurations in a single file.

 

Step 1: Create a Docker Hub Repository

Firstly, sign in to Docker Hub and create a repository. Docker Hub offers both public and private repositories, with options for free and paid accounts. For our purposes, we'll create a public repository named nextjs-blog.

 

After creating the repository, note down the repository URL, in our use case it is  dallotech/nextjs-blog.

 

Step 2: Generate a Security Token

Navigate to your Docker Hub account settings and generate a security token. This token will be used for authentication when pushing Docker images to Docker Hub. 

Create a New access token, and while creating provide necessary permissions as per requirement. When the token is created it gives us the option to copy the token

 

Step 3: Docker Login

In your local terminal, log in to Docker Hub using the following command to login to docker hub

docker login

Provide your Docker Hub username and the security token generated in the previous step when prompted. Upon successful authentication, you'll receive a "Login Succeeded" message.

 

 

Step 4: Dockerize with Docker Compose

Create a file named docker-compose.yml in your project directory and populate it with the following configuration:

version: '3.8'

services:
  app:
    image: dallotech/nextjs-blog:latest
    platform: linux/amd64
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    restart: always
This configuration specifies the Docker image to build, ports to expose, and other service settings. Replace dallotech/nextjs-blog with your Docker Hub repository URL that you created in step 1.
 
  • image: dallotech/nextjs-blog:latest Specifies the Docker image to use for this service. It pulls the latest version of the image named "dallotech/nextjs-blog" from Docker Hub.

  • platform: linux/amd64 Defines the platform architecture for which this service is intended. In this case, it's specified as "linux/amd64".

  • context: . & dockerfile: Dockerfile Specifies how to build the Docker image. It indicates that the Dockerfile (Dockerfile) is located in the current directory (context: .). This means it will look for a Dockerfile in the same directory where the docker-compose.yml file is located.

  • ports: - "3000:3000" Maps port 3000 on the host machine to port 3000 inside the container. This allows traffic to reach the Next.js application running inside the container.

  • restart: always Specifies the restart policy for the container. In this case, it's set to "always", which means Docker will always restart the container if it stops for any reason.
     
     

Step 5: Build and Run Locally

Execute the following command in the terminal in the project root directory

docker-compose build

It will build the  Docker image defined in the docker-compose.yml file.

Once the build is complete, run the following command

docker-compose up

This will start the Next.js project locally. Access the project via localhost:3000 in the browser.

 

Step 6: Push to Docker Hub

Push the Docker image to Docker Hub using the following command

docker-compose push

This command uploads the built image to your Docker Hub repository, making it accessible for deployment.

 

Step 7: Pull and Deploy on Remote Server

SSH into your remote server using the ssh command

ssh -i ubuntu@xxx.xxx.xxx.xxx

Replace the IP with the IP of your server. 

 Create a directory for your Next.js project and navigate into it.

mkdir nextjs-blog
cd nextjs-blog

Create a docker-compose.yml file in the project directory and paste the configuration from Step 4.

 

Execute the following command

docker-compose pull

It pulls Docker images from Docker Hub onto the remote server.

 

Finally, run the following command to run the docker image

docker-compose up

This will run the project in the remote server, you could find the project running in 3000 port. 

 

To access the running project go to xxx.xxx.xxx.xxx:3000 in the browser (please remember to enable inbound requests in the 3000 port) 

For hosting this project in a particular domain, please have a look at this tutorial.

 

In this way, we successfully dockerized the next-js project and deployed it on a remote server 

 

Conclusion

With Docker Compose, Dockerizing and deploying Next.js projects becomes more streamlined and efficient. By following these steps, you can containerize your applications, manage dependencies, and deploy them across different environments with ease. Happy coding! For further information on deployment please write at anuj@dallotech.com

Other related blogs
Technology
Integrating Online Payment in NestJS using Factory Pattern: Project Setup, Service Layer and APIs (Part 1)

If you are integrating a payment features(eSewa, Khalti, ConnectIPS, Stripe, PayPal etc.) in NestJS or are interested in knowing how online payment features can be implemented, this blog is perfect for you.

Technology
Restore MSSQL Database file .bak on a Linux Server

If you need to restore a MSSQL database file (.bak) on a Linux server, this is the perfect blog for you. Additionally, anyone interested in web development, database management, or technology in general may also find this useful and interesting.

Technology
Hexagonal Architecture in NestJS with Sample Code

NestJS is javascript server side progressive framework combining Object Oriented Programming(OOP) and Functional Programming(FP). Since I got introduced to this framework, I have been loving it. Personal Opinion: At first, I became familiar with NestJS framework. After that, I wrote my first API , my first NestJS package and so on. But I was still exploring new things. This time, I wanted to learn about Hexagonal Architecture. Learning never stops !!! Rather than talking about theories, let’s directly dive into codes. I will be referencing code from the below mentioned repository. Github Link: Nestjs-Hexagonal-Architecture-Demo-Project

DevOps
How to Set Up CI/CD pipeline on Next.js project using GitLab on AWS EC2

Continuous Integration and Continuous Deployment (CI/CD) are essential for modern web development, ensuring fast, reliable, and automated software delivery. In this guide, we’ll walk you through setting up a CI/CD pipeline for a Next.js application using GitLab CI/CD. By automating the build and deployment process, you can eliminate manual tasks, reduce errors, and deploy updates seamlessly. Whether you're deploying to a VPS, cloud server, or a containerized environment, this tutorial will help you streamline the deployment of your Next.js app using Docker, GitLab CI/CD, and SSH. Let’s dive into automating your Next.js project for efficient, hassle-free deployment!

Discover Dallo Tech, a leading software development company offering expertise in Mobile and Web App Development, Data Analytics, Visualization, AI, and Blockchain solutions. Elevate your digital journey with our innovative technology services.

Open Hours

Sun - Fri:
10:00 am - 5:00 pm

Saturday:
CLOSED

Dallotech as tech partner
Business Team
Devops and Service
Design Team
Quality Assurance
Software Team
Mobile Team
Hire Expert Team
© 2018 - 2025 . DalloTech Pvt. Ltd. All Rights Reserved