Admin panel in production trying to connect to http://localhost - **NODE_ENV=production during build**

System Information
  • Strapi Version: 4.7.0
  • Operating System: Alpine, docker
  • Database: Postgres 13
  • Node Version: 18.14.2
  • NPM Version: 9.5.0
  • Yarn Version: n/a

This is related to the problem in the topic Deployed strapi admin panel trying to hit localhost:1337

However I have applied the accepted solution for that topic and the problem still occurs - I have specified NODE_ENV=production during both build and deployment. There appears to be something else going on.

I have had this problem intermittently which upgrading to the last few Strapi versions. I generally loose several hours each time trying all sorts of changes until it suddenly starts working, and continues working even when I revert the changes.

Please see my Dockerfile below

# syntax=docker/dockerfile:1

FROM node:18-alpine

ARG NODE_ENV=production



ENV PATH /home/node/node_modules/.bin:$PATH

CMD ["strapi", "start"]

WORKDIR /home/node
RUN mkdir -p src/plugins/my-project/node_modules src/plugins/my-project/dist
RUN chown node.node src/plugins/my-project/node_modules src/plugins/my-project/dist
USER node

COPY package-lock.json package.json ./
RUN npm ci --omit=optional && npm cache clean --force

COPY . .

RUN cd src/plugins/my-project && npm ci --omit=dev --omit=optional && npm cache clean --force
RUN strapi build

USER root
RUN chown -R node.node src/plugins/my-project/dist
USER node
RUN cd src/plugins/my-project && npm run build


I’ve lost one day here. Silent errors occurred in webpack admin build that made my admin app to connect to localhost:1337 :confused:

You should add in ./config/server.ts/js

  1. url: env(‘PUBLIC_URL’, ‘http://localhost:1337’),
  2. in .env file: PUBLIC_URL=
  3. npm run build
1 Like