How can I speed up node.js react startup in a Docker container

Viewed 5243

I am running node js official image inside Docker container and I noticed that the npm start command takes a lot longer to start than when it's outside of Docker.

Are there settings that I can change to make it run faster? Perhaps allocating more memory to the container?

For reference I will paste relevant files below.

Dockerfile:

FROM node:8.1

WORKDIR var/www/app

# Global install yarn package manager
RUN apt-get update && apt-get install -y curl apt-transport-https && \
    curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
    apt-get update && apt-get install -y yarn

RUN npm install -g create-react-app

The command I use to start my container:

docker run --rm -ti \
--link api-container:api \
--name my-container -p 3000:3000 \
-v $(pwd):/var/www/app nxmohamad/my-container \
bash

and the start script is just NODE_PATH=. react-scripts start

2 Answers

Windows

Matt's answer seems to help Mac users more-so than Windows users. If you're on Windows you should run your Docker commands in your Linux distro. It was a night and day difference for me. No messing with caching or anything. If you already have Docker Desktop installed you just have to make sure you already have a Linux distro installed. It's easy to setup if you don't.

Basically, any read/write process between Windows and Linux takes a long time. If you run your Docker container inside Windows Subsystem for Linux the file read/write is near instantaneous because the file are going from Linux to Linux. You will have to move your files from whatever directory in Windows to a directory in your Linux distro, but that shouldn't be a problem assuming you're using git.

Resources:

Article that kind of explains it

Docker documentation on setting up WSL 2 and a Linux Distro

VSCode working with WSL 2

Related