태깅되지 않은 이미지를 검색 및 태그 붙이기
태깅되지 않은 이미지 검색
vagrant@xenial64:~/pulltest$ docker image ls -f "dangling=true"
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> fefad6ab4ef6 11 minutes ago 1.23MB
이미지에 태그를 변경 방법
vagrant@xenial64:~/pulltest$ docker image tag --help
Usage: docker image tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
태킹되지 않은 이미지에 태그를 추가
vagrant@xenial64:~/pulltest$ docker image tag $(docker image ls -f "dangling=true" -q) myanjini/basetest:0.1
vagrant@xenial64:~/pulltest$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myanjini/pulltest latest 181d7129cf05 6 minutes ago 1.23MB
myanjini/basetest lastest e17d780478cf 11 minutes ago 1.23MB
myanjini/basetest latest e17d780478cf 11 minutes ago 1.23MB
myanjini/basetest 0.1 fefad6ab4ef6 14 minutes ago 1.23MB
myanjini/basetest <none> 54d6c33b5a41 17 minutes ago 1.23MB
busybox latest 6858809bf669 5 days ago 1.23MB
Dockerfile로 이미지 빌드 시 주의사항
이미지 빌드가 완료되면 Dockerfile의 명령어 줄 수 만큼의 레이어가 존재
실제 컨테이너에서 사용하지 못하는 파일(디렉터리)이 이미지 레이어에 존재하면 공간만 차지하게 됨
⇒ Dockerfile을 작성할 때 &&로 각 RUN 명령어를 하나로 묶어서 실행
3개의 RUN 명령어 실행 → 실제 이미지 내부에 변경은 없음 (100M 크기의 파일을 생성 후 삭제하므로)
vagrant@xenial64:~/pulltest$ mkdir ~/dockerfile_test && cd ~/dockerfile_test
vagrant@xenial64:~/dockerfile_test$ vi Dockerfile
FROM ubuntu RUN mkdir /test RUN fallocate -l 100m /test/dumy RUN rm /test/dumy |
vagrant@xenial64:~/dockerfile_test$ docker image build -t falloc_100m .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM ubuntu
latest: Pulling from library/ubuntu
54ee1f796a1e: Pull complete f7bfea53ad12: Pull complete 46d371e02073: Pull complete b66c17bbf772: Pull complete Digest: sha256:31dfb10d52ce76c5ca0aa19d10b3e6424b830729e32a89a7c6eee2cda2be67a5
Status: Downloaded newer image for ubuntu:latest
---> 4e2eef94cd6b
Step 2/4 : RUN mkdir /test
---> Running in 0212d1d0b0f0
Removing intermediate container 0212d1d0b0f0
---> a761c33cecb5
Step 3/4 : RUN fallocate -l 100m /test/dumy
---> Running in 2038896a36c9
Removing intermediate container 2038896a36c9
---> e933e53411c2
Step 4/4 : RUN rm /test/dumy
---> Running in ebd0a961b02b
Removing intermediate container ebd0a961b02b
---> cb419b52df77
Successfully built cb419b52df77
Successfully tagged falloc_100m:latest
vagrant@xenial64:~/dockerfile_test$
vagrant@xenial64:~/dockerfile_test$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
falloc_100m latest cb419b52df77 23 seconds ago 179MB
myanjini/pulltest latest 181d7129cf05 18 minutes ago 1.23MB
myanjini/basetest lastest e17d780478cf 22 minutes ago 1.23MB
myanjini/basetest latest e17d780478cf 22 minutes ago 1.23MB
myanjini/basetest 0.1 fefad6ab4ef6 25 minutes ago 1.23MB
myanjini/basetest <none> 54d6c33b5a41 29 minutes ago 1.23MB
busybox latest 6858809bf669 5 days ago 1.23MB
ubuntu latest 4e2eef94cd6b 3 weeks ago 73.9MB
3개의 RUN 명령어를 하나로 줄여서 실행
vagrant@xenial64:~/dockerfile_test$ vi Dockerfile
FROM ubuntu RUN mkdir /test && fallocate -l 100m /test/dumy && rm /test/dumy |
vagrant@xenial64:~/dockerfile_test$ docker build -t recommand .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM ubuntu
---> 4e2eef94cd6b
Step 2/2 : RUN mkdir /test && fallocate -l 100m /test/dumy && rm /test/dumy
---> Running in 341f7850ca20
Removing intermediate container 341f7850ca20
---> 6a667c9fadb5
Successfully built 6a667c9fadb5
Successfully tagged recommand:latest
vagrant@xenial64:~/dockerfile_test$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
recommand latest 6a667c9fadb5 4 seconds ago 73.9MB
falloc_100m latest cb419b52df77 3 minutes ago 179MB
myanjini/pulltest latest 181d7129cf05 21 minutes ago 1.23MB
myanjini/basetest lastest e17d780478cf 26 minutes ago 1.23MB
myanjini/basetest latest e17d780478cf 26 minutes ago 1.23MB
myanjini/basetest 0.1 fefad6ab4ef6 29 minutes ago 1.23MB
myanjini/basetest <none> 54d6c33b5a41 32 minutes ago 1.23MB
busybox latest 6858809bf669 5 days ago 1.23MB
ubuntu latest 4e2eef94cd6b 3 weeks ago 73.9MB
도커 컨테이너 생명주기 (P62)
docker container run 명령의 인자를 이용해서 CMD 명령을 오버라이드 (P64)
libray/alpine:latest 이미지의 Dockerfile 확인
https://hub.docker.com/_/alpine
FROM scratch ADD alpine-minirootfs-3.12.0-x86_64.tar.gz / CMD ["/bin/sh"] |
alipne 이미지를 이용해서 컨테이너를 생성
vagrant@xenial64:~$ docker container run -it alpine
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
df20fa9351a1: Pull complete Digest: sha256:185518070891758909c9f839cf4ca393ee977ac378609f700f60a771a2dfe321
Status: Downloaded newer image for alpine:latest
/ # uname -a ⇐ 컨테이너가 생성되면 컨테이너 내부에 쉘을 실행
Linux 935620ae9dd7 4.4.0-189-generic #219-Ubuntu SMP Tue Aug 11 12:26:50 UTC 2020 x86_64 Linux
/ # ls
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # exit
CMD 명령을 오버라이드
vagrant@xenial64:~$ docker container run -it alpine uname -a
Linux 9bf859f09b0f 4.4.0-189-generic #219-Ubuntu SMP Tue Aug 11 12:26:50 UTC 2020 x86_64 Linux ⇐ uname -a 결과
vagrant@xenial64:~$ docker container run -it alpine ls
bin etc lib mnt proc run srv tmp var
dev home media opt root sbin sys usr
vagrant@xenial64:~$ docker container run -it alpine ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
70: eth0@if71: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
컨테이너 목록 필터링 (P67)
테스트를 위해 이미지 생성하고 컨테이너를 실행
vagrant@xenial64:~/chap02$ docker image build -t example/echo:latest .
vagrant@xenial64:~/chap02$ docker container run -d -p 9000:8080 example/echo:latest
46c40acffd83c0cdf6770aa993683697b51729a5ea4b7c6e34bdd50047702215
vagrant@xenial64:~/chap02$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46c40acffd83 example/echo:latest "go run /echo/main.go" 6 seconds ago Up 6 seconds 0.0.0.0:9000->8080/tcp epic_spence
컨테이너 이름을 부여해서 실행
vagrant@xenial64:~/chap02$ docker container run -d -p 9001:8080 --name myecho example/echo:latest
b10088f2914a82b77bbd759586156fbad3719552079dfb7644b3ad72cc50ba3c
vagrant@xenial64:~/chap02$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b10088f2914a example/echo:latest "go run /echo/main.go" 7 seconds ago Up 6 seconds 0.0.0.0:9001->8080/tcp myecho
46c40acffd83 example/echo:latest "go run /echo/main.go" 2 minutes ago Up 2 minutes 0.0.0.0:9000->8080/tcp epic_spence
중지된 상태의 컨테이너를 포함해서 조회
vagrant@xenial64:~/chap02$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b10088f2914a example/echo:latest "go run /echo/main.go" 58 seconds ago Up 57 seconds 0.0.0.0:9001->8080/tcp myecho
46c40acffd83 example/echo:latest "go run /echo/main.go" 3 minutes ago Up 3 minutes 0.0.0.0:9000->8080/tcp epic_spence
88d914e6d686 alpine "ip a" 10 minutes ago Exited (0) 10 minutes ago ecstatic_aryabhata
bbd991ac33b9 alpine "ls" 10 minutes ago Exited (0) 10 minutes ago determined_bhabha
9bf859f09b0f alpine "uname -a" 11 minutes ago Exited (0) 11 minutes ago admiring_thompson
935620ae9dd7 alpine "/bin/sh" 13 minutes ago Exited (0) 13 minutes ago awesome_shamir
컨터이너 이름으로 조회
vagrant@xenial64:~/chap02$ docker container ls -a --filter "name=myecho"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b10088f2914a example/echo:latest "go run /echo/main.go" 2 minutes ago Up 2 minutes 0.0.0.0:9001->8080/tcp myecho
컨테이너 생성에 사용한 이미지로 조회 (사용자명/이미지명 형식으로 조회)
vagrant@xenial64:~/chap02$ docker container ls -a --filter "ancestor=example/echo:latest"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b10088f2914a example/echo:latest "go run /echo/main.go" 3 minutes ago Up 3 minutes 0.0.0.0:9001->8080/tcp myecho
46c40acffd83 example/echo:latest "go run /echo/main.go" 6 minutes ago Up 6 minutes 0.0.0.0:9000->8080/tcp epic_spence
vagrant@xenial64:~/chap02$ docker container ls -a --filter "ancestor=example/echo"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b10088f2914a example/echo:latest "go run /echo/main.go" 4 minutes ago Up 4 minutes 0.0.0.0:9001->8080/tcp myecho
46c40acffd83 example/echo:latest "go run /echo/main.go" 6 minutes ago Up 6 minutes 0.0.0.0:9000->8080/tcp epic_spence
vagrant@xenial64:~/chap02$ docker container ls -a --filter "ancestor=echo"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Quiz.
태그가 다른 이미지를 이용해서 실행한 컨테이너를 ancestro 필터로 검색할 경우 결과는 어떻게 될까?
#1 example/echo:latest 이미지를 example/echo:old로 태그를 변경
vagrant@xenial64:~/chap02$ docker image tag example/echo:latest example/echo:old
vagrant@xenial64:~/chap02$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
example/echo latest 16a4c3b0f222 18 minutes ago 750MB
example/echo old 16a4c3b0f222 18 minutes ago 750MB
recommand latest 6a667c9fadb5 About an hour ago 73.9MB
falloc_100m latest cb419b52df77 About an hour ago 179MB
myanjini/pulltest latest 181d7129cf05 About an hour ago 1.23MB
myanjini/basetest lastest e17d780478cf About an hour ago 1.23MB
myanjini/basetest latest e17d780478cf About an hour ago 1.23MB
myanjini/basetest 0.1 fefad6ab4ef6 About an hour ago 1.23MB
myanjini/basetest <none> 54d6c33b5a41 2 hours ago 1.23MB
busybox latest 6858809bf669 6 days ago 1.23MB
ubuntu latest 4e2eef94cd6b 3 weeks ago 73.9MB
alpine latest a24bb4013296 3 months ago 5.57MB
golang 1.9 ef89ef5c42a9 2 years ago 750MB
#2 example/echo:old 이미지로 컨테이너를 생성
vagrant@xenial64:~/chap02$ docker container run -d -p 9002:8080 --name myoldecho example/echo:old
e04df49d7bea5364999e2311acc3f80b5586b9d1da21c215112e20286174ffa2
vagrant@xenial64:~/chap02$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e04df49d7bea example/echo:old "go run /echo/main.go" 10 seconds ago Up 9 seconds 0.0.0.0:9002->8080/tcp myoldecho
b10088f2914a example/echo:latest "go run /echo/main.go" 16 minutes ago Up 16 minutes 0.0.0.0:9001->8080/tcp myecho
46c40acffd83 example/echo:latest "go run /echo/main.go" 18 minutes ago Up 18 minutes 0.0.0.0:9000->8080/tcp epic_spence
#3 ancestor=example/echo로 검색
vagrant@xenial64:~/chap02$ docker container ls --filter "ancestor=example/echo"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e04df49d7bea example/echo:old "go run /echo/main.go" 54 seconds ago Up 53 seconds 0.0.0.0:9002->8080/tcp myoldecho
b10088f2914a example/echo:latest "go run /echo/main.go" 17 minutes ago Up 17 minutes 0.0.0.0:9001->8080/tcp myecho
46c40acffd83 example/echo:latest "go run /echo/main.go" 19 minutes ago Up 19 minutes 0.0.0.0:9000->8080/tcp epic_spence
#4 ancestor=example/echo:latest로 검색
vagrant@xenial64:~/chap02$ docker container ls --filter "ancestor=example/echo:latest"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e04df49d7bea example/echo:old "go run /echo/main.go" About a minute ago Up 59 seconds 0.0.0.0:9002->8080/tcp myoldecho
b10088f2914a example/echo:latest "go run /echo/main.go" 17 minutes ago Up 17 minutes 0.0.0.0:9001->8080/tcp myecho
46c40acffd83 example/echo:latest "go run /echo/main.go" 19 minutes ago Up 19 minutes 0.0.0.0:9000->8080/tcp epic_spence
#3번과 #4번의 결과가 같을까? 다를까?
⇒ 같다 ⇒ 필터링에 사용하는 이미지는 "사용자명/이미지명"까지만 식별
출력 형식 지정 (formatting)
https://docs.docker.com/engine/reference/commandline/ps/
vagrant@xenial64:~/chap02$ docker container ls -a --format "{{.ID}} : {{.Command}}"
e04df49d7bea : "go run /echo/main.go"
b10088f2914a : "go run /echo/main.go"
46c40acffd83 : "go run /echo/main.go"
88d914e6d686 : "ip a"
bbd991ac33b9 : "ls"
9bf859f09b0f : "uname -a"
935620ae9dd7 : "/bin/sh"
vagrant@xenial64:~/chap02$ docker container ls -a --format "table {{.ID}} : {{.Names}}\t{{.Command}}"
CONTAINER ID : NAMES COMMAND
e04df49d7bea : myoldecho "go run /echo/main.go"
b10088f2914a : myecho "go run /echo/main.go"
46c40acffd83 : epic_spence "go run /echo/main.go"
88d914e6d686 : ecstatic_aryabhata "ip a"
bbd991ac33b9 : determined_bhabha "ls"
9bf859f09b0f : admiring_thompson "uname -a"
935620ae9dd7 : awesome_shamir "/bin/sh"
컨테이너 정지
vagrant@xenial64:~/chap02$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e04df49d7bea example/echo:old "go run /echo/main.go" 8 minutes ago Up 8 minutes 0.0.0.0:9002->8080/tcp myoldecho
b10088f2914a example/echo:latest "go run /echo/main.go" 24 minutes ago Up 24 minutes 0.0.0.0:9001->8080/tcp myecho
46c40acffd83 example/echo:latest "go run /echo/main.go" 27 minutes ago Up 26 minutes 0.0.0.0:9000->8080/tcp epic_spence
vagrant@xenial64:~/chap02$ docker container stop myoldecho ⇐ 컨테이너 이름
myoldecho
vagrant@xenial64:~/chap02$ docker container stop b10088f2914a ⇐ 컨테이너 ID
b10088f2914a
vagrant@xenial64:~/chap02$ docker container stop 46 ⇐ 컨테이너 ID 일부 (앞에서 부터 식별 가능한 범위)
46
vagrant@xenial64:~/chap02$ docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
vagrant@xenial64:~/chap02$ docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e04df49d7bea example/echo:old "go run /echo/main.go" 9 minutes ago Exited (2) 33 seconds ago myoldecho
b10088f2914a example/echo:latest "go run /echo/main.go" 25 minutes ago Exited (2) 22 seconds ago myecho
46c40acffd83 example/echo:latest "go run /echo/main.go" 27 minutes ago Exited (2) 12 seconds ago epic_spence
88d914e6d686 alpine "ip a" 34 minutes ago Exited (0) 34 minutes ago ecstatic_aryabhata
bbd991ac33b9 alpine "ls" 34 minutes ago Exited (0) 34 minutes ago determined_bhabha
9bf859f09b0f alpine "uname -a" 35 minutes ago Exited (0) 35 minutes ago admiring_thompson
935620ae9dd7 alpine "/bin/sh" 37 minutes ago Exited (0) 37 minutes ago awesome_shamir
컨테이너 재시작
vagrant@xenial64:~/chap02$ docker container restart myoldecho
myoldecho
vagrant@xenial64:~/chap02$ docker container restart b10088f2914a
b10088f2914a
vagrant@xenial64:~/chap02$ docker container restart 46
46
vagrant@xenial64:~/chap02$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e04df49d7bea example/echo:old "go run /echo/main.go" 12 minutes ago Up 29 seconds 0.0.0.0:9002->8080/tcp myoldecho
b10088f2914a example/echo:latest "go run /echo/main.go" 28 minutes ago Up 16 seconds 0.0.0.0:9001->8080/tcp myecho
46c40acffd83 example/echo:latest "go run /echo/main.go" 30 minutes ago Up 3 seconds 0.0.0.0:9000->8080/tcp epic_spence
컨테이너 삭제
중지(Exited) 상태인 컨테이너를 삭제
#1 중지 상태인 컨테이너를 조회
vagrant@xenial64:~/chap02$ docker container ls --filter "status=exited"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
88d914e6d686 alpine "ip a" 39 minutes ago Exited (0) 39 minutes ago ecstatic_aryabhata
bbd991ac33b9 alpine "ls" 39 minutes ago Exited (0) 39 minutes ago determined_bhabha
9bf859f09b0f alpine "uname -a" 40 minutes ago Exited (0) 40 minutes ago admiring_thompson
935620ae9dd7 alpine "/bin/sh" 42 minutes ago Exited (0) 41 minutes ago awesome_shamir
#2 중지 상태인 컨터이너의 ID를 조회
vagrant@xenial64:~/chap02$ docker container ls --filter "status=exited" -q
88d914e6d686
bbd991ac33b9
9bf859f09b0f
935620ae9dd7
#3 중지 상태인 컨테이너를 일괄 삭제
vagrant@xenial64:~/chap02$ docker container rm $(docker container ls --filter "status=exited" -q)
88d914e6d686
bbd991ac33b9
9bf859f09b0f
935620ae9dd7
vagrant@xenial64:~/chap02$ docker container ls --filter "status=exited"
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
모든 컨테이너를 삭제
vagrant@xenial64:~/chap02$ docker container rm -f $(docker container ls -a -q)
~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~
모든 상태의 컨테이너의 ID를 조회
실행 중인 컨테이너는 중지하고 삭제
컨테이너를 매뉴얼(수동)하게 삭제하는 방법
#1 도커 서비스를 중지
$ sudo service docker stop
#2 컨테이너 파일 확인 및 삭제
$ sudo ls /var/lib/docker/containers
$ sudo rm -r /var/lib/docker/containers/CONTAINER_ID
#3 도커 서비스를 실행
$ sudo service docker start
컨테이너를 중지할 때 컨테이너를 자동으로 삭제
컨테이너를 중지하면 Exited 상태로 대기 ⇒ restart 명령으로 재기동 가능
vagrant@xenial64:~/chap02$ docker container run -d -p 9000:8080 example/echo:latest
f02810866949ba9d9c8ed7344d3c4da1daa8147994d92b83bae533666eca4b92
vagrant@xenial64:~/chap02$ docker container stop f02810866949ba9d9c8ed7344d3c4da1daa8147994d92b83bae533666eca4b92
f02810866949ba9d9c8ed7344d3c4da1daa8147994d92b83bae533666eca4b92
vagrant@xenial64:~/chap02$ docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f02810866949 example/echo:latest "go run /echo/main.go" 23 seconds ago Exited (2) 6 seconds ago thirsty_hawking
컨테이너 생성 시 --rm 옵션을 추가하면 컨테이너를 중지하면 해당 컨테이너를 삭제
vagrant@xenial64:~/chap02$ docker container run -d -p 9000:8080 --rm example/echo:latest
6be50783d1ecd8f8dcfda8c075509a1bc0143d141cf3295616fcf311bfff74ec
vagrant@xenial64:~/chap02$ docker container ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6be50783d1ec example/echo:latest "go run /echo/main.go" 6 seconds ago Up 5 seconds 0.0.0.0:9000->8080/tcp clever_elbakyan
vagrant@xenial64:~/chap02$ docker container stop 6be50783d1ec
6be50783d1ec
vagrant@xenial64:~/chap02$ docker container ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f02810866949 example/echo:latest "go run /echo/main.go" 3 minutes ago Exited (2) 2 minutes ago thirsty_hawking
컨테이너 내부의 표준 출력을 호스트로 연결
vagrant@xenial64:~/chap02$ docker container run -d -p 8080:8080 -p 5000:5000 jenkins
~~~~~~~
도커에서 공식 배포하는 최신 버전(latest)
⇒ libary/jenkins:latest 같은 의미
vagrant@xenial64:~/chap02$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
406959fb618b jenkins "/bin/tini -- /usr/l…" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp, 0.0.0.0:8080->8080/tcp, 50000/tcp fervent_heyrovsky
vagrant@xenial64:~/chap02$ docker container logs -f 406959fb618b
Running from: /usr/share/jenkins/jenkins.war
webroot: EnvVars.masterEnvVars.get("JENKINS_HOME")
Sep 15, 2020 2:25:55 AM Main deleteWinstoneTempContents
WARNING: Failed to delete the temporary Winstone file /tmp/winstone/jenkins.war
Sep 15, 2020 2:25:55 AM org.eclipse.jetty.util.log.JavaUtilLog info
INFO: Logging initialized @449ms
Sep 15, 2020 2:25:55 AM winstone.Logger logInternal
:
실행중인 컨테이너 내부로 명령을 전달(실행)
docker container exec 컨테이너이름 명령어
vagrant@xenial64:~/chap02$ docker container run -t -d --name echo --rm example/echo:latest
795009969b4719481861c339a06652a863dd9308b65a81fa8c64badbfbecabc4
vagrant@xenial64:~/chap02$ docker container exec echo pwd
/go
vagrant@xenial64:~/chap02$ docker container exec echo ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
92: eth0@if93: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
컨테이너 내부 쉘을 이용
vagrant@xenial64:~/chap02$ docker container exec -it echo /bin/sh
# pwd
/go
# ls
bin src
# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
92: eth0@if93: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
호스트의 파일 또는 디렉터리를 컨테이너 내부로 복사
docker container cp 호스트경로 컨테이너이름:컨테이너내부경로
호스트의 현재 시간을 파일로 생성
vagrant@xenial64:~/chap02$ date > host_now
vagrant@xenial64:~/chap02$ cat host_now
Tue Sep 15 02:38:55 UTC 2020
호스트의 파일을 echo 컨테이너 내부로 복사
vagrant@xenial64:~/chap02$ docker container cp ./host_now echo:/tmp/
컨테이너로 복사한 파일의 내용을 확인
vagrant@xenial64:~/chap02$ docker container exec echo cat /tmp/host_now
Tue Sep 15 02:38:55 UTC 2020
컨테이너 내부의 파일을 호스트로 복사
docker container cp 컨테이너이름:컨테이너내부경로 호스트경로
vagrant@xenial64:~/chap02$ docker container cp echo:/tmp/host_now ./host_now_from_container
vagrant@xenial64:~/chap02$ cat ./host_now_from_container
Tue Sep 15 02:38:55 UTC 2020
불필요한 컨테이너 및 이미지 삭제
vagrant@xenial64:~/chap02$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
406959fb618b jenkins "/bin/tini -- /usr/l…" 2 hours ago Up 2 hours 0.0.0.0:5000->5000/tcp, 0.0.0.0:8080->8080/tcp, 50000/tcp fervent_heyrovsky
f02810866949 example/echo:latest "go run /echo/main.go" 2 hours ago Exited (2) 2 hours ago thirsty_hawking
vagrant@xenial64:~/chap02$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
f02810866949ba9d9c8ed7344d3c4da1daa8147994d92b83bae533666eca4b92
Total reclaimed space: 4.201MB
vagrant@xenial64:~/chap02$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
406959fb618b jenkins "/bin/tini -- /usr/l…" 2 hours ago Up 2 hours 0.0.0.0:5000->5000/tcp, 0.0.0.0:8080->8080/tcp, 50000/tcp fervent_heyrovsky
vagrant@xenial64:~/chap02$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
example/echo latest 16a4c3b0f222 3 hours ago 750MB
example/echo old 16a4c3b0f222 3 hours ago 750MB
recommand latest 6a667c9fadb5 4 hours ago 73.9MB
falloc_100m latest cb419b52df77 4 hours ago 179MB
myanjini/basetest <none> 54d6c33b5a41 4 hours ago 1.23MB
busybox latest 6858809bf669 6 days ago 1.23MB
ubuntu latest 4e2eef94cd6b 3 weeks ago 73.9MB
alpine latest a24bb4013296 3 months ago 5.57MB
jenkins latest cd14cecfdb3a 2 years ago 696MB
golang 1.9 ef89ef5c42a9 2 years ago 750MB
vagrant@xenial64:~/chap02$ docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: myanjini/basetest@sha256:644df6c58461ab1966c6d311e001fb4fba486b29359fc07b7b1e5576e774238e
deleted: sha256:54d6c33b5a410a5f1f176a469a0cc054ad030468cbac34982732de97351a37a1
deleted: sha256:7d597b30b31ae81b7a165e80410107c6ad0e0225a093bc99a26e38ec0b551261
vagrant@xenial64:~/chap02$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
example/echo latest 16a4c3b0f222 3 hours ago 750MB
example/echo old 16a4c3b0f222 3 hours ago 750MB
recommand latest 6a667c9fadb5 4 hours ago 73.9MB
falloc_100m latest cb419b52df77 4 hours ago 179MB
busybox latest 6858809bf669 6 days ago 1.23MB
ubuntu latest 4e2eef94cd6b 3 weeks ago 73.9MB
alpine latest a24bb4013296 3 months ago 5.57MB
jenkins latest cd14cecfdb3a 2 years ago 696MB
golang 1.9 ef89ef5c42a9 2 years ago 750MB
컨테이너 단위 시스템 리소스 사용 현황 확인
vagrant@xenial64:~/chap02$ docker container stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
406959fb618b fervent_heyrovsky 0.07% 209.1MiB / 991.9MiB 21.08% 4.48MB / 96.8kB 44MB / 17.1MB 32
LAB. 특정 웹 페이지를 포함하고 있는 웹 서버 이미지를 생성
http://localhost:8080/hello.html 요청하면 hello docker 메시지를 반환하는 웹 서비스를 제공하는 이미지를 생성
첫번째 방식. 우분투 이미지를 이용해서 컨테이너를 실행하고 컨테이너 내부를 변경한 후 이미지를 생성
#1 작업 디렉터리 생성
vagrant@xenial64:~/chap02$ mkdir ~/webserver && cd ~/webserver
vagrant@xenial64:~/webserver$
#2 hello.html 파일을 생성
vagrant@xenial64:~/webserver$ echo "hello docker" > hello.html
vagrant@xenial64:~/webserver$ cat hello.html
hello docker
#3 우부투 이미지를 이용해서 컨테이너를 실행
vagrant@xenial64:~/webserver$ docker container rm -f myweb
myweb
vagrant@xenial64:~/webserver$ docker container run -dit -p 8080:80 --name myweb ubuntu:14.04
d8337bf2c8199d9227db7bb77986aa51c2457151d63c2311ee27e968136c6393
vagrant@xenial64:~/webserver$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d8337bf2c819 ubuntu:14.04 "/bin/bash" 15 seconds ago Up 14 seconds 0.0.0.0:8080->80/tcp myweb
#4 컨테이너 내부의 쉘로 접속
vagrant@xenial64:~/webserver$ docker container exec -it myweb /bin/bash
root@d8337bf2c819:/# exit
exit
vagrant@xenial64:~/webserver$ docker container attach myweb
root@d8337bf2c819:/# exit
exit
#5 컨테이너 내부에 아파치 웹 서버를 설치 및 실행
root@d8337bf2c819:/# apt-get update
root@d8337bf2c819:/# apt-get install apache2 -y
root@d8337bf2c819:/# service apache2 status
* apache2 is not running
root@d8337bf2c819:/# service apache2 start
* Starting web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
*
root@d8337bf2c819:/# service apache2 status
* apache2 is running
root@d8337bf2c819:/# ls /var/www/html
index.html
root@d8337bf2c819:/# exit
exit
#6 아파치 웹 서버의 웹 루트에 hello.html 파일을 복사
vagrant@xenial64:~/webserver$ docker container cp ./hello.html myweb:/var/www/html/
vagrant@xenial64:~/webserver$ docker container exec myweb cat /var/www/html/hello.html
hello docker
#7 컨테이너로 웹 서비스를 요청
vagrant@xenial64:~/webserver$ curl http://localhost:8080/hello.html
hello docker
#8 이미지를 생성
vagrant@xenial64:~/webserver$ docker commit myweb myanjini/myweb:latest
sha256:4d477e6f42d06c2e08db4d9f8902a3c89a2beace68291273fc43d7524f5b998c
vagrant@xenial64:~/webserver$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myanjini/myweb latest 4d477e6f42d0 6 seconds ago 221MB
example/echo latest 16a4c3b0f222 4 hours ago 750MB
example/echo old 16a4c3b0f222 4 hours ago 750MB
recommand latest 6a667c9fadb5 5 hours ago 73.9MB
falloc_100m latest cb419b52df77 5 hours ago 179MB
busybox latest 6858809bf669 6 days ago 1.23MB
ubuntu latest 4e2eef94cd6b 3 weeks ago 73.9MB
alpine latest a24bb4013296 3 months ago 5.57MB
ubuntu 14.04 6e4f1fe62ff1 9 months ago 197MB
jenkins latest cd14cecfdb3a 2 years ago 696MB
golang 1.9 ef89ef5c42a9 2 years ago 750MB
두번째 방식. Dockerfile을 작성해서 이미지를 생성
#1 Dockerfile 정의
vagrant@xenial64:~/webserver$ vi Dockerfile
FROM ubuntu:14.04 RUN apt-get update RUN apt-get install -y apache2 ADD hello.html /var/www/html/ EXPOSE 80 CMD apachectl -DFOREGROUND |
#2 Dockerfile을 빌드해서 이미지 생성
vagrant@xenial64:~/webserver$ docker image build -t myanjini/myweb:dockerfile .
vagrant@xenial64:~/webserver$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myanjini/myweb dockerfile 30e168051d24 16 seconds ago 221MB
myanjini/myweb latest 4d477e6f42d0 11 minutes ago 221MB
example/echo latest 16a4c3b0f222 4 hours ago 750MB
#3 생성한 이미지로 컨테이너를 실행
vagrant@xenial64:~/webserver$ docker container run -d -p 9090:80 --name mywebdockerfile myanjini/myweb:dockerfile
d2acd021ff6464bb1c518652a9a5555886b3531dd730e1cede97efc92e78c68a
vagrant@xenial64:~/webserver$ curl http://localhost:9090/hello.html
hello docker
#4 생성한 이미지로 컨테이너를 실행 (호스트 포트를 랜덤하게 지정)
vagrant@xenial64:~/webserver$ docker container run -d -P --name mywebrandport myanjini/myweb:dockerfile
c892aa35710c2656f1fe636850e62ab154086b26f897417bcc1fd602f9f41567
⇒ 호스트의 랜덤하게 할당된 포트와 컨테이너에서 EXPOSE된 포트를 자동으로 맵핑
vagrant@xenial64:~/webserver$ docker port mywebrandport
80/tcp -> 0.0.0.0:32770
vagrant@xenial64:~/webserver$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c892aa35710c myanjini/myweb:dockerfile "/bin/sh -c 'apachec…" 2 minutes ago Up 2 minutes 0.0.0.0:32770->80/tcp mywebrandport
d2acd021ff64 myanjini/myweb:dockerfile "/bin/sh -c 'apachec…" 4 minutes ago Up 4 minutes 0.0.0.0:9090->80/tcp mywebdockerfile
d8337bf2c819 ubuntu:14.04 "/bin/bash" 24 minutes ago Up 24 minutes 0.0.0.0:8080->80/tcp myweb
vagrant@xenial64:~/webserver$ curl http://localhost:32770/hello.html
hello docker
==> 이미지 생성 첫번째 방식은 비효율적. WHY? 어떤 동작을 했는지 다른사람이 알 수 없어
이미 만들어진 이미지를 이용해서 웹 서버 구축
#1 도커 허브에서 적당한 이미지를 검색
https://hub.docker.com/_/nginx
#2 nginx 이미지를 다운로드
vagrant@xenial64:~/webserver$ docker pull nginx
vagrant@xenial64:~/webserver$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
myanjini/myweb dockerfile 30e168051d24 35 minutes ago 221MB
myanjini/myweb latest 4d477e6f42d0 About an hour ago 221MB
example/echo latest 16a4c3b0f222 5 hours ago 750MB
example/echo old 16a4c3b0f222 5 hours ago 750MB
recommand latest 6a667c9fadb5 5 hours ago 73.9MB
falloc_100m latest cb419b52df77 5 hours ago 179MB
nginx latest 7e4d58f0e5f3 4 days ago 133MB
busybox latest 6858809bf669 6 days ago 1.23MB
ubuntu latest 4e2eef94cd6b 3 weeks ago 73.9MB
alpine latest a24bb4013296 3 months ago 5.57MB
ubuntu 14.04 6e4f1fe62ff1 9 months ago 197MB
jenkins latest cd14cecfdb3a 2 years ago 696MB
golang 1.9 ef89ef5c42a9 2 years ago 750MB
#3 nginx 서버를 구동 (컨테이너를 생성)
vagrant@xenial64:~/webserver$ docker container run --name webserver -d -p 80:80 nginx
519381987f6085cbe470abd6b89c617cd21f7f7a93760115de26d33d45540cd0
vagrant@xenial64:~/webserver$ curl http://localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
Commercial support is available at
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
wordpress와 mysql을 연동한 워드프로세스 기반 블로그 서비스
방법1.CentOS 가상 머신으로 실습하는 경우 아래 명령어를 먼저 실행 (DevOps 과정에서 사용했던 가상머신)
C:\HashiCorp\WorkDir> vagrant up
C:\HashiCorp\WorkDir> vagrant ssh
[vagrant@demo blog]$ sudo systemctl start docker.service
[vagrant@demo blog]$ sudo su
#1 작업 디렉터리 생성
vagrant@xenial64:~/webserver$ mkdir ~/blog && cd ~/blog
vagrant@xenial64:~/blog$
#2 mysql 서비스를 제공하는 컨테이너를 실행
도커 허브를 검색 ⇒ https://hub.docker.com/_/mysql
vagrant@xenial64:~/blog$ docker run -d --name wordpressdb -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress mysql:5.7
==> -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress : 컨테이너 내부의 환경 변수를 설정
#3 워드프레스 이미지를 이용한 웹 서버 컨테이너를 실행
워드프레스에서 필요로 하는 값 (설정) ⇒ https://hub.docker.com/_/wordpress
vagrant@xenial64:~/blog$ docker run -d -e WORDPRESS_DB_PASSWORD=password --name wordpress --link wordpressdb:mysql -p 80 wordpress
#4 컨테이너 실행 확인
vagrant@xenial64:~/blog$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7a26c2712474 wordpress "docker-entrypoint.s…" 19 seconds ago Up 18 seconds 0.0.0.0:32772->80/tcp wordpress
d562bcfaa1b0 mysql:5.7 "docker-entrypoint.s…" 7 minutes ago Up 7 minutes 3306/tcp, 33060/tcp wordpressdb
519381987f60 nginx "/docker-entrypoint.…" 17 minutes ago Up 17 minutes 0.0.0.0:80->80/tcp webserver
c892aa35710c myanjini/myweb:dockerfile "/bin/sh -c 'apachec…" About an hour ago Up About an hour 0.0.0.0:32770->80/tcp mywebrandport
d2acd021ff64 myanjini/myweb:dockerfile "/bin/sh -c 'apachec…" About an hour ago Up About an hour 0.0.0.0:9090->80/tcp mywebdockerfile
d8337bf2c819 ubuntu:14.04 "/bin/bash" About an hour ago Up About an hour 0.0.0.0:8080->80/tcp myweb
#5 워드프레스 컨테이너 내부에서 wordpressdb 이름의 컨테이너(mysql로 alias되어 있음)로 연결되는지 확인
vagrant@xenial64:~/blog$ docker container exec -it wordpress /bin/bash
root@7a26c2712474:/var/www/html# ping mysql
bash: ping: command not found
root@7a26c2712474:/var/www/html# apt-get update
:
root@7a26c2712474:/var/www/html# apt-get install -y iputils-ping
:
root@7a26c2712474:/var/www/html# ping mysql
PING mysql (172.17.0.6) 56(84) bytes of data.
64 bytes from mysql (172.17.0.6): icmp_seq=1 ttl=64 time=0.100 ms
64 bytes from mysql (172.17.0.6): icmp_seq=2 ttl=64 time=0.039 ms
64 bytes from mysql (172.17.0.6): icmp_seq=3 ttl=64 time=0.054 ms
64 bytes from mysql (172.17.0.6): icmp_seq=4 ttl=64 time=0.041 ms
^C
--- mysql ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 4ms
rtt min/avg/max/mdev = 0.039/0.058/0.100/0.025 ms
#6 내 PC에서 브라우저에서 wordpress 컨테이너로 접속
방법1. CentOS 가상 머신으로 실행한 경우 ⇒ http://192.168.33.10:32772 접속
방법2. Ubuntu 가상 머신으로 실행한 경우
→ VirtualBox 관리자에서 포트 포워딩 설정을 해야 함
호스트 포트, 게시트 포트 ⇒ docker container ls 했을 때 나오는 호스트의 포트를 명시
게스트 IP ⇒ Ubuntu 가상머신에서 ifconfig 했을 때 나오는 enp0s3 NIC의 IP
내 PC에 브라우저에서 http://localhost:32772 로 접속
컨테이너의 데이터를 영속적(persistent)인 데이터로 활용하는 방법
방법1. 호스트 볼륨 공유
-v 옵션을 이용해서 호스트 볼륨을 공유
⇒ 호스트의 디렉터리를 컨테이너의 디렉터리에 마운트
⇒ 이미지에 원재 존재하는 디렉터리에 호스트의 볼륨을 공유하면 컨테이너의 디렉터리 자체가 덮어쓰게 됨
#0 모든 컨테이너, 이미지, 볼륨을 삭제
vagrant@xenial64:~/blog$ docker container rm -f $(docker container ls -aq)
vagrant@xenial64:~/blog$ docker image rm -f $(docker image ls -aq)
vagrant@xenial64:~/blog$ docker volume rm -f $(docker volume ls -q)
#1 MySQL 이미지를 이용한 데이터베이스 컨테이너를 생성
vagrant@xenial64:~/blog$ docker run -d --name wordpressdb_hostvolume -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -v /home/vagrant/wordpress_db:/var/lib/mysql mysql:5.7
6b7848cca9068f3af8b1cf56acbb9d5fb0160f0500e35cd4498ba13fbe14757a
/home/wordpress_db ⇒ 도커가 자동으로 생성
/var/lib/mysql ⇒ mysql 데이터베이스의 데이터를 저장하는 기본 디렉터리
#2 워드프레스 이미지를 이용해 웹 서버 컨테이너를 생성
vagrant@xenial64:~/blog$ docker run -d -e WORDPRESS_DB_PASSWORD=password --name wordpress_hostvolume --link wordpressdb_hostvolume:mysql -p 80 wordpress
#3 호스트 볼륨 공유를 확인
vagrant@xenial64:~/blog$ ls /home/vagrant/wordpress_db
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 private_key.pem server-cert.pem
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 mysql public_key.pem server-key.pem
#4 컨테이너 내부의 디렉터리를 확인 ⇒ #3에서 확인한 것과 동일
vagrant@xenial64:/home$ docker container exec wordpressdb_hostvolume ls /var/lib/mysql
auto.cnf
ca-key.pem
ca.pem
client-cert.pem
client-key.pem
ib_buffer_pool
ib_logfile0
ib_logfile1
ibdata1
ibtmp1
mysql
performance_schema
private_key.pem
public_key.pem
server-cert.pem
server-key.pem
sys
wordpress
#5 wordpressdb_hostvolume 컨테이너를 삭제한 후 호스트 볼륨을 확인
vagrant@xenial64:/home$ docker container rm -f wordpressdb_hostvolume
wordpressdb_hostvolume
vagrant@xenial64:/home$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
54f4aa9f0aa1 wordpress "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:32777->80/tcp wordpress_hostvolume
vagrant@xenial64:/home$ ls /home/vagrant/wordpress_db/
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem wordpress
⇒ 컨테이너는 삭제되었지만 공유되고 있던 파일(디렉터리)은 그대로 남아 있음을 확인
⇒ 데이터의 영속성을 부여
#6 MySQL 이미지를 이용해서 컨테이너를 실행 (기존 호스트 볼륨을 맵핑)
vagrant@xenial64:/home$ docker run -d --name wordpressdb_hostvolume -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -v /home/vagrant/wordpress_db:/var/lib/mysql mysql:5.7
33c2c4524ad218d0f7c819cae286535aa35e34b0298fb051cc14b45020256509
vagrant@xenial64:/home$ docker container exec wordpressdb_hostvolume ls /var/lib/mysql
auto.cnf
ca-key.pem
ca.pem
client-cert.pem
client-key.pem
ib_buffer_pool
ib_logfile0
ib_logfile1
ibdata1
ibtmp1
mysql
performance_schema
private_key.pem
public_key.pem
server-cert.pem
server-key.pem
sys
wordpress
방법2. 볼륨 컨테이너
-v 옵션으로 볼륨을 사용하는 컨테이너를 다른 컨테이너와 공유하는 것
컨테이너를 생성할 때 --volumes-from 옵션을 설정하면 -v 또는 --volume 옵션을 적용한 컨테이너의 볼륨 디렉터리 공유가 가능
#1 -v 옵션으로 볼륨을 사용하는 컨테이너를 확인
vagrant@xenial64:~$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39786eb6abac wordpress "docker-entrypoint.s…" About an hour ago Up About an hour 0.0.0.0:32778->80/tcp wordpress_hostvolume
35eb27e65ad4 mysql:5.7 "docker-entrypoint.s…" About an hour ago Up About an hour 3306/tcp, 33060/tcp wordpressdb_hostvolume
wordpressdb_hostvolume 없는 경우 아래 명령어로 컨테이너 생성
vagrant@xenial64:~/blog$ docker run -d --name wordpressdb_hostvolume -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=wordpress -v /home/vagrant/wordpress_db:/var/lib/mysql mysql:5.7
#2 호스트 볼륨 공유를 확인
vagrant@xenial64:~$ ls ~/wordpress_db/ ⇐ 호스트의 디렉터리
auto.cnf client-cert.pem ibdata1 ibtmp1 private_key.pem server-key.pem
ca-key.pem client-key.pem ib_logfile0 mysql public_key.pem sys
ca.pem ib_buffer_pool ib_logfile1 performance_schema server-cert.pem wordpress
vagrant@xenial64:~$ docker container exec -it wordpressdb_hostvolume ls /var/lib/mysql ⇐ 컨테이너 내부
auto.cnf client-key.pem ibdata1 private_key.pem sys
ca-key.pem ib_buffer_pool ibtmp1 public_key.pem wordpress
ca.pem ib_logfile0 mysql server-cert.pem
client-cert.pem ib_logfile1 performance_schema server-key.pem
#3 호스트와 볼륨을 공유하는 컨테이너를 공유하는 컨테이너를 생성
vagrant@xenial64:~$ docker run -it --name volume_from_container --volumes-from wordpressdb_hostvolume ubuntu:14.04
root@d6ec70aa8c04:/# ls /var/lib/mysql
auto.cnf client-cert.pem ib_logfile0 ibtmp1 private_key.pem server-key.pem
ca-key.pem client-key.pem ib_logfile1 mysql public_key.pem sys
ca.pem ib_buffer_pool ibdata1 performance_schema server-cert.pem wordpress
방법3. 도커 볼륨
도커 자체가 제공하는 볼륨 기능을 활용
docker volume 명령어를 사용
#1 볼륨 생성
vagrant@xenial64:~$ docker volume create --name myvolume
myvolume
vagrant@xenial64:~$ docker volume ls
DRIVER VOLUME NAME
local 30cc85d3457a1dc55d7f6059f14c27d662d1848a2bd50d4eb95d362f9f43b3a5
local 375b5e63b4ee4dbf08ce79d11cbae985b58a0a2131acb0576aa19cb3bc94bf9e
local eec00043797c8d49331faab9e05f427b787fa8519f1113ac5cfe4ceeec1fac37
local myvolume
#2 생성한 볼륨을 이용해서 컨테이너를 생성
-v 볼륨이름:컨테이너내부디렉터리
vagrant@xenial64:~$ docker run -it --name myvolume1 -v myvolume:/root/ ubuntu:14.04
root@0bf6ac067a56:/# cd root
root@0bf6ac067a56:~# echo hello, volume >> /root/volume
root@0bf6ac067a56:~# exit
exit
#3 동일 볼륨을 사용하는 컨테이너를 생성해서 공유 여부를 확인
vagrant@xenial64:~$ docker run -it --name myvolume2 -v myvolume:/temp/ ubuntu:14.04
root@bc6bc0c35bfc:/# cat /temp/volume
hello, volume
root@bc6bc0c35bfc:/#
#4 docker inspect 명령으로 볼륨의 저장 위치를 확인
vagrant@xenial64:~$ docker inspect --type volume myvolume
[
{
"CreatedAt": "2020-09-16T00:42:36Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/myvolume/_data",
"Name": "myvolume",
"Options": {},
"Scope": "local"
}
]
'리눅스(Linux) > 컨테이너(Container)' 카테고리의 다른 글
리눅스의 컨테이너 격리 (cgroups, namespace) (1) | 2023.07.28 |
---|---|
[Docker] docker inspect (0) | 2021.09.11 |
[도커] 스웜모드 & 스웜모드의서비스 장애 복구 & 서비스 컨테이너에 설정 정보 전달(secret,config) & 도커 스웜 네트워크 & 서비스 디스커버리 (0) | 2020.09.19 |
[도커] 쉘스크립트에 도커 명령어 작성 & Docker compose & swarm (0) | 2020.09.16 |
[도커] 도커개념 & 도커 실행하기(이미지 생성 / 컨테이너 생성) (0) | 2020.09.14 |