在Dockerfile中运行npm run test? [英] Running npm run test in the Dockerfile?
问题描述
使用构建器生成较小的docker映像,什么是运行 npm run test
的好方法?构建后似乎可以在Dockerfile中运行它,但是我可能丢失了一些东西
Using a builder to generate a smaller docker image, what would be a good way to run npm run test
? I seems like running it in the Dockerfile after the build would make sense but maybe I'm missing something
Dockerfile
Dockerfile
# Global args to persist through build stages
ARG docker_build_user
ARG docker_build_time
ARG docker_build_head
ARG docker_build_head_short
ARG docker_build_submodules_head
FROM node:8.9.4-alpine as builder
WORKDIR /app
COPY . .
RUN apk add --no-cache bash
RUN apk add --no-cache git
RUN apk add --no-cache make gcc g++ python
RUN npm install
ENV NODE_ENV=production
RUN npm run build
RUN rm -rf node_modules
RUN npm install
FROM node:8.9.4-alpine
# setup build metadata
ARG docker_build_user
ARG docker_build_time
ARG docker_build_head
ARG docker_build_head_short
ARG docker_build_submodules_head
WORKDIR /app
COPY --from=builder /app .
ENV DOCKER_BUILD_USER $docker_build_user
ENV DOCKER_BUILD_TIME $docker_build_time
ENV DOCKER_BUILD_HEAD $docker_build_head
ENV DOCKER_BUILD_HEAD_SHORT $docker_build_head_short
ENV DOCKER_BUILD_SUBMODULES_HEAD $docker_build_submodules_head
ENV DOCKER_BUILD_DESCRIPTION This build was created by $docker_build_user at $docker_build_time from $docker_build_head_short
ENV NODE_ENV=production
ENV ENABLE_LOGGING=true
RUN echo "DESCRIPTION:${DOCKER_BUILD_DESCRIPTION}"
RUN chown -R 999:999 .
USER 999
# expose our service port
EXPOSE 8080
# Default is to run the server (should be able to run worker)
# Set env var in k8s or run : NPM_RUN_TASK (default is serve)
CMD ["/app/startup.sh"]
推荐答案
从您的承受能力来看,您已经将 multistage build
用于您的 Dockerfile
,这是构建的一个阶段,然后进行打包.
From what you afford, you have already used multistage build
for your Dockerfile
, one stage for build, and one stage for package.
之所以使用它,是因为最后的软件包阶段不需要某些构建依赖关系来进行构建,因此将构建分离到第一阶段.然后测试的事情是一样的,您的dockerfile结构将类似于下一个:
You use this because the final package stage do not need some build dependency for build, so you separate build to first stage. Then the things are same for test, your dockerfile structure will be something like next:
Dockerfile:
# Build stage
FROM node:8.9.4-alpine as builder
# ......
RUN npm install
# Test stage
FROM builder as test
# ......
RUN npm run test
# Package stage
FROM node:8.9.4-alpine
COPY --from=builder /app .
# ......
然后,测试阶段仍可以使用构建阶段中的已构建事物进行测试,但打包阶段将不会在测试阶段中生成任何东西.
Then, the test stage still could use the built out things in build stage to test, but package stage will not have anything generated in test stage.
一些相关指南参考此以及此,以上是其他人每天为他们的nodejs项目docker做的事情整合.
Some related guide refers to this and also this, above is what other folks daily do for their nodejs project docker integration.
这篇关于在Dockerfile中运行npm run test?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!