NextJs deploy on Fly.io

Hi ,

I am trying to deploy a simple NextJs application on Fly.io via below Dockerfile.

# Install dependencies only when needed
FROM node:14-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# Rebuild the source code only when needed
FROM node:14-alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn build

# Production image, copy all the files and run next
FROM node:14-alpine AS runner
WORKDIR /app

ENV NODE_ENV production

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

# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json

USER nextjs

EXPOSE 3000

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
ENV NEXT_TELEMETRY_DISABLED 1

CMD ["yarn", "start"]

But getting an error
Error error building: failed to solve with frontend dockerfile.v0: failed to create LLB definition: dockerfile line greater than max allowed size of 65535

So to avoid this problem I manually did docker build -t

That worked and image got created. But when trying to deploy the image using below command
flyctl deploy --image <some-name>

fly.toml

# fly.toml file generated for muddy-pond-7350 on 2021-08-12T09:52:52+05:30
app = "muddy-pond-7350"

kill_signal = "SIGINT"
kill_timeout = 5

[env]
  PORT = "3000"

[experimental]
  allowed_public_ports = []
  auto_rollback = true

[[services]]
  http_checks = []
  internal_port = 3000
  protocol = "tcp"
  script_checks = []

  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 6
    timeout = "2s"

It is failing with below error

==> Creating release
Release v1 created

You can detach the terminal anytime without stopping the deployment
Monitoring Deployment

1 desired, 1 placed, 0 healthy, 1 unhealthy
v1 failed - Failed due to unhealthy allocations - no stable job version to auto revert to
***v1 failed - Failed due to unhealthy allocations - no stable job version to auto revert to and deploying as v2

Troubleshooting guide at https://fly.io/docs/getting-started/troubleshooting/

Also as mentioned in trouble shooting guide I am not getting the port forwarding/mapping messages.

Can you please help with this? Sorry for lack of knowledge on docker in case I am missing something.

Thanks in advance for help.

What’s the output of fly logs on this app? Maybe some errors pop up there.

@joshua , I was using the wrong env config. I had not count on the awesomeness of FLY.IO. We can define env so easily in fly.toml and rest is magic. I was using Dockerfile for setting my env which I think may have caused the issue. Anyways it is resolved now.

I am in love with fly.io awesome job guys. Remote docker build is like 10X Harry Potter :smile:

1 Like