dockerfile 작성 및 build 기초

2017. 9. 13. 14:04리눅스/우분투( Ubuntu)

 

Docker hub에 올라와 있는 이미지를 사용해도 상관없으나, 때때로 자신만의 환경이 구축된 이미지가 필요할 때가 있다.

그럴 때, 개인 docker image를 빌드하여 사용하면 된다. 또한 이를 공유하고 싶으면 docker hub에 올려 다른 사람들이 사용할 수 있게 할 수도 있다.

 

Dockerfile 작성하기

 - Dockerfile은 Docker 이미지 설정 파일입니다.

 - Dockerfile에 설정된 내용대로 이미지를 생성합니다.

 

# 개인 이미지 빌드 작업을 할 폴더를 생성 및 해당 폴더로 이동.

root@ubuntu02:~# mkdir build_test 
root@ubuntu02:~# cd build_test/

# 이미지 구성 정보가 들어가는 dockerfile을 생성하고 원하는 환경을 구축하기 위한 명령어를 넣는다.

# 테스트 에서는 ubuntu:latest 이미지를 기반으로 file1이라는 파일을 copy하고 이를 이미지로 빌드하도록 구성.

# FROM : 기본 이미지 (ex : ubuntu:latest , centos)

# MAINTAINER : 담당자 정보

# COPY [실제 호스트에 있는 파일 또는 폴더] [도커 이미지 내에 복사할 경로]             --> 절대경로 , 상대경로 모두 가능 (상대경로 사용시 조심하자.)

root@ubuntu02:~/build_test# cat dockerfile
FROM ubuntu:latest                                                                   
MAINTAINER Docker Beginner
COPY file1 /                                                                        --> ubuntu02의 file1을 생성할 docker image의 /에 복사해라.
root@ubuntu02:~/build_test#
root@ubuntu02:~/build_test# echo "This is 1st file." > file1
root@ubuntu02:~/build_test# cat file1
This is 1st file.
root@ubuntu02:~/build_test#
root@ubuntu02:~/build_test# docker build .                              --> 이미지 빌드 명령어
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM ubuntu:latest
 ---> ccc7a11d65b1
Step 2/3 : MAINTAINER Docker Beginner
 ---> Running in 009fe3f66081
 ---> ec43fcf04306
Removing intermediate container 009fe3f66081
Step 3/3 : COPY file1 /
 ---> d72a86053d45
Successfully built d72a86053d45
root@ubuntu02:~/build_test#
root@ubuntu02:~/build_test# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
<none>                  <none>              d72a86053d45        4 seconds ago       120MB                 

--> 이미지 빌드 시 이미지 이름을 지정하지 않았기 때문에 "<none>"로 생성이 되었다.

# docker build -t [생성할 이미지 이름:태그이름] .

root@ubuntu02:~/build_test# docker build -t beginner_image:v1 .    
Sending build context to Docker daemon  3.072kB
Step 1/3 : FROM ubuntu:latest
 ---> ccc7a11d65b1
Step 2/3 : MAINTAINER Docker Beginner
 ---> Using cache
 ---> ec43fcf04306
Step 3/3 : COPY file1 /
 ---> Using cache
 ---> d72a86053d45
Successfully built d72a86053d45
Successfully tagged beginner_image:v1

root@ubuntu02:~/build_test# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
beginner_image          v1                  d72a86053d45        14 minutes ago      120MB

* 참고 : 기존에 none 로 생성된 image는 오버라이트가 되었다.

# 실행 및 확인

root@ubuntu02:~/build_test# docker run --rm beginner_image:v1 cat /file1
This is 1st file.
root@ubuntu02:~/build_test#

 

 

.dockerignore 파일

 - Dockerfile과 같은 디렉터리에 들어있는 모든 파일을 컨텍스트(context)라고 합니다.

 - 특히 이미지를 생성할 때 컨텍스트를 모두 Docker 데몬에 전송하므로 필요 없는 파일이 포함되지 않도록 주의합니다.

 - 컨텍스트에서 파일이나 디렉터리를 제외하고 싶을 때는 .dockerignore 파일을 사용하면 됩니다.

 

#  개인 이미지 빌드 작업을 할 폴더에 1G짜리 파일을 생성.

root@ubuntu02:~/build_test# dd if=/dev/zero of=/root/build_test/bigfile count=1000 bs=1M
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 10.7935 s, 97.1 MB/s
root@ubuntu02:~/build_test#
root@ubuntu02:~/build_test# ls -rlt
total 1024012
-rw-r--r-- 1 root root         59  9월 13 13:43 dockerfile
-rw-r--r-- 1 root root         18  9월 13 13:43 file1
-rw-r--r-- 1 root root 1048576000  9월 13 14:06 bigfile
root@ubuntu02:~/build_test# du -sh bigfile
1001M   bigfile

# 이미지 빌드.
root@ubuntu02:~/build_test# docker build -t beginner_image:v2 .
Sending build context to Docker daemon  1.049GB                                              --> 용량이 1GB 가 넘는다. (bigfile을 포함하기 때문.)
Step 1/3 : FROM ubuntu:latest
 ---> ccc7a11d65b1
Step 2/3 : MAINTAINER Docker Beginner
 ---> Using cache
 ---> ec43fcf04306
Step 3/3 : COPY file1 /
 ---> Using cache
 ---> d72a86053d45
Successfully built d72a86053d45
Successfully tagged beginner_image:v2


root@ubuntu02:~/build_test# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
beginner_image          v1                  d72a86053d45        23 minutes ago      120MB
beginner_image          v2                  d72a86053d45        23 minutes ago      120MB

root@ubuntu02:~/build_test# docker run -ti --rm beginner_image:v2 /bin/bash
root@ac1e5a3ec42e:/# ls -rlt
total 68
drwxr-xr-x   8 root root 4096 Sep 13  2015 lib
drwxr-xr-x   2 root root 4096 Apr 12  2016 home
drwxr-xr-x   2 root root 4096 Apr 12  2016 boot
drwxr-xr-x   2 root root 4096 Aug  2 14:09 srv
drwxr-xr-x   2 root root 4096 Aug  2 14:09 opt
drwxr-xr-x   2 root root 4096 Aug  2 14:09 mnt
drwxr-xr-x   2 root root 4096 Aug  2 14:09 media
drwxr-xr-x   2 root root 4096 Aug  2 14:09 lib64
drwx------   2 root root 4096 Aug  2 14:09 root
drwxr-xr-x   2 root root 4096 Aug  2 14:10 bin
drwxrwxrwt   2 root root 4096 Aug  2 14:10 tmp
drwxr-xr-x  11 root root 4096 Aug 10 20:13 usr
drwxr-xr-x   2 root root 4096 Aug 10 20:13 sbin
drwxr-xr-x  13 root root 4096 Aug 10 20:13 var
drwxr-xr-x   6 root root 4096 Aug 10 20:13 run
dr-xr-xr-x  13 root root    0 Sep 11 07:23 sys
-rw-r--r--   1 root root   18 Sep 13 04:43 file1
drwxr-xr-x  45 root root 4096 Sep 13 05:08 etc
dr-xr-xr-x 152 root root    0 Sep 13 05:08 proc
drwxr-xr-x   5 root root  360 Sep 13 05:08 dev
root@ac1e5a3ec42e:/# cat file1
This is 1st file.
root@ac1e5a3ec42e:~# exit
exit

--> beginner_image:v2 로 실행시킨 container 어디에도 bigfile은 없다. (dockerfile에 해당 파일 관련하여 선언하지 않았기 때문.)

    따라서, 불필요한 파일은 이미지 빌드( docker build ) 할 때 포함하지 않도록 설정을 해줘야 한다.
root@ubuntu02:~/build_test# ls -lt
total 1024012
-rw-r--r-- 1 root root 1048576000  9월 13 14:06 bigfile
-rw-r--r-- 1 root root         18  9월 13 13:43 file1
-rw-r--r-- 1 root root         59  9월 13 13:43 dockerfile
root@ubuntu02:~/build_test#
root@ubuntu02:~/build_test# echo "bigfile" > .dockerignore                           --> 이미지 빌드 할 때, 해당 파일은 제외하고 빌드
root@ubuntu02:~/build_test# docker build -t beginner_image:v2 .
Sending build context to Docker daemon  4.096kB                                        --> 용량이 작아짐. (bigfile을 제외하기 때문)
Step 1/3 : FROM ubuntu:latest
 ---> ccc7a11d65b1
Step 2/3 : MAINTAINER Docker Beginner
 ---> Using cache
 ---> ec43fcf04306
Step 3/3 : COPY file1 /
 ---> Using cache
 ---> d72a86053d45
Successfully built d72a86053d45
Successfully tagged beginner_image:v2
root@ubuntu02:~/build_test#