Nuno Alexandre

Node_modules folder inside a docker container is a nightmare


Today I discovered that docker is hard when it comes to having node application inside of it.

I was trying to run a gatsby project inside a docker container but every time I tried to run the container the nodemodules folder was empty, my thoughts were What is going on, docker, pls???. Therefore, I started to google around and found some strange solutions, like create a cache folder with the nodemodules and after the container runs copy the nodemodules folder into the project folder structure. I know it does the trick but, I wasn't happy with it.

Anyway, I asked a buddy of mine, that works a lot with docker, how he would handle this situation, so he explained to me the following:

When we have a volume set up on the docker-compose.yaml file, after the Dockerfile runs it will replace all the folder structure inside of the container with the one we have locally.

With that in mind we came with a solution, run the yarn install or npm install on the entrypoint command like so:

gatsby telemetry --disable
gatsby develop --host= --port=80


FROM node:14-alpine

RUN apk update && apk add build-base autoconf automake libtool pkgconfig nasm
RUN yarn global add gatsby-cli


version: "3.7"
    image: node:14-alpine
    container_name: frontend
    tty: true
      context: ./frontend
      dockerfile: ./Dockerfile
      - ./frontend:/app
    working_dir: /app
    command: ./
      - "80:80"


It is normal that you found a better solution, if so, please contact me, I would like to know more about this issue.