Dockerizing strapi/next.js corporate starter: Next.js can’t access Strapi on build

  docker, next.js, node.js, strapi

I’m trying to dockerize strapi cms corporate starter.

Locally, when installed with npx, both backend/frontend communicate without a problem.

When I run docker-compose build --no-cache --force-rm the Strapi container builds sucessfully, but the Next container fails with the following error:

17 476.4 info – Compiled successfully 17 476.4 info – Collecting
page data…
17 482.2 Warning: You have opted-out of Automatic Static Optimization due to getInitialProps in pages/_app. This does not
opt-out pages with getStaticProps
17 485.0 api url: http://strapi:1337/pages?_locale=en ← console.log at getStrapiURL()
17 485.0 api url: http://strapi:1337/pages?_locale=fr ← console.log at getStrapiURL()
17 490.1 17 490.1 > Build error occurred
17 490.1 FetchError: request to http://strapi:1337/pages?_locale=en failed,
reason: getaddrinfo EAI_AGAIN strapi

17 490.1 at ClientRequest. (/app/node_modules/node-fetch/lib/index.js:1461:11)
17 490.1 at ClientRequest.emit (node:events:394:28)
17 490.1 at Socket.socketErrorListener (node:_http_client:447:9)
17 490.1 at Socket.emit (node:events:394:28)
17 490.1 at emitErrorNT (node:internal/streams/destroy:193:8)
17 490.1 at emitErrorCloseNT (node:internal/streams/destroy:158:3)
17 490.1 at processTicksAndRejections (node:internal/process/task_queues:83:21) {
17 490.1 type: ‘system’,
17 490.1 errno: ‘EAI_AGAIN’,
17 490.1 code: ‘EAI_AGAIN’
17 490.1 }
17 490.2 error Command failed with exit
code 1.
—— executor failed running [/bin/sh -c yarn build && yarn install –production –ignore-scripts –prefer-offline]: exit code: 1

What am I doing wrong?

In my next.config.js I have such a config:

module.exports = {
  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'
  }
}

The next.js template uses getStrapiURL function to construct the api url:

import getConfig from 'next/config';
const { serverRuntimeConfig, publicRuntimeConfig } = getConfig();
const API_URI = serverRuntimeConfig.URI || publicRuntimeConfig.URI;

export function getStrapiURL(path) {
  console.log(`api url: ${API_URI}${path}`)
  return `${API_URI}${path}`
}

Frontend / backend Dockerfiles, docker-compose.yml can be found here


System information:

  • Strapi Version: 3.6.3

  • Operating System: Windows 10 / Ubuntu 20.04 LTS (via WLS2)

  • Database: sqlite

  • Node Version: 14.17.1

  • Yarn Version: 1.22.5

Source: Docker Questions

LEAVE A COMMENT