TSC error when compiling through Dockerfile - node.js

I have a Node / TypeScript application which I'm trying to run from Docker.
yarn tsc works fine locally, but does not work in the context of the Dockerfile. I think the issue is regarding which directory the command is being run from with the Dockerfile, but I'm not sure how to fix it.
How can I make sure tsc can see the tsconfig.json file?
Dockerfile
FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN yarn
# Copy source files.
COPY . .
# Run tsc.
RUN yarn prepare
# Run app.
CMD [ "yarn", "start" ]
package.json
"scripts": {
"prepare": "yarn tsc",
"tsc": "tsc -p .",
"dev": "ts-node-dev --respawn --transpileOnly server.ts",
"start": "./node_modules/nodemon/bin/nodemon.js ./build/server.js",
},
Error output
docker build --tag app ./
Sending build context to Docker daemon 114.1MB
Step 1/7 : FROM node:10
---> e05cbde47b8f
Step 2/7 : WORKDIR /usr/src/app
---> Using cache
---> faaea91b16ae
Step 3/7 : COPY package*.json ./
---> 64310f50355d
Step 4/7 : RUN yarn
---> Running in be8aed305980
yarn install v1.16.0
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents#1.2.9: The platform "linux" is incompatible with this module.
info "fsevents#1.2.9" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
$ yarn tsc
yarn run v1.16.0
$ tsc -p ./
error TS5057: Cannot find a tsconfig.json file at the specified directory: './'.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
The command '/bin/sh -c yarn' returned a non-zero code: 1

As David said,
Just running yarn is equivalent to yarn install, which runs a "prepare" script.
You could change your script to copy the application code to the given directory before running yarn command, if you do not need for yarn command to run first.
FROM node:10
WORKDIR /usr/src/app
# Copy source files.
COPY . .
RUN yarn
# Run app.
CMD [ "yarn", "start" ]

If you look carefully at your docker build output, you'll notice that the last Dockerfile instruction that gets run is the
Step 4/7 : RUN yarn
In particular, yarn treats a couple of script names as special. Just running yarn is equivalent to yarn install, which runs a "prepare" script. In your case the "prepare" script runs tsc; but, this is happening during the "install dependencies" phase of the Dockerfile, and your application code isn't there yet.
You can work around this by using yarn's --ignore-scripts option. A number of other options make sense here in the context of building a Docker image. (You could use a multi-stage build to build a final image with only --production dependencies and not the tsc compiler or other build-time tools, for example.)
FROM node:10
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
RUN yarn install --ignore-scripts --frozen-lockfile --non-interactive
COPY . .
RUN yarn prepare
CMD ["node", "./build/server.js"]

Related

Service web failed to build docker image

Here is my Dockerfile :
build front end
FROM node:v12.22.12 AS client_build
WORKDIR /client
COPY ./front/video /client/
RUN npm install
RUN node_modules/.bin/ng build --configuration production
build back end
FROM node:v12.22.12 AS server_build
WORKDIR /app
COPY ./front/videoService /app/server
COPY --from=client_build /client/dist/video /app/dist/video
RUN npm install --production
RUN tsc
EXPOSE 7717
CMD ["node", "./app/server/dist/front/videoService/src/index.js"]
build UI
FROM node:v12.22.12 AS ui_build
WORKDIR /ui
COPY ./front/UIVideo /ui/
RUN npm install
CMD ["node", "./ui/front/UIVideo/www.js"]
EXPOSE 7708
The ERROR I got : manifest for node:v12.22.12 not found: manifest unknown: manifest unknown
I dont know what is the source of the problem and failed to found a solution here on other discussions.

ng not found (Docker Compose)

I'm trying to build and run, but it says that "ng not found" and finally, that the container exited with code 127. Well, running npm install and then ng serve --open is sufficient for run my application outside a docker container, but inside it, simple doesn't works. I tried do RUN node_modules/.bin/ng build --prod, but .bin folder isn't there (what I think that is the problem, but I really don't know how to solve it, because even .dockerignore is not ignoring .bin). So, what should I do?
Dockerfile:
FROM node:lts-alpine
ENV NODE_ENV=production
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm install --production --silent
COPY . .
RUN ls node_modules
EXPOSE 3000
USER node
CMD ["npm", "start"]
I also tried to `npm install #angular/cli -g', but I get into another error...

Jest Unit Testing in Docker Container

I am trying to move my tests to my docker image build stage but it seems to ignore my test build at all and just skip it when I build the image.
What can be the problem?
# Base build
FROM node:16.13 AS build
RUN mkdir /app
WORKDIR /app
ADD package*.json ./
RUN npm i -g npm#6.14.15
RUN npm i --production
COPY . .
RUN npm run build
# Clean
RUN npm prune --production
RUN npm install -g node-prune
RUN node-prune
# Test
FROM build AS test
RUN npm i -g npm#6.14.15
RUN npm i -D jest typescript
RUN npm i -D ts-jest #types/jest
RUN npm i -D #shelf/jest-mongodb
RUN npx jest
# Release
FROM node:16.13-alpine
RUN mkdir /app
WORKDIR /app
COPY --from=build /app/dist ./dist/
COPY --from=build /app/node_modules ./node_modules/
COPY --from=build /app/package.json ./
CMD npm start
Above you can see my Dockerfile where I am preparing the build, then plan to have tests and after that, I am making my release image.
I've already been playing around with that for hours; I cleared cache, made tweaks with the order in the file) but it didn't help. It keeps ignoring my test build.
Any hint of that and in general on my Dockerfile is welcomed
Docker internally has two different systems to build images. Newer Dockers default to a newer system called BuildKit. One of the major differences is the way the two systems handle multi-stage builds: the older system just runs all of the build stages until the end, but BuildKit figures out that the final stage COPY --from=build but doesn't use anything from the test stage, and just skips it.
I wouldn't run these tests in a Dockerfile (since it doesn't produce a runnable artifact) or in Docker at all. I'd probably run them in my host-based development environment, before building a Docker image:
# On the host
npm install # devDependencies include all test dependencies
npm run tests # locally
# repeat until the tests pass (without involving a container)
# Build the image _after_ the tests pass
docker build -t myapp .
docker build also has a --target option to name a specific stage so you could tell Docker to "build the test image", but you'd just immediately delete it. This is where I don't think Docker quite makes sense as a test runner.
docker build -t delete-me --target test
docker build -t my-app .
docker rmi delete-me
Your Dockerfile also installs a MongoDB test helper. If the test depends on a live database rather than a mock implementation, the other thing to be aware of here is that code in a Dockerfile can never connect to other containers. You have to run this test from somewhere else in this case.

Building Docker image for Node application using Yarn dependency

I am trying to build a docker image for a node application that uses yarn to install dependencies. My Dockerfile looks like this:
FROM node:7
WORKDIR /app
COPY package.json /app
RUN yarn install
COPY . /app
CMD npm run develop
EXPOSE 8000
Every thing runs well when I run yarn install on my local machine but when I do a docker build, I get this error that blocks for ever.
**docker build -t rs .**
Sending build context to Docker daemon 219.1MB
Step 1/7 : FROM node:7
---> d9aed20b68a4
Step 2/7 : WORKDIR /reason
---> Using cache
---> fe51a1860989
Step 3/7 : COPY package.json /reason
---> Using cache
---> b0e136ee6eeb
Step 4/7 : RUN yarn install
---> Running in e273f8cf1f3e
yarn install v0.24.4
info No lockfile found.
[1/4] Resolving packages...
Couldn't find any versions for "glamor" that matches "next"
? Please choose a version of "glamor" from this list: (Use arrow keys)
❯ 2.20.40
2.20.39
2.20.38
2.20.37
2.20.36
2.20.35
2.20.34
(Move up and down to reveal more choices)warning glamor#3.0.0-3: abandoned, please use v2 instead
warning gatsby-plugin-glamor > glamor-inline#1.0.5: use glamor/inline instead
warning gatsby-plugin-glamor > glamor-react > glamor#3.0.0-3: abandoned, please use v2 instead
warning gatsby-plugin-glamor > glamor-server > glamor#3.0.0-3: abandoned, please use v2 instead
warning gatsby > babel-preset-es2015#6.24.1: 🙌 Thanks for using Babel: we recommend using babel-preset-env now:
please read babeljs.io/env to update!
The console remains in this stage for ever. How can I fix this please.
You should first run yarn install to generate a yarn lockfile (yarn.lock) before building the image. Then make sure to copy it along with the package.json. Your dockerfile should look like this :
FROM node:7
WORKDIR /app
COPY package.json /app
COPY yarn.lock /app
RUN yarn install
COPY . /app
CMD npm run develop
EXPOSE 8000
With this all dependencies should install successfully when building your image
Dockerfile
FROM node:6.9.5-alpine
RUN mkdir -p /code
WORKDIR /code
ADD . /code
RUN npm install -g -s --no-progress yarn && \
yarn && \
yarn run build && \
yarn cache clean
CMD [ "npm", "start" ]
EXPOSE 8080
docker-compose.yml
version: '2'
services:
sample-app:
image: sample-node-yarn-app
ports:
- "8080:8080"
Create docker image
docker build -t sample-node-app .
RUN
docker-compose up -d
You can simplify the above answers by using a predefined yarn docker image. We are assuming here this image is only for development purpose. For production mode, you should only consider the minimum binaries, such as node.
FROM gmolaire/yarn:1.22.4_12.18.3-alpine3.12
WORKDIR /usr/local/app
ADD . .
RUN yarn install && \
yarn build
EXPOSE 8080
CMD [ "yarn", "run", "develop" ]

yarn install error Couldn't find a package.json during Docker build

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

Resources