run ; yum, cpt ๋ฑ ์๋ช
๋ น์ด
cmd; systemlctl ์ด๋ฐ๊ฑฐ
copy ; cp
ADD ; tarํ์ผ์ ํ ์ ์์.
# 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(์ด๋ฏธ์ง - ์ปจํ ์ด๋)
[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
[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
โ๏ธ ๋์ปคํ์ผ ๋ง๋ค ํด๋ ๋ง๋ค๊ณ ํ์ผ ์ค๋นํ๊ธฐ 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, 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์๋ฒ ํฌํธ๋ฒํธ๋ก ์ง์ ํด์ ์๋ํ๋ ์ค ์ค์น ์งํ
[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
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 ํ์ธ
์ด์์๋ก์ ๊ฐ๋ฐ์์๊ฒ ๋์์ฃผ๊ธฐ
[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
โ๏ธ 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๋ฒ ํฌํธ ์ถ๊ฐ)
(์๋ 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
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
๐โ๏ธโ๏ธ๐ขโญ๏ธ๐