Docker + nodejs - JavaScript heap out of memory - node.js

I have a .NET Core & SSR React app and everything runs fine on my computer and on Azure Pipelines and Azure Win and Linux WebApps but when I try to build a Docker Image I always receive 'JavaScript heap out of memory'.
Here is my Dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-alpine AS build
RUN apk add --no-cache bash git openssh nodejs npm
RUN git clone -b dev https://github.com/RicardoGaefke/profile4d.git ./app
WORKDIR /app/src/Web.Identity
RUN NODE_OPTIONS=--max_old_space_size=32768‬
RUN npm install
RUN npm run lint-fix
RUN npm run production1
RUN dotnet publish -c Release ./Web.Identity.csproj -r linux-musl-x64 -o ./publish
FROM mcr.microsoft.com/dotnet/core/runtime-deps:3.0-alpine as runtime
EXPOSE 80
WORKDIR /app
COPY --from=build /app/src/Web.Identity/publish ./
ENTRYPOINT ["dotnet", "Web.Identity.dll"]
And this is my build command:
docker build -f ./Dockerfile.web.identity.cloud -t web_identity_cloud .
Can anybody please help me?
Thanks a lot.

Related

The Next.js app does not work well after creating the image

I have a problem after creating a docker image.
If I build applications without Docker (npm run build, npm start), everything works correctly. Screenshots below.
Please suggest how to fix the problem
Dockerfile:
FROM node:alpine
RUN apk add --no-cache libc6-compat
RUN mkdir /brahhouse
WORKDIR /brahhouse
COPY ./package*.json /brahhouse
RUN npm install
COPY . /brahhouse
RUN npm run build
CMD ["npm", "start"]
I build docker image with command:
docker build -t brahouse .
I run docker image with command:
docker run -p 3000:3000 brahouse
Screenshots:
App without Docker
App in Docker Images
Problem solved
Thank you brc-dd
Solution:
Editing tailwind.config.js (purge -> content) Set the correct paths for components, pages and other folders used by tailwind-css

Can't get my React UI to appear when running Docker container

I have a React client and Go server app that I am trying to containerize. This is my first time using Docker so I'm having a hard time getting it to work. My Dockerfile looks like this which I got from this guide
# Build the Go API-
FROM golang:latest AS builder
ADD . /app
WORKDIR /app/server
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-w" -a -o /main .
# Build the React application
FROM node:alpine AS node_builder
COPY --from=builder /app/client ./
RUN npm install
RUN npm run build
# Final stage build, this will be the container
# that we will deploy to production
FROM alpine:latest
RUN apk --no-cache add ca-certificates
COPY --from=builder /main ./
COPY --from=node_builder /build ./web
RUN chmod +x ./main
EXPOSE 8080
CMD ./main
My server code take in the PORT environment variable to set which port to listen to so I used the following Docker run command:
docker run -p 3000:8080 --env PORT=8080 test1. However, when I go to localhost:3000, I get a 404 page not found response instead of my React app. I've tried several different combinations of the run command but I can't seem to figure out how to get my UI to appear. Is this something to do with my Dockerfile or is my run command missing parameters? Or both?

Why official manual docker example does not work

I'm new to docker, so I have visited the https://www.docker.com/101-tutorial tutorial and executed, folowing their advice:
docker run -dp 80:80 docker/getting-started
This let me read the manual via http:/localhost:80. This works all right and I'm presented with the official docker manual. I follow it. They say to prepare a Dockerfile like this:
FROM node:12-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
which i did. I built the image: docker build -t getting-started . and tried to run it, docker run -dp 3000:3000 getting-started, but with no success. I checked with docker ps that this did not work. After some experimentation (with some "debugging" via an interactive docker launch, with /bin/sh as the initial command) I produced this Dockerfile, which works:
FROM node:alpine
WORKDIR /app
COPY . .
RUN yarn install --production
RUN yarn add express
RUN yarn add sqlite3
CMD ["node", "app/src/index.js"]
I'm quite impressed that without much knowledge about docker, and with absolutely no knowledge about node.js and Alpine Linux, I managed to fix the problem myself. Now I would like to understand what's going on here.
My question is as in the title: why the original instructions from the official manual fail on my computer? Why do I have to install extra libraries and why my working files go to /app/app rather than just /app?
My environment:
> cat /etc/lsb-release
DISTRIB_ID=ManjaroLinux
DISTRIB_RELEASE=20.2
DISTRIB_CODENAME=Nibia
DISTRIB_DESCRIPTION="Manjaro Linux"
> docker --version
Docker version 19.03.13-ce, build 4484c46d9d
My experiment with an interactive container:
> docker run -it my_image /bin/sh
/app # ls
Dockerfile app node_modules package.json yarn.lock
/app # ls app
package.json spec src yarn.lock

serve a gridsome website from within the container

I have a static website built with gridsome, I'm experimenting with docker containerization and I came up with the following Dockerfile:
FROM node:14.2.0-alpine3.10
RUN apk update && apk upgrade
RUN apk --no-cache add git g++ gcc libgcc libstdc++ linux-headers make python yarn
ENV NPM_CONFIG_PREFIX=/home/node/.npm-global
USER node
RUN npm i --global gridsome
RUN yarn global add serve
COPY --chown=node:node gridsome-website/ /home/node/build/
RUN echo && ls /home/node/build/ && echo
WORKDIR /home/node/build
USER node
RUN npm cache clean --force
RUN npm clean-install
# My attempt to serve the website from within, build is successful but serve command doesn't work
CMD ~/.npm-global/bin/gridsome build && serve -d dist/
Gridsome uses the port 8080 by default, after running the container via:
docker run --name my-website -d my-website-image
It doesn't fail, but I can't access my website using the link: http://localhost:8080, the container stops execution right after I run it. I tried copying the ``` dist/`` folder from my container via:
$ docker cp my-website:/home/node/build/dist ./dist
then serving it manually from terminal using:
$ serve -d dist/
It works from terminal, but why does it fail from within the container?
The point of Gridsome is that it produces a static site that you can host anywhere you can put files, right? You don't need nodejs or anything except for a simple webserver to host it.
If you want to produce a clean production Docker image to serve the static files built by Gridsome, then that's a good use-case for a multistage Dockerfile. In the first stage you build your Gridsome project. In the second and final stage you can go with a clean nginx image that contains your dist folder and nothing else.
It could be as simple as this:
FROM node:current AS builder
WORKDIR /build
COPY gridsome-website ./
RUN npm install && gridsome build
FROM nginx:stable
COPY --from=builder /build/dist /usr/share/nginx/html/
EXPOSE 80
After you build and run the image with 8080->80 port mapping, you can access your site at http://127.0.0.1:8080.
docker build -t my-website-image .
docker run -p 8080:80 my-website-image
It's probably a good idea to set up a .dockerignore file as well that ignores at least node_modules.
$ cat .dockerignore
gridsome-website/node_modules

npm install not being running on building container

I have a simple node app with the following Dockerfile:
FROM node:8-alpine
WORKDIR /home/my-app
COPY package.json .
COPY ./app ./app
COPY ./server.js ./
RUN rm -rf node_modules
RUN npm install \
npm run build
EXPOSE 3000
When I build the image with: docker build -t my-app:latest ., I attempt to run the app and it complains that some modules are missing.
When I go into the container via docker run -i -t my-app:latest /bin/sh I can see that the packages have not been installed. After manually running npm install in the container, it seems to work.
I can only conclude that from this RUN npm install not being executed correctly inside the container.

Resources