如何在Dockerfile中编译打字稿 [英] How to compile typescript in Dockerfile
问题描述
我无法从Dockerfile编译nodejs打字稿应用程序。当我构建docker映像时,检查它完全丢失了dist文件夹。
I'm having trouble compiling my nodejs typescript application from a Dockerfile. When I build my docker image an inspect it is missing the dist folder entirely.
Dockerfile:
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"]
当我在本地运行图像并显示文件/文件夹时:
When I run the image locally and ls to reveal files/folders:
# ls
node_modules package-lock.json package.json src
对我要去哪里的任何建议?
Any suggestions to where i am going wrong?
推荐答案
据我所知,
是 WORKDIR
不必自己创建。
这是 WORKDIR的文档。
之后,您无需手动复制到特定文件夹
,因为在 WORKDIR
命令之后,复制命令将复制文件。
As I far as I know,
the WORKDIR
don't have to be created by yourself.
Here's the documentation for WORKDIR.
Afterwards you don't have to copy by hand to the specific folder,
because after WORKDIR
command the copy command would copy the files for you.
因此,我建议您使用以下Dockerfile:
Therefore I suggest you to use the following Dockerfile:
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
中使用打字稿作为依赖项,然后只使用以下文件:
As a tiny tipp: I would use typescript as a dependency in my package.json
and then just use the following file:
FROM node:alpine
WORKDIR /usr/yourapplication-name
COPY package.json .
RUN npm install
COPY . .
RUN tsc
CMD ["node", "./dist/server.js"]
这篇关于如何在Dockerfile中编译打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!