在构建 Docker 映像时要求安装 Typescript [英] Asked to install Typescript when already installed when building Docker image

查看:21
本文介绍了在构建 Docker 映像时要求安装 Typescript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建使用 Typescript 的 Next.js/React 应用程序的 docker 映像.

I am trying to build a docker image of a Next.js/React application which uses Typescript.

Typescript 已安装,我可以在没有 docker 的情况下在本地运行构建.

Typescript is installed, and I am able to run a build locally without docker.

但是,随着 docker 映像的构建,我想到了以下几点:

However, as the docker image is building, I get to the following point:

Step 8/10 : RUN npm run build
 ---> Running in ee577c719739

> project@0.0.5 build /app
> next build

Creating an optimized production build...
Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://nextjs.org/telemetry

It looks like you're trying to use TypeScript but do not have the required package(s) installed.

Please install typescript by running:

        npm install --save-dev typescript

If you are not trying to use TypeScript, please remove the tsconfig.json file from your package root (and any TypeScript files).

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! project@0.0.5 build: `next build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the project@0.0.5 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我已经安装了 Typescript.这让我很困惑.

I have installed Typescript already. This is very confusing for me.

我使用的 Docker 镜像如下所示:

The Docker image I am using looks like this:

FROM gcr.io/companyX/companyX-node-base:12-alpine

# Copy in the project files
COPY . .

# Clean
USER root
RUN rm -fr node_modules

ENV NODE_ENV=production

COPY package*.json ./

RUN npm install && 
    npm cache clean --force

RUN npm run build

EXPOSE 3000

# Running the app
CMD [ "npm", "start" ]

推荐答案

当你运行时(甚至在 Docker 之外)

When you run (even outside Docker)

export NODE_ENV=production
npm install

它只安装 package.json 中的 dependencies 而不是 devDependencies.另一方面,您可能需要 devDependencies 来获取 typescript 之类的工具.

it only installs the dependencies from your package.json and not the devDependencies. On the other hand, you probably need the devDependencies to get tools like typescript.

这里好的解决方案是使用多阶段构建.第一阶段安装所有的依赖;第二阶段仅复制运行应用程序所需的内容.

The good solution here is to use a multi-stage build. The first stage installs all of the dependencies; the second stage copies out only what’s needed to run the application.

FROM gcr.io/companyX/companyX-node-base:12-alpine AS build

# Copy in only the parts needed to install dependencies
# (This avoids rebuilds if the package.json hasn’t changed)
COPY package.json package.lock .

# Install dependencies (including dev dependencies)
RUN npm install

# Copy in the rest of the project
# (include node_modules in a .dockerignore file)
COPY . .

# Build the project
RUN npm run build

# Second stage: runtime
FROM gcr.io/companyX/companyX-node-base:12-alpine

ENV NODE_ENV=production

# Again get dependencies, but this time only install
# runtime dependencies
COPY package.json package.lock .
RUN npm install

# Get the built application from the first stage
COPY --from=build /app/dist dist

# Set runtime metadata
EXPOSE 3000
CMD [ "npm", "start" ]
# CMD ["node", "dist/index.js"]

请注意,此方法与使用主机中的任意内容覆盖应用程序内容或尝试将库存储在 Docker 卷中的设置不兼容.最终的镜像是独立的,不需要任何宿主内容,可以在其他系统上按原样运行,无需单独复制源代码.

Note that this approach isn’t compatible with setups that overwrite the application content with arbitrary content from the host, or that try to store libraries in Docker volumes. The final image is self-contained and doesn’t require any host content, and can be run as-is on other systems without separately copying the source code.

这篇关于在构建 Docker 映像时要求安装 Typescript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆