Docker图片检查< name>
给我16GB
约20层
当我以root身份登录时,此
du -hs /
给我看看2GB
仅供参考,Dockerfile中已经有非常多行的RUN命令.
是否可以在不触摸Dockerfile,重建等的情况下将所有层压缩为一层?
或者可能通过向Dockerfile添加额外的操作来清除/改善缓存
Dockerfile是
FROM heroku/heroku:18
ENV PYENV_ROOT="/pyenv"
ENV PATH="/pyenv/shims:/pyenv/bin:$PATH"
ENV PYTHON_VERSION 3.5.6
ENV GPG_KEY <value>
ENV PYTHONUNBUFFERED 1
ENV TERM xterm
ENV EDITOR vim
RUN apt-get update && apt-get install -y \
build-essential \
gdal-bin \
binutils \
iputils-ping \
libjpeg8 \
libproj-dev \
libjpeg8-dev \
libtiff-dev \
zlib1g-dev \
libfreetype6-dev \
liblcms2-dev \
libxml2-dev \
libxslt1-dev \
libssl-dev \
libncurses5-dev \
virtualenv \
python-pip \
python3-pip \
python-dev \
libMysqLclient-dev \
MysqL-client-5.7 \
libpq-dev \
libcurl4-gnutls-dev \
libgnutls28-dev \
libbz2-dev \
tig \
git \
vim \
nano \
tmux \
tmuxinator \
fish \
sudo \
libnet-ifconfig-wrapper-perl \
ruby \
libssl-dev \
nodejs \
strace \
tcpdump \
# npm & grunt
&& curl -L https://npmjs.com/install.sh | sh \
&& npm install -g grunt-cli grunt \
# ruby & foreman
&& gem install foreman \
# installing pyenv
&& curl https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
COPY . /app
COPY ./requirements /requirements
COPY ./requirements.txt /requirements.txt
COPY ./docker/docker_compose/django/foreman.sh /foreman.sh
COPY ./docker/docker_compose/django/Procfile /Procfile
COPY ./docker/docker_compose/django/entrypoint.sh /entrypoint.sh
# ADD sudoer user django with password django
RUN groupadd -r django -g 1000 && \
useradd -ms /usr/bin/fish -p $(openssl passwd -1 django) --uid 1000 --gid 1000 -r -g django django && \
usermod -a -G sudo django && \
chown -R django:django /app
COPY --chown=django:django ./docker/docker_compose/django/fish /home/django/.config/fish
COPY --chown=django:django ./docker/docker_compose/django/tmuxinator /home/django/.tmuxinator
COPY ./docker/docker_compose/django/fish /root/.config/fish
WORKDIR /app
RUN sed -i 's/\r//' /entrypoint.sh \
&& sed -i 's/\r//' /foreman.sh \
&& chmod +x /entrypoint.sh \
&& chown django /entrypoint.sh \
&& chmod +x /foreman.sh \
&& chown django /foreman.sh \
&& chown -R django:django /home/django/ \
&& pyenv install ${PYTHON_VERSION%%} \
&& mkdir -p /app/log \
&& pyenv global ${PYTHON_VERSION%%} \
&& pyenv rehash \
&& ${PYENV_ROOT%%}/versions/${PYTHON_VERSION%%}/bin/pip install -U pip \
&& ${PYENV_ROOT%%}/versions/${PYTHON_VERSION%%}/bin/pip install -r /requirements.txt \
&& chown -R django:django /pyenv/ \
&& ${PYENV_ROOT%%}/versions/${PYTHON_VERSION%%}/bin/pip install -r /requirements/dev_requirements.txt
# this user receives ENVs from the top
USER django
ENTRYPOINT ["/entrypoint.sh"]
到目前为止,我已经尝试过:
来自docker build实验模式的–squash选项对我来说不是.该Dockerfile是docker-compose中的其他Dockerfile之一.
我也检查了一下:
https://github.com/jwilder/docker-squash
但似乎docker load无法加载压缩的图像.
另外,壁球给了我8GB(距离预期的2GB距离还很远)
docker save <image_id> | docker-squash -t latest_tiny | docker load
回答后更新:
当我添加了这个:
&& apt-get autoremove \ # ? to consider
&& apt-get clean \ # ? to consider
&& rm -rf /var/lib/apt/lists/*
apt-get和–no-cache-dir到每个pip,结果是72GB(是的,甚至更多-docker图像在pip命令之前显示36GB,最终大小为72GB).
我的工作目录是明确的(关于COPY). du -hs /(作为root用户)仍然有2GB.并在重建之前删除了所有图像.
遵循@Mihai方法,我能够将映像从16GB缩小到9GB.
创建一个这样的Dockerfile:
FROM your_image as initial
FROM your_image_base
COPY --from=initial / /
your_image_base应该类似于“ alpine”(高山)-因此,您的图片及其父级的最小图片.
现在构建图像并检查历史记录和大小:
docker build -t your-image:2.0 .
docker image history your-image:2.0
docker image ls
这样,您无需触摸初始Dockerfile即可创建新的Dockerfile(如果您的过程可接受).
让我知道这是否解决了您的问题.
在查看Dockerfile之后进行更新:
也许我想念它,但执行安装后看不到您清理apt-get缓存.您的大型RUN命令应在同一行上以“&& rm -rf / var / lib / apt / lists / *”结尾,以便它不会将整个缓存存储在该层上.