如何在Dockerfile中编译typescript

问题描述 投票:0回答:1

我在从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.: 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"]

当我在本地运行图像并显示文件/文件夹时:

# ls
node_modules  package-lock.json  package.json  src

有什么建议我错了吗?

node.js typescript docker
1个回答
0
投票

据我所知, WORKDIR不必由你自己创造。这是documentation for WORKDIR。 之后您不必手动复制到特定文件夹,因为在WORKDIRcommand之后复制命令会为您复制文件。

因此我建议你使用以下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"]

作为一个小小的tipp:我会在我的package.json中使用typescript作为依赖项,然后使用以下文件:

    FROM node:alpine
    WORKDIR /usr/yourapplication-name
    COPY package.json .
    RUN npm install
    COPY . .
    RUN tsc
    CMD ["node", "./dist/server.js"]
© www.soinside.com 2019 - 2024. All rights reserved.