diff --git a/.gitignore b/.gitignore index 02c7449e..020b4db5 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,9 @@ /my-app/node_modules /api/node_modules +*COMPOSE.png* + + # profiling files chrome-profiler-events*.json speed-measure-plugin*.json diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 8fa0e038..00000000 --- a/Dockerfile +++ /dev/null @@ -1,15 +0,0 @@ -FROM node:10 AS ui-build -WORKDIR /usr/src/app -COPY my-app/ ./my-app/ -RUN cd my-app && npm install && npm run build - -FROM node:10 AS server-build -WORKDIR /root/ -COPY --from=ui-build /usr/src/app/my-app/build ./my-app/build -COPY api/package*.json ./api/ -RUN cd api && npm install -COPY api/server.js ./api/ - -EXPOSE 80 - -CMD ["node", "./api/server.js"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 00000000..9b6b8e80 --- /dev/null +++ b/README.md @@ -0,0 +1,94 @@ +# react-nodejs-example + +This is an application which contains a connected nodejs backend to a react frontend + +## Available Scripts + +In the project directory, you can run: + +### `npm start` + +Runs the app in the development mode.\ +Open [http://localhost:8080](http://localhost:8080) to view it in your browser. + +The page will reload when you make changes.\ +You may also see any lint errors in the console. + +### `npm test` + +Launches the test runner in the interactive watch mode.\ +See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. + +### `npm run build` + +Builds the app for production to the `build` folder.\ +It correctly bundles React in production mode and optimizes the build for the best performance. + +The build is minified and the filenames include the hashes.\ +Your app is ready to be deployed! + +See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. + +### `npm run eject` + +**Note: this is a one-way operation. Once you `eject`, you can't go back!** + +If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. + +Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own. + +You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it. + +# Docker + +![Docker](https://jeddict.github.io/tutorial/Docker/DOCKER.png) + +Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. + +# Dockerfile + +A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image + +# Docker Compose + +![Docker Compose](https://jeddict.github.io/tutorial/Docker/COMPOSE.png "Docker Compose Logo") + + +Docker Compose is a tool for running multi-container applications on Docker +defined using the [Compose file format](https://compose-spec.io). +A Compose file is used to define how one or more containers that make up +your application are configured. +Once you have a Compose file, you can create and start your application with a +single command: `docker compose up`. + + + +Quick Start +----------- + +Using Docker Compose is a three-step process: +1. Define your app's environment with a `Dockerfile` so it can be + reproduced anywhere. +2. Define the services that make up your app in `docker-compose.yml` so + they can be run together in an isolated environment. +3. Lastly, run `docker compose up` and Compose will start and run your entire + app. + +A Compose file looks like this: + +```yaml +services: + web: + build: . + ports: + - "5000:5000" + volumes: + - .:/code + redis: + image: nginx +``` + +Contributing +------------ + +Dont hesitate to create a pull request diff --git a/api/Dockerfile b/api/Dockerfile new file mode 100644 index 00000000..27ee10a1 --- /dev/null +++ b/api/Dockerfile @@ -0,0 +1,16 @@ +FROM node:14-alpine + +WORKDIR /back + +# add '/app/node_modules/.bin' to $PATH +# ENV PATH /app/node_modules/.bin:$PATH +# install application dependencies +COPY package*.json ./ +RUN npm install +# RUN npm install react-scripts -g + +# copy app files +COPY . . + +EXPOSE 4000 +CMD ["npm", "start"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..2e5fd53c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,20 @@ +version: "3.8" +services: + web: + build: ./my-app + depends_on: + - api + ports: + - "3003:3000" + stdin_open: true + networks: + - network-backend + api: + build: ./api + ports: + - "4004:4000" + networks: + - network-backend + +networks: + network-backend: diff --git a/docker.stack.yml b/docker.stack.yml new file mode 100644 index 00000000..b4d438b5 --- /dev/null +++ b/docker.stack.yml @@ -0,0 +1,28 @@ +version : "3.8" +services: + my-app: + image: + deploy: + mode: replicated + replicas: 2 + restart_policy: + condition: on-failure + # To place in particular node + # placement: + # constraints: [node.hostname == ] + ports: + - "3000:3000" + api: + image: + deploy: + mode: replicated + replicas: 2 + restart_policy: + condition: on-failure + # To place in particular node + # placement: + # constraints: [node.hostname == ] + ports: + - "4000:4000" + volumes: + - "/var/run/docker.sock:/var/run/docker.sock" diff --git a/my-app/Dockerfile b/my-app/Dockerfile new file mode 100644 index 00000000..d0aaf69f --- /dev/null +++ b/my-app/Dockerfile @@ -0,0 +1,15 @@ +FROM node:14-alpine + +WORKDIR /sample + +COPY package*.json ./ + +RUN npm install + +COPY . . + +RUN sed -i 's/localhost/api/g' package.json + +EXPOSE 3000 + +CMD ["npm", "start"] diff --git a/my-app/package.json b/my-app/package.json index 29c0a4e2..7d727315 100644 --- a/my-app/package.json +++ b/my-app/package.json @@ -13,7 +13,7 @@ "react-scripts": "3.4.1" }, "scripts": { - "start": "react-scripts start --port 3001", + "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject"