๐Ÿ‘ฉโ€๐Ÿ’ป0711[docker]

๋ง์ง€ยท2022๋…„ 7์›” 11์ผ
0
post-custom-banner

๐Ÿ“Œdocker ์ด๋ก 


--interactive ; -i : ํ‘œ์ค€ ์ž…๋ ฅ์„ ์—ฐ๋‹ค
--tty ; -t : ํ„ฐ๋ฏธ๋„์„ ์‚ฌ์šฉํ•œ๋‹ค.
-> -it

๐Ÿ“Œdocker - ์‹ค์Šต

โœ”๏ธ ์ปจํ…Œ์ด๋„ˆ ํ•œ๋ฒˆ์— ์ง€์šฐ๊ธฐ

# docker ps -a // docker container ls -a ์ค„์ž„
# # docker rm -f $(docker ps -a -q)

โœ”๏ธ ์ด๋ฏธ์ง€ ์ง€์šฐ๊ธฐ

# docker image rm nginx //ID ๋„ฃ์–ด๋„ ๋˜๊ณ  ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ง€์›Œ์ ธ์•ผ ์ด๋ฏธ์ง€๊ฐ€ ์ง€์›Œ์ง.

# docker rmi centos:latest //image rm ์ค„์ž„ ; rmi

# docker rmi ID ID ID // ์ด๋ฏธ์ง€ ์—ฌ๋Ÿฌ๊ฐœ ์‚ญ์ œ

# docker images // docker image ls ์ค„์ž…

# docker rmi $(docker images -q) // ์ด๋ฏธ์ง€ ํ•œ๋ฒˆ์— ์ง€์šฐ๊ธฐ

docker images -q ; ์ด๋ฏธ์ง€๋“ค์˜ ์•„์ด๋”” ๋ชจ๋‘ ์ถœ๋ ฅ

โœ”๏ธ ๋ช…๋ น์–ด run --rm

# docker run --name test_cal centos:7 /bin/cal
# docker run --name test_cal1 --rm centos:7 /bin/cal //์‹คํ–‰ ํ›„ ๋ฐ”๋กœ ์‚ญ์ œํ•˜๋Š” ์˜ต์…˜ ์ถ”๊ฐ€ํ•ด์„œ ๋งŒ๋“ค์–ด๋ณด๊ธฐ (--rm)

โœ”๏ธ ๋ช…๋ น์–ด run -it

# docker run -t --name test_bash centos:7 /bin/bash // t๋งŒ ์ ์šฉ -> ๋ช…๋ น์–ด ์•ˆ๋จนํž˜. (์ƒํ˜ธ์ž‘์šฉ X) > i๋ฅผ ๋„ฃ์–ด์ค˜์•ผ ํ•จ.
# docker run -i --name test_bash1 centos:7 /bin/bash //๋“ค์–ด๊ฐ€์ง€๊ธด ํ•˜๋Š”๋ฐ ํ„ฐ๋ฏธ๋„์ด ์ œ๋Œ€๋กœ ์•ˆ๋‚˜์˜ด ์‹œ์›์ฐฎ์Œ.

# docker run -it --name test_bash2 centos:7 

โœ”๏ธ ๋ช…๋ น์–ด run -d, ์ž์›ํ• ๋‹น

[root@localhost ~]# docker run -d --name 
test_ping centos:7 /bin/ping localhost

b7714d2a46e2117539a8befb878c78699fc06197551195fe52b8dafecf2eef27

[root@localhost ~]# docker ps

CONTAINER ID   IMAGE      COMMAND                 CREATED          STATUS          PORTS     NAMES
0baf49feca26   centos:7   "/bin/ping localhost"   5 minutes ago    Up 5 minutes              test_ping
1942b02ee487   centos:7   "/bin/bash"             11 minutes ago   Up 11 minutes             test_bash1
41c5cfdd0b8a   centos:7   "/bin/bash"             14 minutes ago   Up 14 minutes             test_bash



# docker run -d -p 8080:80 --name test_port nginx

# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                   NAMES
1da2c26d3b56   nginx      "/docker-entrypoint.โ€ฆ"   56 seconds ago   Up 54 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   test_port
0baf49feca26   centos:7   "/bin/ping localhost"    6 minutes ago    Up 6 minutes                                            test_ping
1942b02ee487   centos:7   "/bin/bash"              12 minutes ago   Up 12 minutes                                           test_bash1
41c5cfdd0b8a   centos:7   "/bin/bash"              16 minutes ago   Up 16 minutes                                           test_bash

# docker stats test_port

-> 192.168.1.91:8080
(docker server IP):ํฌํŠธ๋ฒˆํ˜ธ๋กœ ๋“ค์–ด๊ฐ€์„œ ์ƒˆ๋กœ๊ณ ์นจ ํ†ตํ•ด์„œ CPU ์‚ฌ์šฉ๋Ÿ‰ ์˜ฌ๋ ค๋ณด๊ธฐ

โœ”๏ธ ๋ช…๋ น์–ด run - ์ž์›ํ• ๋‹น ๋ฐ ๋งˆ์šดํŠธ

# docker run -d -p 8181:80 --cpus 1 --memory 256m --name test_sesource nginx

# docker stats test_sesource //๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ํ™•์ธ
CONTAINER ID   NAME            CPU %     MEM USAGE / LIMIT   MEM %     NET I/O     BLOCK I/O     PIDS
5fff17ecd5d1   test_sesource   0.00%     1.973MiB / 256MiB   0.77%     656B / 0B   0B / 6.66kB   3

# docker run -d -p 8282:80 --cpus 1 --memory 512m -v /tmp:/usr/share/nginx/html --name volume-container nginx // ๋ณผ๋ฅจ์„ ๋„์ปค ์„œ๋ฒ„ tmpํด๋”์™€ ๋งˆ์šดํŠธํ•œ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ
[root@localhost tmp]# cat index.html
hello world
[root@localhost tmp]#

โœ”๏ธ ์ปจํ…Œ์ด๋„ˆ ๋ชฉ๋ก ํ…Œ์ด๋ธ”, ํ•„ํ„ฐ ์ด์šฉํ•ด์„œ ํ™•์ธํ•˜๊ธฐ

[root@localhost tmp]# docker ps -a --format "table {{.Command}}\t{{.Ports}}"

COMMAND                  PORTS
"/docker-entrypoint.โ€ฆ"   0.0.0.0:8282->80/tcp, :::8282->80/tcp
"/docker-entrypoint.โ€ฆ"   0.0.0.0:8181->80/tcp, :::8181->80/tcp
"/docker-entrypoint.โ€ฆ"   0.0.0.0:8080->80/tcp, :::8080->80/tcp
"/bin/ping localhost"
"/bin/bash"
"/bin/bash"
"/bin/cal"


[root@localhost tmp]# docker ps -a -f name=test_bash //ํ•„ํ„ฐ์‚ฌ์šฉ

CONTAINER ID   IMAGE      COMMAND       CREATED          STATUS          PORTS     NAMES
1942b02ee487   centos:7   "/bin/bash"   45 minutes ago   Up 45 minutes             test_bash1
41c5cfdd0b8a   centos:7   "/bin/bash"   48 minutes ago   Up 48 minutes             test_bash
[root@localhost tmp]#

โœ”๏ธ ๋ช…๋ น์–ด exec

[root@localhost tmp]# docker exec -it test_bash /bin/bash

[root@41c5cfdd0b8a /]#
[root@41c5cfdd0b8a /]#
[root@41c5cfdd0b8a /]#
[root@41c5cfdd0b8a /]#
[root@41c5cfdd0b8a /]#

โœ”๏ธ ๋ช…๋ น์–ด rename

[root@localhost tmp]# docker rename test_port webserver
[root@localhost tmp]# docker ps -a -f name=webserver
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
1da2c26d3b56   nginx     "/docker-entrypoint.โ€ฆ"   43 minutes ago   Up 43 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   webserver

โœ”๏ธ ๋ช…๋ น์–ด cp

[root@localhost tmp]# cd ~
[root@localhost ~]# ls
[root@localhost ~]# docker cp webserver:/usr/share/nginx/html/index.html /root/in  dex.html
[root@localhost ~]# vi index.html
nginx -> apache๋กœ ์ˆ˜์ •
[root@localhost ~]# docker cp ./index.html webserver:/usr/share/nginx/html/index.html


ํ™•์ธ

[root@localhost ~]# ls
food.tar  index.html

[root@localhost ~]# tar -xvf food.tar -C html
[root@localhost ~]# ls html/
assets  index.html  vendors

[root@localhost ~]# docker cp ./html webserver:/usr/share/nginx //ํด๋” ์ด๋™


ํ™•์ธ

โœ”๏ธ ๋ช…๋ น์–ด diff

[root@localhost ~]# docker diff webserver

C ; change
A ; add
D ; delete

โœ”๏ธ ๋ช…๋ น์–ด commit

[root@localhost ~]# docker commit -a "mj<test@example.com>" -m "FOOD" webserver test_commit:v1.0

sha256:f266d80886c08f6a95a40836d40dba7b82c728c25140dd08dbcd5b78065f6476

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED              SIZE
test_commit   v1.0      f266d80886c0   About a minute ago   170MB
nginx         latest    55f4b40fe486   2 weeks ago          142MB
centos        7         eeb6ee3f44bd   9 months ago         204MB

โœ”๏ธ ๋ช…๋ น์–ด save


[root@localhost ~]# docker save -o test_commit.tar test_commit:v1.0

[root@localhost ~]# ls
food.tar  html  index.html  test_commit.tar

๐Ÿ“Œ docker- network

bridge๋Š” ๊ณง docker 0

9: vethc3636e7@if8
veth14545c8@if10
veth0d6750f@if14
veth1e61a64@if16
veth23c2690@if20

# ip a

ํ–ˆ์„ ๋•Œ veth~๊ฐ€ ์ปจํ…Œ์ด๋„ˆ๋ž‘ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” ์ •๋ณด๋“ค. (up๋˜์–ด์žˆ๋Š” ์ปจํ…Œ์ด๋„ˆ๋งŒ ๋‚˜์˜ด)

[root@localhost ~]# docker exec -it test_bash /bin/bash
[root@41c5cfdd0b8a /]# yum install -y net-tools // ifconfig์„ค์น˜ . ํ•  ํ•„์š” ์—†์—ˆ์Œ..
[root@41c5cfdd0b8a /]# yum install -y iproute // ip a ์„ค์น˜

[root@41c5cfdd0b8a /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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
8: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

8; ๋ฐ”๊นฅ ๊ฐ€์ƒ ์Šค์œ„์น˜ ํฌํŠธ ๋ฒˆํ˜ธ / 9; ๋‚ด๋ถ€ ๋žœ์นด๋“œ

โœ”๏ธ ๋ช…๋ น์–ด network ; network๋งŒ๋“ค๊ธฐ

[root@localhost ~]# docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
29e67c759ba3   bridge    bridge    local //docker0
a8ae279c0395   host      host      local
dd00f8d63e73   none      null      local

[root@localhost ~]# docker inspect bridge //inspect; ์ž์„ธํ•˜๊ฒŒ ๋ณด๊ธฐ

[root@localhost ~]# docker network create -d bridge --subnet 192.168.123.3/24 --ip-range 192.168.123.128/25 test_bridge //-d; driver , test_bridge: ๋„คํŠธ์›Œํฌ ์ด๋ฆ„.

[root@localhost ~]# docker inspect bridge

[root@localhost ~]# docker run -d -p 8383:80 --name webserver1 --network test_bridge  nginx
[root@localhost ~]# docker network connect test_bridge test_bash
[root@localhost ~]# docker inspect test_bash
[root@localhost ~]# docker network disconnect test_bridge test_bash

[root@localhost ~]# docker network connect test_bridge test_bash
[root@localhost ~]# docker network connect test_bridge webserver
[root@localhost ~]# docker exec -it test_bash /bin/bash

[root@41c5cfdd0b8a /]# ping webserver //๊ฐ™์€ ๋„คํŠธ์›Œํฌ์—์„œ๋Š” ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„์ด ๋„๋ฉ”์ธ์ฒ˜๋Ÿผ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Œ.
PING webserver (172.17.0.5) 56(84) bytes of data.
64 bytes from webserver.bridge (172.17.0.5): icmp_seq=1 ttl=64 time=0.061 ms
64 bytes from webserver.bridge (172.17.0.5): icmp_seq=2 ttl=64 time=0.044 ms

-> ip๋ฐ”๋€Œ์–ด๋„ ๊ฐ™์€ ๋„คํŠธ์›Œํฌ ์•ˆ์—์„œ๋Š” ์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„์œผ๋กœ ํ†ต์‹  ๊ฐ€๋Šฅํ•˜๋‹ค. (๋„๋ฉ”์ธ์ฒ˜๋Ÿผ)

๐Ÿ“Œ docker - wordpress

https://hub.docker.com/_/mariadb Environment Variables์ฐธ๊ณ 

โœ”๏ธ db์„œ๋ฒ„(์ปจํ…Œ์ด๋„ˆ)์ƒ์„ฑ

[root@localhost ~]# docker run -d -p 3306:3306 --name dbserver \
-e MYSQL_DATABASE=wordpress \
-e MYSQL_USER=wpuser \
-e MYSQL_PASSWORD=wppass \
-e MYSQL_ROOT_PASSWORD=password --network test_bridge mariadb

โœ”๏ธ ์›น์„œ๋ฒ„(์ปจํ…Œ์ด๋„ˆ) ์ƒ์„ฑ

[root@localhost ~]# docker run -itd -p 8888:80 --name apache --network test_bridge centos:7
[root@localhost ~]# docker exec -it apache bash
[root@4858727d043d /]# yum install -y httpd php php-mysql php-gd php-mbstring wget unzip
[root@4858727d043d /]# wget https://ko.wordpress.org/wordpress-4.8.2-ko_KR.zip
[root@4858727d043d /]# cd /var/www/html
[root@4858727d043d html]# unzip /wordpress-4.8.2-ko_KR.zip
[root@4858727d043d html]# mv wordpress/* .
[root@4858727d043d html]# chown -R apache:apache /var/www/*
[root@4858727d043d html]# httpd &
->์—”ํ„ฐ

[root@4858727d043d html]# ping dbserver
PING dbserver (192.168.123.132) 56(84) bytes of data.
64 bytes from dbserver.test_bridge (192.168.123.132): icmp_seq=1 ttl=64 time=0.090 ms
64 bytes from dbserver.test_bridge (192.168.123.132): icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from dbserver.test_bridge (192.168.123.132): icmp_seq=3 ttl=64 time=0.044 ms

๐Ÿ“Œ dockerhub

์‹ค๋ฌดํŠนํ™” pdf 77๋ฒˆ์Šฌ๋ผ์ด๋“œ ์ฐธ๊ณ 

๐Ÿ“™ ๋งŒ๋“ค์–ด๋‘” ์ด๋ฏธ์ง€๋ฅผ docker hub์— push

[root@localhost ~]# docker tag test_commit:v1.0 mj030kk/test_commit:v1.0
//๋ณ„์นญ ๋‹ฌ๊ธฐ
//์ฒ˜์Œ commitํ• ๋•Œ๋ถ€ํ„ฐ ์ด๋ฏธ์ง€ ์ด๋ฆ„ ์•ž์— dockerhubID/ ํ•ด์ฃผ๋ฉด ํƒœ๊ทธ ๋”ฐ๋กœ ํ•  ํ•„์š” ์—†์Œ.

[root@localhost ~]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED        SIZE
test_commit           v1.0      f266d80886c0   4 hours ago    170MB
mj030kk/test_commit   v1.0      f266d80886c0   4 hours ago    170MB
nginx                 latest    55f4b40fe486   2 weeks ago    142MB
mariadb               latest    ea81af801379   4 weeks ago    383MB
centos                7         eeb6ee3f44bd   9 months ago   204MB

[root@localhost ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: mj030kk
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

[root@localhost ~]# docker push mj030kk/test_commit:v1.0

๐Ÿ“™ ์ด๋ฏธ์ง€๋ฅผ ๋ฐ›์„ ์šฐ๋ถ„ํˆฌ ์„œ๋ฒ„ ๋„์ปค ์„ค์น˜ ๋ฐ ์ด๋ฏธ์ง€ ๋‹ค์šด๋กœ๋“œ

root@ubuntu-node02:~# sudo apt update
root@ubuntu-node02:~# sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
root@ubuntu-node02:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
root@ubuntu-node02:~# sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
root@ubuntu-node02:~# sudo apt update
root@ubuntu-node02:~# sudo apt-cache policy docker-ce
root@ubuntu-node02:~# sudo apt install docker-ce -y

root@ubuntu-node02:~# docker run -d -p 80:80 --name webserver mj030kk/test_commit:v1.0

๐Ÿ“Œ Dockerfile

์‹ค๋ฌดํŠนํ™” pdf 74~

[root@localhost ~]# mkdir test
[root@localhost ~]# cd test
[root@localhost test]# vi Dockerfile
FROM ubuntu:18.04
MAINTAINER mj@example.com
LABEL "name"="webserver"
ENV aloha=date
ENV path=/var/www/html
RUN sed -i 's/archive.ubuntu.com/ftp.daumkakao.com/g' /etc/apt/sources.list
RUN apt-get update
RUN apt-get install apache2 -y
COPY nihao /var/www/html/nihao
COPY hello.html $path
ADD aws.tar /var/www/html
WORKDIR /var/www/html
RUN echo ohayo >> ohayo.html
VOLUME /var/www/html
EXPOSE 80
ENTRYPOINT ["apachectl"]
CMD ["-D", "FOREGROUND"]

๐Ÿ“™โœ”๏ธโœ๏ธ๐Ÿ“ขโญ๏ธ๐Ÿ“Œ

๐Ÿ“Œ ๊ธฐํƒ€

โญ๏ธ

profile
๊พธ์ค€ํžˆ, ์ฐจ๊ทผ์ฐจ๊ทผ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€