I'm working with a node js backend and I want to deploy the application via docker.
I changed two npm packages to get my application working and automatically install these changes with npm patch-package with the help of a post install script in my package.json.
"postinstall": "patch-package"
I installed both both postinstall-postinstall and patch-package as dev dependencies.
Running yarn install and yarn build seperatly this works fine but once I want to dockerize this application I get an error during the build phase, which basically says that the patch wasn't applied to the node_modules.
This is my dockerfile:
# stage 1
FROM node as builder
WORKDIR /srv
COPY package.json yarn.lock patches ./
RUN yarn install --frozen-lockfile --unsafe-perm
COPY . .
RUN yarn build
I don't really know if the yarn install script in the dockerfile is not running post install or if the error only happens in the yarn build script.
Thanks in advance
As #donjus mentioned in the comments, the patches were being copied to the root directory, not inside of patches.
The solution is to change:
COPY package.json yarn.lock patches ./
RUN yarn install --frozen-lockfile --unsafe-perm
to
COPY package.json yarn.lock ./
COPY ./patches ./patches
RUN yarn install --frozen-lockfile --unsafe-perm
Related
I have an M1 mac. I'm trying to build a docker image. I'm trying to build it on x86_64. Everything works fine until yarn install. It's stuck forever. Here's my Dockerfile:
FROM --platform=linux/x86_64 public.ecr.aws/docker/library/node:16
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && unzip awscliv2.zip
RUN ./aws/install && aws --version
RUN mkdir code
WORKDIR /code
COPY package.json yarn.lock /code/
RUN yarn install --production --frozen-lockfile && yarn cache clean
EXPOSE 3000
ADD lib /code/lib
USER node
CMD ["node", "lib/index.js"]
If I don't set --platform=linux/x86_64 it's failing on installing aws-cli. If I skip aws cli (and don't set platform), yarn install finishes within a minute, and the build succeeds.
I've also tried --platform=linux/amd64, same result.
What causes yarn install take forever?
I just started learning Docker and i'm trying to run my node ts app with few simple commands. Already googled and tried a lot of things but still no success. App works perfectly when i run it directly from terminal, so mistake might only be in the Dockerfile, nothing else crossed my mind.
It always builds successfully, but breaks when i try to run.
FROM node:14.17.1-alpine
# Install base packages
RUN apk update
RUN apk upgrade
# Change TimeZone
RUN apk add --update tzdata
ENV TZ Europe/Berlin
# Clean APK cache
RUN rm -rf /var/cache/apk/*
WORKDIR /app
COPY package*.json ./
RUN npm i
COPY . .
ENV NODE_ENV=production
CMD ["node","src/app.ts"]
Any help would be really appreciated,
Thanks
Typscript app cannot be run with Node. You need to compile .ts files to .js files and then you can run .js file with Node.
For more detailed you can checkout a blogpost on how to containerize. For example: https://itnext.io/dockerize-a-typescript-app-in-15-mins-a0e8c1e904b3
But there is ts-node, REPL kind of thing for TS. You can add it in the package.json as devDependencies and then can do:
CMD ["npx", "ts-node", "src/app.ts"]
You can check more here: How to run TypeScript files from command line?
EDIT:
if you have tsconfig file then you can install tsc and run it in the Dockerfile to compile. Then you can run the compiled JS file in Node.
You can take hint from this: How to compile typescript in Dockerfile
RUN npm install tsc -g
RUN tsc
CMD ["node", "<urapppath>/app.js"]
To cache node_modules I add the package.json first then I run npm i inside docker image.
which is works great. but I also need to have version inside the package.json, and each deploy/build I increment the version number.
Because package.json has been changed, docker is not cache mode_modules because of it.
How can I cache node_modules in this senirio?
FROM node
# If needed, install system dependencies here
# Add package.json before rest of repo for caching
ADD package.json /app/
WORKDIR /app
RUN npm install
ADD . /app
# If needed, add additional RUN commands here
You can achieve this cache using BUILD_VERSION along with package.json version.
ARG BUILD_VERSION=0.0.0
Set some default value to BUILD_VERSION, keep the same value from BUILD_VERSION as package.json version to ignore the npm installation process.
Suppose you have the version in package.json is 0.0.0 and build version should be 0.0.0 to ignore installation.
FROM node:alpine
WORKDIR /app
ARG BUILD_VERSION=0.0.0
copy package.json /app/package.json
RUN echo BUILD_VERSION is $BUILD_VERSION and Package.json version is $(node -e "console.log(require('/app/package.json').version);")
RUN if [ "${BUILD_VERSION}" != $(node -e "console.log(require('/app/package.json').version);") ];then \
echo "ARG version and Package.json is different, installing node modules";\
npm install;\
else \
echo "npm installation process ignored";\
fi
To ignore npm installation during the build, run build command with
docker build --no-cache --build-arg BUILD_VERSION=0.0.0 -t test-cache-image .
Now, if you want to install node_modules just update the run command and it will work as you are expecting but more control as compared to caching track.
docker build --no-cache --build-arg BUILD_VERSION=0.0.1 -t test-cache-image .
This will install node_modules if the package.json version did not match with build-version.
I am attempting to perform npm install within a docker image. As part of the package.json, I need version 1.8.8 of react-pattern-library. Within the docker image, only version 0.0.1 appears to be available.
If I locally run
npm view react-pattern-library versions
I can see version 1.8.8
However the same command within my docker file only show version 0.0.1
Can anyone tell me what configuration setting I need to be able to find the correct version when attempting my docker build?
docker build -t jhutc/molly-ui
Contents of Dockerfile
FROM node:10
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
#COPY package*.json ./
COPY package.json ./
RUN npm set strict-ssl false
ENV HTTP_PROXY="http://proxy.company.com:8080"
ENV HTTPS_PROXY="https://proxy.company.com:8080"
RUN echo $HTTP_PROXY
RUN echo $HTTPS_PROXY
RUN npm view react-pattern-library versions
#RUN npm install
Try deleting the package-lock.json and running npm install again.
Yarn version: 0.21.2
Nodejs version: 6.9 and 4.7
When running yarn locally it works
When running npm install it works
When running yarn install Dockerfile (docker build .) it fails with: error Couldn't find a package.json file in "/root/.cache/yarn/npm-readable-stream-2.2.2-a9e6fec3c7dda85f8bb1b3ba7028604556fc825e"
I have absolutely no idea why.
Step 16 : RUN yarn install
---> Running in 917c2b1b57fb
yarn install v0.21.2
[1/4] Resolving packages...
[2/4] Fetching packages...
error Couldn't find a package.json file in "/root/.cache/yarn/npm-readable-stream-2.2.2-a9e6fec3c7dda85f8bb1b3ba7028604556fc825e"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
The command '/bin/sh -c yarn install' returned a non-zero code: 1
Yarn is installed this way in the Dockerfile
RUN curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 0.21.2
ENV PATH /root/.yarn/bin:$PATH
When you build a docker image, no files are copied automatically in the docker image despite that are part of the docker build context . (depends also of your .dockerignore file configuration if you have any). To add files from the docker context to your docker image you can do it explicitly with running commands like ADD or COPY.
Below an example of dockerfile:
WORKDIR /app
COPY ["yarn.lock", "package.json", "./"]
# Install app dependencies
RUN yarn install --check-files --non-interactive --ignore-optional --frozen-lockfile