Node.js 如何在Dockerfile中编译typescript
从Dockerfile编译nodejs typescript应用程序时遇到问题。当我构建docker映像时,它完全缺少dist文件夹 Dockerfile:Node.js 如何在Dockerfile中编译typescript,node.js,typescript,docker,Node.js,Typescript,Docker,从Dockerfile编译nodejs typescript应用程序时遇到问题。当我构建docker映像时,它完全缺少dist文件夹 Dockerfile: # Template: Node.js dockerfile # Description: Include this file in the root of the application to build a docker image. # Enter which node build should be used. E.g.: nod
# Template: Node.js dockerfile
# Description: Include this file in the root of the application to build a docker image.
# Enter which node build should be used. E.g.: node:argon
FROM node:latest
# Create app directory for the docker image
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app/dist
# Install app dependencies from package.json. If modules are not included in the package.json file enter a RUN command. E.g. RUN npm install <module-name>
COPY package.json /usr/src/app/
RUN npm install
RUN npm install tsc -g
RUN tsc
# Bundle app source
COPY . /usr/src/app
# Enter the command which should be used when the image starts up. E.g. CMD ["node", "app.js"]
CMD [ "node", "server.js"]
FROM node:alpine
WORKDIR /usr/yourapplication-name
COPY package.json .
RUN npm install\
&& npm install tsc -g
COPY . .
RUN tsc
CMD ["node", "./dist/server.js"]
对我的错误有什么建议吗?据我所知,WORKDIR
不必自己创建。
这是你的建议。之后,您不必手动复制到特定文件夹, 因为在
WORKDIR
命令之后,copy命令将为您复制文件
因此,我建议您使用以下Dockerfile:
# Template: Node.js dockerfile
# Description: Include this file in the root of the application to build a docker image.
# Enter which node build should be used. E.g.: node:argon
FROM node:latest
# Create app directory for the docker image
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app/dist
# Install app dependencies from package.json. If modules are not included in the package.json file enter a RUN command. E.g. RUN npm install <module-name>
COPY package.json /usr/src/app/
RUN npm install
RUN npm install tsc -g
RUN tsc
# Bundle app source
COPY . /usr/src/app
# Enter the command which should be used when the image starts up. E.g. CMD ["node", "app.js"]
CMD [ "node", "server.js"]
FROM node:alpine
WORKDIR /usr/yourapplication-name
COPY package.json .
RUN npm install\
&& npm install tsc -g
COPY . .
RUN tsc
CMD ["node", "./dist/server.js"]
作为一个小技巧:我会在我的package.json
中使用typescript作为依赖项,然后只使用以下文件:
FROM node:alpine
WORKDIR /usr/yourapplication-name
COPY package.json .
RUN npm install
COPY . .
RUN tsc
CMD ["node", "./dist/server.js"]
我可以看到几个问题:1。您可以将mkdir/usr/src/app和WORKDIR添加到不存在的子目录。2.在复制文件之前运行tsc,后者返回:
/bin/sh:1:tsc:notfound
@DerkJanSpeelman:因为该类型脚本必须是项目的依赖项(如我在回答中所述)。然后npm安装将为您的项目安装所有依赖项,并将找到命令tsc。我想在这个聊天室中进一步讨论我的问题:,如果您能抽出时间帮助我,我将不胜感激!