ÔõÑùʹÓÃDocker¹¹½¨¸ß¿É¿¿µÄÂþÑÜʽϵͳ¼Ü¹¹£¿
ÔõÑùʹÓÃdocker¹¹½¨¸ß¿É¿¿µÄÂþÑÜʽϵͳ¼Ü¹¹£¿
ÕªÒª£ºDockerÊÇÏÖÔÚ×îÊ¢ÐеÄÈÝÆ÷»¯Æ½Ì¨£¬¿ÉÒÔ×ÊÖúÎÒÃÇÇáËɹ¹½¨ºÍ°²ÅÅÓ¦ÓóÌÐò¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃDocker¹¹½¨¸ß¿É¿¿µÄÂþÑÜʽϵͳ¼Ü¹¹£¬²¢Í¨¹ý´úÂëʾÀýÏêϸÐðÊöʵÏÖÒªÁì¡£
´î½¨DockerÇéÐÎ
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÿ̨ЧÀÍÆ÷ÉÏ×°ÖÃDocker£¬ÒÔ±ãÄܹ»ÔËÐÐÈÝÆ÷»¯µÄÓ¦ÓóÌÐò¡£¿ÉÒÔƾ֤¹Ù·½ÎĵµÌṩµÄ°ì·¨¾ÙÐÐ×°Ö㬻òÕßͨ¹ýʹÓÃDockerÌṩµÄ½ÅÔÀ´¼ò»¯×°ÖÃÀú³Ì¡£
½¨ÉèDocker¾µÏñ
ÔÚ¹¹½¨¸ß¿É¿¿µÄÂþÑÜʽϵͳ¼Ü¹¹ÖУ¬Ò»¸öÒªº¦µÄ°ì·¨Êǽ¨Éè¿É¿¿µÄDocker¾µÏñ¡£¾µÏñÊÇÓÃÀ´¹¹½¨ºÍÔËÐÐÈÝÆ÷µÄÄ£°å£¬Ëü°üÀ¨ÁËÓ¦ÓóÌÐòºÍÆäËùÐèµÄÔËÐÐÇéÐΡ£¿ÉÒÔʹÓÃDockerfileÀ´½ç˵¾µÏñµÄ¹¹½¨¹æÔò¡£
ÀýÈ磬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸ö»ùÓÚJavaµÄ΢ЧÀÍÓ¦ÓóÌÐòµÄDocker¾µÏñ¡£Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÏîÄ¿¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪDockerfileµÄÎļþ£¬²¢±àдÒÔÏÂÄÚÈÝ£º
# ʹÓùٷ½µÄJava 8¾µÏñ×÷Ϊ»ù´¡¾µÏñ FROM java:8 # ½«Ó¦ÓóÌÐò¸´ÖƵ½¾µÏñÖеÄÖ¸¶¨Ä¿Â¼ COPY target/my-application.jar /app/my-application.jar # ÉèÖÃÈÝÆ÷Æô¶¯Ê±ÒªÖ´ÐеÄÏÂÁî CMD ["java", "-jar", "/app/my-application.jar"]
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃÇʹÓùٷ½µÄJava 8¾µÏñ×÷Ϊ»ù´¡¾µÏñ£¬²¢½«´ò°üºÃµÄÓ¦ÓóÌÐò¸´ÖƵ½¾µÏñÖеÄÖ¸¶¨Ä¿Â¼¡£È»ºó£¬Í¨¹ýÉèÖÃÈÝÆ÷Æô¶¯Ê±ÒªÖ´ÐеÄÏÂÁîÀ´Ö¸¶¨Ó¦ÓóÌÐòµÄÆô¶¯·½·¨¡£
½ÓÏÂÀ´£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´¹¹½¨¾µÏñ²¢ÉÏ´«µ½¾µÏñ¿ÍÕ»£¨ÀýÈçDocker Hub£©£º
docker build -t my-application . docker push my-application
µÇ¼ºó¸´ÖÆ
ÉèÖÃDocker Swarm
Docker SwarmÊÇDocker×Ô´øµÄÓÃÓÚ¼¯ÈºÖÎÀíµÄ¹¤¾ß£¬Ëü¿ÉÒÔ×ÊÖúÎÒÃǼò»¯ÂþÑÜʽӦÓóÌÐòµÄ°²ÅźÍÖÎÀí¡£ÔÚʹÓÃDocker SwarmÇ°£¬ÐèÒª½¨ÉèÒ»¸öSwarm¼¯Èº£¬²¢ÉèÖúü¯ÈºÖеĽڵ㡣
Ê×ÏÈ£¬Ñ¡Ôñһ̨ЧÀÍÆ÷×÷ΪSwarm Manager½Úµã£¬²¢Ö´ÐÐÒÔÏÂÏÂÁî³õʼ»¯Swarm¼¯Èº£º
docker swarm init --listen-addr <manager-ip>
µÇ¼ºó¸´ÖÆ
È»ºó£¬½«ÆäËûЧÀÍÆ÷¼ÓÈëSwarm¼¯Èº×÷ΪWorker½Úµã£º
docker swarm join --token <join-token> <manager-ip>
µÇ¼ºó¸´ÖÆ
ÔÚÕâÀÐèÒª½« Ì滻ΪSwarm Manager½ÚµãµÄIPµØµã£¬ Ì滻ΪSwarm Manager½ÚµãÌṩµÄ¼ÓÈëÁîÅÆ¡£
°²ÅÅÈÝÆ÷»¯Ó¦ÓóÌÐò
×îºó£¬¿ÉÒÔʹÓÃDocker SwarmÀ´°²ÅÅÈÝÆ÷»¯µÄÓ¦ÓóÌÐò¡£Í¨¹ýʹÓÃDocker Compose±àдһ¸ödocker-stack.ymlÎļþÀ´½ç˵ӦÓóÌÐòµÄЧÀͺ͹æÄ£¡£
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý£º
version: '3.8' services: my-application: image: my-application deploy: replicas: 3 restart_policy: condition: on-failure ports: - "8080:8080"
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪmy-applicationµÄЧÀÍ£¬Ê¹ÓÃ֮ǰ¹¹½¨µÄ¾µÏñ£¬²¢Ö¸¶¨Ð§À͵ĹæģΪ3¸ö¸±±¾¡£Í¬Ê±£¬½«ÈÝÆ÷µÄ8080¶Ë¿ÚÓ³Éäµ½ËÞÖ÷»úµÄ8080¶Ë¿Ú¡£
×îºó£¬Í¨¹ýÒÔÏÂÏÂÁîÀ´Æô¶¯Ó¦ÓóÌÐòµÄЧÀÍ£º
docker stack deploy -c docker-stack.yml my-application
µÇ¼ºó¸´ÖÆ
´Ëʱ£¬Docker Swarm»á×Ô¶¯ÔÚ¼¯ÈºÖеĽڵãÉϽ¨ÉèÏìÓ¦µÄÈÝÆ÷£¬²¢ÈÏÕæµ÷ÀíºÍÖÎÀíÕâЩÈÝÆ÷¡£
×ܽ᣺
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃDocker¹¹½¨¸ß¿É¿¿µÄÂþÑÜʽϵͳ¼Ü¹¹¡£Í¨¹ý½¨Éè¿É¿¿µÄDocker¾µÏñ¡¢ÉèÖÃDocker SwarmºÍ°²ÅÅÈÝÆ÷»¯Ó¦ÓóÌÐò£¬ÎÒÃÇ¿ÉÒÔÇáËɹ¹½¨ºÍÖÎÀíÂþÑÜʽϵͳ¡£Í¨¹ýºÏÀíµÄÍýÏëºÍʹÓÃDockerÌṩµÄ¹¤¾ßºÍ¹¦Ð§£¬ÎÒÃÇ¿ÉÒÔʵÏÖ¸ü¸ßµÄϵͳ¿É¿¿ÐԺͿÉÀ©Õ¹ÐÔ¡£
²Î¿¼Á´½Ó£ºhttps://docs.docker.com/get-started/
´úÂëʾÀý£º
@RestController public class HelloController { @RequestMapping("/") public String index() { return "Hello, Docker!"; } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÊÇÒ»¸ö¼òÆÓµÄSpring BootÓ¦ÓóÌÐòµÄ¿ØÖÆÆ÷À࣬ÓÃÓÚ´¦Öóͷ£HTTPÇëÇó²¢·µ»ØÒ»¸ö¼òÆÓµÄ×Ö·û´®¡£ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃÁËSpring BootµÄ×¢½â@RestControllerÀ´±ê¼ÇÕâÊÇÒ»¸ö¿ØÖÆÆ÷À࣬²¢Ê¹ÓÃ@RequestMapping×¢½âÀ´Ö¸¶¨´¦Öóͷ£¸ù·¾¶µÄÇëÇó¡£µ±Ó¦ÓóÌÐòÔËÐÐÔÚDockerÈÝÆ÷ÖÐʱ£¬¿ÉÒÔͨ¹ý»á¼ûÈÝÆ÷µÄIPµØµãºÍ¶Ë¿ÚÀ´»á¼ûÕâ¸ö½Ó¿Ú¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃDocker¹¹½¨¸ß¿É¿¿µÄÂþÑÜʽϵͳ¼Ü¹¹£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡