简体   繁体   English

如何在Dockerfile中编译typescript

[英]How to compile typescript in Dockerfile

I'm having trouble compiling my nodejs typescript application from a Dockerfile. 我在从Dockerfile编译nodejs typescript应用程序时遇到问题。 When I build my docker image an inspect it is missing the dist folder entirely. 当我构建我的docker镜像时,检查它完全缺少dist文件夹。

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? 有什么建议我错了吗?

As I far as I know, 据我所知,
the WORKDIR don't have to be created by yourself. WORKDIR不必由您自己创建。 Here's the documentation for WORKDIR . 这是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. 之后您不必手动复制到特定文件夹,因为在WORKDIR命令之后,复制命令将为您复制文件。

Therefore I suggest you to use the following Dockerfile: 因此我建议你使用以下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"]

As a tiny tipp: I would use typescript as a dependency in my package.json and then just use the following file: 作为一个小小的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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM