DockerºÍLinux£ºÔõÑùʵÏÖÈÝÆ÷¼äµÄÍøÂçͨѶ£¿
dockerºÍlinux£ºÔõÑùʵÏÖÈÝÆ÷¼äµÄÍøÂçͨѶ£¿
СÐò£º
ÔÚÏÖ´úÓ¦ÓóÌÐòµÄ¿ª·¢ºÍ°²ÅÅÀú³ÌÖУ¬ÈÝÆ÷ÊÖÒÕÊÎÑÝÁËÖ÷Òª½ÇÉ«¡£Í¨¹ýʹÓÃÈÝÆ÷ÊÖÒÕ£¬ÎÒÃÇ¿ÉÒÔ½«Ó¦ÓóÌÐò¼°ÆäÒÀÀµÏî´ò°ü³ÉÒ»¸ö×ÔÁ¦µÄÈÝÆ÷£¬´Ó¶ø°ü¹ÜÓ¦ÓóÌÐòµÄ¿ÉÒÆÖ²ÐÔºÍÒ»ÖÂÐÔ¡£È»¶ø£¬µ±ÎÒÃÇÐèÒª½«¶à¸öÈÝÆ÷ÅþÁ¬ÆðÀ´£¬Ê¹ËüÃÇÄܹ»¾ÙÐÐÍøÂçͨѶʱ£¬ÉèÖÃÈÝÆ÷¼äµÄÍøÂçͨѶ¾Í±äµÃºÜÊÇÖ÷Òª¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚDockerºÍLinuxÇéÐÎÖÐʵÏÖÈÝÆ÷¼äµÄÍøÂçͨѶ¡£
DockerÖеÄÍøÂçģʽ
DockerÌṩÁËËÄÖÖ²î±ðµÄÍøÂçģʽÀ´Ö§³ÖÈÝÆ÷µÄÍøÂçͨѶ£¬»®·ÖÊÇ£º
ÇŽÓģʽ£¨bridge£©: ĬÈÏģʽ£¬ÈÝÆ÷ͨ¹ýÐéÄâÍøÇÅÅþÁ¬µ½ËÞÖ÷»úÍøÂç¡£
Ö÷»úģʽ£¨host£©: ÈÝÆ÷Ö±½ÓʹÓÃËÞÖ÷»úÍøÂ磬²»¾ÙÐÐÍøÂç¸ôÀë¡£
noneģʽ: ÈÝÆ÷ûÓÐÍøÂç½Ó¿Ú£¬ÓëÍⲿÍøÂçÍêÈ«¸ôÀë¡£
ÈÝÆ÷ģʽ£¨container£©: ÈÝÆ÷¹²ÏíÒ»¸öÍøÂçÃüÃû¿Õ¼ä£¬¿ÉÒÔÖ±½Ó»á¼ûÆäËûÈÝÆ÷¡£
ÈÝÆ÷¼äµÄÍøÂçͨѶʾÀý
½ÓÏÂÀ´£¬ÎÒÃǽ«Í¨¹ýÒ»¸ö¼òÆÓµÄʾÀýÀ´ÑÝʾÔõÑùÔÚDockerºÍLinuxÇéÐÎÖÐʵÏÖÈÝÆ÷¼äµÄÍøÂçͨѶ¡£¼ÙÉèÎÒÃÇÓÐÁ½¸öÈÝÆ÷£¬Ò»¸öÊÇwebÈÝÆ÷£¬Ò»¸öÊÇdbÈÝÆ÷£¬ÎÒÃÇÏ£ÍûwebÈÝÆ÷Äܹ»»á¼ûdbÈÝÆ÷ÌṩµÄÊý¾Ý¿â¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öÍøÂ磬ÓÃÓÚÈÝÆ÷¼äµÄͨѶ¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁÉèÒ»¸öÃûΪmy_networkµÄÇŽÓÍøÂ磺
$ docker network create my_network
µÇ¼ºó¸´ÖÆ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½¨Éè²¢ÔËÐÐwebÈÝÆ÷£¬ÓÃÓÚÌṩwebЧÀÍ¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁÉèÒ»¸öÃûΪweb_containerµÄÈÝÆ÷£¬²¢½«ÆäÅþÁ¬µ½my_networkÍøÂ磺
$ docker run -d --name web_container --network my_network web_image
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬web_imageÊÇÎÒÃÇ×Ô¼º¹¹½¨µÄwebÈÝÆ÷¾µÏñ¡£
È»ºó£¬ÎÒÃÇÐèÒª½¨Éè²¢ÔËÐÐdbÈÝÆ÷£¬ÓÃÓÚÌṩÊý¾Ý¿âЧÀÍ¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁÉèÒ»¸öÃûΪdb_containerµÄÈÝÆ÷£¬²¢½«ÆäÅþÁ¬µ½my_networkÍøÂ磺
$ docker run -d --name db_container --network my_network db_image
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬db_imageÊÇÎÒÃÇ×Ô¼º¹¹½¨µÄdbÈÝÆ÷¾µÏñ¡£
ÏÖÔÚ£¬ÎÒÃÇÒѾ½¨ÉèÁËÁ½¸öÈÝÆ÷£¬²¢½«ËüÃÇÅþÁ¬µ½ÁËͳһ¸öÍøÂç¡£½ÓÏÂÀ´£¬ÎÒÃÇÐèҪȷ±£webÈÝÆ÷Äܹ»»á¼ûdbÈÝÆ÷ÌṩµÄÊý¾Ý¿â¡£
ÔÚwebÈÝÆ÷ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃdb_containerµÄÃû³ÆÀ´»á¼ûËü¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚwebÈÝÆ÷ÖеĴúÂëÖÐʹÓÃÒÔÏÂÅþÁ¬×Ö·û´®À´ÅþÁ¬Êý¾Ý¿â£º
jdbc:mysql://db_container:3306/my_database
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÅþÁ¬×Ö·û´®ÖУ¬db_containerÊÇdbÈÝÆ÷µÄÃû³Æ£¬3306ÊÇÊý¾Ý¿âµÄĬÈ϶˿ںţ¬my_databaseÊÇÊý¾Ý¿âµÄÃû³Æ¡£
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇÒѾÀÖ³ÉʵÏÖÁËwebÈÝÆ÷ºÍdbÈÝÆ÷Ö®¼äµÄÍøÂçͨѶ¡£webÈÝÆ÷¿ÉÒÔͨ¹ýÈÝÆ÷Ãû³ÆÀ´»á¼ûdbÈÝÆ÷ÌṩµÄÊý¾Ý¿âЧÀÍ¡£
½áÂÛ£º
ÔÚDockerºÍLinuxÇéÐÎÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖÃÍøÂçģʽºÍ½¨ÉèÍøÂçÀ´ÊµÏÖÈÝÆ÷¼äµÄÍøÂçͨѶ¡£Í¨¹ý׼ȷÉèÖÃÍøÂçÅþÁ¬£¬ÎÒÃÇ¿ÉÒÔÔÚÈÝÆ÷¼ä½¨ÉèͨѶͨµÀ£¬´Ó¶øʵÏÖÓ¦ÓóÌÐòµÄ¶àÈÝÆ÷°²ÅźÍÂþÑÜʽ¼Ü¹¹¡£
´úÂëʾÀý£º
webÈÝÆ÷µÄDockerfile£º
FROM ubuntu:latest RUN apt-get update RUN apt-get install -y apache2 EXPOSE 80 CMD ["apache2ctl", "-D", "FOREGROUND"]
µÇ¼ºó¸´ÖÆ
dbÈÝÆ÷µÄDockerfile£º
FROM ubuntu:latest RUN apt-get update RUN apt-get install -y mysql-server EXPOSE 3306 CMD ["mysqld"]
µÇ¼ºó¸´ÖÆ
webÈÝÆ÷ÖеÄJava´úÂëʾÀý£º
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { public static void main(String[] args) { String url = "jdbc:mysql://db_container:3306/my_database"; String user = "root"; String password = "password"; try (Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement()) { String query = "SELECT * FROM my_table"; ResultSet rs = stmt.executeQuery(query); while (rs.next()) { System.out.println(rs.getString("column1")); } } catch (SQLException e) { e.printStackTrace(); } } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÊǹØÓÚÔõÑùÔÚDockerºÍLinuxÇéÐÎÖÐʵÏÖÈÝÆ÷¼äµÄÍøÂçͨѶµÄÏÈÈݺÍʾÀý¡£Í¨¹ý׼ȷµÄÍøÂçÉèÖúÍÅþÁ¬ÉèÖã¬ÎÒÃÇ¿ÉÒÔÇáËÉʵÏÖÈÝÆ÷¼äµÄͨѶ£¬²¢¹¹½¨Ô½·¢ÎÞаºÍ¿ÉÀ©Õ¹µÄÓ¦ÓóÌÐò¼Ü¹¹¡£
ÒÔÉϾÍÊÇDockerºÍLinux£ºÔõÑùʵÏÖÈÝÆ÷¼äµÄÍøÂçͨѶ£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡