The Docker build was successful, but it appears I cannot access my backend.
pedroota
HOBBYOP

2 years ago

The Docker build was successful, but when I access the application, it seems I cannot connect to my backend due to a network error. This application is built with Next.js 14 (App Router). When I try to fetch data, it uses the URL endpoint from my frontend (e.g., https://mysite.com) instead of the backend endpoint provided in my .env file (Railway Environment Variables).

13 Replies

2 years ago

Please do not use or include .env files in your repo, use service variables.


pedroota
HOBBYOP

2 years ago

Please do not use or include .env files in your repo, use service variables.

That's exactly what I did, I'm using Railway Environment Variables. I did not included any .env on my repo


2 years ago

May I asked why you mentioned an .env file then?


pedroota
HOBBYOP

2 years ago

May I asked why you mentioned an .env file then?

Because seems like the docker container is not using the Railway Environment Variables


2 years ago

Right but how does that relate to .env files?


pedroota
HOBBYOP

2 years ago

Right but how does that relate to .env files?

Bro...I'm having an issue with my project. The production build, created with Docker, can't access my backend. The backend URL is provided in the Railway environment variables. Do you know how to solve this?


2 years ago

Are you using a Dockerfile or nixpacks?


pedroota
HOBBYOP

2 years ago

Dockerfile.


2 years ago

Send it please


pedroota
HOBBYOP

2 years ago

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* ./
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 --frozen-lockfile; \
  else echo "Lockfile not found." && exit 1; \
  fi


FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .


RUN \
  if [ -f yarn.lock ]; then yarn run build; \
  elif [ -f package-lock.json ]; then npm run build; \
  elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
  else echo "Lockfile not found." && exit 1; \
  fi

FROM base AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

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

RUN mkdir .next
RUN chown nextjs:nodejs .next

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

USER nextjs

EXPOSE 3000

ENV PORT 3000

CMD HOSTNAME="0.0.0.0" node server.js


pedroota
HOBBYOP

2 years ago

It's not working yet!My Dockerfile:```

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* ./
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 --frozen-lockfile; \
  else echo "Lockfile not found." && exit 1; \
  fi


FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .


RUN \
  if [ -f yarn.lock ]; then yarn run build; \
  elif [ -f package-lock.json ]; then npm run build; \
  elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
  else echo "Lockfile not found." && exit 1; \
  fi

FROM base AS runner
WORKDIR /app

ARG AUTH_SECRET
ARG AUTH_TRUST_SOURCE
ARG NEXT_PUBLIC_NODE_ENV
ARG NEXT_PUBLIC_API_URL
ARG NEXT_PUBLIC_POSTHOG_KEY
ARG NEXT_PUBLIC_POSTHOG_HOST

ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

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

RUN mkdir .next
RUN chown nextjs:nodejs .next

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

USER nextjs

EXPOSE 3000

ENV PORT 3000

CMD HOSTNAME="0.0.0.0" node server.js

```


2 years ago

As the docs page mentions -

Be sure to declare your environment variables in the stage they are required in


Loading...