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

pedrootaHOBBY

a year 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

a year ago

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


pedrootaHOBBY

a year 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


a year ago

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


pedrootaHOBBY

a year ago

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

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


a year ago

Right but how does that relate to .env files?


pedrootaHOBBY

a year 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?


a year ago

Are you using a Dockerfile or nixpacks?


pedrootaHOBBY

a year ago

Dockerfile.


a year ago

Send it please


pedrootaHOBBY

a year 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


pedrootaHOBBY

a year 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

```


a year ago

As the docs page mentions -

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


The Docker build was successful, but it appears I cannot access my backend. - Railway Help Station