반응형
빌더 패턴이란 복합 객체의 생성 과정과 표현 방법을 분리하여 동일한 생성 절차에서 서로 다른 표현 결과를 만들 수
있게 하는 패턴이다.
경량화에 사용 한다.
1. helloworld.go 파일을 빌드 후 실행하는 컨테이너 이미지 생성

[root@localhost test]# mkdir build-pattern
[root@localhost test]#
[root@localhost test]# cd build-pattern/
[root@localhost build-pattern]#
[root@localhost build-pattern]# vi helloworld.go

package main
import "fmt"
func main() {
fmt.Println("Hello World")
}

[root@localhost build-pattern]# vi Dockerfile

FROM golang:1.14
WORKDIR /myapp
COPY helloworld.go .
RUN go build -o helloworld .
ENTRYPOINT [ "./helloworld" ]
2. 이미지 생성 및 확인

[root@localhost build-pattern]# docker image build -t helloworld:v1 .

[root@localhost build-pattern]# docker image ls
==============> 이미지 용량 813MB
3. 빌드 패턴을 이용해 helloworld.go 파일을 빌드 후 실행하는 컨테이너 이미지 생성

[root@localhost build-pattern]# vi Dockerfile.builder

FROM golang:1.14
WORKDIR /myapp
COPY helloworld.go .
RUN go build -o helloworld .
ENTRYPOINT [ "./helloworld" ]

[root@localhost build-pattern]# vi Dockerfile.runtime

FROM alpine:latest
WORKDIR /myapp
COPY helloworld .
ENTRYPOINT [ "./helloworld" ]
4. 두 개의 도커 파일을 순차적으로 빌드하는 스크립트 파일 생성

[root@localhost build-pattern]# vi helloworld-builder.sh

docker image build -t helloworld-build -f Dockerfile.builder .
docker container create --name build-container helloworld-build
docker container cp build-container:/myapp/helloworld .
docker image build -t helloworld-runtime -f Dockerfile.runtime .
docker container rm -f build-container
rm -f helloworld

[root@localhost build-pattern]# chmod 777 helloworld-builder.sh
5. 이미지 생성 및 확인

[root@localhost build-pattern]# docker image build -t helloworld-build -f Dockerfile.builder .

[root@localhost build-pattern]# docker container create --name build-container helloworld-build

[root@localhost build-pattern]# docker container cp build-container:/myapp/helloworld .

[root@localhost build-pattern]# docker image build -t helloworld-runtime -f Dockerfile.runtime .

[root@localhost build-pattern]# docker container rm -f build-container

[root@localhost build-pattern]# rm -f helloworld

[root@localhost build-pattern]# docker image ls
==============> 이미지 용량 7.6MB
반응형
'CentOS > Docker' 카테고리의 다른 글
| [Docker][k8s]kubespray - kubernetes 설치 (0) | 2022.06.04 |
|---|---|
| [Docker][k8s]minikube - helloworld (0) | 2022.06.03 |
| [Docker]CMD vs ENTRYPOINT (0) | 2022.05.25 |
| [Docker]container 접속 (0) | 2022.05.25 |
| [Docker]docker container run (0) | 2022.05.25 |