你可以为你的测试环境预先下载cypress/browsers:node12.6.0-chrome75,然后再次进行docker build时,它将使用本地镜像,而不是尝试从互联网下载。
如果以上不是您所需要的,那么您可以选择使用纤薄的镜像,请参阅node12.6.0-chrome75 Dockerfile 及其基础镜像cypress/base:12.6.0 Dockerfile,您可以使用node:12.6.0 找到它,您可以使用@ 构建自己的镜像987654325@ 比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(注意:我删除了合并 dockerfile 中的 git --version,因为您的场景似乎不需要 git,同时 node:12.6.0-slim 没有预安装 git):
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 等,以继续减小大小。