본문 바로가기
요즘공부

[도커&쿠버네티스] 교육 2일차_ Dockerfile 생성

by 게으른 피글렛 2025. 3. 9.
반응형

1. Dockerfile 생성

 

Dockerfile 생성 도커파일을 기준으로 도커이미지 생성 가능함

 

<docker파일생성>

#work 폴더 생성
vagrant@ubuntu2204:~$ mkdir work
vagrant@ubuntu2204:~$ ls
dbstorage  httpd.tar  mariadb.tar  work
vagrant@ubuntu2204:~$ cd work

#work폴더에 파일생성
#의미 : 이 Dockerfile은 Alpine 리눅스를 기반으로 Apache2 웹 서버를 설치하려고 하며, 
#서버가 실행되지 않는 대신 sleep 명령어로 컨테이너를 계속 실행 상태로 유지합니다.
vagrant@ubuntu2204:~/work$ vi myfile
vagrant@ubuntu2204:~/work$ cat myfile
FROM alpine
RUN apk update
RUN apk add apache2
#RUN apachectl -D FOREGROUND
RUN bin/sleep 600s

 

 

<docker 파일로 이미지파일 생성>

#도커 이미지 빌드
vagrant@ubuntu2204:~/work$ docker build --tag apache:alpine -f myfile /home/vagrant/work

 

vagrant@ubuntu2204:~/work$ docker build --tag apache:alpine -f myfile /home/vagrant/work
[+] Building 6.9s (8/8) FINISHED                                 docker:default
 => [internal] load build definition from myfile                           0.0s
 => => transferring dockerfile: 126B                                       0.0s
 => [internal] load metadata for docker.io/library/alpine:latest           0.0s
 => [internal] load .dockerignore                                          0.0s
 => => transferring context: 2B                                            0.0s
 => [1/4] FROM docker.io/library/alpine:latest                             0.0s
 => CACHED [2/4] RUN apk update                                            0.0s
 => CACHED [3/4] RUN apk add apache2                                       0.0s
 => [4/4] RUN bin/sleep 6s                                                 6.4s
 => exporting to image                                                     0.3s
 => => exporting layers                                                    0.3s
 => => writing image sha256:b7ba2fadef7fe41d4fb5b2f2aa09e6d2a17f577775e65  0.0s
 => => naming to docker.io/library/apache:alpine

 

 

이미지가 생성된 것 확인

 

 

<생성한 이미지로 컨테이너 생성>

vagrant@ubuntu2204:~/work$ docker run -d --name myalpine apache:alpine
f0c0ee618c72e59ba53c5a283aa1f23356cf4bbc83b0d80b82796edd4efb77ed

#도커파일에서 600초를 6초로 바꿨더니 뜨지 않음..
#600초일 경우 떠야 맞음
vagrant@ubuntu2204:~/work$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

#생성된 것 확인
vagrant@ubuntu2204:~/work$ docker ps -a
CONTAINER ID   IMAGE           COMMAND     CREATED          STATUS                      PORTS     NAMES
f0c0ee618c72   apache:alpine   "/bin/sh"   40 seconds ago   Exited (0) 29 seconds ago             myalpine

 

2. Dockerfile 생성 2

<기존에 만들어둔 Dockerfile 수정>

vagrant@ubuntu2204:~/work$ vi myfile
vagrant@ubuntu2204:~/work$ cat myfile
FROM alpine
RUN apk update
RUN apk add apache2
RUN touch /tmp/test.txt
RUN echo "hello container" > /tmp/test2.txt
#RUN apachectl -D FOREGROUND
CMD /bin/sleep 60s

 

<도커파일로 도커이미지 생성>

#도컾파일로 도커 이미지 생성
vagrant@ubuntu2204:~/work$ docker build --tag apache:alpine -f myfile /home/vagrant/work
 => => writing image sha256:7595f4f33370b3c9ed93cc2fba33222b5921e9067a93b
 
#도커 이미지 생성 확인
vagrant@ubuntu2204:~/work$ docker images
REPOSITORY              TAG       IMAGE ID       CREATED          SIZE
apache                  alpine    7595f4f33370   18 seconds ago   14.2MB
<none>                  <none>    b7ba2fadef7f   7 minutes ago    14.2MB
newhttpd                latest    e38abdc91cd3   21 hours ago     146MB
custom_httpd            1.0       b4f324c7ff81   21 hours ago     211MB
mariadb                 latest    a914eff5d2eb   3 weeks ago      336MB
alpine                  latest    aded1e1a5b37   3 weeks ago      7.83MB
quay.io/centos/centos   stream9   5ef0f9d95ba6   3 weeks ago      164MB
nginx                   latest    97662d24417b   4 weeks ago      192MB
httpd                   latest    4d98e80840bb   6 weeks ago      148MB
mysql                   latest    5568fddd4f66   6 weeks ago      797MB
hello-world             latest    74cc54e27dc4   6 weeks ago      10.1kB

 

기존에 만들어 두었던 도커 이미지는 none으로 변경됨

동일한이름/동일한태그로 인해 none이 변경됨

 

3. Dockerfile 생성 3

 

도커파일로 베너 출력하는 방법

베너출력메세지 입력 > 도커파일에 베너 출력되도록 작성> 도커파일빌드(이미지) > 실행 > Dockerfile 수정테스트

 

 

<src폴더(소스코드폴더) 에 베너출력메세지 입력>

#소스코드 폴더(src) 만들고 거기에 베너 출력 메세지 입력
vagrant@ubuntu2204:~/work$ mkdir src
vagrant@ubuntu2204:~/work$ echo "Hello Docker" > src/greet.txt
vagrant@ubuntu2204:~/work$ cat src/greet.txt
Hello Docker

 

 

<Dockerfile을 생성>

여기서 figlet은 글자를 베너(아트?)이렇게 만들어주는 명령어로 새로 생성할 alpine 서버에 설치가되어있지 않으므로

알파인설치 > 업데이트 > figlet 설치 > src/greet.txt 복사해서 tmp 저장 > 배너출력

vagrant@ubuntu2204:~/work$ vi Dockerfile
vagrant@ubuntu2204:~/work$ cat Dockerfile
FROM alpine
RUN apk update
RUN apk add figlet
COPY src/greet.txt /tmp
CMD cat /tmp/greet.txt /tmp | figlet

 

 

<도커파일 빌드>

# 현재위치에 도커빌드
# 기존에 -f myfile 이라고 명칭을 주어야만 실행이 가능했지만
# Dockerfile은 자동으로 인식됨
# . 현재폴더임
vagrant@ubuntu2204:~/work$ docker build -t banner .

#banner 이미지 생성 확인
vagrant@ubuntu2204:~/work$ docker images banner
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
banner       latest    c7c601911396   12 seconds ago   13.5MB

 

 

<docker run>

vagrant@ubuntu2204:~/work$ docker run banner
 _   _      _ _         ____             _
cat: read error: Is a directory
| | | | ___| | | ___   |  _ \  ___   ___| | _____ _ __
| |_| |/ _ \ | |/ _ \  | | | |/ _ \ / __| |/ / _ \ '__|
|  _  |  __/ | | (_) | | |_| | (_) | (__|   <  __/ |
|_| |_|\___|_|_|\___/  |____/ \___/ \___|_|\_\___|_|

생성완료

 

 

 

< Dockerfile 수정테스트>

1) 도커파일에 cmd를 여러개 넣을 경우 어떤 것이 인식되는지확인

 

밑에 2줄 추가

추가된 내용 : 샘플파일 생성, issue 출력

vagrant@ubuntu2204:~/work$ vi Dockerfile
vagrant@ubuntu2204:~/work$ cat Dockerfile
FROM alpine
RUN apk update
RUN apk add figlet
COPY src/greet.txt /tmp
CMD cat /tmp/greet.txt /tmp | figlet
CMD touch /tmp/sample.txt
CMD cat /etc/issue

 

가장 마지막 문구만 출력됨

cmd 명령어는 가장 마지막만 출력됨

#banner2 빌드
vagrant@ubuntu2204:~/work$ docker build -t banner2 .

# 실행
vagrant@ubuntu2204:~/work$ docker run banner2
Welcome to Alpine Linux 3.21
Kernel \r on an \m (\l)

 

 

 

2) entrypoint 명령어 추가 

ENTRYPOINT cat /tmp/greet.txt | figlet 명령어 추가함

CMD 일경우 출력되지 않음 ENTRYPOINT 일 경우 확인

vagrant@ubuntu2204:~/work$ vi Dockerfile
vagrant@ubuntu2204:~/work$ cat Dockerfile
FROM alpine
RUN apk update
RUN apk add figlet
COPY src/greet.txt /tmp
#CMD cat /tmp/greet.txt /tmp | figlet
ENTRYPOINT cat /tmp/greet.txt | figlet
CMD touch /tmp/sample.txt
CMD cat /etc/issue

 

이번에는 ENTRYPOINT 만 출력됨

vagrant@ubuntu2204:~/work$ docker build -t banner3 .
vagrant@ubuntu2204:~/work$ docker run banner3
 _   _      _ _         ____             _
| | | | ___| | | ___   |  _ \  ___   ___| | _____ _ __
| |_| |/ _ \ | |/ _ \  | | | |/ _ \ / __| |/ / _ \ '__|
|  _  |  __/ | | (_) | | |_| | (_) | (__|   <  __/ |
|_| |_|\___|_|_|\___/  |____/ \___/ \___|_|\_\___|_|

 

ENTRYPOINT 가 CMD 보다 상위 명령어 임을 확인

 

3) entrypoint 명령어, CMD 명령어 적절히 사용하는 방법

apine 서버 생성 > ping을 3번출력 > google.com 의 ping 으로~

vagrant@ubuntu2204:~/work$ vi Dockerfile
vagrant@ubuntu2204:~/work$ cat Dockerfile
FROM alpine
ENTRYPOINT ["ping","-c3"]
CMD ["google.com"]

 

vagrant@ubuntu2204:~/work$ docker build -t myping .
vagrant@ubuntu2204:~/work$ docker run myping
iPING google.com (142.250.196.110): 56 data bytes
64 bytes from 142.250.196.110: seq=0 ttl=254 time=53.483 ms
64 bytes from 142.250.196.110: seq=1 ttl=254 time=43.848 ms
64 bytes from 142.250.196.110: seq=2 ttl=254 time=50.206 ms

--- google.com ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 43.848/49.179/53.483 ms

 

실행할때 원하는 주소를 입력하면 변경가능

vagrant@ubuntu2204:~/work$ docker run myping apple.com

PING apple.com (17.253.144.10): 56 data bytes
64 bytes from 17.253.144.10: seq=0 ttl=254 time=8.870 ms
64 bytes from 17.253.144.10: seq=1 ttl=254 time=9.408 ms
64 bytes from 17.253.144.10: seq=2 ttl=254 time=11.121 ms

--- apple.com ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 8.870/9.799/11.121 ms

 

 

 

반응형