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
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
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.
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.
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.
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
However, the workaround is to use a polling mechanism:
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
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With