๐ŸŒฑ Infra/Container_Docker

[Docker-Basic (8)] Container Image ๊ด€๋ฆฌํ•˜๊ธฐ

mini_world 2020. 9. 28. 17:19
๋ชฉ์ฐจ ์ ‘๊ธฐ

์ด๋ฒˆํฌ์ŠคํŒ…์—์„œ๋Š” ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ๊ธฐ๋ณธ ๋‚ด์šฉ์„ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค.


 

์‹œ์ž‘ํ•˜๊ธฐ์ „์—, ๋ชจ๋“  ๋„์ปค ์ด๋ฏธ์ง€์™€ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ญ์ œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ํ—ท๊ฐˆ๋ฆด์ˆ˜์žˆ์œผ๋‹ˆ๊นŒ์š”!

๋จผ์ € ์ปจํ…Œ์ด๋„ˆ ์ „์ฒด ์‚ญ์ œ!

[root@docker-master /]# docker rm -f $(docker ps -aq)
2bf8c98f110d

๊ทธ๋‹ค์Œ ์ด๋ฏธ์ง€๋„ ๋ชจ๋‘ ์‚ญ์ œํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

[root@docker-master /]# docker rmi -f $(docker images -q)
Untagged: registry:2
Untagged: registry@sha256:8be26f81ffea54106bae012c6f349df70f4d5e7e2ec01b143c46e2c03b9e551d
Deleted: sha256:2d4f4b5309b1e41b4f83ae59b44df6d673ef44433c734b14c1c103ebca82c116
Deleted: sha256:36a83751a1193f6edf65d21f62fe7988394d562d71904ee20cb4aa72f14148bd
Deleted: sha256:c2588dfa0db64031c1c0683ed59a00ad0943889efb819bffd0455ae9b232016c
Deleted: sha256:6eb97e254200a2d82064e63977f84626e376256fe4d4c033a8b7817b2950071d
Deleted: sha256:7c6634cc9948baef3600559e298228a962bd9499cbbaf840d4d4889f5abaca2a
Deleted: sha256:3e207b409db364b595ba862cdc12be96dcdad8e36c59a03b7b3b61c946a5741a

 


 

์ด๋ฒˆ ์‹ค์Šต์—์„œ๋Š” Image์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ๋‹ค๋ฃฐ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.
๋ณธ๊ฒฉ์ ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— Docker Folw์„ ๊ฐ„๋žตํžˆ ์‚ดํŽด๋ณผ๊ฒŒ์š” :)

์ถœ์ฒ˜ : https://sh-tsang.medium.com/docker-tutorial-4-exporting-container-and-saving-image-c3a7d792cfb6

 

 

1. Docker Image Pull : ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ

docker pull ๋ช…๋ น์–ด๋กœ ์‰ฝ๊ฒŒ  ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ์™ธ๋ถ€ ์ €์žฅ์†Œ์—์„œ ๋‹ค์šด๋กœ๋“œ ํ•ฉ๋‹ˆ๋‹ค. 

[root@docker-master /]# docker pull centos/httpd-24-centos7
Using default tag: latest
latest: Pulling from centos/httpd-24-centos7
75f829a71a1c: Pull complete
e2c4942f4189: Pull complete
f1498894b11c: Pull complete
da56c9694723: Pull complete
063ff8a1435c: Pull complete
09351523c1de: Pull complete
9a03c4ed0deb: Pull complete
17f560ff6bfd: Pull complete
Digest: sha256:df111e9031b838fa9b5c120b07cba0f23188a539e9be91e020b0a9f77d65ec9e
Status: Downloaded newer image for centos/httpd-24-centos7:latest
docker.io/centos/httpd-24-centos7:latest

์ด๋ฏธ์ง€๊ฐ€ ์ž˜ ์ €์žฅ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

[root@docker-master /]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
centos/httpd-24-centos7   latest              24aec7b98d6b        3 days ago          344MB

 

 

2. Docker Image๋กœ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ๋ฐ ํ™•์ธ

์œ„์—์„œ ๋‹ค์šด๋ฐ›์€ ์ด๋ฏธ์ง€๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ฒ ์Šต๋‹ˆ๋‹ค.

[root@docker-master /]# docker run --name httpd -d -p 8180:8080 centos/httpd-24-centos7
dc1351b75b8d9f63e0444f2a5e74f7f08f55011a593abd4108d24cad74419494

์ž˜ ์‹คํ–‰์ค‘์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

[root@docker-master /]# docker container ls
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                              NAMES
dc1351b75b8d        centos/httpd-24-centos7   "container-entrypoin…"   5 seconds ago       Up 4 seconds        8443/tcp, 0.0.0.0:8180->8080/tcp   httpd

 

 

3. ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ด๋ฏธ์ง€๋กœ ์ €์žฅ : Docker Commit

commit ์€ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ด๋ฏธ์ง€๋กœ ์ €์žฅํ•˜๋Š” ๋ช…๋ น์–ด ์ž…๋‹ˆ๋‹ค. :)
์ด๋ฒˆ ๋‹จ๊ณ„์—์„œ๋Š” ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ˆ˜์ •ํ•˜๊ณ , commit ํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฒ„์ „์˜ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์ž, ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก์„ ํ™•์ธํ•˜๊ณ  ์œ„์—์„œ ์‹คํ–‰ํ•œ ์ปจํ…Œ์ด๋„ˆ์— ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค.

[root@docker-master /]# docker container ls
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                              NAMES
dc1351b75b8d        centos/httpd-24-centos7   "container-entrypoin…"   4 minutes ago       Up 4 minutes        8443/tcp, 0.0.0.0:8180->8080/tcp   httpd

[root@docker-master /]# docker container exec -it httpd /bin/bash

index.html ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. :)
์ด์ œ, ์ด ์ปจํ…Œ์ด๋„ˆ๋Š” This is Docker Test ๋ผ๋Š” index.html์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

bash-4.2$ echo 'This is Docker Test' > /var/www/html/index.html
bash-4.2$ exit
exit

์ž, ์ด์ œ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ค‘์ง€ํ•˜๊ณ , ์ปค๋ฐ‹ํ•ฉ๋‹ˆ๋‹ค.

* docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
-a = ์ž‘์„ฑ์ž ์ง€์ •
-m = ๋ฉ”์‹œ์ง€ ์ง€์ • 
[root@docker-master /]# docker container stop httpd
httpd

[root@docker-master /]# docker container commit -a 'mini' -m 'Add Index.html' httpd
sha256:d6422173cd696f5f2b3750cb7025d8962ea9a7ff493a55466b0058c1100884c4

์ด๋ฏธ์ง€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. None ์ด๋ผ๋Š” ํƒœ๊ทธ๋ฅผ ๊ฐ€์ง„ ์ด๋ฏธ์ง€๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๋„ค์š” :)

[root@docker-master /]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
<none>                    <none>              d6422173cd69        6 seconds ago       344MB
centos/httpd-24-centos7   latest              24aec7b98d6b        3 days ago          344MB

 

 

4. Docker Image ํƒœ๊ทธ

์ด๋ฏธ์ง€์— ํƒœ๊ทธ๋ฅผ ๋‹ฌ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

[root@docker-master /]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
<none>                    <none>              d6422173cd69        6 seconds ago       344MB
centos/httpd-24-centos7   latest              24aec7b98d6b        3 days ago          344MB

docker image tag ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€์— ํƒœ๊ทธ๋ฅผ ๋‹ฌ์•„์ค๋‹ˆ๋‹ค :)

* docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] 

์ดํ›„์— ์ด๋ฏธ์ง€๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด, ํƒœ๊ทธ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์„ค์ •๋œ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[root@docker-master /]# docker image tag 24aec7b98d6b 1mini2/myrepo:addIndex_v1

[root@docker-master /]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
1mini2/myrepo             addIndex_v1         24aec7b98d6b        3 days ago          344MB
centos/httpd-24-centos7   latest              24aec7b98d6b        3 days ago          344MB

 

 

5. Docker registry์— Push/Pull ํ•˜๊ธฐ (๋„์ปค ํ—ˆ๋ธŒ ์‚ฌ์šฉ)

๋„์ปค  ํ—ˆ๋ธŒ์— ์ ‘์†ํ•˜์‹œ๊ณ , ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
์ด ๋‹จ๊ณ„์—์„œ๋Š” ๋ณ„๋„๋กœ ์•ˆ๋‚ดํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค :)

docker login ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋„์ปค ํ—ˆ๋ธŒ์— ๋กœ๊ทธ์ธ ํ•ฉ๋‹ˆ๋‹ค.

[root@docker-master /]# docker login --username 1mini2

Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

์ด์ œ, ์ด ์ด๋ฏธ์ง€๋ฅผ ๋„์ปค ํ—ˆ๋ธŒ ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋กœ Pushํ•ฉ๋‹ˆ๋‹ค.

[root@docker-master /]# docker image push 1mini2/myrepo:addIndex_v1

The push refers to repository [docker.io/1mini2/myrepo]
c6c197ea251d: Mounted from centos/httpd-24-centos7
be00ea727b88: Mounted from centos/httpd-24-centos7
4973b9e35e84: Mounted from centos/httpd-24-centos7
f4626fba958f: Mounted from centos/httpd-24-centos7
a317a6aa16e0: Mounted from centos/httpd-24-centos7
7da7d3716181: Mounted from centos/httpd-24-centos7
0b0618c6ced8: Mounted from centos/httpd-24-centos7
613be09ab3c0: Mounted from centos/httpd-24-centos7 

๋„์ปค ํ—ˆ๋ธŒ ์ ‘์† ์‹œ ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ด์ œ ์ด ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

[root@docker-master /]# docker rmi -f $(docker images -q)
Untagged: 1mini2/myrepo:addIndex_v1
Untagged: 1mini2/myrepo@sha256:df111e9031b838fa9b5c120b07cba0f23188a539e9be91e020b0a9f77d65ec9e
Deleted: sha256:24aec7b98d6bc27d1a9af66958328cf4193c6f29b770b3005da1ae00baa13e61

๋„์ปค ์ด๋ฏธ์ง€ ๋ชฉ๋ก์„ ํ™•์ธํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์•„๋ฌด๊ฒƒ๋„ ์—†์Šต๋‹ˆ๋‹ค.

[root@docker-master /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

docker pull ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋„์ปค ํ—ˆ๋ธŒ์— ์—…๋กœ๋“œํ•ด๋‘์—ˆ๋˜ ์ด๋ฏธ์ง€๋ฅผ ๋‹ค์šด๋ฐ›์Šต๋‹ˆ๋‹ค.

[root@docker-master /]# docker pull 1mini2/myrepo:addIndex_v1
addIndex_v1: Pulling from 1mini2/myrepo
75f829a71a1c: Already exists
e2c4942f4189: Already exists
f1498894b11c: Already exists
da56c9694723: Already exists
063ff8a1435c: Already exists
09351523c1de: Already exists
9a03c4ed0deb: Already exists
17f560ff6bfd: Already exists
Digest: sha256:df111e9031b838fa9b5c120b07cba0f23188a539e9be91e020b0a9f77d65ec9e
Status: Downloaded newer image for 1mini2/myrepo:addIndex_v1
docker.io/1mini2/myrepo:addIndex_v1

๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

[root@docker-master /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
1mini2/myrepo       addIndex_v1         24aec7b98d6b        3 days ago          344MB

 

 

6. Docker ์ด๋ฏธ์ง€ ์ €์žฅ(save) ๋ฐ ์‚ฌ์šฉ (load)

๋„์ปค ์ปจํ…Œ์ด๋„ˆ๋Š” tarํŒŒ์ผ๋กœ ์ €์žฅ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ด๋ ‡๊ฒŒ tar๋กœ ์ €์žฅํ•˜์—ฌ ๋ณด๊ด€ํ•˜๊ณ  ๋‹ค์‹œ ๋กœ๋“œํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๊ฒŒ ๋จผ์ € ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  ๊ทธ ์•ˆ์—์„œ ์ž‘์—…ํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

[root@docker-master /]# mkdir /docker_images && cd docker_images/

๋„์ปค ์ด๋ฏธ์ง€ ๋ชฉ๋ก์„ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.

[root@docker-master docker_images]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
1mini2/myrepo       addIndex_v1         24aec7b98d6b        3 days ago          344MB

๋„์ปค ์ด๋ฏธ์ง€๋ฅผ tarํŒŒ์ผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

* ๋ช…๋ น์–ด : docker save [OPTIONS] IMAGE [IMAGE...]
[root@docker-master docker_images]# docker save -o myweb.tar 1mini2/myrepo:addIndex_v1

์ €์žฅ์ด ์ž˜ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
์ž˜ ์ €์žฅ ๋˜์—ˆ๋„ค์š” :)

[root@docker-master docker_images]# ls -al
ํ•ฉ๊ณ„ 348232
drwxr-xr-x.  2 root root        23  9์›” 29 02:44 .
dr-xr-xr-x. 18 root root       262  9์›” 29 02:44 ..
-rw-------.  1 root root 356587008  9์›” 29 02:44 myweb.tar

์ž ์ด์ œ ์ด tarํŒŒ์ผ์„ ๋‹ค์‹œ ์ด๋ฏธ์ง€๋กœ ๊ฐ€์ ธ์˜ฌ ์˜ˆ์ •์ธ๋ฐ, ๊ทธ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์—!
๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ์ „์ฒด ์‚ญ์ œํ•ด์ค๋‹ˆ๋‹ค. ํ—ท๊ฐˆ๋ฆด ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ์š”.

[root@docker-master docker_images]# docker rmi -f $(docker images -q)
Untagged: 1mini2/myrepo:addIndex_v1
Untagged: 1mini2/myrepo@sha256:df111e9031b838fa9b5c120b07cba0f23188a539e9be91e020b0a9f77d65ec9e
Deleted: sha256:24aec7b98d6bc27d1a9af66958328cf4193c6f29b770b3005da1ae00baa13e61

[root@docker-master docker_images]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

์ž, ์ด์ œ docker load ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ tarํŒŒ์ผ์„ ์ด๋ฏธ์ง€๋กœ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.

[root@docker-master docker_images]# docker load -i ./myweb.tar
Loaded image: 1mini2/myrepo:addIndex_v1

์ž˜ ๋กœ๋“œ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.

[root@docker-master docker_images]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
1mini2/myrepo       addIndex_v1         24aec7b98d6b        3 days ago          344MB

 

 

7. Docker ์ด๋ฏธ์ง€ & ์ปจํ…Œ์ด๋„ˆ ์‚ญ์ œ

๋ชจ๋“  ํ…Œ์ŠคํŠธ๊ฐ€ ๋งˆ๋ฌด๋ฆฌ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค :)
์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ •๋ฆฌํ•ด๋ด…์‹œ๋‹ค.
๋„์ปค ์ด๋ฏธ์ง€์™€ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

[root@docker-master /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
1mini2/myrepo       addIndex_v1         24aec7b98d6b        3 days ago          344MB

[root@docker-master /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
dc1351b75b8d        24aec7b98d6b        "container-entrypoin…"   28 minutes ago      Exited (0) 23 minutes ago                       httpd

๋„์ปค ์ด๋ฏธ์ง€ ์ „์ฒด ์‚ญ์ œ๋ฅผ docker rmi -f $(docker images) ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

[root@docker-master /]# docker rmi -f $(docker images)
Untagged: 1mini2/myrepo:addIndex_v1
Deleted: sha256:24aec7b98d6bc27d1a9af66958328cf4193c6f29b770b3005da1ae00baa13e61

๋„์ปค ์ปจํ…Œ์ด๋„ˆ ์ „์ฒด ์‚ญ์ œ๋ฅผ docker rm -f $(docker ps -aq) ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

[root@docker-master /]# docker rm -f $(docker ps -aq)
dc1351b75b8d

์ปจํ…Œ์ด๋„ˆ์™€ ์ด๋ฏธ์ง€๊ฐ€ ์ž˜ ์‚ญ์ œ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค :)

[root@docker-master /]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

[root@docker-master /]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

 

728x90