基于 Google Cloud Build 构建的 Dockerfile 未打印测试日志

Dockerfile build on Google Cloud Build not printing test logs

我有一个简单的 Dockerfile:

FROM node:8

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

CMD ["npm", "run", "test"]

我 运行 它在 Google Cloud Build 中并获得以下日志:

...
Removing intermediate container 19ae183b28c6
 ---> 5802f218fa4e
Step 5/6 : COPY . .
 ---> a1600834b6e0
Step 6/6 : CMD npm run test
 ---> Running in bfb7be511b96
Removing intermediate container bfb7be511b96
 ---> 6e974d117670
Successfully built 6e974d117670
...

在步骤 6/6npm run test,我没有看到我在本地计算机上看到的日志:

npm run test:

 PASS  __tests__/order.spec.ts
 PASS  __tests__/news.spec.ts
 PASS  __tests__/product.spec.ts (5.328s)
 PASS  __tests__/review.spec.ts
 PASS  __tests__/donation.spec.ts
 PASS  __tests__/referral.spec.ts
 PASS  __tests__/nonprofit.spec.ts

Test Suites: 7 passed, 7 total
Tests:       22 passed, 22 total
Snapshots:   0 total
Time:        8.291s, estimated 9s

如何让这些日志显示在构建日志中?

这是因为您正在构建图像而不是 运行宁它 :)。当您 运行 容器时,您将看到打印出的日志,例如

docker run -it my-image