Docker ã¬ãå ¥é ãã¤ãã£ã¦å¦ã¶ docker pull & run ç·¨ã
ããã«ã¡ã¯ï¼
ãã¾æå±ãã¦ãã LAPRAS ã¨ããä¼ç¤¾ã®ã¡ã³ãã¼ã§ãLAPRAS å¤ã®èªç±ç 究ãªã¬ã¼ãã¨é¡ãã¦ãããããèå³ã®ããé åã«ã¤ãã¦ã®è¨äºãæ¸ããï¼ã¨ããä¼ç»ããã£ã¦ããããã®ä¸ç°ã§è¨äºãæ¸ããã¨ã«ãã¾ããï¼ ãã®è¨äºã1çºç®ã®è¨äºãªã®ã§æ°åããããã¦ããããã¨æãã¾ããä»æ¥ããä»ã®ã¡ã³ãã¼ãåã ã®èªç±ç 究ã®è¨äºãçºä¿¡ãã¦è¡ãã®ã§ãã²ãã§ãã¯ãã¦ãã ããï¼
ãã¦ããããªåã®å¤ã®èªç±ç 究ã®ãã¼ã㯠ãã¤ãã£ã¦å¦ã¶ Docker pull & run ç·¨ã
ã§ããDocker ã«ã¤ãã¦ããæ·±ãç¥ãããã«ãdocker pull 㨠run ãèªåã§ã¤ãã£ã¦ã¿ããã¨æãã¾ãã
ãä»äºã§ã趣å³ã®éçºã§ãããã¯ã Docker ãªãã§ã¯ã¢ããªã±ã¼ã·ã§ã³ã¯ä½ããªãã¨ãããããã« Docker ã使ã£ã¦ãã¾ãã LAPRAS ã§ã¯å ¨ã¦ã®ã³ã¼ã㯠Docker ã¤ã¡ã¼ã¸ã«ããã±ã¼ã¸ã³ã°ããã¦ãã¦ãæ¬çªãµã¼ãã¹ã®ã³ã¼ãã¯å ¨ã¦ Kubernetes ä¸ã§åãã¦ãã¾ãã ãã Docker ãªãã§ã¯çããããªãã§ããï¼
ããã¾ã§ Docker ããã£ã¤ã使ã£ã¦ããæ¹ããã¾ã ãã¾ãããç¥ããªãã¨ããæ¹ãããã®è¨äºãå¼ãã§ä¸ç·ã« Docker ãä½ããªããå¦ãã§ã¿ã¾ãããï¼
(ã¡ãªã¿ã« Docker ã®å ¥éã¦ããªã¿ã¤ãã«ã§ãã docker ã®åºæ¬çãªä½¿ãæ¹ãªã©ã¯ä¸ååºã¦ãã¾ããã®ã§ãäºæ¿ãã ããã»ã»ã»ã»ã ã¬ãå ¥éã§ãã)
ç®æ¬¡
- 1. docker pull ã«ã¤ãã¦ãã£ããç解
- 2. docker run ã«ã¤ãã¦ãã£ããç解
- 3. docker pull ãã¤ãã£ã¦ã¿ã
- 4. docker run ãã¤ãã£ã¦ã¿ã
- 5. ã¾ã¨ã
ã«ã¤ãã¦æ¸ãã¾ãã
1. docker pull ã«ã¤ãã¦ãã£ããç解
ãã¦ãã¾ã㯠docker pull ãã¤ããããã«ãdocker pull ãã©ãããå¦çã«ãªã£ã¦ãããã調ã¹ã¦ã¿ã¾ãã
Docker ã®ãã¡ã¤ã«ã·ã¹ãã ã«ã¤ãã¦
以åãã¡ãã®ãDocker ãå©ç¨ãã¦ãã union filesystem ã«ã¤ãã¦ã®è¨äºãæ¸ãã¾ããã union filesystem 㯠Docker ã®ç¹å¾´ã®1ã¤ã ã¨æã£ã¦ããã®ã§ Docker ãç解ããä¸ã§ã¨ã¦ã大äºãªæ¦å¿µã§ãã
ãã®è¨äºãæ¸ãéã«èª¿ã¹ããã¨ãã¾ã¨ããã¨ãunion filesystem ã使ããã¨ã§ä»¥ä¸ã®ãããªè¨è¨ãããã¦ãããã¨ããããã¾ããã
- Docker ã³ã³ããå ã®ãã¡ã¤ã«ã¯è¤æ°ã®ã¬ã¤ã¤ã¼ã§æ§æããã¦ãã
- Docker ã¤ã¡ã¼ã¸ã®ã¬ã¤ã¤ã¼ã¯ readonly ã¨ãã¦ãã¦ã³ããã¦ãã
- ã³ã³ããåå¥ã®æ¸ãè¾¼ã¿å¯è½ãªã¬ã¤ã¤ã¼ã Docker ã¤ã¡ã¼ã¸ã®ã¬ã¤ã¤ã¼ã«è¢«ãããã¨ã§ãã³ã³ããå ã§æ¸ãè¾¼ã¿ãçºçãããã¼ã¿ã¯ãã®ã¬ã¤ã¤ã¼ã«é©ç¨ããã
å¦çã®æµã
ä»å docker pull ãã¤ãã£ã¦ã¿ãã«ããããã¡ãã®ãªãã¸ããªãåèã«ããã¦ããã ãã¾ããã
ãã¡ãã®ãªãã¸ããªã«ãã docker pull ã®ãµã³ãã«ã³ã¼ããè¦ã¦ã¿ãã¨ä»¥ä¸ã®ãããªå¦çããã£ã¦ãããã¨ããããã¾ãã
- Docker Hub ã®ã¬ã¸ã¹ããªããã¢ã¯ã»ã¹ãã¼ã¯ã³ãåå¾ãã
- æå®ãã Docker ã¤ã¡ã¼ã¸ã®ãããã§ã¹ããã¡ã¤ã«ãåå¾ãã
- ãããã§ã¹ããã¡ã¤ã«å ã«ãã Docker ã¤ã¡ã¼ã¸å ã®ã¬ã¤ã¤ã¼æ¯ã®ãã¤ã¸ã§ã¹ããåå¾ãã
- Docker Hub ã®ã¬ã¸ã¹ããªããã¬ã¤ã¤ã¼ã®ãã¤ããªããã¦ã³ãã¼ããã
Docker ã¤ã¡ã¼ã¸ãåå¾ããããã®å¿ è¦æä½éã®å¦çã ããã¿ãã¨ãDocker ã¬ã¸ã¹ããªã® API ã«ã¢ã¯ã»ã¹ãã¦ãã¼ã¿ãåã£ã¦ããã ããªã®ã§ãæå¤ã¨é£ããã¯ãªãããã ã¨ãããã¨ããããã¾ããã
Docker ã¬ã¸ã¹ã㪠API ã®ä»æ§
Docker ã¬ã¸ã¹ããªã® API ã«ã¤ãã¦ã®å ¬å¼ããã¥ã¡ã³ããæ¢ãã¦ã¿ãã¨ä»¥ä¸ã®ãã®ãè¦ã¤ããã¾ããã Docker ã¬ã¸ã¹ããªã®èªè¨¼ã«ã¤ãã¦ã¯ãã¡ãã«ä»æ§ãæ¸ããã¦ãã¾ãã
ã¾ããDocker ã¤ã¡ã¼ã¸ã®ãããã§ã¹ããã¡ã¤ã«ã®ã¹ãã¼ãã¯ãã¡ãã«å®ç¾©ãããã¾ãã
2. docker run ã«ã¤ãã¦ãã£ããç解
docker run ãã¤ãã£ã¦ã¿ãã«ããã£ã¦ã¯ãã¡ãã®ãªãã¸ããªãåèã«ããã¦ããã ãã¾ããã ãã¡ãã®ãªãã¸ããªã¯ã¯ã¼ã¯ã·ã§ããã®å½¢å¼ã«ã¾ã¨ãããã¦ãã¦ã1ã¤1ã¤é ã追ã£ã¦ã³ã³ããã®ä»çµã¿ãã¤ãã£ã¦ãããããã«ãªã£ã¦ãã¾ãã ã¨ã¦ãåãããããã®ã§ãªã¹ã¹ã¡ã§ãï¼ github.com
å¦çã®æµã
ãã¦ããã¡ãã®ãªãã¸ããªã®ã³ã¼ããè¦ãã¨ã以ä¸ã®ãããªå¦çããã£ã¦ãããã¨ããããã¾ãã
- ã³ã³ããå ã®ã«ã¼ããã£ã¬ã¯ããªã«ãªããã£ã¬ã¯ããªãä½ã
- Docker ã¤ã¡ã¼ã¸ã®ãã£ã¬ã¯ããªã¨ãã³ã³ããåå¥ã®ãã£ã¬ã¯ããªãã³ã³ããã®ããã»ã¹ã«ãã¦ã³ããã
- ããã¤ãã®åºæ¬çãªããã¤ã¹ãã¡ã¤ã«ãåæåãã¦ãã¦ã³ããã
- cgroups ãå©ç¨ãã¦ã³ã³ããã使ç¨ãã CPU, ã¡ã¢ãª ãéé¢ã»å¶éãã
- ã³ã³ããã®ããã»ã¹ã® uid ãå¤æ´ãã
å¿ è¦æä½éãªå¦çã®æµããã¿ãã¨ã»ã¨ãã©ã®å¦çã¯ã·ã¹ãã ã³ã¼ã«å´ã§å®è£ ããã¦ããã®ã§ãOS ã§æ¢ã«ç¨æããã¦ããä»çµã¿ãé©åã«å¼ã³åºãã¦ããã° docker run ãå®ç¾ã§ãããã ã¨ãããã¨ãåããã¾ãã
åè
éå»ã«è»½ã Docker ã®ã½ã¼ã¹ã³ã¼ããèªãã§ã³ã³ããã®èµ·åãã©ããã£ã¦ãããã調ã¹ããã¨ããããã§ãããã³ã³ããã®èµ·åé¨åã®å¦çã¯å
ã
Docker ã«å
å
ããã¦ãããã®ãç¾å¨ã¯ runc
ã¨ããã¢ã¸ã¥ã¼ã«ã«åãåºããã¦ã¡ã³ããã³ã¹ããã¦ãã¾ãã
ã¡ããã¨èªã¿è§£ãã¦ããªãã£ãã®ã§ã¾ããã¤ãããæ°ãåºã¦ããæã«ã³ã¼ããèªãã§ã¿ããã¨æãã¾ãã»ã»ã»ã»ã»ã
3. docker pull ãã¤ãã£ã¦ã¿ã
å
ã»ã©èª¿ã¹ãå
容ã¨åèã«ãããªãã¸ããªã®ã³ã¼ããå
ã« docker pull
ãå®ç¾ããã³ã¼ããæ¸ãã¦ã¿ã¾ããã
github.com
Docker ã¤ã¡ã¼ã¸ã®ã¬ã¤ã¤ã¼ãã¼ã¿ã®ãã¦ã³ãã¼ãå¦ç
Docker ã¤ã¡ã¼ã¸ã®ãã¼ã¿ã¯ã¬ã¤ã¤ã¼æ¯ã« tar ãã¡ã¤ã«ã®ãã¤ããªããã¦ã³ãã¼ãã§ããããã«ãªã£ã¦ãã¾ãã æ¬æ¥ã¯ tar ãã¡ã¤ã«ã®ã¾ã¾ããããã¯ã¬ã¤ã¤ã¼æ¯ã«è§£åãããã¡ã¤ã«ä¸å¼ãåãã¦ãã¼ã«ã«ã«ä¿æãã¦ããã®ãæ£ããã¯ãã§ããããã¦ã³ãã¼ãæã«å ¨ã¦ã®ã¬ã¤ã¤ã¼ã® tar ã解åãã¦1ã¤ã®ãã£ã¬ã¯ããªããªã¼ã«ãã¼ã¸ããããã«å®è£ ãã¦ã¾ãã ããã¯åç´ã«æ¨ªçããã ãã§ããã¯ãã
https://github.com/Chanmoro/build-docker-from-scratch/blob/master/app/pull.py#L131-L150
# åã¬ã¤ã¤ã¼ããã¦ã³ãã¼ããã for digest in manifest.layer_digests: print('Fetching layer %s..' % digest) # ãã¦ã³ãã¼ãããã¬ã¤ã¤ã¼ã tar ã¨ãã¦ä¿åãã local_layer_tar_name = os.path.join(image_layers_path, digest) + '.tar' with open(local_layer_tar_name, 'wb') as f: for chunk in client.download_layer(library, image, digest): if chunk: f.write(chunk) # tar ãå±éãã with tarfile.open(local_layer_tar_name, 'r') as tar: # tar ãã¡ã¤ã«ã®ä¸èº«ãä¸é¨è¡¨ç¤ºãã for member in tar.getmembers()[:10]: print('- ' + member.name) print('...') tar.extractall(str(contents_path)) print('extract done') print(f'Save docker image to {image_base_dir}')
4. docker run ãã¤ãã£ã¦ã¿ã
ãã¦ã次㯠docker pull
ã§åå¾ããã¤ã¡ã¼ã¸ã使ã£ã¦ docker run
ãããã³ã¼ããæ¸ãã¾ããã
docker pull ã¨æ¯ã¹ã¦ããã¯çµæ§ãããããã£ãã§ãã
ã·ã¹ãã ã³ã¼ã«ãå¼ã¶ããã®ã©ããã¼ã¢ã¸ã¥ã¼ã«
åºæ¬çãªæµãã¯äºåã«èª¿ã¹ãéãã§ãPython ããããã¤ãã® OS ã³ãã³ããå¼ã¶ããã®ã©ã¤ãã©ãªãèªåã§æ¸ãã¾ããã C 㧠Python ã®ã¢ã¸ã¥ã¼ã«ãæ¸ãã®ã¯åãã¦ã ã£ããã§ãããåèã«ãã https://github.com/Fewbytes/rubber-docker ã«ããã³ã¼ãããã¼ã¹ã«å®è£ ã§ããã®ã§ããã¾ã§ããããã¨ãªãä½ãã¾ããã
Docker ã¤ã¡ã¼ã¸ã®ãã¡ã¤ã«ã®ãã¦ã³ã
Docker ã¤ã¡ã¼ã¸ã®ãã¡ã¤ã«ã readonly ã§ãã¦ã³ããã¦ãã³ã³ããåå¥ã®æ¸ãè¾¼ã¿å¯è½ãªé åã OverlayFS ã§ãã¦ã³ããã¦ããã¨ããã®ã³ã¼ãã¯ãããªæãã§å®è£ ã§ãã¾ãã ãããããããããã¨ãï¼ãã®éããããï¼ãã¨ããã®ãå®éã«ã¤ãã£ã¦ã¿ã¦ã¨ã¦ãããåããã¾ããã
https://github.com/Chanmoro/build-docker-from-scratch/blob/master/app/run.py#L79-L96
def _mount_image_dir(self, image: Image, container_dir: ContainerDir): """ Docker ã¤ã¡ã¼ã¸ããã¦ã³ããã :param image: :param container_dir: :return: """ image_path = self._get_image_base_path(image, self.IMAGE_DATA_DIR) image_root = os.path.join(image_path, 'layers/contents') # ãªã¼ãã¼ã¬ã¤ FS ã¨ãã¦ãã¦ã³ããã linux.mount( 'overlay', container_dir.root_dir, 'overlay', linux.MS_NODEV, f"lowerdir={image_root},upperdir={container_dir.rw_dir},workdir={container_dir.work_dir}" )
OverlayFS ã®ãã¦ã³ãã§æå®ãã workdir
ã£ã¦ãªããªãã ããï¼ã¨æã£ã¦ã°ã°ã£ã¦ã¿ãããªããªã詳細ãªæ
å ±ãåºã¦ãããã¡ãã® QA ã®åçãä¸çªããã£ã½ãã£ãã§ãã
OverlayFS ã®æ¸ãè¾¼ã¿ã§å©ç¨ããä¸æãã£ã¬ã¯ããªã¨ãããã¨ã®ããã§ããã
ã«ã¼ããã£ã¬ã¯ããªã®å¤æ´
æãã³ã³ããã£ã½ãã¨å人çã«æã£ã¦ãããã³ã³ããå
ã®ãã¡ã¤ã«ã¯å
¨ãå¥ã§éã OS ã¨ãã¦ãåããã¨ããåä½ã®ä¸çªéè¦ãªã¨ããã¯ãã® pivot_root
(ããã㯠chroot
) ãªããããªãã§ããããã
ããã«ãã£ã¦ãã®ããã»ã¹ã®ã«ã¼ããã£ã¬ã¯ããªãèªç±ã«å¤æ´ã§ãã¾ãã
ããè¦ãã¨ãã³ãã«ãã ã·ã¹ãã ã³ã¼ã«ãå¼ãã§ãã ããªã®ã§ã巨人ã®è©ã®ä¸ã«ä¹ã£ã¦ããæããã¨ã¦ãããã¾ããã
https://github.com/Chanmoro/build-docker-from-scratch/blob/master/app/run.py#L151-L164
def _change_root_dir(self, container_root_dir: str): """ ã³ã³ããå ã®ã«ã¼ããã£ã¬ã¯ããªãå¤æ´ãã :param container_root_dir: :return: """ old_root = os.path.join(container_root_dir, 'old_root') os.makedirs(old_root) linux.pivot_root(container_root_dir, old_root) os.chdir('/') linux.umount2('/old_root', linux.MNT_DETACH) os.rmdir('/old_root')
ãã®ä»
ãã® docker run ã®å®è£
ãããªã横çãã¦ç«¯æã£ã¦ãã¾ãã»ã»ã»ã»ã
cgroups ã«ããCPUãã¡ã¢ãªããã£ã¹ã¯ã®åé¢ããã³ã³ããæ¯ã®ãããã¯ã¼ã¯ã®ã«ã¼ãã£ã³ã°ãªã©ãªã©ã¨ã¦ãéè¦ãªæ©è½ãã¾ã ã¾ã ãããã§ããã pivot_root
㨠OverlayFS ã«ãããã¦ã³ããå®è£
ã§ããã ãã§å人çã«ããããªãæºè¶³ãã¦ãã¾ãã¾ããã
ãã¡ããä»å¾ã¾ãããæ°ãåºã¦ããããã©ã¤ãã¦ã¿ããã¨æãã¾ãã»ã»ã»ã»ã»ï¼
5. ã¾ã¨ã
ãã¦ãä»å㯠Docker ã¬ãå ¥éã® ã¤ãã£ã¦å¦ã¶ docker pull & run ç·¨ ã¨ãããã¨ã§ docker pull 㨠run ãèªåã§ã¤ãã£ã¦ã¿ããã¨ã§ãã®åããç解ãããï¼ã¨ããå 容ãæ¸ãã¾ããã
å®è£ ããã³ã¼ãã¯ãã¡ãã®ãªãã¸ããªã§å ¬éãã¦ãã¾ããREADME ã«å®è¡æ¹æ³ãæ¸ãã¦ããã®ã§ããããã® PC ä¸ã§ç¢ºèªãã¦ãããã¾ãã(åããªãã£ãã issue ãã¤ãã£ã¦ããã ããã¨å©ããã¾ãï¼) github.com
ä»åå®éã«èªå㧠Docker ã®æ©è½ã®ä¸é¨ãã¤ãã£ã¦ã¿ã¦ Docker ã®åä½ã¸ã®ç解ãããªãæ·±ã¾ã£ãã¨æãã¾ãã åæã«ãã³ã³ããåããªã½ã¼ã¹åé¢ã®ããã® OS ã®æ©è½ã¯ UNIX ããæ¢ã«å®è£ ããã¦ãããããã®ã§ãã³ã¢ãªæè¡èªä½ã¯å®ã¯ãããªã«æ°ãããã®ã§ã¯ãªãã¨ãããã¨ãæ¹ãã¦ããåããã¾ããã
Docker ãããã ãæµè¡ãã¦ã»ã¼ããã¡ã¯ãã«ãªã£ã¦ããç¶æ³ãã¿ãã¨ã ã³ã³ããå®è¡ã®ããã®ã©ã³ã¿ã¤ã ããããããã±ã¼ã¸ã³ã°ã¨ãã®é å¸ãç°¡åã«ããããã® Dockerfile 㨠Docker ã¬ã¸ã¹ããªã¨ Docker Hub ã®åå¨ãããªã大ããã£ãããããªãããªã¨æãã¾ãã
ç¸å¤ããã Docker ã¯é常ã«ç´ æ´ãããã½ããã¦ã§ã¢ã ã¨æãã¾ãããåããããã¡ã¯ãã«ãªãã¤ã¤ãã Kubernetes ãç¥ãã°ç¥ãã»ã©å¥¥ãæ·±ãã®ã§ãä»å¾ãç©æ¥µçã«ä½¿ã£ã¦ãããã¨ãããã¾ãã
ããã§ã¯çããããã®å¤ã« Docker ãã¤ãã£ã¦ã¿ã¦å¦ã³ãæ·±ããå¿«é©ãª Docker ã©ã¤ãããéãããã ããï¼