Chen Guevara
Chen Guevara

Reputation: 344

Docker image working with docker run but not with docker-compose up

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

Upvotes: 4

Views: 2911

Answers (1)

matiferrigno
matiferrigno

Reputation: 950

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/

Upvotes: 3

Related Questions