我在从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
有什么建议我错了吗?
据我所知,
WORKDIR
不必由你自己创造。这是documentation for WORKDIR。
之后您不必手动复制到特定文件夹,因为在WORKDIR
command之后复制命令会为您复制文件。
因此我建议你使用以下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"]