Skip to content

Instantly share code, notes, and snippets.

@przbadu
Last active May 12, 2024 15:34
Show Gist options
  • Save przbadu/4a62a5fc5f117cda1ed5dc5409bd4ac1 to your computer and use it in GitHub Desktop.
Save przbadu/4a62a5fc5f117cda1ed5dc5409bd4ac1 to your computer and use it in GitHub Desktop.
Setup Docker for React development

STEP 2: setup docker to run react app (dev and production) configuration: https://gist.github.com/przbadu/929fc2b0d5d4cd78a5efe76d37f891b6

Setup Docker for React development

Because we are using Docker, we are not going to install node, npm, create-react-app in our development machine, not even for generating create-react-app scaffold.

For this purpose I am using 2-step docker configuration:

  • In first step, we will create a simple docker container, that does only one thing, install create-react-app and we can use it to generate create-react-app scaffold.
  • And second docker configuration is project specific. It will handle nodemon, npm, development servers, etc.

Build docker image for create-react-app

Because we are not installing create-react-app locally in our development machine, Let's build new docker container for it. For this container, we are not going to write Dockerfile or docker-compose manually. because we can use this container for creating lots of create-react-app.

Create dockerfile

Dockerfile

FROM node:8.2.1-alpine
RUN npm install -g create-react-app \
                   create-react-native-app \
                   react-native-cli
RUN mkdir /app
WORKDIR /app
ADD . /app

docker-compose.yml

version: '3'
services:
  web:
    build: .
    image: react-cli
    container_name: react-cli
    volumes:
      - .:/app

NOTE: I am unable to do this without docker-compose. The problem was, the scaffold that were generated by cli were on docker container, not in our development machine. The idea is, the cleaner solution would be to build the image without docker-compose example: docker build . -t react-cli. Then we can use this image where ever we want, example: docker run react-cli create-react-app myApp.

Usage

Now using docker-compose we can generate our react application:

docker-compose run web create-react-app myApp

and setup myApp with seperate Dockerfile, Dockercompose that will actually handles project level configuration. Above Dockerfile is just for the purpose of generating create-react-app cli without having to install npm and node js in our system.

If anyone knows how to use react-cli image to generate create-react-app without using docker-compose.yml file, please let me know. Please read NOTE section above for expected result and gotcha.

Thanks

@przbadu
Copy link
Author

przbadu commented Oct 29, 2020

Hi there, sorry for the confusion, yes we need separate configuration for second part, and here is the second part that I prepared to avoid further confusion:

https://gist.github.com/przbadu/929fc2b0d5d4cd78a5efe76d37f891b6

@PierreClouet
Copy link

Hi,

So if we need a some npm packages, we always need to run the docker container to access npm ? For exemple :

$ docker run --mount type=bind,source="$(pwd)",target=/app react-cli npm i --save-dev babel-core ?

@khalifeserge
Copy link

This is what I use to avoid having the created folder being owned by root
docker run --rm -v "$(pwd)":/app --user "$(id -u)":"$(id -g)" react/cli create-react-app my-react-app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment