2

i am trying to containerize my nextjs application but I always encounter a series of problems.

#Dockerfile

# syntax=docker.io/docker/dockerfile:1

FROM node:18-alpine AS base

FROM base AS deps
RUN apk add --no-cache libc6-compat

WORKDIR /app

COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* ./
RUN \
  if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
  elif [ -f package-lock.json ]; then npm ci; \
  elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i; \
  else echo "Lockfile not found." && exit 1; \
  fi

FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npx prisma generate --schema=prisma/schema.prisma
RUN npm run build

FROM base AS runner
WORKDIR /app

ENV NODE_ENV=production

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001



COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/@prisma ./node_modules/@prisma

COPY --from=builder /app/public ./public


USER nextjs

EXPOSE 3000

ENV PORT=3000

CMD HOSTNAME="0.0.0.0" node server.js

#docker-compose.yaml

services:
  
  web:
    image: apdsmarketingportalver3
    container_name: portal
    build: .
    networks:
      portalnet:
        ipv4_address: SERVER_IP_ADD
    ports:
      - "3000:3000"
    environment:
      - NODE_ENV=production
      - DATABASE_URL=sqlserver://SERVER_IP_ADD:1433;database=MY_DB;user=sa;password=Pa{$}{$}w0rd;trustServerCertificate=true;poolTimeout=60
    restart: always

networks:
    portalnet:
      driver: bridge
      ipam:
        driver: default
        config:
          - subnet: 10.86.240.0/24
            gateway: 10.86.240.1
#prisma.schema
generator client {
  provider = "prisma-client-js"
  output   = "../src/app/generated/prisma"
}

datasource db {
  provider = "sqlserver"
  url      = env("DATABASE_URL")
}

I want my container to connect to an external database so I first create networks:... and add it in my web but after building and running the container I got an error of Can't reach database server at SERVER_IP_ADD:1433. So I tried to remove the networks:.. but I got an error of Error opening a TLS connection: The TLS settings didn't allow the connection to be established. Please review your connection string.. Can anyone point out my mistake? Is it in Dockerfile or docker-compose? my application is working fine when I run npm run dev

2
  • You almost never need networks:, and manually specifying IP addresses in Docker is rarely if ever necessary. I don't see the database listed in your Compose setup; where is it actually running? If the connection is over TLS, do you need to use a host name (not an IP address) that matches the server certificate? Do you need to load a custom CA certificate? Commented May 13 at 10:58
  • Hi Sir @DavidMaze i did not include my database in my compose setup because my database is in a remote server. I am trying to establish a connection to my external database but prisma can't connect to it Commented May 14 at 1:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.