1

I have a gatsby docker image which works as expected when run with docker run, but when I run it with docker-compose up I get the following error:

There was a problem loading the local develop command. Gatsby may not be installed in your site's "node_modules" directory. Perhaps you need to run "npm install"? You might need to delete your "package-lock.json" as well. 

My Dockerfile looks like this:

FROM node:12-buster

RUN npm install --global gatsby-cli && gatsby telemetry --disable

WORKDIR /app

COPY package*.json /app/

RUN npm install --force

COPY . .

EXPOSE 8000

CMD ["npm", "run", "develop"]

The compose file looks like this:

   frontend:
    build: frontend
    image: frontend
    volumes:
      - ./frontend:/app
    ports:
      - "8000:8000"
    depends_on:
      - backend
Chen Guevara
  • 194
  • 1
  • 2
  • 9

1 Answers1

2

The image will work fine when the code is in the context of build, because all commands RUN will be executed during docker build image process.

When you instance a image as you do on docker-compose, you are not running:

RUN npm install --force

because it was executed during build image time. No during launch container.

So, for solve your problem and considering how your image was built you need not to include a volume instead include your code as build context.

version: "3.7"
services:
  webapp:
    build:
      context: ./dir
      dockerfile: Dockerfile

https://docs.docker.com/compose/compose-file/

matiferrigno
  • 601
  • 5
  • 8
  • Being brief, remove volume from you docker-compose and only include your code in context. – matiferrigno Feb 29 '20 at 13:19
  • Another solution is to add a data volume as explained here: https://stackoverflow.com/questions/30043872/docker-compose-node-modules-not-present-in-a-volume-after-npm-install-succeeds. So adding `/app/node_modules` to the volumes also works. – Chen Guevara Feb 29 '20 at 13:26
  • Exactly, it is because the npm_modules has already installed and you don't need npm install. It breaks a little the docker filosofy of "reborn" but is ok for agilize local development. – matiferrigno Feb 29 '20 at 13:28
  • Adding `node_modules` as an anonymous volume causes Docker to consider that directory user data; you will never ever get any updates to that directory. Removing the `volumes:` entirely as suggested here and using the code actually built into the image is better practice. – David Maze Feb 29 '20 at 20:52