node.js - 如何在 Dockerfile.template 中的 `tsc` 之前运行 TypeScript `COPY`?

标签 node.js typescript docker npm dockerfile

在通过 Dockerfile.template 文件部署到 docker 时,我有一个可行的解决方案来构建一个 TypeScript Node 应用程序:

# Thanks: https://github.com/balenalabs/multicontainer-getting-started
FROM balenalib/%%BALENA_MACHINE_NAME%%-node

# Defines our working directory in container
WORKDIR /usr/src/app

# Install packages
RUN install_packages build-essential libraspberrypi-bin

# Copies the package.json first for better cache on later pushes
COPY package.json package.json

# Install npm dependencies on the balena.io build server,
# making sure to clean up the artifacts it creates in order to reduce the image size.
#NOTE: I removed --production option because I need to run tsc after COPY..., so larger image size remains for now.
RUN JOBS=MAX npm i

# This will copy all files in our root to the working directory in the container
COPY . ./

# Build from TypeScript
# TODO: This feels messy. How may I run `npm i --production` followed by `tsc` before copying files in Docker?
# Best answer so far for the next line: https://stackoverflow.com/questions/51083134/how-to-compile-typescript-in-dockerfile
RUN ./node_modules/typescript/bin/tsc -p ./tsconfig.json

# server.js will run when container starts up on the device
# in package.json: "serve": "node dist/server.js"
CMD ["npm", "run", "serve"]

我宁愿跑 tsc在将文件复制到应用程序目录之前。另外,正在运行 npm i --production这样我就不必在应用程序中有开发依赖项。但是在 npm i -g typescript 之后这似乎不起作用(在 COPY 之前),没有找到命令。最好的方法是什么?

更新解决方案

chosen solution ,这是一个更新的脚本,适用于搜索此内容的下一个人:
###################################
# First Stage: Compile TypeScript #
###################################

# Thanks: https://stackoverflow.com/questions/60916271/how-do-i-run-typescript-tsc-before-copy-in-dockerfile-template/60917273#60917273
FROM balenalib/%%BALENA_MACHINE_NAME%%-node AS build

# Install needed packages to build raspicam Node dependencies.
RUN install_packages build-essential libraspberrypi-bin

WORKDIR /usr/src/app

# Install the Javascript dependencies, including all devDependencies.
COPY package.json .
RUN npm i

# Copy the rest of the application in and build it.
COPY . ./

# RUN TypeScript build
RUN ./node_modules/typescript/bin/tsc -p ./tsconfig.json

# Clean up node_modules to not include dev dependencies.
RUN rm -rf ./node_modules
RUN JOBS=MAX npm i --production

##################################
# Second Stage: Prepare Dist App #
##################################

FROM balenalib/%%BALENA_MACHINE_NAME%%-node

# Defines our working directory in container
WORKDIR /usr/src/app

# Install packages
RUN install_packages build-essential libraspberrypi-bin

# This will copy all files in our root to the working directory in the container
COPY --from=build /usr/src/app/dist dist
COPY package.json package.json

# server.js will run when container starts up on the device
CMD ["npm", "run", "serve"]

最佳答案

您可以使用 multi-stage build为了这。第一阶段包括所有开发依赖项,包括 tsc ;第二阶段只包含运行构建的应用程序所需的文件。

(我不熟悉您正在使用的特定构建环境,因此这将是标准的 node 图像。)

# First stage: compile things.
FROM node:12 AS build
WORKDIR /usr/src/app

# (Install OS dependencies; include -dev packages if needed.)

# Install the Javascript dependencies, including all devDependencies.
COPY package.json .
RUN npm install

# Copy the rest of the application in and build it.
COPY . .
# RUN npm build
RUN npx tsc -p ./tsconfig.json

# Now /usr/src/app/dist has the built files.

# Second stage: run things.
FROM node:12
WORKDIR /usr/src/app

# (Install OS dependencies; just libraries.)

# Install the Javascript dependencies, only runtime libraries.
COPY package.json .
RUN npm install --production

# Copy the dist tree from the first stage.
COPY --from=build /usr/src/app/dist dist

# Run the built application when the container starts.
EXPOSE 3000
CMD ["npm", "run", "serve"]

关于node.js - 如何在 Dockerfile.template 中的 `tsc` 之前运行 TypeScript `COPY`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60916271/

相关文章:

PHP 和 Node.js 还是只有 Node.js?

typescript - 如何以可用的方式重新导出其中包含接口(interface)的导入模块?

docker - 源文件更新了,但是CMD没有反射(reflect)

docker - 我可以在Docker Alpine容器内使用 “mount”吗?

javascript - 如何防止 Node.js 12 工作线程立即终止?

javascript - __dirname 在带有 webpack 捆绑的 Node js 中不起作用

node.js - "path"参数必须是字符串类型。收到未定义。不知道为什么?

typescript - Visual Studio代码无法正确踩入TypeScript代码

javascript - Angular2 中对象的 $$hashKey?

java - OpenJDK9 (Docker), gradle 4.2.1, GitLab