Operating System: Windows 10 / Ubuntu 20.04 LTS (via WSL2)
Database: sqlite
Node Version: 14.17.1
Yarn Version: 1.22.5
Hello evryone!
I’m trying to dockerize a corporate starter (strapi/next.js).
Locally, installed with npx, it runs without a problem.
When I run docker-compose build (on ubuntu / wsl2) I get strapi image built but next.js build fails:
#17 118.2 > Build error occurred #17 118.2 FetchError: request to http://localhost:1337/pages?_locale=en failed, reason: connect ECONNREFUSED 127.0.0.1:1337 #17 118.2 at ClientRequest. (/app/node_modules/node-fetch/lib/index.js:1461:11) #17 118.2 at ClientRequest.emit (node:events:394:28) #17 118.2 at Socket.socketErrorListener (node:_http_client:447:9) #17 118.2 at Socket.emit (node:events:394:28) #17 118.2 at emitErrorNT (node:internal/streams/destroy:193:8) #17 118.2 at emitErrorCloseNT (node:internal/streams/destroy:158:3) #17 118.2 at processTicksAndRejections (node:internal/process/task_queues:83:21) { #17 118.2 type: ‘system’, #17 118.2 errno: ‘ECONNREFUSED’, #17 118.2 code: ‘ECONNREFUSED’ #17 118.2 } #17 118.2 error Command failed with exit code 1.
What am I missing here? Should I have defined some env variable?
Can I build images like that given next.js needs accessing pages endpoint during its build?
127.0.0.1 is local to each container service. As both services are in the same docker network, you can access the services use their hostnames. So to access the “strapi” service from the “next” service, you need to use hostname “strapi” instead of “127.0.0.1”.
Change your next config to access strapi on “strapi:1337”.
module.exports = {
i18n: {
locales: ["en", "fr"],
defaultLocale: "en",
},
serverRuntimeConfig: {
// Will only be available on the server side
URI: 'http://strapi:1337'
},
publicRuntimeConfig: {
// Will be available on both server and client
URI: 'http://localhost:1337'
}
}
Did you manage to find a solution? I am having the same issue.
I have an nginx reverse proxy plus nextjs and strapi. The nginx container can access the other containers but the nextjs app is having trouble during build time with getstaticprops/getstaticpaths.
I get the exact same error message.
Tried referencing the strapi container by it’s internal ip within docker but that didn’t work unfortunately.
This will spin up a strapi instance with a postgres database.
You need to create an .env or use the one in strapi already.
From here you can build one just for the frontend, note the use of depends_on which tells docker it will wait for it to start.
Dockerfile
FROM node:14-alpine
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /opt/
COPY ./package.json ./
COPY ./yarn.lock ./
ENV PATH /opt/node_modules/.bin:$PATH
RUN yarn config set network-timeout 600000 -g
RUN yarn install
WORKDIR /opt/app
COPY ./ .
RUN yarn build
EXPOSE 1337
CMD ["yarn", "start"]
Both files needs to be be in the root of the strapi project.
Then you can use 0.0.0.0 or localhost as normal and no need to say http://strapi:1337 as it runs on localhost.
If this is running on a server then you use the IP or domain to the server.