Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To use Docker to deploy a Next.js application on HTTPS (SSL connection), you need to follow these steps:

  1. Install Docker : First thing you need to do is Install Docker on your local machine.

  2. Create a Next.js Application: Create a Next.js application by running the following commands in your terminal:

    npx create-next-app <app-name>
    cd <app-name>
    
  3. Create a Dockerfile : Create a Dockerfile in the root directory of your application with the following contents:

    FROM node:14.15.0-alpine
    
    RUN mkdir -p /app
    WORKDIR /app
    
    COPY package.json .
    RUN npm install --production
    
    COPY . .
    
    EXPOSE 3000
    
    CMD ["npm", "run", "start"]
    
  4. Build the Docker Image: In order to build the Docker image, run the following command:

    docker build -t <image-name> .
    
  5. Run the Docker Container: Run the Docker container by running the following command:

    docker run -p 3000:3000 -d <image-name>
    
  6. Configure HTTPS: Configure HTTPS by adding the following code to your Next.js application:

    const httpsOptions = {
     key: fs.readFileSync('/path/to/key.pem'),
     cert: fs.readFileSync('/path/to/cert.pem'),
    };
    
    const server = https.createServer(httpsOptions, app);
    
  7. Update the Dockerfile: Update the Dockerfile by adding the following lines:

    COPY path/to/key.pem /app
    COPY path/to/cert.pem /app
    
  8. Rebuild and run Docker container: Rebuild and run the Docker container by running the following command:

    docker build -t <image-name> .
    docker run -p 3000:3000 -d <image-name>
    

Your Next.js application is now deployed on HTTPS (SSL connection) using Docker.