ãã®ããã»ã¹å šäœããæž©ãããã¥ãŒããã¯ã»ã«ã¢ãŒãã䜿çšããå€ãè¯ãã³ã³ãã¥ãŒã¿ãŒã²ãŒã ãšããŠçŽ¹ä»ããŸããã
ã©ã®ã²ãŒã ã«ããµããããããã«ãããªãŒãã£ã³ã°ããå§ããŸãã ã¯ããã«ãDockerã®åºåãèŠãŠã¿ãŸãããã
Dockerã® Webãµã€ãã§ã®å€ãã®åºåã®çŽæãçç¥ã§ããŸããã€ãŸããéçºãšå±éã®é床ã13åã«é«ããéçºã®ç§»æ€æ§ãé«ããããšãçŽæããŸãïŒç¹ã«ããç§ã®ãã·ã³ã§æ©èœãããç§è·¡ãåãé€ããŸãïŒã ããããããã¯æ¬åœã§ããïŒ
ä»ããããã®å£°æã蚌æ/åè«ããããšããŸãã
ã¬ãã«1
ã²ãŒã ã«åå ããŠããã®ã§ãäºæ³ã©ãããæãåçŽãªã¬ãã«ããå§ããŸãã
æåã®ã¬ãã«ã§ã®ç§ãã¡ã®äœ¿åœã¯äœã§ããïŒ ããããå€ãã®äººã«ãšã£ãŠãããã¯éåžžã«ç°¡åã§ç解ãããããã®ã§ããæãåå§çãªJavaã¢ããªã±ãŒã·ã§ã³ã§ããDockerã§ãã©ãããããå¿ èŠããããŸãã
ãããè¡ãã«ã¯ãç§è·¡çãªHello JavaMeetupïŒãåºåããåçŽãªJavaã¯ã©ã¹ãå¿ èŠã§ãã ãŸãã Dockerã€ã¡ãŒãžãäœæããã«ã¯ã Dockerfileãå¿ èŠã§ã ã æ§æã«é¢ããŠã¯ãéåžžã«ç°¡åã§ã-åºæ¬ã€ã¡ãŒãžãšããŠjavaïŒ8ã䜿çšããJavaã¯ã©ã¹ãè¿œå ãïŒ ADDã³ãã³ãïŒããããã³ã³ãã€ã«ãïŒ RUNã³ãã³ãã䜿çšïŒãã³ã³ãããŒã®èµ·åæã«å®è¡ãããã³ãã³ããæå®ããŸãïŒ CMDã³ãã³ãïŒã
HelloWorld.java
public class HelloWorld { public static void main (String[] args) { System.out.println("Hello World!"); } }
Dockerfile
FROM java:8 ADD HelloWorld.java . RUN javac HelloWorld.java CMD ["java", "HelloWorld"]
Dockerã³ãã³ãïŒ
$ docker build -t java-app:demo . $ docker images $ docker run java-app:demo
ãã®ããžãã¹ããã¹ãŠåéããã«ã¯ãå®éã«ã¯1ã€ã®ã³ãã³ããå¿ èŠã§ããããã¯docker buildã§ãã çµã¿ç«ãŠããšãã«ãç»åã®ååãšããã«å²ãåœãŠãã¿ã°ãæå®ããŸãïŒãã®ããã«ããŠãã¢ããªã±ãŒã·ã§ã³ã®ããŸããŸãªã¢ã»ã³ããªãããŒãžã§ã³ç®¡çã§ããŸãïŒã 次ã«ã docker imagesã³ãã³ããå®è¡ããŠãã€ã¡ãŒãžãçµã¿ç«ãŠãããšã確èªããŸãã ã¢ããªã±ãŒã·ã§ã³ãå®è¡ããã«ã¯ã docker runã³ãã³ããå®è¡ããŸã ã
ããããããã¹ãŠãå®ç§ã«è¡ããŸããã
ã¯ããããã·ã§ã³ãå®äºããŸããã ããããç§ãã¡ãšæèŠã亀ããçç±ããããŸãã ãªããããªãã¯å°ããŸãããããŠãã®ãããªééãã次ååé¿ããæ¹æ³ïŒ
- 䜿çšããåºæ¬çãªdockerã€ã¡ãŒãžã¯éæšå¥šãšå®£èšãããŠãããdockerã³ãã¥ããã£ã§ã¯ãµããŒããããŠããŸããã DockerCon17ã§ãããJava EEã®äžçã§ããç¥ãããŠããå€ãã®Arun Guptaã¯ãopenjdkãããŒã¹ã€ã¡ãŒãžãšããŠäœ¿çšããããšãæšå¥šããŠããŸãïŒããã«ã€ããŠã¯ãã€ã¡ãŒãžã®èª¬æãšæŽæ°æ¥ä»https://hub.docker.com/_/openjdk/ã§ã瀺åãããŠããŸã ïŒã
- ãµã€ãºãå°ããããã«ã¯ãAlpineã«åºã¥ãç»åã䜿çšããããšããå§ãããŸãããã®ååžã«åºã¥ãç»åã¯æã軜éã§ãã
- jdkã€ã¡ãŒãžã䜿çšããŠã³ã³ãã€ã«ããjreã®äœ¿çšãéå§ããŸãïŒãã£ã¹ã¯ã¹ããŒã¹ãç¯çŽããŸããããŸã å¿ èŠã§ãïŒã
ããã§ãæåã®ã¬ãã«ãæ£åžžã«å®äºãããšæ³å®ã§ããŸãã 2çªç®ã«äžæããŸãã
æåã®ã¬ãã«ãæž¡ãããã®äŸ¿å©ãªãªã³ã¯
ã¬ãã«2
Javaãæ±ãå ŽåãMavenãŸãã¯Gradleã䜿çšããå¯èœæ§ãæãé«ããªããŸãã ãããã£ãŠããããžã§ã¯ããšããã«ãŒã€ã¡ãŒãžããã«ãããããã®åäžã®ç°å¢ãåŸãããã«ããã«ãã·ã¹ãã ãDockerãšäœããã®åœ¢ã§çµ±åãããšäŸ¿å©ã§ãã
幞ããªããšã«ãã»ãšãã©ã®ãã©ã°ã€ã³ã¯ãã§ã«äœæãããŠããŸã-MavenãšGradleã®äž¡æ¹ã
Docker fabric8ioããã³spotifyçšã®æã人æ°ã®ããMavenãã©ã°ã€ã³ã Gradleã®å Žåã¯ããã©ã°ã€ã³Benjamin Mushkoã䜿çšã§ããŸããããã¯ãGradleã®éçºè ã§ããããGradle in Actionããšããæ¬ã®èè ã§ãã
Dockerãã¢ããªã±ãŒã·ã§ã³ã®ãã«ãã·ã¹ãã ã«æ¥ç¶ããã«ã¯ãgradleæ§æã§ãã³ã³ãããŒãåéããŠå®è¡ããããã€ãã®ã¿ã¹ã¯ãäœæããã ãã§ãªããã«ããŽãªããããã€ãã®äžè¬æ å ±ã瀺ããŸããããŒã¹ã€ã¡ãŒãžãšããŠäœ¿çšããã€ã¡ãŒãžãšãçµã¿ç«ãŠãã€ã¡ãŒãžã«ä»ããåå
åé·ã«ãªããªãããã«ããŸããããbmuschko/ gradle-docker-pluginãšGradle ãã©ã°ã€ã³ã䜿çšããŠãã ãã ïŒMavenãã¡ã³ãšXMLæ奜家ã®çãããå°ã ãåŸ ã¡ãã ããïŒïŒã
æåã®ã¿ã¹ã¯ãå®äºããŸãããä»ã¯ãã®ãã©ã°ã€ã³ã䜿çšããŸãã build.gradleã®å¿ èŠãªäž»èŠéšåïŒ
docker { javaApplication { baseImage = 'openjdk:latest' tag = 'java-app:gradle' } } task createContainer(type: DockerCreateContainer) { dependsOn dockerBuildImage targetImageId { dockerBuildImage.getImageId() } } task startContainer(type: DockerStartContainer) { dependsOn createContainer targetContainerId { createContainer.getContainerId() } }
gradle startContainerã³ãã³ããå®è¡ããŠãã€ã¡ãŒãžã®ã¢ã»ã³ããªãŒãšã³ã³ãããŒã®èµ·åã確èªããŸãã ãããããHello JavaMeetupïŒããšãããŠã§ã«ã«ã ã¡ãã»ãŒãžã®ä»£ããã«ããã«ããæåãããšããéç¥ãåãåããŸãïŒ
ã©ããã§ééããããšããããŸããïŒ å®éã«ã¯ãã³ã³ããã®åºåãã³ã³ãœãŒã«ã«ãªãã€ã¬ã¯ãããã ãã§ãã
task logContainer(type: DockerLogsContainer, dependsOn: startContainer) { targetContainerId { startContainer.getContainerId() } follow = true tailAll = true onNext { message -> logger.quiet message.toString() } }
gradle logContainerã³ãã³ããå®è¡ãã ... Hoorayãåæãããã¡ãã»ãŒãžãšåæ Œããã¬ãã«ã
å®éãããããã¹ãŠã§ãã Dockerfileãå¿ èŠãããŸããïŒãã ããäœåãªãã®ã§ã¯ãããŸãã-Gradleãæå ã«ãªãããšã¯æ±ºããŠããããŸããïŒã
ç¶ããŸãããïŒ
ã¬ãã«3
ã»ãšãã©ã®å Žåãå®éã«ã¯ãã¢ããªã±ãŒã·ã§ã³ã¯ãHello Worldãã衚瀺ãããããé£ããããšãè¡ããŸãã ãããã£ãŠã次ã®ã¬ãã«ã§ã¯ãããŒã¿ããŒã¹ããããã€ãã®ã¬ã³ãŒããååŸããSpring Webã¢ããªã±ãŒã·ã§ã³ã§ããè€éãªã¢ããªã±ãŒã·ã§ã³ã®å®è¡æ¹æ³ãåŠç¿ããŸãã
ããŒã¹ãšã¢ããªã±ãŒã·ã§ã³èªäœãäžããããã«ã Docker Composeã䜿çšããŸãã æåã«ãæ°ãããã¡ã€ã«ãäœæããŸãïŒå¥ã®æ°ããæ§æãã¡ã€ã«ãåŒåžããŸãããåæ¢ããŸãããïŒïŒ- docker-compose.yml ã ãã®äžã§ãããŒã¿ããŒã¹ã®ã€ã¡ãŒãžãšã¢ããªã±ãŒã·ã§ã³ã®ã€ã¡ãŒãžãäžããããã®ãµãŒãã¹ãåã«èŠå®ããŸãã Docker Composeèªäœã¯ãçŸåšã®ãã£ã¬ã¯ããªã§ymlãã¡ã€ã«ãèŠã€ããå¿ èŠãªã³ã³ãããŒãšã€ã¡ãŒãžãããã¯ã¢ãããŸãã¯åéããŸãã
ãã®å šäœãéå§ããããã«ãæåã«ç»åãåéããŸãã ãã®äŸã§ã¯ãDockerã®mavenãã©ã°ã€ã³ïŒhoorayãXMLïŒïŒãfabric8ioãã䜿çšããããããæåã«mvn installã³ãã³ããå®è¡ããŸã ã
<plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.20.1</version> <configuration> <images> <image> <name>app</name> <build> <dockerFileDir>${project.basedir}/src/main/docker</dockerFileDir> <assembly> <mode>dir</mode> <targetDir>/app</targetDir> <descriptor>${project.basedir}/src/main/docker/assembly.xml</descriptor> </assembly> </build> </image> </images> </configuration> <executions> <execution> <id>build</id> <phase>install</phase> <goals> <goal>build</goal> </goals> </execution> </executions> </plugin>
ãããžã§ã¯ããšdockerã€ã¡ãŒãžãã¢ã»ã³ãã«ããããŸã§åŸ ã¡ãymlãã¡ã€ã«ããããã£ã¬ã¯ããªã«ç§»åããŠdocker-compose up -dã³ãã³ããå®è¡ããŸãã
docker psãå®è¡ããŠãäž¡æ¹ã®ã³ã³ãããŒãå®è¡ãããŠããããšã確èªããŸãã
Webã¢ããªã±ãŒã·ã§ã³ãåäœããããŒã¿ããŒã¹ããäœããåãåºãããšã確èªããããã«ãããŒã¿ããŒã¹å ã®äœããçŽæ¥å€æŽããã¢ãã¬ã¹httpïŒ// localhostïŒ8080 /ã«ã¢ã¯ã»ã¹ããŠãç®çã®ããŒã¿ã確èªããŸãã
ããã¯ãã¹ãŠè€éã«èŠãããããããŸããããå®éã«ã¯éåžžã«ç°¡åã§ãã 第3ã¬ãã«ãå®äºããŸããã ãŸããã»ãšãã©ã
ãŸã ããŒãã¹ã¬ãã«ããããŸãã ãã®äžã§ãå°ãïŒéåžžã«å°ããïŒDocker SwarmãåçããŸã ãæ£ç¢ºã«ã¯ã Docker Swarm Modeã§åçããŸã ã
ããŒãã¹ã¬ãã«
Docker Swarm Modeã¯ç¹ã«è€éã§ã¯ãããŸãã-Dockerãç«ã€ãã·ã³ã®ã¯ã©ã¹ã¿ãŒã§ãã ãŠãŒã¶ãŒã«ãšã£ãŠããã®ã¯ã©ã¹ã¿ãŒã¯1å°ã®ãã·ã³ã®ããã«èŠãããã¹ãŠã®ããŒã ã¯ããã®Docker Swarmãããã§ãªãã£ãå Žåãšã»ãŒåãããã«æ©èœããŸãã
Swarmã¢ãŒãã§ã¯ãã¢ããªã±ãŒã·ã§ã³ã®è€æ°ã®ã€ã³ã¹ã¿ã³ã¹ãå®è¡ã§ããŸã-ããšãã°ãè² è·åæ£ã®ããã ãŸããã¹ã¿ãã¯ãªã©ã®æœè±¡åãããã«è¡šç€ºãããŸããDockerSwarmã䜿çšãããšãã¢ããªã±ãŒã·ã§ã³å šäœããããã€ã§ããŸãã ãŸããéåžžã®ã¹ã±ãŒãªã³ã°ãšåæ§ã«ã ã¹ã¿ãã¯ã®è€æ°ã®ã¬ããªã«ãæ¡åŒµã§ããŸã ã
Swarmã¢ãŒãã®Dockerã³ãã³ãïŒ
$ docker service create --name japp --publish 8080:80 --replicas 3 java-app:demo $ docker stack deploy -c docker-compose.yml javahelloworld
å®éãã³ãã³ãæ§æã¯éåžžã«åçŽã§ãéåžžã®ã³ã³ãããŒã®äœæã«äŒŒãŠããŸãã
docker composeã䜿çšã§ããŸãã
$ docker-compose scale jm-app=3
ããŠãéå»3ã€ã®ã¬ãã«ã§ãJavaã¢ããªã±ãŒã·ã§ã³ã®ç§»æ€æ§ãéæããŸããã æåŸã®ã¬ãã«ã«é²ã¿ãDockerããç§ã®ãã·ã³ã§åäœããããšãããã¬ãŒãºããã¯ãæå³ããªããªããšãã声æã確èªãŸãã¯åè«ãããšãã§ãã
æçµã¬ãã«
éãã¢ããªã±ãŒã·ã§ã³ããããšæ³åããŠãã ããã ãŸãã¯ãåããã·ã³äžã«ååšã§ããå€æ°ã®ãã€ã¯ããµãŒãã¹ã ãã®å ŽåãJavaã¢ããªã±ãŒã·ã§ã³ïŒæ£ç¢ºã«ã¯JVMïŒã¯ããã¹ããã·ã³ã®ãªãœãŒã¹ããããæŠãã§ãç§ãã¡ã®å°ããªã·ããã¬ã¹ã¯ãžã©ãšç¢ºãã«æ ŒéããŸãã ãšããã§ãããã¯ãã®èšäºã§è©³ãã説æãããŠããŸã ã
ãã®ã¬ãã«ã§ã¯ãã³ãŒãã®äŸã¯å°ãªããªããŸãããç°ãªãdocker container launch configurationããããŸãã Dockerã䜿çšããããã»ã¹ãšãªãœãŒã¹ãåé¢ããäž»ãªæ段ã¯ã cgroupãšåå空éã§ãã ããããäž»ãªåé¡ã¯ãããããã¹ãŠã®Javaãå°ããªãã©ã ã§ãããšããããšã§ãã 圌女ã¯è²ªæ¬²ã§å°ã貪欲ã§ã-memoryãã©ã°ã䜿çšããŠJavaã¢ããªã±ãŒã·ã§ã³ã§ã³ã³ãããäœæãããšãã«ã¡ã¢ãªå¶éãèšå®ããŠããå®éã«ã¯ããå€ãã®ãªãœãŒã¹ãããããšãããããŸãã ããã確èªããã«ã¯ãã³ã³ããå ã§freeã³ãã³ããå®è¡ããŸãã ãããããJava 8ã®äžè¬çãªæšå¥šäºé ã«åŸã£ãŠãâXmxãã©ã¡ãŒã¿ãŒãèšå®ããâmemoryãã©ã¡ãŒã¿ãŒãâXmxã®å°ãªããšã2åã«ããŸãã Java 9ã®åéããã®æå ±-cgroupã®ãµããŒããè¿œå ãããŸããã
Javaã§ã®ã¡ã¢ãªãªãŒã¯ã®ã·ãã¥ã¬ãŒã·ã§ã³ã¯éåžžã«ç°¡åã§ãã æ¢è£œã®ç»åvalentinomiazzo / jvm-memory-testãååŸããããŸããŸãªããŒããµã€ãºãªãã·ã§ã³ãšdockerçšã®--memoryã§å®è¡ããŸãã
æåã®ã±ãŒã¹ã§ã¯ãã³ã³ããã®ã¡ã¢ãªãJavaã¢ããªã±ãŒã·ã§ã³ãããå°ãªããããäžæ確ãªãšã©ãŒãçºçããŸãã ãããŠãOutOfMemoryExceptionãååŸããããšæããŸãã ãæ»ãã ãã³ã³ããã調ã¹ããšãã³ã³ãããOOMKillerã«ãã£ãŠæ®ºãããããšã«æ°ä»ãã§ããããããã¯ãäºæž¬ã§ããªãçµæãJavaããã»ã¹ã®ããªãŒãºããªãœãŒã¹ã®äžé©åãªã·ã£ããããŠã³ããã®ä»ã®ããããäžå¿«ãªãã®ã«ã€ãªããå¯èœæ§ããããŸãïŒã«ãŒãã«ãããã¯ã«ããééããŸããïŒã èµ·ããããæé«ã®ããšã§ã¯ãããŸããã
ã¬ãŒããäžããŠãã³ã³ããã«ããå€ãã®ã¡ã¢ãªãå²ãåœãŠãŸãã ä»åã¯OutOfMemoryExceptionããã£ããã§ããŸããæ€æ»åŸãOOMKillerãã³ã³ããã«è§ŠããŠããªãããšã確èªããäžèšã®åé¡ããã¹ãŠåé¿ããŸãã
æåŸã®ã¬ãã«ãæž¡ãããŸãããèŠçŽããŠã¿ãŸãããã
åé
ããã§ãç§ãã¡ã¯ã²ãŒã ã®ãã¹ãŠã®ã¬ãã«ã«åæ ŒããçµæãšããŠäœãåŸãŸãããïŒ Dockerã¯ç§ãã¡ã«å±±ãåãããšçŽæããŠããŸããïŒ
移æ€æ§ã¯ç§ãã¡ãæãã»ã©è¯ããããŸããããJava 9ã¯ãããã®åé¡ã解決ãããšçŽæããŠããããã§ãã æè»æ§ãå¢ããšããã¹ãŠãããå¿«é©ã«ãªããŸããdockerã䜿çšãããšãã¡ã€ã³ã³ãŒãããããã»ã©é ããªãã³ãŒãã§ç°å¢ã®åçŸå¯èœãªæ§æãååŸã§ããŸãã ãã®ãããªãã®ã远跡ããã®ã¯ãã«ãŒãã®äžã®ã©ããã§ããã€ãäœãä¿®æ£ã亀æããŸãã¯ç Žå£ãã人ãããç°¡åã«è¿œè·¡ã§ããŸãã ãŸããäžè¬çã«ã1å°ã®ãã·ã³ã§å€ãã®ã³ã³ãããå®è¡ã§ããããããªãœãŒã¹ãå€§å¹ ã«åæžã§ããŸããããã¯ãã¹ãäžã«éèŠã«ãªãå¯èœæ§ããããŸãã
ã€ãŸãããã¹ããéçºã«ã¯ãDockerãçæ³çã ãšæããŸãã ãã ããæ¬çªç°å¢ã§äœæ¥ããå Žåã¯ããã®å Žåã®è² è·ãéåžžã«é«ããªãå¯èœæ§ãããããããã泚æããå¿ èŠããããŸãã ãããŠãããã«ãŒã®é害ãä¹ãè¶ããããšã¯çµ¶å¯Ÿã«äžå¿«ã§ãã
ãããŠæåŸã«-dockerã§Java 9ãåéã«ããããã«å¿ èŠãªåããã©ã°ïŒ
ã²ãŒã ãªãŒããŒ
æåŸã®ã¬ãã«ãæž¡ãããã®äŸ¿å©ãªãªã³ã¯ïŒ
https://hackernoon.com/crafting-perfect-Java-docker-build-flow-740f71638d63
https://jaxenter.com/nobody-puts-Java-container-139373.html
https://github.com/valentinomiazzo/docker-jvm-memory-test
PSäžèšã®ãã¹ãŠã®äŸã¯ããã«ãããŸãïŒ
github.com/alexff91/Java-meetup-2018