ã¯ããã«
ãååã®è¨äºã§ã¯ãä»®æ³ç°å¢ã§ã¢ããªã±ã¼ã·ã§ã³ã管çï¼å®è¡ããããã®ãªã¼ãã³ã½ã¼ã¹ã®ã½ããã§ããDockerã®æ¦è¦ã¨ãWindowsç°å¢ã¸ã®ã¤ã³ã¹ãã¼ã«ã®æé ãç´¹ä»ãã¾ãããä»åã®è¨äºã§ã¯ãDockerã®å®è¡ç°å¢ã§ããã³ã³ããã®ãã¨ã¨ãªãDockerã¤ã¡ã¼ã¸ãèªåã§çæã§ããDockerfileã使ã£ã¦ãã³ã¼ãã£ã³ã°ã«ããWebã¢ããªã±ã¼ã·ã§ã³ã®å®è¡ç°å¢ãèªåã§æ§ç¯ããæé ãç´¹ä»ãã¾ãã
対象èªè
ãæ¬è¨äºã¯ã次ã®æ¹ã対象ã«ãã¦ãã¾ãã
- ã³ã¼ãã使ã£ã¦ã¤ã³ãã©ã®æ§æ管çãããã人
- ãããã¯ã¼ã¯ãLinuxã®åºç¤ç¥èããã人
- Webã·ã¹ãã ã®éçºç°å¢ãæ§ç¯ãããã¨ããã人
Dockerfileãã³ã¼ãã£ã³ã°
ãDockerfileã¯ãDockerä¸ã§åä½ãããå®è¡ç°å¢ï¼ã³ã³ããï¼ã®æ§ææ å ±ãè¨è¿°ããããã®ãã¡ã¤ã«ã§ããDockerã¯ããã®Dockerfileããã¨ã«ãã¦ã³ã³ããã®ãã¨ã«ãªããã¤ã¡ã¼ã¸ããä½æãã¾ãã
ãã¾ãBoot2Dockerã®ã³ã³ã½ã¼ã«ä¸ã§ãDockerfileãä½æããããã®ä½æ¥ç¨ã®ãã£ã¬ã¯ããªãä½æãã¾ãã
$ mkdir docker-sample $ cd docker-sample/
ãDockerfileã®åºæ¬æ§æã¯æ¬¡ã®ããã«ãªã£ã¦ãã¾ãã
å½ä»¤ å¼æ°
ãã¾ãã#ã§å§ã¾ãè¡ã¯ã³ã¡ã³ãæã«ãªã£ã¦ãã¾ããå½ä»¤ã¯å¤§æåã§ãå°æåã§ããã¾ãã¾ãããã大æåã§çµ±ä¸ãã¦æ¸ããã¨ããå§ããã¾ãã
ãä»åã¯ãCentOS 6ã«Apache httpdã¨phpãã¤ã³ã¹ãã¼ã«ããç°¡åãªWebã¢ããªã±ã¼ã·ã§ã³å®è¡ç°å¢ãä½æãã¾ãã
OSã¤ã¡ã¼ã¸ã®æå®
ãã¾ããã³ã³ããä¸ã§å®è¡ãããOSã®ã¤ã¡ã¼ã¸ãFROMå½ä»¤ã§æå®ãã¾ãããã®FROMå½ä»¤ã¯å¿ é é ç®ã§ãã®ã§å¿ ãè¨è¿°ãã¦ãã ããã
FROM <ã¤ã¡ã¼ã¸>:<ã¿ã°å>
ãããã§ãDockerã¬ã¸ã¹ããªããèªåçã«ãã¦ã³ãã¼ããã¾ãããã¨ãã°ãCentOSã®ãã¼ã¸ã§ã³6ã使ãã¨ãã¯æ¬¡ã®ããã«ãªãã¾ãããªãã¿ã°åã§ããcentos6ãçç¥ããã¨ãCentOSã®ææ°çããã¦ã³ãã¼ããã¾ãã
FROM centos:centos6
ãä»ã®OSããã£ã¹ããªãã¥ã¼ã·ã§ã³ã使ãããã¨ãã¯ãdocker searchã³ãã³ãã使ãã¾ãããã¨ãã°ãubuntuã®ã¤ã¡ã¼ã¸ãæ¤ç´¢ããã¨ãã¯æ¬¡ã®ããã«ãªãã¾ããDockerHubã«ç»é²ããã¦ããã¦ã¼ã¶ãä½æããç¬èªã¤ã¡ã¼ã¸ãæ¤ç´¢ã§ãã¾ãã
$ docker search ubuntu
ããªããå¿ é é ç®ã§ã¯ããã¾ããããä½æè ã®æ å ±ã¯MAINTAINERå½ä»¤ã«è¨è¿°ãã¾ããååãã¡ã¼ã«ã¢ãã¬ã¹ãªã©ããã¦ããã¾ãã
MAINTAINER SHIHO ASA [email protected]
ããã«ã¦ã§ã¢ã®ã¤ã³ã¹ãã¼ã«
ã次ã«ãWebã¢ããªã±ã¼ã·ã§ã³ãµã¼ãã§å®è¡ããããã®ããã«ã¦ã§ã¢ãªã©ãã¤ã³ã¹ãã¼ã«ãã¾ããCentOSã§ã¯ãWebãµã¼ãã®æ§ç¯ã«å¿ è¦ãªããã«ã¦ã§ã¢ãããã±ã¼ã¸ã¯ãLinuxã®RPMäºæããã±ã¼ã¸ç®¡çã·ã¹ãã ã§ããyumã¬ãã¸ããªã使ã£ã¦ã¤ã³ã¹ãã¼ã«ãã¾ãããããã±ã¼ã¸ã«ãã£ã¦ã¯ææ°çãå°å ¥ã§ããªããã®ãããã¾ããä»åã¯PHP 5.5.ãã¤ã³ã¹ãã¼ã«ãããããrpmã³ãã³ãã使ã£ã¦ãµã¼ããã¼ãã£ã®Remi/EPELã¬ãã¸ããªãå°å ¥ãã¾ãã
ãDockerfileã§ã³ãã³ããå®è¡ããã«ã¯ãRUNå½ä»¤ã使ãã¾ãã
RUN <å®è¡ãããã³ãã³ã>
ããã¨ãã°ãApache httpdã¨PHP5.5ã¤ã³ã¹ãã¼ã«ããã«ã¯ãDockerfileã«æ¬¡ã®ãããªå½ä»¤ãè¨è¿°ãã¾ãã
RUN rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm RUN rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm RUN yum -y install httpd RUN yum -y install --enablerepo=remi-php55 php php-mbstring php-pear
ãPHPæ¬ä½ã«å ãã¦ãæ¥æ¬èªãªã©ãã«ããã¤ãæåã使ãã¢ã¸ã¥ã¼ã«ã§ããphp-mbstringãPHPã®ããã±ã¼ã¸ã©ã¤ãã©ãªéã§ããphp-pearãä½µãã¦ã¤ã³ã¹ãã¼ã«ãã¦ãã¾ãã
Webãã¼ã¸ã®é ç½®
ãWebãµã¼ãã«ãä½æããHTMLãªã©ã®ã³ã³ãã³ããé ç½®ãã¾ããä»åã¯ãPHPã®åä½ç¢ºèªãããããã«ã<?php phpinfo(); ?>ã®ã¿ãã³ã¼ãã£ã³ã°ãããindex.phpãDockerfileã®æ ¼ç´ãã£ã¬ã¯ããªã¨åãå ´æã«ç¨æãã¾ãã
/home/docker/docker-sample |-- Dockerfile |-- index.php
ã次ã«ããã®index.phpãé ç½®ããããã®Dockerfileãæ¸ãã¾ããDockerfileã§ã³ã³ããå ã«ãã¡ã¤ã«ãã³ãã¼ããã«ã¯ADDå½ä»¤ã使ãã¾ã
ADD <ã³ãã¼å ãã¡ã¤ã«> <ã³ã³ããå ã®ã³ãã¼å ãã¡ã¤ã«>
ããã¨ãã°ããã¹ãã®index.phpãã³ã³ããå ã®/var/www/html/index.phpã«ã³ãã¼ããã¨ãã¯æ¬¡ã®ããã«æå®ãã¾ãã
ADD index.php /var/www/html/index.php
ãã³ãã¼å ã®ãã¡ã¤ã«ã¯ç¸å¯¾ãã¹ã§ãæå®ã§ãã¾ãããã³ã³ããå ã®ã³ãã¼å ãã¡ã¤ã«ã¯çµ¶å¯¾ãã¹ã§æå®ããå¿ è¦ãããã¾ãã
Apacheãµã¼ãã¹ã®èµ·å
ãæå¾ã«ãApache httpdã®ãµã¼ãã¹ãèµ·åããããã®Dockerfileãè¨è¿°ãã¾ãããµã¼ãã¹ãèµ·åããã¨ãã¯ãCMDå½ä»¤ã使ãã¾ãã
CMDã["å®è¡ã³ãã³ã","ãã©ã¡ã¼ã¿1", "ãã©ã¡ã¼ã¿2"]
ãWebãµã¼ããèµ·åãããããApache httpdããã©ã¢ã°ã©ã¦ã³ãã§å®è¡ããããã«ã¯æ¬¡ã®ããã«æå®ãã¾ãã
CMD ["/usr/sbin/httpd","-D", "FOREGROUND"]
ãDockerfileã¯ãä¸è¨ä»¥å¤ã«ã次ã®ãããªå½ä»¤ãããã¾ãã
å½ä»¤ | 説æ |
---|---|
FROM | OSã¤ã¡ã¼ã¸ã®æå® |
MAINTAINER | ä½æè æ å ± |
RUN | ã³ãã³ãå®è¡ |
CMD | ã³ã³ããèµ·åæã®ã³ãã³ã |
EXPOSE | éä¿¡ãã¼ãè¨å® |
ENV | ç°å¢å¤æ°æå® |
ADD | ãã¹ãããã®ãã¡ã¤ã«ï¼ãã£ã¬ã¯ããªã³ãã¼ |
COPY | ãã¡ã¤ã«ã®ã³ãã¼ |
VOLUME | ããªã¥ã¼ã ã®ãã¦ã³ã |
USER | ã³ã³ããã®å®è¡ã¦ã¼ã¶ |
WORKDIR | ä½æ¥ãã£ã¬ã¯ããªã®æå® |
ONBUILD | ãã«ãã®å®äºå¾ã«å®è¡ãããå½ä»¤ |
ã詳ããã¯ä»¥ä¸ã®å ¬å¼ãµã¤ããåç §ãã¦ãã ããã