InfoboxCloudã§ã¯ã Dockerã䜿çšããŠUbuntu 14.04ã®å®æããã€ã¡ãŒãžãäœæããŸããã ãµãŒããŒã®äœææã«ãOSã«ãŒãã«å¶åŸ¡ãèš±å¯ãããããã¯ã¹ããã§ãã¯ããããšãå¿ããªãã§ãã ãã;ããã¯Dockerãæ©èœããããã«å¿ èŠã§ãã
Dockerfile
åã®èšäºã§èª¬æããdocker commitã¢ãããŒãã¯ãDockerã«ã¯ãå§ãããŸããã ããã«ãæšæºãµãŒããŒã®ã»ããã¢ããã«æ £ããŠããã®ãšã»ãŒåãããã«ã³ã³ãããŒãæ§æããŸãã
ãã®ã¢ãããŒãã®ä»£ããã«ã Dockerfileã¢ãããŒããšdocker build commandã䜿çšããããšããå§ãããŸãã Dockerfileã¯ãéåžžã®DSLãšDockerã€ã¡ãŒãžã®äœææé ã䜿çšããŸãã ãã®åŸãdockerfileã®æ瀺ã䜿çšããŠæ°ããã€ã¡ãŒãžããã«ãããããã«docker buildã³ãã³ããå®è¡ãããŸãã
Dockerfileã®äœæ
Dockerfileã䜿çšããŠç°¡åãªWebãµãŒããŒã€ã¡ãŒãžãäœæããŸãããã ãŸãããã£ã¬ã¯ããªãšDockerfileèªäœãäœæããŸãã
mkdir static_web cd static_web touch Dockerfile
äœæããããã£ã¬ã¯ããªã¯ãDockerãã³ã³ããã¹ããåŒã³åºãããã³ã³ããã¹ããæ§ç¯ãããã«ãç°å¢ã§ãã Dockerã¯ãã€ã¡ãŒãžã¢ã»ã³ããªãéå§ããããšãDockerããŒã¢ã³ã®åäœäžã«ãã©ã«ããŒå ã®ã³ã³ããã¹ããããŒãããŸãã ãã®ããã«ããŠãDockerããŒã¢ã³ã¯ãã€ã¡ãŒãžã«å«ããã³ãŒãããã¡ã€ã«ããŸãã¯ãã®ä»ã®ããŒã¿ã«ã¢ã¯ã»ã¹ã§ããŸãã
ã€ã¡ãŒãžæ§ç¯æ å ±ãDockerfileã«è¿œå ããŸãã
# Version: 0.0.1 FROM ubuntu:14.04 MAINTAINER Yuri Trukhin <trukhinyuri@infoboxcloud.com> RUN apt-get update RUN apt-get install -y nginx RUN echo 'Hi, I am in your container' \ >/usr/share/nginx/html/index.html EXPOSE 80
Dockerfileã«ã¯ãåŒæ°ä»ãã®åœä»€ã»ãããå«ãŸããŠããŸãã ååœä»€ã¯å€§æåã§æžãããŠããŸãïŒFROMãªã©ïŒã åœä»€ã¯äžããäžã«åŠçãããŸãã ååœä»€ã¯ãã€ã¡ãŒãžã«æ°ããã¬ã€ã€ãŒãè¿œå ããå€æŽãã³ãããããŸãã Dockerã¯ãããã»ã¹ã«åŸã£ãŠæ瀺ãå®è¡ããŸãã
- ã€ã¡ãŒãžããã³ã³ãããŒãèµ·åãã
- åœä»€ã®å®è¡ãšã³ã³ããã®å€æŽ
- æ°ããã€ã¡ãŒãžã¬ã€ã€ãŒã«å€æŽãæžã蟌ãã®ãšåçã®docker commitãå®è¡ãã
- æ°ããã€ã¡ãŒãžããæ°ããã³ã³ãããŒãèµ·åãã
- ãã¡ã€ã«ã§æ¬¡ã®æ瀺ãå®è¡ããããã»ã¹ã®ã¹ããããç¹°ãè¿ããŸãã
ããã¯ãäœããã®çç±ã§Dockerfileã®å®è¡ãåæ¢ããå ŽåïŒããšãã°ãåœä»€ãå®äºã§ããªãå ŽåïŒããã®æ®µéãŸã§ã€ã¡ãŒãžã䜿çšã§ããããšãæå³ããŸãã ããã¯ãããã°ã«éåžžã«äŸ¿å©ã§ããã€ã³ã¿ã©ã¯ãã£ãã«ã€ã¡ãŒãžããã³ã³ãããèµ·åããæåŸã«äœæãããã€ã¡ãŒãžã䜿çšããŠåœä»€ãå®è¡ãããªãã£ãçç±ãèŠã€ããããšãã§ããŸãã
Dockerfileã¯ã³ã¡ã³ãããµããŒãããŠããŸãã ïŒã§å§ãŸãè¡ã¯ã³ã¡ã³ããæå³ããŸãã
Dockerfileã®æåã®åœä»€ã¯åžžã«FROMã§ããå¿ èŠããããã©ã®ã€ã¡ãŒãžããã€ã¡ãŒãžãæ§ç¯ãããã瀺ããŸãã ãã®äŸã§ã¯ãubuntuããŒãžã§ã³14:04ã®ããŒã¹ã€ã¡ãŒãžããã€ã¡ãŒãžãæ§ç¯ããŠããŸãã
次ã«ãã€ã¡ãŒãžäœæè ãšãã®ã¡ãŒã«ã®Dockerã«äŒããMAINTAINERåœä»€ã瀺ããŸãã ããã¯ãç»åãŠãŒã¶ãŒãå¿ èŠã«å¿ããŠäœè ã«é£çµ¡ã§ããããã«ããã®ã«äŸ¿å©ã§ãã
RUNåœä»€ã¯ãç¹å®ã®ã€ã¡ãŒãžã§ã³ãã³ããå®è¡ããŸãã ãã®äŸã§ã¯ãããã䜿çšããŠãAPTãªããžããªãæŽæ°ããNGINXã§ããã±ãŒãžãã€ã³ã¹ããŒã«ããŠããããã¡ã€ã«/usr/share/nginx/html/index.htmlãäœæããŸãã
ããã©ã«ãã§ã¯ãRUNåœä»€ã¯/ bin / sh -cã³ãã³ãã©ãããŒã䜿çšããŠã·ã§ã«å ã§å®è¡ãããŸãã ã·ã§ã«ãªãã§ãã©ãããã©ãŒã ã§åœä»€ãå®è¡ããå ŽåããŸãã¯åã«ã·ã§ã«ãªãã§ã¹ããŒãã¡ã³ããå®è¡ããå Žåã¯ãå®è¡åœ¢åŒãæå®ã§ããŸãã
RUN ["apt-get", "install", "-y", "nginx"]
ãã®åœ¢åŒã䜿çšããŠãå®è¡ããã³ãã³ããšã³ãã³ããã©ã¡ãŒã¿ãŒãå«ãé åãæå®ããŸãã
次ã«ãã³ã³ããå ã®ã¢ããªã±ãŒã·ã§ã³ãã³ã³ããå ã®ç¹å®ã®ããŒãã䜿çšããå¿ èŠãããããšãDockerã«äŒããEXPOSEã¹ããŒãã¡ã³ããæå®ããŸãã ããã¯ãã³ã³ããããŒãïŒãã®äŸã§ã¯ããŒã80ïŒã§å®è¡ãããŠãããµãŒãã¹ã«èªåçã«ã¢ã¯ã»ã¹ã§ãããšããæå³ã§ã¯ãããŸããã ã»ãã¥ãªãã£äžã®çç±ãããDockerã¯ããŒããèªåçã«éããŸãããããŠãŒã¶ãŒãdocker runã³ãã³ãã§ãããè¡ãããšãæåŸ ããŠããŸãã å€ãã®EXPOSEåœä»€ãæå®ããŠãã©ã®ããŒããéãå¿ èŠããããã瀺ãããšãã§ããŸãã EXPOSEã¹ããŒãã¡ã³ãã¯ãã³ã³ããéã§ããŒãã転éããå Žåã«ã圹ç«ã¡ãŸãã
ãã¡ã€ã«ããç»åãäœæãã
docker build -t trukhinyuri/nginx ~/static_web
ããã§ãtrukhinyuriã¯ã€ã¡ãŒãžãä¿åããããªããžããªã®ååã§ãnginxã¯ã€ã¡ãŒãžã®ååã§ãã æåŸã®ãã©ã¡ãŒã¿ãŒã¯ãDockerfileããããã©ã«ããŒãžã®ãã¹ã§ãã ã€ã¡ãŒãžã®ååãæå®ããªãå Žåãèªåçã«ææ°ã®ååãä»ããããŸãã Dockerfileãããgitãªããžããªãæå®ããããšãã§ããŸãã
docker build -t trukhinyuri/nginx \ git@github.com:trukhinyuri/docker-static_web
ãã®äŸã§ã¯ãDockerã®ã«ãŒããã£ã¬ã¯ããªã«ããDockerfileããã€ã¡ãŒãžãæ§ç¯ããŠããŸãã
ã³ã³ããã¹ããã«ãã®ã«ãŒãã«.dockerignoreãã¡ã€ã«ãããå ŽåãäŸå€ãã¿ãŒã³ã®ãªã¹ããšããŠè§£éãããŸãã
åœä»€ã倱æãããšã©ããªããŸããïŒ
Dockerfileã®nginxã®ååãnginã«å€æŽããŠã¿ãŸãããã
ã€ã¡ãŒãžID 066b799ea548ã䜿çšããŠãæåŸãã2çªç®ã®ã¹ãããããã³ã³ãããŒãäœæã§ããŸãã
docker run -i -t 066b799ea548 / bin / bash
å®è¡ããããã°ããŸãã
ããã©ã«ãã§ã¯ãDockerã¯ãã¹ãŠã®ã¹ãããããã£ãã·ã¥ããã¢ã»ã³ããªãã£ãã·ã¥ãæ§ç¯ããŸãã ããšãã°ãææ°ã®apt-getæŽæ°ã䜿çšãããªã©ããã£ãã·ã¥ãç¡å¹ã«ããã«ã¯ã-no-cacheãã©ã°ã䜿çšããŸãã
docker build --no-cache -t trukhinyuri/nginx
ãã³ãã¬ãŒãåã«ã¢ã»ã³ããªãã£ãã·ã¥ã䜿çšãã
ã¢ã»ã³ããªãã£ãã·ã¥ã䜿çšããŠãDockerfileããåçŽãªãã³ãã¬ãŒãã®åœ¢åŒã§ã€ã¡ãŒãžãæ§ç¯ã§ããŸãã ããšãã°ãUbuntuã§APTãã£ãã·ã¥ãæŽæ°ããããã®ãã³ãã¬ãŒãïŒ
FROM ubuntu:14.04 MAINTAINER Yuri Trukhin <trukhinyuri@infoboxcloud.com> ENV REFRESHED_AT 2014â10â16 RUN apt-get -qq update
ENVåœä»€ã¯ãã€ã¡ãŒãžå ã®ç°å¢å€æ°ãèšå®ããŸãã ãã®å Žåããã³ãã¬ãŒãããã€æŽæ°ããããã瀺ããŸãã ãã«ããããã€ã¡ãŒãžãæŽæ°ããå¿ èŠãããå Žåã¯ãENVã®æ¥ä»ãå€æŽããã ãã§ãã Dockerã¯ãã£ãã·ã¥ããã©ãã·ã¥ããã€ã¡ãŒãžå ã®ããã±ãŒãžããŒãžã§ã³ãæåŸã«ãªããŸãã
Dockerfileã®æé
ä»ã®Dockerfileã®æ瀺ãèŠãŠã¿ãŸãããã å®å šãªãªã¹ãã¯ããã«ãããŸã ã
CMD
CMDåœä»€ã¯ãã³ã³ããã®å®è¡äžã«å®è¡ããã³ãã³ãã瀺ããŸãã RUNã³ãã³ããšã¯ç°ãªããæå®ãããã³ãã³ãã¯ã€ã¡ãŒãžã®ãã«ãäžã§ã¯ãªããã³ã³ãããŒã®èµ·åäžã«å®è¡ãããŸãã
CMD ["/bin/bash", "-l"]
ãã®å Žåãbashãå®è¡ããé åãšããŠãã©ã¡ãŒã¿ãŒãæž¡ããŸãã é åã®åœ¢åŒã§ã¯ãªãã³ãã³ããæå®ããå Žå-/ bin / sh -cã§å®è¡ãããŸãã docker runã䜿çšããŠCMDã³ãã³ãããªãŒããŒããŒãã§ããããšã«æ³šæããŠãã ããã
ãšã³ããªãŒãã€ã³ã
å€ãã®å ŽåãCMDã³ãã³ãã¯ENTRYPOINTãšæ··åãããŸãã éãã¯ãã³ã³ããã®èµ·åæã«ENTRYPOINTããªãŒããŒããŒãã§ããªãããšã§ãã
ENTRYPOINT ["/usr/sbin/nginx"]
ã³ã³ãããèµ·åãããšããã©ã¡ãŒã¿ã¯ENTRYPOINTã§æå®ãããã³ãã³ãã«æž¡ãããŸãã
docker run -d trukhinyuri/static_web -g "daemon off"
ENTRYPOINTãšCMDãçµã¿åãããããšãã§ããŸãã
ENTRYPOINT ["/usr/sbin/nginx"] CMD ["-h"]
ãã®å ŽåãENTRYPOINTã®ã³ãã³ãã¯ã©ã®ãããªå Žåã§ãå®è¡ãããã³ã³ããã®èµ·åæã«ä»ã®ã³ãã³ããéä¿¡ãããªãå Žåã¯CMDã®ã³ãã³ããå®è¡ãããŸãã å¿ èŠã«å¿ããŠã-entrypointãã©ã°ã䜿çšããŠENTRYPOINTã³ãã³ãããªãŒããŒããŒãã§ããŸãã
WORKDIR
WORKDIRã䜿çšãããšãENTRYPOINTããã³CMDã³ãã³ããèµ·åããäœæ¥ãã£ã¬ã¯ããªãèšå®ã§ããŸãã
WORKDIR /opt/webapp/db RUN bundle install WORKDIR /opt/webapp ENTRYPOINT ["rackup"]
-wãã©ã°ã䜿çšããŠãå®è¡æã«ã³ã³ããäœæ¥ãã£ã¬ã¯ããªãåããŒãã§ããŸãã
ãŠãŒã¶ãŒ
ã€ã¡ãŒãžãå®è¡ãããŠãŒã¶ãŒãæå®ããŸãã ãŠãŒã¶ãŒåãŸãã¯UIDãšã°ã«ãŒããŸãã¯GIDãæäŸã§ããŸãã
USER user USER user:group USER uid USER uid:gid USER user:gid USER uid:group
ã³ã³ããã®èµ·åæã«ãåè©-uã䜿çšããŠãã®ã³ãã³ãããªãŒããŒããŒãã§ããŸãã ãŠãŒã¶ãŒãæå®ãããŠããªãå Žåãããã©ã«ãã§ã«ãŒãã䜿çšãããŸãã
ããªã¥ãŒã
VOLUMEåœä»€ã¯ãã€ã¡ãŒãžã«ããªã¥ãŒã ãè¿œå ããŸãã ããªã¥ãŒã ã¯ã1ã€ä»¥äžã®ã³ã³ãããŒå ã®ãã©ã«ããŒããŸãã¯Union File SystemïŒUFSïŒãä»ããŠè»¢éããããã¹ããã©ã«ããŒã§ãã
ããªã¥ãŒã ã¯ã³ã³ããéã§å ±æãŸãã¯åå©çšã§ããŸãã ããã«ãããã€ã¡ãŒãžã«ã³ãããããã«ããŒã¿ãè¿œå ããã³å€æŽã§ããŸãã
VOLUME ["/opt/project"]
äžèšã®äŸã§ã¯ãã€ã¡ãŒãžããäœæãããã³ã³ããã«å¯ŸããŠããŠã³ããã€ã³ã/ opt /ãããžã§ã¯ããäœæãããŸãã ããã«ãããã¢ã¬ã€å ã®è€æ°ã®ããªã¥ãŒã ãæå®ã§ããŸãã
è¿œå
ADDåœä»€ã¯ããã«ãç°å¢ãããã¡ã€ã«ãŸãã¯ãã©ã«ããŒãã€ã¡ãŒãžã«è¿œå ããŸããããã¯ãããšãã°ã¢ããªã±ãŒã·ã§ã³ãã€ã³ã¹ããŒã«ãããšãã«åœ¹ç«ã¡ãŸãã
ADD software.lic /opt/application/software.lic
ãœãŒã¹ã¯ãURLããã¡ã€ã«åããŸãã¯ãã£ã¬ã¯ããªã«ããããšãã§ããŸãã
ADD http://wordpress.org/latest.zip /root/wordpress.zip
ADD latest.tar.gz /var/www/wordpress/
æåŸã®äŸã§ã¯ãtar.gzã¢ãŒã«ã€ãã¯/ var / www / wordpressã«è§£åãããŸãã å®å ãã¹ãæå®ãããŠããªãå Žåããã£ã¬ã¯ããªãå«ãå®å šãªãã¹ã䜿çšãããŸãã
ã³ããŒ
COPYåœä»€ã¯ããã«ãã³ã³ããã¹ãããããŒã«ã«ãã¡ã€ã«ãã³ããŒããããã®ãã®ã§ããããã¡ã€ã«ã®è§£åããµããŒãããªããšããç¹ã§ADDãšã¯ç°ãªããŸãã
COPY conf.d/ /etc/apache2/
ãªã³ãã«ã
ONBUILDã¹ããŒãã¡ã³ãã¯ãç»åã«ããªã¬ãŒãè¿œå ããŸãã ããªã¬ãŒã¯ãç»åãå¥ã®ç»åã®ããŒã¹ãšããŠäœ¿çšãããå Žåãããšãã°ãç»åã«å¿ èŠãªãœãŒã¹ã³ãŒãããŸã å©çšå¯èœã§ã¯ãªãããç¹å®ã®ç°å¢ãå¿ èŠãªå Žåã«å®è¡ãããŸãã
ONBUILD ADD . /app/src ONBUILD RUN cd /app/src && make
ã³ã³ããéã®éä¿¡
ååã®èšäºã§ã¯ãDockeråé¢ã³ã³ãããŒãå®è¡ããæ¹æ³ãšããã¡ã€ã«ã·ã¹ãã ããããã«è»¢éããæ¹æ³ã瀺ããŸããã ããããã¢ããªã±ãŒã·ã§ã³ãçžäºã«éä¿¡ããå¿ èŠãããå Žåã¯ã©ãã§ãããã 2ã€ã®æ¹æ³ããããŸããããŒã転éã«ããéä¿¡ãšã³ã³ããã®ãªã³ã¯ã§ãã
ããŒã転é
ãã®éä¿¡æ¹æ³ã¯ãã§ã«ä»¥åã«ç€ºãããŠããŸãã ããŒã転éã®ãªãã·ã§ã³ãããå°ãåºãèŠãŠã¿ãŸãããã
Dockerfileã§EXPOSEãŸãã¯-pãã©ã¡ãŒã¿ãŒport_numberã䜿çšãããšãã³ã³ãããŒããŒãã¯ä»»æã®ãã¹ãããŒãã«ãã€ã³ããããŸãã ãã®ããŒãã¯ã docker psãŸãã¯containerã®docker portã³ãã³ãcontainer_name port_numberã§è¡šç€ºã§ããŸãã ã€ã¡ãŒãžãäœæããæç¹ã§ã¯ãã³ã³ããã®èµ·åæã«ãã·ã³ã§äœ¿çšå¯èœãªããŒããããããªãå ŽåããããŸãã
ã³ã³ãããŒããŒãããã€ã³ãããç¹å®ã®ãã¹ãããŒãã瀺ãã«ã¯ãdocker run -pãã©ã¡ãŒã¿ãŒhost_portïŒcontainer_portã䜿çšã§ããŸãã
ããã©ã«ãã§ã¯ãããŒãã¯ãã·ã³ã®ãã¹ãŠã®ã€ã³ã¿ãŒãã§ãŒã¹ã§äœ¿çšãããŸãã ããšãã°ãlocalhostã«æ瀺çã«ãã€ã³ãã§ããŸãã
docker run -p 127.0.0.1:80:80
/ udpãæå®ããŠãUDPããŒãããã€ã³ãã§ããŸãã
docker run -p 80:80/udp
ã³ã³ããã®ãªã³ã¯
ãããã¯ãŒã¯ããŒããä»ããéä¿¡ã¯ãéä¿¡ãã1ã€ã®æ¹æ³ã«ãããŸããã Dockerã¯ãè€æ°ã®ã³ã³ãããŒããªã³ã¯ãã1ã€ã®ã³ã³ãããŒããå¥ã®ã³ã³ãããŒã«æ¥ç¶æ å ±ãéä¿¡ã§ãããªã³ã¯ã·ã¹ãã ãæäŸããŸãã
æ¥ç¶ã確ç«ããã«ã¯ãã³ã³ããã®ååã䜿çšããå¿ èŠããããŸãã åã«ç€ºããããã«ãäœææã«ã³ã³ããã«ååãä»ããã«ã¯ã-nameãã©ã°ã䜿çšããŸãã
2ã€ã®ã³ã³ãããwebãšdbããããšããŸãã ãªã³ã¯ãäœæããã«ã¯ãWebã³ã³ãããŒãåé€ãã-link nameïŒaliasã³ãã³ãã䜿çšããŠåäœæããŸãã
docker run -d -P --name web --link db:db trukhinyuri/webapp python app.py
docker -psã䜿çšãããšãé¢é£ããã³ã³ãããŒã衚瀺ã§ããŸãã
ãªã³ã¯ãããšå®éã«äœãèµ·ãããŸããïŒ ã³ã³ãããäœæãããããèªäœã«é¢ããæ å ±ãåä¿¡è ã³ã³ããã«æäŸãããŸãã ããã«ã¯2ã€ã®æ¹æ³ããããŸãã
- ç°å¢å€æ°çµç±
- / etc / hostsçµç±
ç°å¢å€æ°ã¯ãenvã³ãã³ããå®è¡ããŠè¡šç€ºã§ããŸãã
$ sudo docker run --rm --name web2 --link db:db training/webapp env . . . DB_NAME=/web2/db DB_PORT=tcp://172.17.0.5:5432 DB_PORT_5432_TCP=tcp://172.17.0.5:5432 DB_PORT_5432_TCP_PROTO=tcp DB_PORT_5432_TCP_PORT=5432 DB_PORT_5432_TCP_ADDR=172.17.0.5
DB_ãã¬ãã£ãã¯ã¹ã¯ããšã€ãªã¢ã¹ã³ã³ããããååŸãããŸããã
ãã¹ãããã®æ å ±ãåçŽã«äœ¿çšã§ããŸããããšãã°ãping dbã³ãã³ãïŒdb-aliasïŒãæ©èœããŸãã
ãããã«
ãã®èšäºã§ã¯ãDockerfileã®äœ¿çšæ¹æ³ãšãã³ã³ãããŒéã®éä¿¡ãæŽçããæ¹æ³ãåŠã³ãŸããã ããã¯æ°·å±±ã®äžè§ã«ããããå€ãã¯èå°è£ã«æ®ã£ãŠãããä»åŸæ€èšãããäºå®ã§ãã ããã«èªãã«ã¯ã The Docker Bookããå§ãããŸãã
Dockerã䜿çšããæ¢è£œã®ç»åã¯ã InfoboxCloudã¯ã©ãŠãã§å©çšã§ããŸãã
Habrã§è³ªåã§ããªãå Žåã¯ã InfoboxCloudã³ãã¥ããã£ã§è³ªåããããšãã§ããŸãã
èšäºã«ééããèŠã€ããå Žåãèè ã¯åãã§ä¿®æ£ããŸãã PMã«é£çµ¡ããããããã«ã€ããŠé»åã¡ãŒã«ãéä¿¡ããŠãã ããã
Dockerã®äœ¿çšã«æåããŸããïŒ