GithubHelp home page GithubHelp logo

doc22940 / node-react-docker-compose Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mrcoles/node-react-docker-compose

1.0 1.0 0.0 491 KB

A reference for running a node server and create-react-app app via docker-compose.

JavaScript 70.33% HTML 17.32% CSS 4.92% Dockerfile 7.43%

node-react-docker-compose's Introduction

Node + Create React App + Docker Compose

A project that runs a Node server and a create-react-app app via two separate containers, using Docker Compose.

Development

docker-compose up

For development, the server/ and client/ directories have their own docker containers, which are configured via the docker-compose.yml file.

The client server is spun up at localhost:3000 and it proxies internally to the server using the linked name as server:8080.

The local directories are mounted into the containers, so changes will reflect immediately. However, changes to package.json will likely need to a rebuild: docker-compose down && docker-compose build && docker-compose up.

Notes

Adding new scss files

In a previous version of this, you needed to restart the client for new scss files to be recognized by the watch command. This may have changed (TODO: test if this still matters with react-scripts updates):

docker-compose restart client

Installing npm dependencies

All changes to node_modules should happen inside the containers. Install any new dependencies by inside the container. You can do this via docker-compose run, but it’s easier to just upadte a running container and avoid having to rebuild everything:

docker-compose exec client

Then inside:

npm install --save <new_dependency>

Production

docker-compose -f docker-compose.prod.yml up

For production, this uses the Dockerfile at the root of the repo. It creates a static build of the client React app and runs Express inside server, which handles both the API and serving of React files.

As a result, different code is executing to serve the React files, but all of the API calls should remain the same. The difference between development and production isn’t ideal, but it does offer the simplicity of having the entire app run in one server on one machine.

This is one of multiple ways a Node + React app could be setup, as suggested here:

  • Keep them together - have Express serve both the API and React files
  • Split them apart - have Express API on one machine and the React files on another (e.g., on S3 and use CORS to access the API)
  • Put the API behind a proxy - use something like NGINX to proxy the Express API server and React static files separately

This project uses the “keep them together” approach. For better performance, you can set up a proxy (like Cloudflare) in between your server and the Internet to cache the static files. Or with some extra work you can fashion it to do either of the other two options.

Notes

Using docker compose

I have comp aliased to docker-compose on my computer.

Start via:

comp up

# or detached
comp up -d

Run a container of the server image via:

comp run server /bin/bash

Check status:

comp ps

Stop:

comp down

Run the production image:

comp -f docker-compose.prod.yml up

NOTE: if any dependencies change in package.json files, you probably will need to rebuild the container for the changes to appear, e.g.,

comp down
comp build
comp up

Setup references

References for setting up a Node project with Docker and docker-compose:

Express + React:

node-react-docker-compose's People

Contributors

mrcoles avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.