๐ŸŒฑ Infra/Container_Docker

[Docker-Basic (9)] Docker File ์‚ฌ์šฉํ•˜๊ธฐ -์‹ค์Šต1 HTTP ์›น

mini_world 2020. 9. 29. 11:57
๋ชฉ์ฐจ ์ ‘๊ธฐ

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ค‘ ํ•˜๋‚˜์ธ DockerFile์— ๋Œ€ํ•ด์„œ ๋‹ค๋ฃจ์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!!!


 

์‹œ์ž‘ํ•˜๊ธฐ์ „์—, ๋„์ปคํŒŒ์ผ์ด๋ž€ ๋ญ˜๊นŒ์š”?  docs.docker.com/engine/reference/builder/

 

Dockerfile reference

Docker can build images automatically by reading the instructions from a Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command...

docs.docker.com

๊ฐ„๋‹จํ•˜๊ฒŒ "๋ ˆ์‹œํ”ผ"๋ผ๊ณ  ๋ณผ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์š”๋ฆฌ๋ฅผ ๋งŒ๋“ค๋–„ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์–ด์•ผ ๋ง›์žˆ๋Š”์ง€ ๋ ˆ์‹œํ”ผ๋ฅผ ์ •๋ฆฌํ•˜์—ฌ ์ •์˜๋œ ๋ฐฉ์‹๋Œ€๋กœ ์š”๋ฆฌ๋ฅผ ํ•˜๋“ฏ, 
์ปจํ…Œ์ด๋„ˆ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋„์ปคํŒŒ์ผ์„ ์ด์šฉํ•˜์—ฌ ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค๊ฒƒ์ธ์ง€์— ๋Œ€ํ•ด ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

๋„์ปคํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜๋ฉด ํŒจํ‚ค์ง€์„ค์น˜, ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋ณ€๊ฒฝ, ์„ค์ •ํŒŒ์ผ ๋ณ€๊ฒฝ๋“ฑ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ํ•˜๋‚˜ํ•˜๋‚˜ ์ปจํ…Œ์ด๋„ˆ ๋งŒ๋“ค๊ณ  ์„ค์ •์„ ์ ์šฉํ•˜๊ณ  ์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐ˜๋ณตํ•  ํ•„์š” ์—†์ด, Dockerfile์„ ์ด์šฉํ•˜์—ฌ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์ œ๋ถ€ํ„ฐ, ๋„์ปคํŒŒ์ผ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ™œ์šฉํ•˜์—ฌ ์‹ค์Šตํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

Docker File ์˜ˆ์ œ (apache Web)

 

1. Dockerfile ์ž‘์„ฑ

์ด๋ฒˆ์—๋„ ์ƒˆ๋กœ์šด ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ๊ฒฝ๋กœ์—์„œ ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

[root@docker-worker /]# mkdir /dockerLAB-web

[root@docker-worker /]# cd /dockerLAB-web

Dockerfile์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋Œ€์†Œ๋ฌธ์ž์— ์ฃผ์˜ํ•ด์ฃผ์„ธ์š”!

[root@docker-worker dockerLAB-web]# vim Dockerfile

FROM centos:7
MAINTAINER The CentOS Project <cloud-ops@centos.org>
LABEL Vendor="CentOS"\
      License=GPLv2\
      Version=2.4.6-40

RUN yum -y --setopt=tsflags=nodocs update && \
    yum -y --setopt=tsflags=nodocs install httpd && \
    yum clean all

EXPOSE 80

ADD run-httpd.sh /run-httpd.sh
RUN chmod -v +x /run-httpd.sh

CMD ["/run-httpd.sh"]

run-httpd.sh ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.  

[root@docker-worker dockerLAB-web]# vim run-httpd.sh

#!/bin/bash
rm -rf /run/httpd/* /tmp/httpd*
exec /usr/sbin/apachectl -DFOREGROUND

 

 

2.  ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ

๋„์ปค ํŒŒ์ผ์„ ์ด์šฉํ•ด์„œ ๋„์ปค ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
docker build ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค :)

docker build  <์˜ต์…˜> <๋„์ปคํŒŒ์ผ ๊ฒฝ๋กœ> 
-t(--tag) ์˜ต์…˜์€ ์ƒ์„ฑํ•  ์ด๋ฏธ์ง€ ์ด๋ฆ„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
[root@docker-worker dockerLAB-web]# docker build -t web-server .

Sending build context to Docker daemon  4.096kB
Step 1/8 : FROM centos:7
7: Pulling from library/centos
75f829a71a1c: Pull complete 
Digest: sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Status: Downloaded newer image for centos:7
 ---> 7e6257c9f8d8
Step 2/8 : MAINTAINER The CentOS Project <cloud-ops@centos.org>
 ---> Running in 4ef41baed202
Removing intermediate container 4ef41baed202
 ---> 5b08bd1fe165
Step 3/8 : LABEL Vendor="CentOS"      License=GPLv2      Version=2.4.6-40
 ---> Running in 6c7412c489b5
Removing intermediate container 6c7412c489b5
 ---> da321585858e
Step 4/8 : RUN yum -y --setopt=tsflags=nodocs update &&     yum -y --setopt=tsflags=nodocs install httpd &&     yum clean all
 ---> Running in 412dee75234c
 ---> 37c5cbc37671
Step 5/8 : EXPOSE 80
 ---> Running in 58be0106a293
Removing intermediate container 58be0106a293
 ---> 97576edd73ee
Step 6/8 : ADD run-httpd.sh /run-httpd.sh
 ---> 3e1b7bcfeb36
Step 7/8 : RUN chmod -v +x /run-httpd.sh
 ---> Running in b9cffeee4929
mode of '/run-httpd.sh' changed from 0644 (rw-r--r--) to 0755 (rwxr-xr-x)
Removing intermediate container b9cffeee4929
 ---> eb93504aaa05
Step 8/8 : CMD ["/run-httpd.sh"]
 ---> Running in d2f6f8d1ecd2
Removing intermediate container d2f6f8d1ecd2
 ---> d18677cad8de
Successfully built d18677cad8de
Successfully tagged web-server:latest

์ž˜ ์ƒ์„ฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.

[root@docker-worker dockerLAB-web]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
web-server          latest              d18677cad8de        36 seconds ago      260MB
centos              7                   7e6257c9f8d8        7 weeks ago         203MB

 

 

3. ๋„์ปค ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ ๋ฐ ํ…Œ์ŠคํŠธ

Dockerfile์„ ๊ฐ€์ง€๊ณ  ์ด๋ฏธ์ง€๋ฅผ ๋นŒ๋“œํ–ˆ์Šต๋‹ˆ๋‹ค.
์ด์ œ ์ด ์ด๋ฏธ์ง€๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

web-server ์ด๋ฏธ์ง€๋ฅผ ์ด์šฉํ•ด์„œ web_test๋ผ๋Š” ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

[root@docker-worker dockerLAB-web]# docker run --name web_test -d -p 80:80 web-server
83765a69d85a559c81c59ef22a13d04458fa95fbe16328d311e94e0df35d6c9a

์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ž˜ ์‹คํ–‰๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

[root@docker-worker dockerLAB-web]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
83765a69d85a        web-server          "/run-httpd.sh"     6 seconds ago       Up 5 seconds        0.0.0.0:80->80/tcp   web_test

์ปจํ…Œ์ด๋„ˆ์— ์ ‘์†ํ•ด์„œ index.html ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

[root@docker-worker dockerLAB-web]# docker exec -it web_test /bin/bash

[root@83765a69d85a /]# echo "Test WEB Server" > /var/www/html/index.html
[root@83765a69d85a /]# exit
exit

์ปจ๋ฐ์ด๋„ˆ๊ฐ€ ์ž˜ ๋™์ž‘ํ•˜๋Š”์ง€ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค :)
"Test WEB Server"๋ผ๊ณ  ์ž˜ ๋‚˜์˜ค๋„ค์š”!!

[root@docker-worker dockerLAB-web]# curl 127.0.0.1
Test WEB Server

 

์‹ค์Šต์ด ๋งˆ๋ฌด๋ฆฌ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค :)

๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค.

728x90