So far I have build the image and deployed it to AWS EC2. But I want to use
serve -s build
to serve the app in production mode.
I did it locally and apparently everything seems to be fine..I get this..
┌──────────────────────────────────────────────────┐
│ │
│ Serving! │
│ │
│ - Local: http://localhost:5000 │
│ - On Your Network: http://192.168.1.91:5000 │
│ │
│ Copied local address to clipboard! │
│ │
└──────────────────────────────────────────────────┘
And the page enters..but the when I try to make a request to the api I get 404.
I wanted to know how does react build works and what do I need to do to put it in production mode. And also, what is the dist folder for?
This is my Dockerfile:
FROM node
#Create app directory
WORKDIR /app
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
#Install dependencies
RUN npm install
RUN npm install react-scripts#3.4.1 -g --silent
#Copy app's source code inside the Docker image
COPY . .
#Expose the app's port
EXPOSE 3000
#Run the app when the container is ran
CMD ["npm", "start""]
Thanks!
This is my Dockerfile I use when I run it with serve -s build
# pull official base image
FROM node:13.12.0-alpine
# set working directory
WORKDIR /app
# Copies package.json and package-lock.json to Docker environment
COPY package*.json ./
# Installs all node packages
RUN npm install
# Copies everything over to Docker environment
COPY . .
# Build for production.
RUN npm run build --production
# Install `serve` to run the application.
RUN npm install -g serve
# Uses port which is used by the actual application
EXPOSE 5000
# Run application
#CMD [ "npm", "start" ]
CMD serve -s build
I just switch between npm start (debug) and serve (for production).
Related
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).
I'm deploying a dockerized React app to Heroku (via Travis CI, but I don't think that matters). Heroku recommends not running as root in dev, since in production they don't run as root, by adding
RUN adduser -D myuser
USER myuser
However, this Dockerfile, which was running fine until I added that chunk (or similar), now fails:
# 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 && npm audit fix --force
# add app
COPY . ./
### ------------------- works without this section ------------------- ###
RUN addgroup -g 1001 -S appuser && \
adduser -u 1001 -S appuser -G appuser && \
chown -R appuser ./
USER appuser
### ------------------------------------------------------------------ ###
# start app
CMD ["npm", "start"]
Docker Desktop outputs:
> react-scripts start
ℹ 「wds」: Project is running at http://172.18.0.2/
ℹ 「wds」: webpack output is served from
ℹ 「wds」: Content not from webpack is served from /app/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...
Failed to compile.
EACCES: permission denied, open '/app/.eslintcache'
It all points to issues with users and permissions but I don't know if my syntax is wrong or if this is just not what Heroku is actually recommending, but it doesn't work for me.
Still new to Docker so I don't know if this is all conceptually correct but I think I solved it.
First, I thought I got it to work by removing the .:/app volume in docker-compose.yml but then I realized that, by de-linking the host with the image, I lost React's hot reloading. So docker-compose.yml has to look like this:
version: '3.8'
services:
frontend:
container_name: poeclient
stdin_open: true
build:
context: .
dockerfile: Dockerfile
volumes:
- '.:/app' # required for hot reloading, needs Dockerfile user = local user
- '/app/node_modules'
ports:
- 3001:3000
environment:
- CHOKIDAR_USEPOLLING=true
Also, because my local user id is 1000 I have to use that id in the Dockerfile. Since that Apline image comes with a user named 'node' with that id, I don't have to create one in the Dockerfile:
# 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 && npm audit fix --force
# add app with non root owner
COPY . ./
# switch to non-root user 'node' (uid: 1000), which comes with alpine image
# switch now instead of earlier to allow for other commands to be run as root
# for react hot reloading needs to be 1000 because host user is also 1000
USER 1000
# start app
CMD ["npm", "start"]
Despite all this, the .eslintcache file still showed up in the container as belonging to root. So one last thing that was needed was, in my local folder, to chown the .eslintcache from root to my local user (1000) and then the Dockerfile was able to show an .eslintcache that belonged to 1000.
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
I am using react and when i use npm start to open a window to check my react app it gives the error:
npm update check failed │
│ Try running with sudo or get access │
│ to the local update config store via │
│ sudo chown -R $USER:$(id -gn $USER) C:\Users\nikhil\.config │
I am not getting what is the problem please help.
Aside from the quality of the question:
Try running with sudo or get access means that your current user has no access to that directory, where the node_modules are in. Use sudo, change the permission of the directory with chmod -R 644, chmod -R 755 or switch to a user who has access to the directory.
You can try this out works fine for me :
sudo rm -rf node_modules/
sudo yarn install
sudo npm start
I am getting an error when travis-ci builds my app in a docker container. The build folder is not coming down.Here is the error logs
Deploying application
Initialized empty Git repository in /tmp/d20190115-5107-
1w5c6ge/work/.git/
Switched to a new branch 'gh-pages'
cd -
cd /tmp/d20190115-5107-1w5c6ge/work
rsync: change_dir "/app/build" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors)
(code 23) at main.c(1183) [sender=3.1.0]
Could not copy /app/build.
Here are my .travis.yml and dockerfile .
# Grants super user permissions
sudo: required
# travis ci installs docker into travis container
services:
- docker
# before tests are ran build docker image
before_install:
- docker build -t dvontrec/fn-killers -f Dockerfile.dev .
script:
# SHOULD ADD TESTS
- docker run dvontrec/fn-killers pwd
- docker run dvontrec/fn-killers ls
# Steps before deploy:
defore_deploy:
- docker run dvontrec/fn-killers -f npm run build
# Steps to deploy to github pages
deploy:
provider: pages
skip_cleanup: true
github_token: $github_token
on:
branch: master
FROM node:alpine
WORKDIR './app'
COPY package.json .
RUN npm install
COPY . .
CMD ["npm", "run", "start-docker"]
Does anyone know how to get the files down from the container?
I found out what i did wrong, to deploy with docker you need to have an nginx container that will copy everything down. Here is the Dockerfile i used.
# Build phase
FROM node:alpine as builder
WORKDIR '/app'
COPY package.json .
RUN npm install
COPY . .
RUN npm run build
# Run phase
FROM nginx
EXPOSE 80
COPY --from=builder /app/build /usr/share/nginx/html