ReactJS + Docker-Compose ERROR | "Could not find a required file" - reactjs

I have a ReactJS application which has been dockerized. The application is built into a docker image and run on a container for development also using docker-compose tool. This works well when I use it with Docker for Windows. Below is the package.json, it is a very old reactjs scaffolding.
{
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"react": "^16.8.0",
"react-dom": "^16.8.0",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.2",
"react-listbox":"^1.2.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
],
"devDependencies": {
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.7.1"
}
}
Folder Structure of the Client Code (Approximate Idea) -
client
-- public
- index.html
-- src
- App.js
package.json
Dockerfile
# base image
FROM node:11.6.0-alpine
# set working directory
WORKDIR /usr/src/app
# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install
# start app
CMD ["npm", "start"]
docker-compose-dev.yml
version: '3.7'
services:
client:
build:
context: ./services/client
dockerfile: Dockerfile
volumes:
- './services/client:/usr/src/app' #(map) outside: inside the docker
- '/usr/src/app/node_modules'
ports:
- 3007:3000
environment:
- NODE_ENV=development
But when I try to push this docker image to a repository and then use it to deploy into a cloud container (possibly, a unix based system), the deployment fails with NPM ERR (as described below).
STDERR
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! client#0.1.0 start: `react-scripts start`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the client#0.1.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-03-11T03_53_28_432Z-debug.log
STDOUT
> client#0.1.0 start /usr/src/app
> react-scripts start
Could not find a required file.
Name: index.html
Searched in: /usr/src/app/public
It fails saying the index.html is not found when I am quite sure, the file is under /public/ which gets mounted to the dockerized image.
NOTE - I know the fact that this is not the way the reactjs code should be deployed on server. It should be through a web server like nginx. But the legacy team had little knowledge about the front end development. Please help regardless.

You are mounting client folder as a volume in the docker image. So, where ever you deploy the docker image, it will expect the volume to be mounted. Instead, you can copy the folder to docker image and make it self contained.
FROM node:11.6.0-alpine
# set working directory
WORKDIR /usr/src/app
# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY public /usr/src/app/public
COPY src /usr/src/app/src
COPY package.json /usr/src/app/package.json
RUN npm install
# start app
CMD ["npm", "start"]
your docker-compose-dev.yml will be:
version: '3.7'
services:
client:
build:
context: ./services/client
dockerfile: Dockerfile
ports:
- 3007:3000
environment:
- NODE_ENV=development

Related

Dockerizing react for production not working

I have created a Dockerfile and Docker-compose.yml for containerzing my create-react-app to production. My problem of understanding is how should I create the production build if the scripts that are needed to run the command are dev-dependencies?
Also I can`t find anything on deployment docs.
> [builder 7/7] RUN yarn build:
#13 0.507 yarn run v1.22.19
#13 0.556 $ react-scripts build
#13 0.579 /bin/sh: react-scripts: not found
#13 0.590 error Command failed with exit code 127.
#13 0.590 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
------
executor failed running [/bin/sh -c yarn build]: exit code: 127
ERROR: Service 'app' failed to build : Build failed
`docker-compose` process finished with exit code 1
package.json
{
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-i18next": "^11.17.2",
"react-router-dom": "^6.3.0",
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build", <--- THIS WILL BE USED FOR PRODUCTION
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"devDependencies": {
"react-scripts": "5.0.1", <--- THIS WILL NOT BE INSTALLED WITH --production FLAG
"tailwindcss": "^3.0.24",
"typescript": "^4.6.3"
}
}
Dockerfile
FROM node:18-alpine AS builder
ENV NODE_ENV production
# Add a work directory
WORKDIR /app
# Cache and Install dependencies
COPY package.json .
COPY yarn.lock .
RUN yarn install --production <---- THIS DOES NOT INSTALL DEV DEPENENCIES
# Copy app files
COPY . .
# Build the app
RUN yarn build <---- THIS CAUSES THE CRASHING
# ---------------------------------------------------
# Bundle static assets with nginx
FROM nginx:1.23.1-alpine AS production
ENV NODE_ENV production
# Copy built assets from builder
COPY --from=builder /app/build /usr/share/nginx/html
# Add your nginx.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Expose port
EXPOSE 80
# Start nginx
CMD ["nginx", "-g", "daemon off;"]
docker-compose.yml
version: "3.8"
services:
app:
container_name: my_app
build:
context: .
target: production
dockerfile: Dockerfile
In the final image stage, you're only COPY --from=builder the /app/build directory but not any of the node_modules tree. It doesn't matter in the initial stage if you have development or production dependencies, since they're not going to be in the final image.
Since you do need the devDependencies at this point, I'd remove the yarn install --production option. If it makes a difference to your build environment you can set NODE_ENV=production after you install the dependencies.
RUN yarn install # not --production
COPY . .
# ENV NODE_ENV=production
RUN yarn build

Publish React app to Github Pages using github Actions

I made a simple Reactjs app and now I want to publish it on gh-pages.
I followed this Facebook tutorial for deploying it and it is also getting deployed from my PC. But now instead of every time manually deploying it, I thought of using Github Actions to deploy on every push to the master branch. so I wrote the below Action.
name: gh pages publish
on:
push:
branches: master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 12
registry-url: https://registry.npmjs.org/
- name: publish package
run: |
yarn
npm run deploy
But this action fails because it requires the user to input the username and password.
> gh-pages -d build
fatal: could not read Username for 'https://github.com': No such device or address
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! timetablemanager#0.1.0 deploy: `gh-pages -d build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the timetablemanager#0.1.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/runner/.npm/_logs/2020-06-21T07_07_19_700Z-debug.log
##[error]Process completed with exit code 1.
How can I fix this?
Edit:
Here is the package.json file
{
"homepage": "http://itissandeep98.github.io/TimeTableManager/",
"name": "timetablemanager",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^4.2.4",
"#testing-library/react": "^9.3.2",
"#testing-library/user-event": "^7.1.2",
"bootstrap": "^4.5.0",
"bootstrap-social": "^5.1.1",
"font-awesome": "^4.7.0",
"gh-pages": "^3.1.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1"
},
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build",
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
According to the create react app github pages troubleshoot here, you need to do the following.
Create a new Personal Access Token
git remote set-url origin https://<user>:<token>#github.com/<user>/<repo>.
Try npm run deploy again
Also make sure everything in your package.json is correct, but this definitely looks like an authentication issue.
Putting this here for those looking to do the same as the OG:
I created and added a personal token, and also faced some more issues.
Here are the changes I ended up making on the action file:
- name: setup git authentication
run: |
git config --global user.email "<me>#<gmail>.com"
git config --global user.name "<my_name>"
git remote set-url origin https://<user>:<token>#github.com/<user>/<repo>
- name: publish
run: |
yarn
npm run deploy
for anyone else who ends up here, you can use the secrets.GITHUB_TOKEN that is available on every action, without setting it directly in the settings for your repo.
use Joey Baruch's answer above, just replace <token> with:
${{ secrets.GITHUB_TOKEN }}
P.s. stackoverflow wont let me comment yet so I had to post this as a separate answer.

"eslint: Permission denied" when deploying React app on Firebase through GitLab

I'm currently trying to setup CI through GitLab for my React app hosted on Firebase. I'm struggling to get past this point. There were some other post suggesting the use of sudo but the console told me the command was not found.
Any help would be greatly appreciated. Thanks you.
Here are my current configurations:
gitlab-ci.yml configuration file
image: node:10.15.3
cache:
paths:
- node_modules/
stages:
- build
- deploy
deploy_dev:
stage: deploy
script:
- echo "Deploying to staging environment"
- npm install -g firebase-tools
- firebase deploy --token $FIREBASE_DEPLOY_KEY --project $CI_ENVIRONMENT_NAME
environment:
name: dev
only:
- master
Package.json
{
"name": "react-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^4.4.1",
"firebase": "^6.6.2",
"firebase-functions": "^3.3.0",
"moment": "^2.24.0",
"node-sass": "^4.13.1",
"react": "^16.12.0",
"react-beautiful-dnd": "^11.0.5",
"react-dates": "^20.3.0",
"react-dom": "^16.12.0",
"react-moment": "^0.9.7",
"react-perfect-scrollbar": "^1.5.3",
"react-router-dom": "^5.1.2",
"react-scripts": "^3.3.0",
"reactstrap": "^8.2.0",
"recompose": "^0.30.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
The console error indicating permission error
$ firebase deploy -m "Pipeline $CI_PIPELINE_ID, build $CI_BUILD_ID" --non-interactive --token $FIREBASE_DEPLOY_KEY --project $CI_ENVIRONMENT_NAME
⚠ functions: package.json indicates an outdated version of firebase-functions.
Please upgrade using npm install --save firebase-functions#latest in your functions directory.
=== Deploying to 'cmd-dev-bbdc4'...
i deploying functions, hosting
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions# lint /builds/cmdc/cmd/functions
> eslint .
sh: 1: eslint: Permission denied
npm ERR! code ELIFECYCLE
npm ERR! errno 126
npm ERR! functions# lint: `eslint .`
npm ERR! Exit status 126
npm ERR!
npm ERR! Failed at the functions# lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-02-03T01_51_09_788Z-debug.log
Error: functions predeploy error: Command terminated with non-zero exit code126
ERROR: Job failed: exit code 1
So through some experimentation I was able to determine that I had to cd into the 'functions' directory and also run NPM install. I guess this was a due to a fundamental misunderstanding of how Firebase projects are structured and of node packages.
I would love to learn more so if anyone share some reading about this, it would be appreciated.
Ended up with a script that look like the following.
deploy_dev:
stage: deploy
script:
- echo "Deploying to staging environment"
- npm install -g firebase-tools #--allow-root
- npm ci #--allow-root
- cd functions # required or would throw the "eslint: not found" error
- npm ci
- cd ..
- firebase use --token $FIREBASE_DEPLOY_KEY $CI_ENVIRONMENT_NAME
- firebase deploy -m "Pipeline $CI_PIPELINE_ID, build $CI_BUILD_ID" --non-interactive --token $FIREBASE_DEPLOY_KEY
environment:
name: dev

Deploy react application in Git pages using Azure DevOps

I'm trying to deploy my react application (Just a very basic app) in git pages using Azure DevOps. This is to get my hands wet with Azure Devops.
First of all I configured my packag.json as below.
{
"name": "gitpages",
"version": "0.1.0",
"homepage": "https://senal.github.io/gitpage-react",
"private": true,
"dependencies": {
"gh-pages": "^2.0.1",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
Please pay attention to "homepage" and "deploy" script.
In my Azure DevOps, I have modify the yml file as below.
# Node.js with React
# Build a Node.js project that uses React.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
- master
pool:
vmImage: 'ubuntu-latest'
steps:
- task: NodeTool#0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
git config --global user.email "xxxx.yyyy#mymail.com"
git config --global user.name "RSF"
npm install
npm run build
npm run deploy
displayName: 'npm install and build then deploy'
However, when I run the build in DevOps I get the following error:
fatal: could not read Username for 'https://github.com': terminal prompts disabled
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! gitpages#0.1.0 deploy: `gh-pages -d build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the gitpages#0.1.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/vsts/.npm/_logs/2019-06-13T12_03_34_094Z-debug.log
##[error]Bash exited with code '1'.
Please advice what't with my steps/scripts.
Feel free to contact me should you require more info.
UPDATE: I was able to create the page by running
npm run deploy
from my terminal.
What I would like to know is, why we can't do the same thing in DevOps?
Cheers,
RSF
I had the same issue and fixed it by supplying the Github token in the --repo option. Replace npm run deploy with,
npx gh-pages --repo https://$(GITHUB_TOKEN)#github.com/org/repo.git -d dist
and add your token as a pipeline secret.

Docker stuck at npm install

I have a project that I want to dockerize. I run npm install and npm build with no problem on my computer, but it has some problems when I build with Docker.
Docker output:
Sending build context to Docker daemon 56.96MB
Step 1/7 : FROM node:12.2.0-alpine
---> f391dabf9dce
Step 2/7 : WORKDIR /app
---> Using cache
---> b50a8efbf074
Step 3/7 : ENV PATH /app/node_modules/.bin:$PATH
---> Using cache
---> 3358967a13ab
Step 4/7 : COPY package.json /app/package.json
---> Using cache
---> 851ac31a0adb
Step 5/7 : RUN npm install
---> Running in 8cc36a435cec
npm WARN deprecated core-js#1.2.7: core-js#<2.6.8 is no longer maintained. Please, upgrade to core-js#3 or at least to actual version of core-js#2.
It is stuck in this here:
Dockerfile:
# base image
FROM node:12.2.0-alpine
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /app/package.json
RUN npm install
RUN npm install react-scripts#3.0.1 -g --silent
# start app
CMD ["npm", "start"]
I have done this with other dockerfiles, but the result was the same.
package.json:
{
"name": "front",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"bootstrap": "^4.3.1",
"express": "^4.17.0",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router": "^5.0.0",
"react-router-dom": "^5.0.0",
"react-scripts": "2.1.8"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
So, this might be a temporary solution till this is properly addressed, but you can use
npm config set registry http://registry.npmjs.org/
I have used it for docker environment and it worked fine.
when ever you run docker run my-image it will run in a new container and previous container remain unused,
we can use docker build location-of-the-Dockerfile --no-cache switch to not using cached images but it didn't work for me
in my case, removing unused containers and images fixed the problem ,
remove all containers > docker rm $(docker ps -aq)
remove all images if you want >> docker image rm $(docker images -q)
I had the same issue. I just waited for 1 hour and it went to the next step.
Edit :
While building the npm install downloads all the packages and it takes time to do that, it depends on the size of your application how long will it take.

Resources