参考链接:https://blog.fundebug.com/2017/05/15/write-excellent-dockerfile/

为什么不能使用 commit 制作的镜像

https://yeasy.gitbooks.io/docker_practice/content/image/commit.html

要把经常改动的部分放在 dockerfile 的后面

docker 镜像的缓存是一层一层的堆上去的,如果把经常更改的代码放在前面,那么后面的缓存层全部失效了。如下面代码所示,如果把 sqlaudit 的项目代码放到第6层,那么后面的 Python 也要重新编译安装,修改下代码就让让人痛不欲生。

  1. FROM centos:7
  2. MAINTAINER kougazhang
  3. WORKDIR /kylin
  4. ENV PATH="/kylin:usr/bin:${PATH}"
  5. COPY ./sqlaudit_package /kylin/sqlaudit_package/
  6. RUN yum groupinstall -y "Development Tools" && \
  7. yum -y update zlib* && \
  8. yum -y install zlib* && \
  9. yum install -y openssl-devel && \
  10. yum localinstall -y /kylin/sqlaudit_package/oracle-instantclient12.1-basic-12.1.0.2.0-1.x86_64.rpm && \
  11. sh -c "echo /usr/lib/oracle/12.1/client64/lib > /etc/ld.so.conf.d/oracle-instantclient.conf" && \
  12. ldconfig && \
  13. echo 'export LD_LIBRARY_PATH=/usr/lib/oracle/12.1/client64/lib:$LD_LIBRARY_PATH' >> ~/.bash_profile && \
  14. mkdir -p /usr/lib/oracle/12.1/client64/lib/network/admin && \
  15. /kylin/sqlaudit_package/Python-3.6.0/configure --prefix=/usr/local/python3 --with-ssl && \
  16. make && \
  17. make install && \
  18. ln -s /usr/local/python3/bin/python3 /usr/bin/python3 && \
  19. ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3 && \
  20. rm -r /usr/local/python3/lib/python3.6/site-packages && \
  21. mv /kylin/sqlaudit_package/site-packages/ /usr/local/python3/lib/python3.6/
  22. COPY ./sqlaudit /kylin/sqlaudit/
  23. RUN /usr/bin/pip3 install --no-cache -r /kylin/sqlaudit/rq3.txt
  24. EXPOSE 8000
  25. EXPOSE 22
  26. CMD python3 sqlaudit/main.py