7

I have been working with Strapi and Gatsby through the command line to get a website and CMS up and running, similar to this set up - https://strapi.io/blog/building-a-static-website-using-gatsby-and-strapi. All works well and I'm ready to move onto the next step and place the project into Docker to deploy.

Getting Strapi and the Mongo DB instance up and running in Docker has been very simple, pretty much identical to this set up - https://strapi.io/documentation/3.0.0-beta.x/installation/docker.html.

Problems arise when I try to get my Gatsby installation into Docker. I have tried a mountain of different configuration settings in the docker-compose.yml and Dockerfile but I keep running into the error of

ERROR #11321 PLUGIN

"gatsby-source-strapi" threw an error while running the sourceNodes lifecycle: connect ECONNREFUSED 127.0.0.1:1337

Error: connect ECONNREFUSED 127.0.0.1:1337

The Strapi and Mongo services run fine from the same docker-compose.yml file, as can be seen below

version: "3"
services:
  strapi:
    image: strapi/strapi
    environment:
      DATABASE_CLIENT: mongo
      DATABASE_NAME: strapi
      DATABASE_HOST: mongo
      DATABASE_PORT: 27017
      DATABASE_USERNAME: strapi
      DATABASE_PASSWORD: strapi
    links:
      - mongo:mongo
    volumes:
      - ./cms:/srv/cms
    ports:
      - "1337:1337"

  mongo:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: strapi
      MONGO_INITDB_ROOT_PASSWORD: strapi
    volumes:
      - ./db/db:/db/db
    ports:
      - "27017:27017"

  gatsby:
    image: docker-gatsby-client
    build: ./website
    command: gatsby develop -H 0.0.0.0
    volumes:
      - ./website:/usr/src/app/website
      - /usr/src/app/website/node_modules
    ports:
      - "8000:8000"
    depends_on:
      - strapi
      - mongo
    environment:
      GATSBY_WEBPACK_PUBLICPATH: /
      STRAPI_URL: strapi:1337

My Dockerfile looks like this

FROM node:12

WORKDIR /usr/src/app/website

COPY package*.json ./

RUN npm install

RUN npm install -g gatsby-cli

RUN npm install --save gatsby-source-strapi

COPY . .

EXPOSE 8000

CMD ["yarn", "develop", "-H", "0.0.0.0" ]

I have tried lots of different settings in the compose file, like not using volumes, and also the Dockerfile, Like using different node versions. None of them work and all return the same error above.

If I run the Gatsby service from the command line, it connects to the Strapi service from the Docker container and all is good with the world. That makes me think there is a setting not set right with the Gatsby service in Docker which is producing the connection error.

Anyone have any ideas?

TGMNF
  • 161
  • 1
  • 4

1 Answers1

9

After going through the logs for the Gatsby container, I replaced my completed website with a plain Hello World instance so I could cross that off the list or potential obstacles. Gatsby proceeded to spin up successfully.

Changing some content in the gatsby-config.js file to contain data relating to the api resulted in the connection errors returning. Changing the apiURL value in the file from http://localhost:1337 to http://0.0.0.0:1337 didn't change anything.

A search for cross container connection issues on a Mac resulted in a lead which ended up working for me.

This question - https://github.com/docker/for-mac/issues/2705 - held the key. Changing the apiURL value to http://docker.for.mac.localhost:1337 allowed the APIs to connect between Strapi and Gatsby. The value didn't sit well with me as some colleagues use PCs so that didn't seem right. Further digging unearthed the apiURL value of http://host.docker.internal:1337 which worked. I'll test all this on the production build compared to the development build and see how it all holds up.

TGMNF
  • 161
  • 1
  • 4