Nuno Alexandre

Node_modules folder inside a docker container is a nightmare

#docker
#gatsby
#node_modules

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:

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

(entrypoint.sh)

FROM node:14-alpine

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

(Dockerfile)

version: "3.7"
services:
  frontend:
    image: node:14-alpine
    container_name: frontend
    tty: true
    build:
      context: ./frontend
      dockerfile: ./Dockerfile
    volumes:
      - ./frontend:/app
    working_dir: /app
    command: ./entrypoint.sh
    ports:
      - "80:80"

(docker-compose.yaml)

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