11

How can i use docker container to develop Reactjs with docker on windows ?

So far I've been able to run my app, but livereload does not work.

App/structure

  • build
  • node_module
  • public
  • src
  • docker-compose.yml
  • Dockerfile

Dockerfile

FROM node:5.11.0-slim

# Prepare app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app/

# Install dependencies
COPY package.json /usr/src/app/
RUN npm install --silent

ADD . /usr/src/app/

CMD [ "npm", "start" ]

docker-compose.yml:

version: "2"

services:
  frontend:
    container_name: "boilerplate"
    build: .
    environment:
      env_file: .env
      NODE_ENV: development
    ports:
      - '3000:3000'
    volumes:
      - .:/usr/src/app
5
  • You don't need the EXPOSE or ADD in your dockerfile since you have them in compose. What path is livereload watching? Commented Mar 23, 2017 at 14:03
  • Cool, I'll remove here :) I would like to monitor the /src Commented Mar 23, 2017 at 14:17
  • I've not used livereload but it looks like you need to set server.watch("/usr/src/app"), or alternatively use nodemon which drops in place of node and doesn't need any extra settings to do live reloading. Commented Mar 23, 2017 at 14:59
  • @ChrisTanner, with nodemon work for me :) but is a little slow :( thank very much for the answer Commented Mar 23, 2017 at 16:05
  • @BrunoReis how do you edit your code? do u use git? Commented Feb 5, 2019 at 21:04

2 Answers 2

13
+50

It's a known limitation for Windows (don't worry too much, there is a good workaround)

This is a known limitation for Docker on Windows host. Here is what the Docker's documentation says about the problem:

inotify on shared drives does not work

Currently, inotify does not work on Docker for Windows. This will become evident, for example, when an application needs to read/write to a container across a mounted drive. Instead of relying on filesystem inotify, we recommend using polling features for your framework or programming language.


The workaround

However, the workaround is to use a polling mechanism:

  • chokidar - A neat wrapper around node.js fs.watch / fs.watchFile / fsevents.
  • nodemon - Monitor for any changes in your node.js application and automatically restart the server - perfect for development.
  • webpack - If watching does not work for you, try out polling option. Watching does not work with NFS and machines in VirtualBox.
  • etc ...

Complete Docker & React setup

Just for your case I've started react-create-app in a Docker container and livereload feature works perfect. The gotcha is to enable chokidar polling by creating .env configuration file.

Here are my configurations (inspired by @haluvibe):

Dockerfile

FROM node:6.9.4

# Prepare app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app/

# Install dependencies
COPY package.json /usr/src/app/
RUN npm install --silent

ADD . /usr/src/app/

EXPOSE 3000
CMD [ "npm", "start" ]

docker-compose.yml

version: "2"

services:
  frontend:
    container_name: "boilerplate"
    build: .
    environment:
      env_file: .env
      NODE_ENV: development
    ports:
      - '3000:3000'
    volumes:
      - .:/usr/src/app

.env

CHOKIDAR_USEPOLLING=true

Scripts

  • docker-compose up -d - Start your project and it will be available at http://localhost:3000.
  • docker-compose run --rm boilerplate /bin/bash - Access your container.
Sign up to request clarification or add additional context in comments.

3 Comments

Cool! and how exactly could one use vscode with git integration, while running said container e.g. inside centos7 vm? i could ssh into this vm and use mapped volume but that gonna ruin all container goodness.
This is out of my knowledge and experience. The answer is focused on running Docker locally on your OS, in order the developers to have a reproducible development environment.
I did the configuration with nodemon and CHOKIDAR_USEPOLLING = true in .env and it worked, but my VM consumes 100% of the CPU to be analyzing the changes, any alternative solution for that?
2

some times livereload dose not work when the app is running inside a container, If the project runs inside a virtual machine such as (a Vagrant provisioned) VirtualBox, create an .env file in your project directory if it doesn’t exist, and add CHOKIDAR_USEPOLLING=true to it. This ensures that the next time you run npm start, the watcher uses the polling mode, as necessary inside a VM.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.