도커 컨테이너를 이미지로 커밋하는 방법

컨테이너의 현재 상태를 저장하려면 커밋 명령을 사용하여 새 이미지를 생성할 수 있습니다. 이는 VM웨어의 스냅샷 원리와 유사합니다.

먼저 톰캣 컨테이너를 실행합니다:

[root@server ~]# docker run -it -p 8017:8080 tomcat

새 터미널을 열고 실행 중인 컨테이너를 확인합니다:

[root@server ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED             STATUS             PORTS                                                  NAMES
1f43a2a3da09   tomcat                "catalina.sh run"        22 seconds ago      Up 21 seconds      0.0.0.0:8017->8080/tcp, :::8017->8080/tcp              ecstatic_dewdney
0452805ef0cc   portainer/portainer   "/portainer"             About an hour ago   Up About an hour   0.0.0.0:8025->9000/tcp, :::8025->9000/tcp              tender_kepler
7a350c2f01ff   wordpress             "docker-entrypoint.s…"   42 hours ago        Up 42 hours        0.0.0.0:8007->80/tcp, :::8007->80/tcp                  my_wordpress
94239899c5bd   mysql                 "docker-entrypoint.s…"   42 hours ago        Up 42 hours        33060/tcp, 0.0.0.0:8006->3306/tcp, :::8006->3306/tcp   my_mysql

기본 톰캣 이미지에는 webapps 애플리케이션이 없으므로, 필요한 파일을 webapps 디렉토리로 복사합니다:

[root@server ~]# docker exec -it 1f43a2a3da09 /bin/bash
root@1f43a2a3da09:/usr/local/tomcat# cd webapps
root@1f43a2a3da09:/usr/local/tomcat/webapps# ls
root@1f43a2a3da09:/usr/local/tomcat/webapps# cd ..
root@1f43a2a3da09:/usr/local/tomcat# ls
BUILDING.txt     LICENSE  README.md      RUNNING.txt  conf  logs            temp     webapps.dist
CONTRIBUTING.md  NOTICE   RELEASE-NOTES  bin          lib   native-jni-lib  webapps  work
root@1f43a2a3da09:/usr/local/tomcat# cp -r  webapps.dist/* webapps
root@1f43a2a3da09:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager

브라우저에서 접속하여 배포가 성공했는지 확인합니다. 그런 다음 컨테이너에서 나옵니다:

exit

수정된 톰캣을 커밋하여 새 이미지로 저장하면, 이후에는 수정된 이미지를 직접 사용할 수 있습니다. tomcat_custom은 변경된 이미지입니다:

[root@server ~]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS             PORTS                                                          NAMES
1f43a2a3da09   tomcat                "catalina.sh run"        9 minutes ago   Up 9 minutes       0.0.0.0:8017->8080/tcp, :::8017->8080/tcp              ecstatic_dewdney
0452805ef0cc   portainer/portainer   "/portainer"             2 hours ago     Up About an hour   0.0.0.0:8025->9000/tcp, :::8025->9000/tcp              tender_kepler
7a350c2f01ff   wordpress             "docker-entrypoint.s…"   42 hours ago    Up 42 hours        0.0.0.0:8007->80/tcp, :::8007->80/tcp                  my_wordpress
94239899c5bd   mysql                 "docker-entrypoint.s…"   42 hours ago    Up 42 hours        33060/tcp, 0.0.0.0:8006->3306/tcp, :::8006->3306/tcp   my_mysql

[root@server ~]# docker commit -a="admin" -m="add webapps applications" 1f43a2a3da09 tomcat_custom:1.0
sha256:02ea20370fecf241eb1126ce9861749fddd14d622358348fc1fcfd2ff8ed9186

[root@server ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED         SIZE
tomcat_custom         1.0       02ea20370fec   5 seconds ago   684MB
nginx                 latest    605c77e624dd   2 years ago     141MB
tomcat                latest    fb5657adc892   2 years ago     680MB
wordpress             latest    c3c92cc3dcb1   2 years ago     616MB
redis                 latest    7614ae9453d1   2 years ago     113MB
mysql                 latest    3218b38490ce   2 years ago     516MB
centos                latest    5d0da3dc9764   2 years ago     231MB
portainer/portainer   latest    580c0e4e98b0   2 years ago     79.1MB
elasticsearch         7.6.2     f29a1ee41030   3 years ago     791MB

태그: 도커 컨테이너 이미지 커밋 톰캣

7월 13일 23:23에 게시됨