๐Ÿ‘ฉโ€๐Ÿ’ป0712[Docker]

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

๐Ÿ“Œ Dockerfile ๋ช…๋ น์–ด ์—ฐ์Šต

๐Ÿ“™ ๋ช…๋ น์–ด

run ; yum, cpt ๋“ฑ ์‰˜๋ช…๋ น์–ด
cmd; systemlctl ์ด๋Ÿฐ๊ฑฐ
copy ; cp
ADD ; tarํŒŒ์ผ์„ ํ’€ ์ˆ˜ ์žˆ์Œ.

๐Ÿ“™ dockerfile ์ƒ์„ฑ, push

# vi Dockerfile
FROM ubuntu:18.04
MAINTAINER johnlee
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
##'/etc/apt/sources.list'์ด ํŒŒ์ผ์—์„œ archive.ubuntu.com์ด๊ฑธ ์ฐพ์•„์„œ ftp.daumkakao.com์ด๊ฑธ๋กœ ๋ฐ”๊ฟ”๋ผ.

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 # cd, exec -it
RUN echo ohayo >> ohayo.html
VOLUME /var/www/html # -v tmp:/var/www/html ; VOLUME์€ ํ˜ธ์ŠคํŠธ ๊ฒฝ๋กœ๋ฅผ ๋‚ด๊ฐ€ ์ง€์ • ํ•  ์ˆ˜ ์—†์Œ.
EXPOSE 80 ## ์ปจํ…Œ์ด๋„ˆ ํฌํŠธ. ์ด๋ ‡๊ฒŒ ํ•ด๋‘๊ณ  run ํ•  ๋•Œ -Pํ•˜๋ฉด ํ˜ธ์ŠคํŠธํฌํŠธ๋Š” ๋žœ๋ค์œผ๋กœ ํ• ๋‹น๋ฐ›์Œ.
ENTRYPOINT ["apachectl"] ##์‹คํ–‰๋ช…๋ น์–ด ["apachectl","-D", "FOREGORUND"]
CMD ["-D", "FOREGROUND"] ##์˜ต์…˜ ["apachectl","-D", "FOREGORUND"]

[root@localhost test]# mkdir nihao
[root@localhost test]# echo "NIHAO" > nihao/index.html
[root@localhost test]# echo "HELLO" > hello.html
[root@localhost test]# ls
Dockerfile  hello.html  nihao


# docker build -t mj030kk/hello:v1.0 . //์ด๋ฏธ์ง€ ๋งŒ๋“ค๊ธฐ dockerfile์ด ํ˜„์žฌ ๊ฒฝ๋กœ์— ์žˆ๋Š” ๊ฑฐ๋‹ˆ๊นŒ '.'
# docker push mj030kk/hello:v1.0 // ๋„์ปคํ—ˆ๋ธŒ์— ํ‘ธ์‹œ

๋„์ปคํŒŒ์ผ ; ์„œ๋ฒ„ ํ…œํ”Œ๋ฆฟ

์—…๋กœ๋“œ ํ™•์ธ!

์ปจํ…Œ์ด๋„ˆ๋Š” ์ด๋ฏธ์ง€๋ฅผ ์‹คํ–‰์‹œํ‚จ ๊ฒƒ. -> build push run(์ด๋ฏธ์ง€ - ์ปจํ…Œ์ด๋„ˆ)

๐Ÿ“™ run

[root@localhost test]# docker run -d -P --name hello mj030kk/hello:v1.0

[root@localhost test]# docker ps
CONTAINER ID   IMAGE                COMMAND                  CREATED          STATUS          PORTS                                     NAMES
a6c01b8a24f8   mj030kk/hello:v1.0   "apachectl -D FOREGRโ€ฆ"   14 seconds ago   Up 12 seconds   0.0.0.0:49154->80/tcp, :::49154->80/tcp   hello

โœ”๏ธ ๊ฒ€์ฆํ•˜๊ธฐ

  • ADD aws.tar /var/www/html

  • COPY nihao /var/www/html/nihao

  • COPY hello.html $path

  • WORKDIR /var/www/html
    RUN echo ohayo >> ohayo.html

[root@localhost homepage]# docker exec -it hello bash
root@a6c01b8a24f8:/var/www/html#
root@a6c01b8a24f8:/var/www/html#
root@a6c01b8a24f8:/var/www/html# ls
assets  css  hello.html  index.html  js  nihao  ohayo.html

  • VOLUME /var/www/html
[root@localhost ~]# find / -name ohayo.html // docker inspect hello ํ†ตํ•ด์„œ ์†Œ์Šค ํ™•์ธํ•˜๋Š”๊ฒŒ ๋” ํŽธํ•จ.
/var/lib/docker/overlay2/a1f81e1228da7fcc637d026a78f7a12d01b7362d3bf19c1573d044930df43013/merged/var/www/html/ohayo.html
/var/lib/docker/overlay2/6298e8fca931b175ee2d31d20326f89045675741adcc2c68b61a797a22d432e3/diff/var/www/html/ohayo.html
/var/lib/docker/volumes/8217233bc36638e9be313cdcafe8985c20f6b3f4d60a77acf267b637ecd120f9/_data/ohayo.html
/var/lib/docker/volumes/fa42d7c3300de4a4d452b4cc4cfd4464a63738914d8314f494dc341a71d1165a/_data/ohayo.html
[root@localhost ~]# cd /var/lib/docker/volumes/fa42d7c3300de4a4d452b4cc4cfd4464a63738914d8314f494dc341a71d1165a/_data/
[root@localhost _data]# ls
assets  css  hello.html  index.html  js  nihao  ohayo.html

[root@localhost _data]# vi hello.html
HELLO ALOHA

๐Ÿ“Œ Dockerfile ์ •์  ์›น์‚ฌ์ดํŠธ ์ด๋ฏธ์ง€ ๋ฐฐํฌ

โœ”๏ธ ๋„์ปคํŒŒ์ผ ๋งŒ๋“ค ํด๋” ๋งŒ๋“ค๊ณ  ํŒŒ์ผ ์ค€๋น„ํ•˜๊ธฐ two-rabbits, index.html

[root@localhost ~]# mkdir homepage
[root@localhost ~]# cd homepage
[root@localhost homepage]# mkdir images
[root@localhost homepage]# cd images/
[root@localhost images]# ls
[root@localhost images]# ls
two-rabbit.jpg
[root@localhost images]# cd ..
[root@localhost homepage]# ls
images  index.html

โœ”๏ธ tarํŒŒ์ผ๋กœ ๋งŒ๋“ค๊ธฐ, dockerfile ์ƒ์„ฑ

[root@localhost homepage]# tar cvf test.tar images index.html  // image์™€ index.html tarํŒŒ์ผ ๋งŒ๋“ค๊ธฐ
[root@localhost homepage]#vi Dockerfile
FROM nginx:latest //๋ฒ ์ด์Šค ์ด๋ฏธ์ง€
ADD aws.tar /usr/share/nginx/html
CMD ["nginx", "-g", "daemon off;"] //niginx ๊ตฌ๋™ ๋ช…๋ น์–ด

์œ„์— ๋‘ ์ค„(FROM,ADD)์€ ์ด๋ฏธ์ง€๋กœ ๋งŒ๋“ค๋•Œ ๊ธฐ๋™.
์ด๋ฏธ์ง€๊ฐ€ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋˜๋ฉด CMD, ENTRYPOINT ๊ธฐ๋™.

โœ”๏ธ build, push, run


[root@localhost homepage]# docker build -t mj030kk/homepage:v1.0 .
[root@localhost homepage]# docker push mj030kk/homepage:v1.0
[root@localhost homepage]# docker run -d -p 9000:80 --name home mj030kk/homepage:v1.0

๐Ÿ“ŒDockerfile-wordpress

โœ”๏ธ์›น์„œ๋ฒ„ dockerfile, build, push , run

[root@localhost ~]# mkdir wordpress && cd $_
[root@localhost wordpress]## vi Dockerfile
FROM centos:7
MAINTAINER mj@example.com
RUN yum install -y httpd php php-mysql php-gd php-mbstring wget unzip
RUN wget https://ko.wordpress.org/wordpress-4.8.2-ko_KR.zip
WORKDIR /var/www/html
RUN unzip /wordpress-4.8.2-ko_KR.zip
RUN mv wordpress/* .
RUN chown -R apache:apache /var/www/*
CMD httpd -DFOREGROUND //["httpd","-DFOREGROUND"] ์ด๋Ÿฐ๋ชจ์–‘์œผ๋กœ ์จ๋„ ๋จ. ์ด๊ฒŒ ๋” ์„ฑ๋Šฅ ์ข‹์Œ.
# docker build -t mj030kk/wordpress:v1.0 .
# docker push mj030kk/wordpress:v1.0 //ship (push, pull)
# docker run -d -p 88:80 --name wordpress --network test_bridge mj030kk/wordpress:v1.0 
# docker inspect wordpress

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

# 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

-> ์›น ๋ธŒ๋ผ์šฐ์ €๋กœ web์„œ๋ฒ„ ํฌํŠธ๋ฒˆํ˜ธ๋กœ ์ง„์ž…ํ•ด์„œ ์›Œ๋“œํ”„๋ ˆ์Šค ์„ค์น˜ ์ง„ํ–‰

๐Ÿ“Œ ๋„์ปค ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ

๐Ÿ“™ Bind Mount (ํด๋” ๊ณต์œ )


[root@localhost ~]# mkdir volume && cd $_
# mkdir bm01 ; touch bm01/test.txt
# docker run -itd --name bm-test -v /root/volume/bm01:/mnt centos:7
# docker exec bm-test ls /mnt

๐Ÿ“™ Volume (๋ณผ๋ฅจ ์ƒ์„ฑํ•ด์„œ ๋งˆ์šดํŠธ)

inspect, list ๋“ฑ์˜ ๋ช…๋ น์–ด ์‚ฌ์šฉํ•˜์—ฌ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ์Œ .


# docker volume create my-vol01
# docker volume list
# docker volume inspect my-vol01
"Mountpoint": "/var/lib/docker/volumes/my-vol01/_data"
# docker run -itd --name vol-test -v my-vol01:/mnt centos:7
# docker run -itd -p 801:80 --name vol-web -v my-vol01:/usr/local/apache2/htdocs:ro httpd:latest
# curl 192.168.1.91:801
<html><body><h1>It works!</h1></body></html>
# docker container exec vol-test sh -c "echo "Nihao" > /mnt/index.html"
# curl 192.168.1.91:801
Nihao

?? It works! ๊ฐ€ ์–ด๋–ป๊ฒŒ ๋‚˜์˜จ๊ฑด์ง€..?
/usr/local/apache2/htdocs:ro ์—ฌ๊ธฐ์— ์›๋ž˜ ์žˆ๋Š” ํŒŒ์ผ..?

-> ์ปจํ…Œ์ด๋„ˆํ™˜๊ฒฝ ์•„ํŒŒ์น˜์—์„œ์˜ ์›ฐ์ปดํŽ˜์ด์ง€!!

๐Ÿ“Œ ๋„์ปค ๋„คํŠธ์›Œํฌ ๊ด€๋ฆฌ

โœ”๏ธ ๊ธฐ๋ณธ ๋„คํŠธ์›Œํฌ ์ •๋ณด ํ™•์ธํ•ด๋ณด๊ธฐ

# docker network list
# docker network inspect bridge
"com.docker.network.bridge.name": "docker0",

โœ”๏ธ ์ƒˆ ๋„คํŠธ์›Œํฌ ์ƒ์„ฑ


# docker network create new-net --subnet 172.31.0.0/16 --ip-range 172.31.0.0/20 --gateway 172.31.0.1
# docker network list
NETWORK ID     NAME          DRIVER    SCOPE
3ce4ba4e8a2d   bridge        bridge    local
a8ae279c0395   host          host      local
3aceaee87e11   new-net       bridge    local
dd00f8d63e73   none          null      local
24e21c0c84ca   test_bridge   bridge    local

# docker inspect new-net
# docker run -d -P --network new-net nginx
# docker ps //์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„ ์•Œ์•„๋‚ด๊ธฐ
# docker inspect nice_turing //ip 172.31.0.2 ํ™•์ธ

๐Ÿ“Œ onbuild

์šด์˜์ž๋กœ์„œ ๊ฐœ๋ฐœ์ž์—๊ฒŒ ๋„์›€์ฃผ๊ธฐ

๐Ÿ“™ ์šด์˜์ž ์—ญํ• 

[root@localhost ~]# mkdir onbuild && cd $_
[root@localhost onbuild]# vi Dockerfile.base
FROM ubuntu:18.04
RUN sed -i 's/archive.ubuntu.com/ftp.daumkakao.com/g' /etc/apt/sources.list
RUN apt-get -y update
RUN apt-get -y install nginx
EXPOSE 80
ONBUILD ADD website*.tar /var/www/html/ ##๊ฐœ๋ฐœ์ž ์ชฝ์—์„œ ๋นŒ๋“œํ•  ๋•Œ ADD ์‹คํ–‰.
CMD ["nginx", "-g", "daemon off;"]

[root@localhost onbuild]# docker build -t mj030kk/web-base:v1.0 -f Dockerfile.base .
[root@localhost onbuild]# docker login
[root@localhost onbuild]# docker push mj030kk/web-base:v1.0
[root@localhost onbuild]# vi Dockerfile 
FROM mj030kk/web-base:v1.0

-> Dockerfile ํŒŒ์ผ์„ ๊ฐœ๋ฐœ์ž์—๊ฒŒ ์ „๋‹ฌ.

๐Ÿ“™ ๊ฐœ๋ฐœ์ž ์—ญํ• 

์›น์„œ๋ฒ„ ํƒ€๋ฅดํŒŒ์ผ , dockerfile ๋ฐ›์•„์˜จ ํŒŒ์ผ ์ค€๋น„
ansible ubuntu-node02์„œ๋ฒ„ ํ™œ์šฉ

root@ubuntu-node02:~# mkdir onbuild && cd $_
root@ubuntu-node02:~/onbuild# ls
website.tar
Dockerfile

root@ubuntu-node02:~/onbuild# docker build -t mj030kk/web-site:v1.0 .
root@ubuntu-node02:~/onbuild# docker run -d -p 80:80 --name=web-site mj030kk/web-site:v1.0

root@ubuntu-node02:~/onbuild# docker login
root@ubuntu-node02:~/onbuild# docker push mj030kk/web-site:v1.0

๐Ÿ“™ AWS-์šด์˜์ž ์—ญํ•  ( ์ตœ์ข… ์šด์˜ ํ™˜๊ฒฝ )

โœ”๏ธ 1. ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
์ด๋ฆ„ : docker
์ด๋ฏธ์ง€ : ami-0fd0765afb77bcca7 (์•„๋งˆ์กด linux 2)
์œ ํ˜• : t2.micro
ํ‚คํŽ˜์–ด : ์ƒˆ ํ‚ค ํŽ˜์–ด ; docker-key
vpc : MY-VPC
์„œ๋ธŒ๋„ท : MY-PUBLIC-SUBNET-2C
๋ณด์•ˆ๊ทธ๋ฃน : ๋ณด์•ˆ๊ทธ๋ฃน ์ƒ์„ฑ ; ์ด๋ฆ„ ; MY-SG-WEB / SSH, HTTP ์œ„์น˜๋ฌด๊ด€์œผ๋กœ ์ถ”๊ฐ€
๊ณ ๊ธ‰์„ธ๋ถ€์ •๋ณด - ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ :

#!/bin/bash
sudo amazon-linux-extras install docker -y
sudo systemctl start docker && systemctl enable docker
curl https://raw.githubusercontent.com/docker/docker-ce/master/components/cli/contrib/completion/bash/docker -o /etc/bash_completion.d/docker.sh
sudo usermod -a -G docker ec2-user

โœ”๏ธ 2. route53์— ๋ ˆ์ฝ”๋“œ ์ถ”๊ฐ€

โœ”๏ธ 3.mobaxterm์œผ๋กœ ์ ‘์†ํ•˜์—ฌ ์•„๋ž˜ ๋ช…๋ น์–ด ์ˆ˜ํ–‰

[ec2-user@ip-10-14-36-119 ~]$ docker --version
Docker version 20.10.13, build a224086
[ec2-user@ip-10-14-36-119 ~]$ docker run -d -p 80:80 --name webserver mj030kk/web-site:v1.0

โœ”๏ธ 4. ์ตœ์ข… ์šด์˜ ํ™˜๊ฒฝ ํ™•์ธ

๐Ÿ“Œ ๋„์ปค ์‚ฌ์„ค ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ

โœ”๏ธ 0. restart ๋ช…๋ น์–ด?

[root@localhost onbuild]# docker run -d -p 8888:80 --restart always --name test-restart mj030kk/web-site:v1.0
[root@localhost onbuild]# reboot

restart always - ์„œ๋ฒ„ ๊ป๋‹ค ์ผœ์ง„ ํ›„์— ์ž๋™์œผ๋กœ ์˜ฌ๋ผ์˜ด.

๐Ÿ“™ ์‚ฌ์„ค ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ ์ƒ์„ฑ

aws์„œ๋ฒ„์—์„œ ์ง„ํ–‰

[ec2-user@ip-10-14-36-119 ~]$  docker run -d -p 5000:5000 --restart=always --name private-docker-registry registry 

์•„๊นŒ ์ƒ์„ฑํ•œ MY-SG-WEB๋ณด์•ˆ๊ทธ๋ฃน ์ˆ˜์ •(5000๋ฒˆ ํฌํŠธ ์ถ”๊ฐ€)

๐Ÿ“™ ํด๋ผ์ด์–ธํŠธ1 (centos Docker) - ์‚ฌ์„ค ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ์— push

(์›๋ž˜ Dockerserver)

[root@localhost ~]# vi /etc/docker/daemon.json 
{ "insecure-registries":["docker.lovemj.shop:5000"] } //๋ณด์•ˆ ํ—ˆ์šฉ ์ž‘์—…
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker tag mj030kk/homepage:v1.0  docker.lovemj.shop:5000/homepage:v1.0
[root@localhost ~]# docker push docker.lovemj.shop:5000/homepage:v1.0

๐Ÿ“™ ํด๋ผ์ด์–ธํŠธ 2 (ubuntu-node02) - ์‚ฌ์„ค ๋ ˆ์ง€์ŠคํŠธ๋ ˆ์—์„œ ๊ฐ€์ ธ์™€์„œ run

root@ubuntu-node02:~/onbuild# vi /etc/docker/daemon.json
{ "insecure-registries":["docker.lovemj.shop:5000"] }
root@ubuntu-node02:~/onbuild# systemctl restart docker
root@ubuntu-node02:~/onbuild# docker run -d -p 8888:80 --name test docker.lovemj.shop:5000/homepage:v1.0

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

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

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