Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The mix mode of Ansible and Docker

The mix mode of Ansible and Docker

# 混用 Ansible 建置 Docker image

If you are interested, you can see the "28. 怎麼混用 Ansible 建置 Docker image?" [1].

PS: This is a lighting talk slide at "Docker Birthday #4 Celebration" [2].

----
[1]: http://ithelp.ithome.com.tw/articles/10187861
[2]: https://www.meetup.com/Docker-Taipei/events/237978710/

Chu-Siang Lai

March 19, 2017
Tweet

More Decks by Chu-Siang Lai

Other Decks in Technology

Transcript

  1. 簡單的 Dockerfile # This dockerfile uses the ubuntu image #

    VERSION 2 - EDITION 1 # Author: docker_user # Command format: Instruction [arguments / command] .. # 基本映像檔,必須是第一個指令 FROM ubuntu # 維護者: docker_user <docker_user at email.com> (@docker_user) MAINTAINER docker_user [email protected] # 更新映像檔的指令 RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list RUN apt-get update && apt-get install -y nginx RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf # 建立新容器時要執行的指令 CMD /usr/sbin/nginx 一般由基底映像檔資訊、維護者資訊、映像檔操作指令和容器 啟動指令四部分組成。 這裡用到了以下 4 個指令: • FROM • MAINTAINER • RUN • CMD 資料來源: • 《Docker — 從入門到實踐­》正體中文版 | GitBook
  2. 較複雜的 Dockerfile FROM ubuntu MAINTAINER imtnd RUN apt-get update -y

    && \ apt-get install -y apache2 php5 wget php5-mysql RUN wget -q http://sourceforge.net/projects/testlink/files/TestLink%201.9/TestLink%201. 9.14/testlink-1.9.14.tar.gz/download -O testlink-1.9.14.tar.gz &&\ tar zxvf testlink-1.9.14.tar.gz && \ mv testlink-1.9.14 /var/www/html/testlink && \ rm testlink-1.9.14.tar.gz RUN echo "max_execution_time=3000" >> /etc/php5/apache2/php.ini && \ echo "session.gc_maxlifetime=60000" >> /etc/php5/apache2/php.ini ENV APACHE_RUN_USER www-data ENV APACHE_RUN_GROUP www-data ENV APACHE_LOG_DIR /var/log/apache2 ENV APACHE_PID_FILE /var/run/apache2.pid 以 imtnd/testlink image 的 Dockerfile 為例。 ENV APACHE_RUN_DIR /var/run/apache2 ENV APACHE_LOCK_DIR /var/lock/apache2 RUN mkdir -p $APACHE_RUN_DIR $APACHE_LOCK_DIR $APACHE_LOG_DIR RUN mkdir -p /var/testlink/logs /var/testlink/upload_area RUN chmod 777 -R /var/www/html/testlink && \ chmod 777 -R /var/testlink/logs && \ chmod 777 -R /var/testlink/upload_area EXPOSE 80/tcp CMD ["/usr/sbin/apache2","-D", "FOREGROUND"]
  3. 公司的 Dockerfile FROM ubuntu MAINTAINER imtnd RUN apt-get update -y

    && \ apt-get install -y apache2 php5 wget php5-mysql RUN wget -q http://sourceforge.net/projects/testlink/files/TestLink%201.9/TestLink%201.9.14/testlink-1.9.14.tar.gz/download -O testlink-1.9.14.tar.gz &&\ tar zxvf testlink-1.9.14.tar.gz && \ mv testlink-1.9.14 /var/www/html/testlink && \ rm testlink-1.9.14.tar.gz RUN echo "max_execution_time=3000" >> /etc/php5/apache2/php.ini && \ echo "session.gc_maxlifetime=60000" >> /etc/php5/apache2/php.ini …... 絕對沒有那麼簡單!
  4. 混用 Ansible 的 Dockerfile FROM williamyeh/ansible:ubuntu14.04-onbuild MAINTAINER Chu-Siang Lai <[email protected]>

    # fix policy-rc.d for Docker # @see http://www.monblocnotes.com/node/2057 # @see http://askubuntu.com/a/365912 RUN sed -i -e 's/exit\s\s*101/exit 0/' /usr/sbin/policy-rc.d # init RUN sudo apt-get install -y curl git make ;\ git clone https://github.com/chusiang/testlink.ansible.role.git ;\ cd testlink.ansible.role ;\ make init ENV PLAYBOOK testlink.ansible.role/setup.yml RUN ansible-playbook-wrapper -vvv RUN cp run.sh / 以 chusiang/testlink image 的 Dockerfile 為例。 EXPOSE 80 443 VOLUME [ "/data" ] ENTRYPOINT [ "/run.sh" ]
  5. 為什麼要混用 Ansible? 比 Shell Script 好維護 支援多種環境 (實體機、VM、Docker) Infrastructure as

    Code 可一份組態適用多個 OS 透過 Ansible 存取系統變數 不是每家公司都敢在 Productoin 跑 Docker
  6. Reference 《Docker — 從入門到實踐­》正體中文版 | GitBook 28. 怎麼混用 Ansible 建置

    Docker image? | iT 邦幫忙 從 Ansible 到 Docker:混血模式 | 軟體架構・絮語A Google Slides Themes UX Template | Free Google Slides Templates