简体   繁体   中英

TSC error when compiling through Dockerfile

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"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM