docker

20210812 (목) image build, Dockerfile

gusalstm 2021. 8. 12. 18:09
반응형

09:32 review

 Docker network

  - bridge, host, null, macvlan, overlay network

  - link : /etc/hosts에 hostname을 추가하여 hostname으로 통신이 가능. 아이피가 바뀌어도 통신가능

        → 컨테이너를 정지(stop)하고 ip를 다시 할당받아도 linked hostname의 ip를 추적하여 그대로 반영.

            단, 켜질때 linked container를 먼저 start해줘야하며, linked container는 link container를 자동으로 인식 못함

        → echo " ~~~ " >> /etc/hosts를 하게 되면 재부팅 후 삭제됨.

  - portforwarding : Docker host의 port에서 container의 port로 포워딩

        → 호스트의 방화벽이 꺼져있으면 포트포워딩 컨테이너가 생성되지 않음. 

  

Docker data storage

  - Bind Mount : Docker Host의 File System 내의 특정 디렉터리 공간을 컨테이너에게 제공하는 방식

        → 호스트의 마운트 포인트를 컨테이너 간 실시간 공유 가능, 

  - Volume : Docker가 관리하는 스토리지 공간으로 Docker 명령어에 의해 관리됨

        

 

##### volume 의 읽기전용 #####
컨테이너에서 읽기만 가능하도록 설정할 수 있다.

[user@docker-host0 storage]$ docker container run -it --volume app1:/home/app1:ro --name centos7-volume-ro centos:7
[root@26fae6d30c08 /]# cd /home/app1/
[root@26fae6d30c08 app1]# ls
vol-app1data1  vol-app1file1
[root@26fae6d30c08 app1]# echo "hello" > vol-app1file1
bash: vol-app1file1: Read-only file system
[root@26fae6d30c08 app1]# mount |grep app1
/dev/mapper/centos-root on /home/app1 type xfs (ro,relatime,seclabel,attr2,inode64,noquota)

 

 Creating docker image 

   1 도커 컨테이너로부터 도커 컨테이너 이미지 생성 

     ( $ docker container commit CONTAINER_NAME IMAGE_NAME )

    [user@docker-host0 ~]$ docker container commit centos7-makeimg centos:makeimg → 이미지생성
    [user@docker-host0 ~]$ docker container run -it --name centos7-newimg centos:makeimg → 이미지활용한 컨테이너 생성

   → commit 하여 img를 생성하고, 해당 img를 통해 새로운 컨테이너 생성 가능

   2 Dockerfile 작성 후 도커 이미지 빌드

    ( $ docker image build REPOSITORY:TAG PATH )

 

  Dockerfile에 작성하는 명령 

FROM from REPO:TAG 바탕이 되는 이미지 
LABEL 컨테이너 이미지에 대한 설명(레이블) 작성
RUN 컨테이너 이미지 빌드 시, 실행할 명령어
CMD 컨테이너 실행 시, 실행할 명령어 (시점 차이 : 빌드할때, 실행할때)
EXPOSE EXPOSE PORT 컨테이너에서 외부에 노출할 포트 지정
ENV ENV VARIABLE=VALUE 환경변수 지정
COPY COPY HOST_PATH CONTAINER_PATH 호스트의 파일을 컨테이너 이미지로 복사
ENTRYPOINT ENTRYPOINT COMMNAD 컨테이너 실행 시, 기본 동작할 명령어 지정

 

예제

##### nginx설치, index.html 파일 copy #####

[user@docker-host0 nginx]$ cat index.html
Hello nginx<br>
CentOS 7 <br>

[user@docker-host0 nginx]$ cat Dockerfile
# STEP 1 : BASE IMAGE
FROM centos:7

# STEP 2 : Install Package
RUN yum install -y epel-release
RUN yum install -y nginx

# STEP 3 : Make App Content
COPY index.html /usr/share/nginx/html/

# STEP 4 : Allow Service
EXPOSE 80

# STEP 5 : Start web service nginx
CMD nginx -g "daemon off;"

[user@docker-host0 nginx]$
[user@docker-host0 nginx]$ docker image build --tag nginx-server:v1 . → 현재경로의 Dockerfile을 읽도록 build실행
[user@docker-host0 nginx]$ curl http://172.17.0.2
Hello nginx<br>
CentOS 7 <br>


##### httpd 설치, portforwarding 테스트, echo명령을 통한 index.html 생성 #####

[user@docker-host0 first_image]$ cat Dockerfile
FROM centos:7

RUN yum install httpd net-tools vim -y
RUN echo test web > /var/www/html/index.html

EXPOSE 80

CMD /usr/sbin/httpd -DFOREGROUND

[user@docker-host0 first_image]$ docker image build -t centos:7-httpd .
[user@docker-host0 first_image]$ docker container run -d -p 8080:80 --name test5 centos:7-httpd
[user@docker-host0 first_image]$ curl http://172.17.0.3
test web
[user@docker-host0 first_image]$ curl http://192.168.56.100:8080
test web
728x90
##### entrypoint 예제 #####

[user@docker-host0 entry-point]$ cat Dockerfile
FROM ubuntu:latest

RUN mkdir /work

ADD test.sh /work/test.sh
RUN chmod a+x /work/test.sh

ENTRYPOINT /bin/bash /work/test.sh

[user@docker-host0 entry-point]$ cat test.sh
#!/bin/bash

echo "Today is `date '+%F %H:%M:%S'`"
echo "Ubuntu Docker Container : `hostname`"
echo $1 $2

[user@docker-host0 entry-point]$ docker container run --rm entry-point:latest
Today is 2021-08-12 07:37:12
Ubuntu Docker Container : f80bfa779fc6

[user@docker-host0 entry-point]$ docker container run -ti --rm --entrypoint "echo" entry-point:latest first second
first second

 

반응형

 

 도커 이미지는 UTC 기준시를 따르고 있음
 컨테이너를 생성할때 매번 이미지의 UTC를 KST로 변경해야함.

[user@docker-host0 tztest]$ docker container run -it alpine:latest
/ # date
Thu Aug 12 09:02:42 UTC 2021  →  일반적인 이미지로 확인해보면 KST가 아님.


##### TZ 변경 #####
[user@docker-host0 tztest]$ cp /usr/share/zoneinfo/Asia/Seoul .  → TZ파일을 현재 디렉터리로 복사
[user@docker-host0 tztest]$ cat Dockerfile → Dockerfile로 TZ파일을 /etc/localtime으로 복사
FROM alpine:latest

COPY ./Seoul /etc/localtime
[user@docker-host0 tztest]$ docker image build --tag test:1 .  → 빌드
Sending build context to Docker daemon  3.584kB
Step 1/2 : FROM alpine:latest
 ---> 021b3423115f
Step 2/2 : COPY ./Seoul /etc/localtime
 ---> 2b4fe149af1d
Successfully built 2b4fe149af1d
Successfully tagged test:1
[user@docker-host0 tztest]$ docker container run -it --rm test:1  → 컨테이너 생성 및 콘솔접속
/ # date
Thu Aug 12 18:02:05 KST 2021  → KST로 출력
/ # exit

 

※ 그외.. 볼륨 마운트를 통한 변경
$ sudo docker run --rm -d -v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone:ro alpine:latest
→ host의 locatltime을 컨테이너의 localtime에 마운트
→ host의 timezone을 컨테이너의 timezone에 마운트 하여 사용


[user@docker-host0 tztest]$ docker container run -ti -v /etc/localtime:/etc/localtime:ro -v /etc/timezone:/etc/timezone:ro al
pine:latest
/ # date
Thu Aug 12 18:15:04 KST 2021

 

 

##### docker container 일괄 삭제 #####
[user@docker-host0 ~]$ docker container rm -f `docker container ls -aq`
[user@docker-host0 ~]$ docker container ls -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

 

728x90