减少赛普拉斯docker映像的大小

时间:2019-09-04 23:53:57

标签: reactjs docker testing yarnpkg cypress

我正在使用赛普拉斯及其docker映像cypress/browsers:node12.6.0-chrome75来运行UI测试。不幸的是,该图像的大小为1.276GB,这使我的测试速度大大降低。

有什么方法可以减少这种情况,还是有较小的图像可用?

这是我的docker文件:

FROM cypress/browsers:node12.6.0-chrome75

WORKDIR /opt/app

COPY ./.npmrc /opt/app/.npmrc
COPY ./package.json /opt/app/package.json
COPY ./yarn.lock /opt/app/yarn.lock
RUN yarn

COPY . /opt/app/

ENTRYPOINT ["yarn"]
CMD ["build"]

1 个答案:

答案 0 :(得分:1)

您可以为测试环境预先下载cypress/browsers:node12.6.0-chrome75,然后在再次构建docker时,它将使用本地映像,而不是尝试从Internet下载。

如果以上内容不是您所需要的,则可以选择使用细长图像,请参见node12.6.0-chrome75 Dockerfile及其基础图像cypress/base:12.6.0 Dockerfile,您可以使用node:12.6.0找到它,可以使用node:12.6.0-slim构建自己的图片,该图片比node:12.6.0小:

node              12.6.0-slim           2523ec7bd8fd        7 weeks ago         151MB
node              12.6.0                7c412a558705        7 weeks ago         907MB

合并所有dockerfile之后,接下来是最后一个可用的Dockerfile(注意:由于您的情况似乎不需要git,因此我删除了合并的dockerfile中的git --version,同时node:12.6.0-slim尚未预安装git):< / p>

Dockerfile:

FROM node:12.6.0-slim

## https://superuser.com/a/1423685/458816
# RUN printf "deb http://archive.debian.org/debian/ jessie main\ndeb-src http://archive.debian.org/debian/ jessie main\ndeb http://security.debian.org jessie/updates main\ndeb-src http://security.debian.org jessie/updates main" > /etc/apt/sources.list

RUN apt-get update && \
  apt-get install --no-install-recommends -y \
  libgtk2.0-0 \
  libgtk-3-0 \
  libnotify-dev \
  libgconf-2-4 \
  libnss3 \
  libxss1 \
  libasound2 \
  libxtst6 \
  xauth \
  xvfb \
  # install Chinese fonts
  # this list was copied from https://github.com/jim3ma/docker-leanote
  fonts-arphic-bkai00mp \
  fonts-arphic-bsmi00lp \
  fonts-arphic-gbsn00lp \
  fonts-arphic-gkai00mp \
  fonts-arphic-ukai \
  fonts-arphic-uming \
  ttf-wqy-zenhei \
  ttf-wqy-microhei \
  xfonts-wqy \
  # clean up
  && rm -rf /var/lib/apt/lists/*

RUN npm install -g npm@6.10.0
RUN npm install -g yarn@1.16.0

# a few environment variables to make NPM installs easier
# good colors for most applications
ENV TERM xterm
# avoid million NPM install messages
ENV npm_config_loglevel warn
# allow installing when the main user is root
ENV npm_config_unsafe_perm true

# versions of local tools
RUN echo  " node version:$(node -v) \n" \
  "npm version: $(npm -v) \n" \
  "yarn version:$(yarn -v) \n" \
  "debian version:  $(cat /etc/debian_version) \n" \
  "user:$(whoami) \n"

USER root

RUN node --version
RUN echo "force new chrome here"

# install Chromebrowser
RUN \
  wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - && \
  echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list
RUN apt-get update
# disabled dbus install - could not get it to install
# but tested an example project, and Chrome seems to run fine
# RUN apt-get install -y dbus-x11
RUN apt-get install -y google-chrome-stable
RUN rm -rf /var/lib/apt/lists/*

# "fake" dbus address to prevent errors
# https://github.com/SeleniumHQ/docker-selenium/issues/87
ENV DBUS_SESSION_BUS_ADDRESS=/dev/null

# Add zip utility - it comes in very handy
RUN apt-get update && apt-get install -y zip

# versions of local tools
RUN echo  " node version:$(node -v) \n" \
  "npm version: $(npm -v) \n" \
  "yarn version:$(yarn -v) \n" \
  "debian version:  $(cat /etc/debian_version) \n" \
  "Chrome version:  $(google-chrome --version) \n"

最后,使用docker build -t cypress/browsers:custom .来构建自己的映像,将其放入dockerhub,然后您的应用程序Dockerfile可以切换为使用此docker映像。

$ docker images | grep cypress/browsers
cypress/browsers     custom                15b31db2df81    About a minute ago   810MB
cypress/browsers     node12.6.0-chrome75   a029268ee2c8    8 weeks ago          1.41GB

当然,您可以继续减少上述Dockerfile中不必要的软件包,例如zip等,以继续减小大小。