본문 바로가기
요즘공부

[도커&쿠버네티스] 교육 3일차_ Dockerfile 실습(저장용량)

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

1. Dockerfile 실습 테스트1

hello.c 파일 생성 > Dockerfile 생성 > 실행 > 용량 확인

 

<hello.c 파일 생성> 

도커 테스트에 앞서 hello.c 파일을 만들어줌

vagrant@ubuntu2204:~/work$ cd src
vagrant@ubuntu2204:~/work/src$ vi hello.c
vagrant@ubuntu2204:~/work/src$ cat hello.c
#include <stdio.h>
 int main()
 {

printf("Hello Docker Container \n");

 return 0;
 }

 

바이너리? 실행 hello

vagrant@ubuntu2204:~/work$ gcc -o hello hello.c

#생성이 안된다면 아래 명령어로 설치 후 실행
vagrant@ubuntu2204:~/work$ sudo apt update
vagrant@ubuntu2204:~/work$ sudo apt install gcc

 

이 화면이 뜨면 그냥 enter 

vagrant@ubuntu2204:~/work$ ldd src/hello
        linux-vdso.so.1 (0x00007ffd47de9000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f915b02a000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f915b260000)
        
#여기 폴더를 생성해줌 
vagrant@ubuntu2204:~/work$ mkdir -p lib/x86_64-linux-gnu
vagrant@ubuntu2204:~/work$ cp /lib/x86_64-linux-gnu/libc.so.6  lib/x86_64-linux-gnu
vagrant@ubuntu2204:~/work$ mkdir -p lib64
vagrant@ubuntu2204:~/work$  cp /lib64/ld-linux-x86-64.so.2 lib64/

 

vagrant@ubuntu2204:~/work$ cd src
vagrant@ubuntu2204:~/work/src$ gcc -static -o hello2 hello.c

 

 

 

<Dockerfile 생성>

vagrant@ubuntu2204:~/work$ vi Dockerfile
vagrant@ubuntu2204:~/work$ cat Dockerfile
FROM ubuntu:22.04
RUN apt-get update
RUN apt-get install -y gcc
COPY src/hello.c /tmp
WORKDIR /tmp
RUN gcc -o hello-world hello.c
CMD ["/tmp/hello-world"]

 

<실행>

hello.c 파일에 있던 내용이 출력됨

vagrant@ubuntu2204:~/work$ docker build -t myhello-world .
vagrant@ubuntu2204:~/work$ docker run myhello-world
Hello Docker Container

 

<용량확인>

vagrant@ubuntu2204:~/work$ docker images myhello-world
REPOSITORY      TAG       IMAGE ID       CREATED         SIZE
myhello-world   latest    1e26b322b284   2 minutes ago   320MB

 

 

2. Dockerfile 실습 테스트2

Dockerfile 생성 > 실행 > 용량 확인

 

<Dockerfile 생성>

vagrant@ubuntu2204:~/work$ cat Dockerfile
 FROM ubuntu:22.04 AS build-image
 RUN apt-get update
 RUN apt-get install -y gcc
 COPY src/hello.c /tmp
 WORKDIR /tmp
 RUN gcc -o hello-world hello.c
 #CMD ["/tmp/hello-world"]


 FROM ubuntu:22.04 AS runtime-image
 COPY --from=httpd /usr/local/apache2/htdocs/index.html /tmp
 COPY --from=build-image /tmp/hello-world .
 CMD ["./hello-world"]

 

<실행>

동일하게 실행됨

vagrant@ubuntu2204:~/work$ docker build -t myhello-world2 .
vagrant@ubuntu2204:~/work$ docker run myhello-world2
Hello Docker Container

 

<용량확인>

기존 도커 이미지보다 더 적은용량임을 확인 할 수 있음

vagrant@ubuntu2204:~/work$ docker images myhello-world2
REPOSITORY       TAG       IMAGE ID       CREATED              SIZE
myhello-world2   latest    92169a67b5ad   About a minute ago   77.9MB

 

 

 

반응형