Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJs development on docker container

Tags:

docker

reactjs

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
like image 830
Bruno Reis Avatar asked Mar 23 '17 12:03

Bruno Reis


People also ask

Can I use docker With React?

Docker React eliminates the need to manage host systems and instead lets us specify a Docker file that then lists all the dependencies of the project. Docker React builds images of all of these dependencies that can be run and reused by multiple computers.

Is docker useful for frontend?

Docker is a great tool that helps developers build, deploy, and run applications more efficiently in a standardized way. For frontend applications, we only need the Docker image for local development, because we deploy it to a static hosting provider.

Is docker good for development?

Docker is great at setting up a local development environment because it easily adds the running process without duplicating the virtualized resource. Second, it's more modular. Docker makes it easy to run multiple versions or instances of the same program without configuration headaches and port collisions.


1 Answers

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.

  • Workaround for nodemon and Node.js - If you are using nodemon with Node.js, try the fallback polling mode described here: nodemon isn’t restarting node applications
  • Docker for Windows issue on GitHub - See the issue Inotify on shared drives does not work

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.
like image 62
Jordan Enev Avatar answered Sep 22 '22 02:09

Jordan Enev