ÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄ³¤ÆÚ»¯´æ´¢ºÍÊý¾Ý±¸·Ý
ÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄ³¤ÆÚ»¯´æ´¢ºÍÊý¾Ý±¸·Ý
Docker ÊÇÒ»ÖÖ¿ªÔ´µÄÈÝÆ÷»¯Æ½Ì¨£¬Äܹ»×ÊÖú¿ª·¢Ö°Ô±¸üºÃµØ¹¹½¨¡¢°²ÅźÍÖÎÀíÓ¦ÓóÌÐò¡£ÔÚ Docker ÖУ¬ÈÝÆ÷ÊÇÓ¦ÓóÌÐòºÍÆäÒÀÀµµÄ×ÔÁ¦µ¥Î»¡£È»¶ø£¬ÓÉÓÚÈÝÆ÷µÄʵÖÊ£¬ÈÝÆ÷ÖеÄÊý¾ÝÊÇÔÝʱÐԵģ¬ÈÝÆ÷×èÖ¹ºóÊý¾Ý»áÏûÊÅ¡£ÔÚһЩ³¡¾°Ï£¬ÎÒÃÇÐèÒª½«ÈÝÆ÷µÄÊý¾Ý³¤ÆÚ»¯´æ´¢£¬±£´æÈÝÆ÷ÖеÄÖ÷ÒªÊý¾Ý¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓà Docker ¾ÙÐÐÈÝÆ÷µÄ³¤ÆÚ»¯´æ´¢ºÍÊý¾Ý±¸·Ý£¬²¢ÌṩÏêϸ´úÂëʾÀý¡£
Ò»¡¢Ê¹Óà Docker Volume ¾ÙÐÐÈÝÆ÷Êý¾ÝµÄ³¤ÆÚ»¯
Docker Volume ÊÇ Docker ÌṩµÄÒ»ÖÖ»úÖÆ£¬ÓÃÓÚÔÚÖ÷»úºÍÈÝÆ÷Ö®¼ä¹²ÏíºÍ³¤ÆÚ»¯´æ´¢Êý¾Ý¡£Í¨¹ýʹÓà Docker Volume£¬ÎÒÃÇ¿ÉÒÔ½«ÈÝÆ÷ÖеÄÊý¾Ý´æ´¢µ½Ö÷»úÉϵÄÖ¸¶¨Ä¿Â¼ÖУ¬´Ó¶øʵÏÖÊý¾ÝµÄ³¤ÆÚ»¯´æ´¢¡£
½¨ÉèÒ»¸ö Docker Volume
ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁÉèÒ»¸ö Docker Volume£º
$ docker volume create myvolume
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚ Docker Öн¨ÉèÒ»¸öÃûΪ “myvolume” µÄ Docker Volume¡£
½« Docker Volume ¹ÒÔص½ÈÝÆ÷ÖÐ
½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª½«½¨ÉèµÄ Docker Volume ¹ÒÔص½ÈÝÆ÷ÖС£¿ÉÒÔÔÚʹÓà docker run ÏÂÁîʱÌí¼Ó -v ²ÎÊý¾ÙÐйÒÔØ¡£
$ docker run -v myvolume:/path/to/mount myimage
µÇ¼ºó¸´ÖÆ
Õâ»á½«ÃûΪ “myvolume” µÄ Docker Volume ¹ÒÔص½ÈÝÆ÷ÖÐµÄ “/path/to/mount” Ŀ¼ÉÏ¡£
½«Êý¾Ý´æ´¢µ½ Docker Volume ÖÐ
½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔ½«Êý¾Ý´æ´¢µ½ Docker Volume ÖС£ÔÚÈÝÆ÷ÖУ¬½«Êý¾Ý´æ´¢µ½¹ÒÔصÄĿ¼Öм´¿É£º
$ echo "Hello, Docker Volume!" > /path/to/mount/data.txt
µÇ¼ºó¸´ÖÆ
Õ⽫ÔÚ Docker Volume Öн¨ÉèÒ»¸öÃûΪ “data.txt” µÄÎļþ£¬²¢½«ÆäÄÚÈÝÉèÖÃΪ “Hello, Docker Volume!”¡£
³¤ÆÚ»¯´æ´¢
µ±ÈÝÆ÷×èÖ¹»òɾ³ýʱ£¬Docker Volume ÖеÄÊý¾ÝÒÀÈ»»áÉúÑÄÔÚÖ÷»úÉϵÄÖ¸¶¨Ä¿Â¼ÖУ¬´Ó¶øʵÏÖÁËÈÝÆ÷Êý¾ÝµÄ³¤ÆÚ»¯´æ´¢¡£
¶þ¡¢Ê¹Óà Docker Compose ¾ÙÐÐÈÝÆ÷Êý¾ÝµÄ±¸·Ý
Docker Compose ÊÇÒ»¸öÓÃÓÚ½ç˵ºÍÔËÐжàÈÝÆ÷ Docker Ó¦ÓóÌÐòµÄ¹¤¾ß¡£ËüʹÓÃÒ»¸ö YAML ÎļþÀ´ÉèÖÃÓ¦ÓóÌÐòµÄЧÀÍ£¬²¢¿ÉÒÔ½«²î±ðµÄÈÝÆ÷ÅþÁ¬ÔÚÒ»Æð¡£ÎÒÃÇ¿ÉÒÔʹÓà Docker Compose À´Àû±ãµØÖÎÀíÈÝÆ÷µÄÔËÐУ¬Í¬Ê±Ò²¿ÉÒÔʹÓÃËüÀ´°´ÆÚ±¸·ÝÈÝÆ÷ÖеÄÊý¾Ý¡£
½¨ÉèÒ»¸ö Docker Compose Îļþ
Ê×ÏÈ£¬½¨ÉèÒ»¸öÃûΪ “docker-compose.yml” µÄ Docker Compose Îļþ£º
version: '3' services: backup: image: alpine volumes: - myvolume:/backup command: cp -r /path/to/mount /backup volumes: myvolume:
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁË Alpine ¾µÏñ£¬²¢½«ÃûΪ “myvolume” µÄ Docker Volume ¹ÒÔص½ÈÝÆ÷ÖÐµÄ “/backup” Ŀ¼ÉÏ¡£È»ºó£¬ÎÒÃÇʹÓà cp ÏÂÁÊý¾Ý´ÓÈÝÆ÷ÖÐµÄ “/path/to/mount” Ŀ¼¸´ÖƵ½ “/backup” Ŀ¼ÖС£
ÔËÐÐ Docker Compose
½ÓÏÂÀ´£¬Ê¹ÓÃÒÔÏÂÏÂÁîÔËÐÐ Docker Compose£º
$ docker-compose up -d
µÇ¼ºó¸´ÖÆ
Õ⽫½¨Éè²¢Æô¶¯Ò»¸öÃûΪ “backup” µÄÈÝÆ÷£¬¸ÃÈÝÆ÷½«°´ÆÚ±¸·ÝÊý¾Ýµ½Ö¸¶¨µÄĿ¼ÖС£
ÖÁ´Ë£¬ÎÒÃÇÒѾÏÈÈÝÁËÔõÑùʹÓà Docker ¾ÙÐÐÈÝÆ÷µÄ³¤ÆÚ»¯´æ´¢ºÍÊý¾Ý±¸·Ý¡£Í¨¹ýʹÓà Docker Volume ¾ÙÐÐÈÝÆ÷Êý¾ÝµÄ³¤ÆÚ»¯´æ´¢£¬ÎÒÃÇ¿ÉÒÔ½«ÈÝÆ÷ÖеÄÊý¾ÝÉúÑÄÔÚÖ÷»úÉϵÄÖ¸¶¨Ä¿Â¼ÖС£¶øʹÓà Docker Compose °´ÆÚ±¸·ÝÈÝÆ÷ÖеÄÊý¾Ý£¬Ôò¿ÉÒÔÈ·±£Êý¾ÝµÄÇå¾²ÐԺͿɿ¿ÐÔ¡£Ï£ÍûÒÔÉÏÄÚÈݶÔÄãÓÐËù×ÊÖú£¬½Ó´ýÁôÑÔ½»Á÷ÌÖÂÛ¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃDocker¾ÙÐÐÈÝÆ÷µÄ³¤ÆÚ»¯´æ´¢ºÍÊý¾Ý±¸·ÝµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡