如何在Dockerfile中编译打字稿

时间:2018-06-28 12:36:58

标签: node.js typescript docker

我无法从Dockerfile编译nodejs打字稿应用程序。当我构建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显示文件/文件夹时:

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

关于我要去哪里的任何建议吗?

1 个答案:

答案 0 :(得分:0)

据我所知
WORKDIR不必自己创建。 这是documentation for WORKDIR

之后,您无需手动复制到特定文件夹, 因为在执行WORKDIR命令之后,复制命令将为您复制文件。

因此,我建议您使用以下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中的打字稿用作依赖项,然后仅使用以下文件:

    FROM node:alpine
    WORKDIR /usr/yourapplication-name
    COPY package.json .
    RUN npm install
    COPY . .
    RUN tsc
    CMD ["node", "./dist/server.js"]