React App on Fargate is ignoring env variable - reactjs

I deployed my React app and backend using Fargate with load balancers, I noticed my React app is using the wrong endpoint to access my API, it seems to be using the it's load balancer's DNS name as it's endpoint even though this endpoint is configured by using environment variables (On a React App, so this would be REACT_APP_API_URL).
On my local machine, this container works fine and is able to connect to my backend. Here it shows the correct endpoint.
If I were to run this same exact image in Fargate, the wrong endpoint is shown, this is actually the URL to visit my web app.
And I configured the correct endpoint in my task definition like so, it matches the correct endpoint shown in figure 1. I also made sure the service is using the latest task definition with this endpoint.
If needed this is my docker file.
# The first image is for compiling the client files, the second is for serving.
# BUILD IMAGE
FROM node:14-alpine as build-stage
WORKDIR /app
# Install dependencies
COPY package*.json ./
RUN npm install
RUN npm install cross-env -g
RUN npm install npm install #craco/craco -g
# Build
COPY . .
RUN npm run build
# -----------------------------------------------------------------------------
# SERVING IMAGE
FROM fitiavana07/nginx-react
# Copy built files
COPY --from=build-stage /app/build /usr/share/nginx/html
# 80 for HTTP
EXPOSE 80
# Run nginx
CMD nginx -g 'daemon off;'

Turns out I'm an idiot lol, you need a .env file at the root of your project, this means you can't pass environment variables to be docker image. I managed to overcome this by creating .env on my docker file like so.
# The first image is for compiling the client files, the second is for serving.
# BUILD IMAGE
FROM node:14-alpine as build-stage
WORKDIR /app
# Install dependencies
COPY package*.json ./
RUN npm install
RUN npm install cross-env -g
RUN npm install npm install #craco/craco -g
# Build
COPY . .
# Create a .env for react app
# Define a required build arg
ARG api_url
# Create a .env file
RUN touch .env
# Write the api url into the env file
RUN echo REACT_APP_API_URL=${api_url} > .env
RUN cat .env
RUN npm run build
# -----------------------------------------------------------------------------
# SERVING IMAGE
FROM fitiavana07/nginx-react
# Copy built files
COPY --from=build-stage /app/build /usr/share/nginx/html
# 80 for HTTP
EXPOSE 80
# Run nginx
CMD nginx -g 'daemon off;'
On my GitHub Actions, I use this action to dynamically set this build argument

Related

Next js app (running inside a docker container) fails to load static file images located in public dir

I am trying to run a next js app locally inside a docker file. When I run the container, everything works as expected with the exception of my image files failing to render on the page. Inspection via the developer tools indicates a failed network request for those images (no 4XX code is indicated). The failed request looks as follows:
When I build npm run build and run the app locally npm run start, I see this same request successfully run. Same success story when I run in development mode npm run dev.
Here is the section of code utilizing the next Image module. import Image from "next/image";
<Image
src="/images/computerStation.png"
alt=""
height={300}
width={600}
/>
And my public directory tree:
root
│
└───public
│
└───images
│
└───computerStation.png
Given my local build/dev-env success, my thought is that I am doing something wrong with my docker file. I pretty much just ripped this off from the Next js docs and tweaked it to run with npm instead of yarn. See Dockerfile below:
# Install dependencies only when needed
FROM node:alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install --frozen-lockfile
# Rebuild the source code only when needed
FROM node:alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN npm run build
# Production image, copy all the files and run next
FROM node:alpine AS runner
WORKDIR /app
ENV NODE_ENV production
# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
RUN chown -R nextjs:nodejs /app/.next
USER nextjs
EXPOSE 3000
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
RUN npx next telemetry disable
CMD ["node_modules/.bin/next", "start"]
Any help on this would be greatly appreciated. Thanks!
You need to check your docker Node version, which should be
FROM node:14-alpine AS deps
FROM node:14-alpine AS builder
FROM node:14-alpine AS runner
Next.js has some problems, in Node 16 (node:alpine).

npm run tests in docker with travis CI

I am deploying a react app to Heroku via TravisCI. The fact that I'm using Heroku doesn't really affect what I'm about to ask, I'm pretty sure, it's just there for context. Travis successfully deploys the app until I add a testing step (the script section) in .travis.yml:
language: generic
sudo: required
services:
- docker
before_install:
- docker build -t myapp:prod -f Dockerfile.prod .
script:
- docker run -e CI=true myapp:prod npm run test
after_success:
- docker build -t myapp:prod -f Dockerfile.prod .
- echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_ID" --password-stdin
- docker push myapp:prod
deploy:
provider: heroku
app: myapp
skip_cleanup: true
api_key:
secure: <my_key>
However, my Dockerfile.prod is a multi-stage node + nginx where the nginx stage doesn't keep any node or npm stuff:
# build environment
FROM node:13.12.0-alpine as builder
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install app dependencies
COPY package.json ./
COPY package-lock.json ./
# some CI stuff I guess
RUN npm ci
RUN npm install react-scripts#3.4.1 -g --silent
COPY . ./
RUN npm run build
# production environment
FROM nginx:stable-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
# If using React Router
COPY --from=builder /app/build /usr/share/nginx/html
# For Heroku
CMD sed -i -e 's/$PORT/'"$PORT"'/g' /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'
Therefore, it is my understanding that .travis.yml tries to run that npm run test command inside my nginx container and can't execute npm commands (no node installed, right?). So guided by SO answers such as this one I started adding commands into that nginx stage such as
COPY package.json ./
COPY package-lock.json ./
RUN apk add --update npm
but I realized I might be approaching this the wrong way. Should I perhaps be adding npm through Travis? That is, should I include in .travis.yml in the scripts section something like docker run -e CI=true myapp:prod apk add --update npm and whatever else is necessary? This would result in a smaller nginx image no? However, would I run into problems with package.json from the node stage in Dockerfile.prod or anything like that?
In summary, to use TravisCI to test a dockerized react app served with nginx, at what point should I install npm into my image? Does it happen as part of script in .travis.yml or does it happen in Dockerfile.prod? If it is recommened to npm run tests inside Dockerfile.prod, would I do that in the first stage (node) or the second (nginx)?
Thanks
EDIT: Not sure if this can be considered solved, but a user on Reddit recommended to simply RUN npm run test right before the RUN npm run build.

Running a react app in docker : react app in docker fails to run

I am trying to run a react app in a docker image however it exits without an error message
DokerFile
# pull official base image
FROM node:13.12.0-alpine
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install
# add app
COPY . ./
# start app
CMD npm start --port 3000
then I proceeded to build
docker build -t react-app:latest .
then I run
docker run -p 7000:3000 react-app:latest
gives the following out put
then exits out
this is what I see on the browser
Your docker closes because the tty is not enabled.
In order to work, you have to run the docker with
docker run -t -p 7000:3000 react-app:latest
For more info: https://github.com/facebook/create-react-app/issues/8688
But this should be only for testing/development. In production you should build your react app and then serve it with serve or with nginx

Docker COPY does not copy certain files in application

I have an Angular application with the following Dockerfile
FROM node:8
ENV APP_HOME /app
RUN mkdir -pv $APP_HOME
WORKDIR $APP_HOME
ENV NODE_ENV production
ENV NPM_CONFIG_LOGLEVEL warn
ENV CUSTOM_REGISTRY https://registry.npmjs.org/
RUN npm config set strict-ssl false
RUN npm config set registry $CUSTOM_REGISTRY
COPY . $APP_HOME
RUN npm install -g #angular/cli
RUN npm install
As you can see, in the copy command, we copy the current directory to the working directory of the container, however, after I SSH into the container to check if all the files were copied, I see that files which have two . in them are not copied.
Some examples are -
.angular-cli.json
.travis.yml
How can I move these files over to the container, the .angular-cli.json is specially critical for me to make the application run.

Dockerfile - npm: not found

I want to create a development environment for a reactjs application. I am new to Docker and have been trying to create an environment using Docker. Below is my Dockerfile code.
# Base node image
FROM node
# create working directory
ADD ./code /usr/src/app
WORKDIR /usr/src/app
# add node_modules path to environment
ENV PATH /usr/src/app/node_modules/.bin:PATH
# copy and install dependencies
COPY ./code/package.json /usr/src/app/package.json
RUN npm install --quiet
RUN npm install react-scripts#1.1.1 -g --silent
# start app
# CMD ["npm","start"]
However, I am getting the error "npm: not found" at line RUN npm install --quiet.
I confirm that node comes with npm:
$ docker run -it --rm node /bin/bash
root#b35e1a6d68f8:/# npm --version
5.6.0
But the line
ENV PATH /usr/src/app/node_modules/.bin:PATH
overwrites the initial PATH, so you should try replacing it with
ENV PATH /usr/src/app/node_modules/.bin:${PATH}
Also, note that your ADD ./code ... line is clumsy, because it would add all the files of your application (including ./code/package.json!) and this step comes too early (w.r.t. Docker's cache mechanism), so I'd suggest to simply remove that line ADD ./code /usr/src/app and add a line COPY ./code ./ after the RUN npm install ...
Finally you may also want to take a look at the official documentation for "dockerizing" a Node.js app: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/

Resources