docker image 경량화

이상민·2023년 4월 18일
0

docker

목록 보기
9/19

docker image의 크기가 작을 수록 push, pull, container를 띄우는 속도 감소, docker image 양 증가.

run, cache

/fastcampus-devops/3-docker-kubernetes/3-dockerfile$ vi slacktee/Dockerfile

#
# slacktee
#
# build:
#   docker build --force-rm -t slacktee .
# run:
#   docker run --rm -it --name slacktee slacktee
#

FROM alpine:3.14
LABEL maintainer="FastCampus Park <fastcampus@fastcampus.com>"
LABEL description="Simple utility to send slack message easily."

# Install needed packages
RUN \
  apk add --no-cache bash curl git && \
  git clone https://github.com/course-hero/slacktee /slacktee && \
  apk del --no-cache git
RUN chmod 755 /slacktee/slacktee.sh

# Run
WORKDIR /slacktee
ENTRYPOINT ["/bin/bash", "-c", "./slacktee.sh"]

RUN 파트에서 && 문법을 사용하여 여러 명령어를 한번에 처리 -> 3가지 명령어를 하나의 layer로 합침. 원래대로라면 run을 3번 실행해야 함.
--no cache를 통해 필요없는 캐시가 쌓이지 않도록 만듦

경량 베이스 이미지 선택

slim

기존 이미지

$ docker images

REPOSITORY                                                   TAG       IMAGE ID       CREATED        SIZE
my-app                                                       v1        4a021aa4a38d   22 hours ago   408MB
my-ubuntu                                                    v1        7ebf1377db43   24 hours ago   72.8MB
nodejs-server                                                latest    1c6460fadeaa   25 hours ago   916MB
skybluelee/my-nginx                                          v1.0.0    1c6460fadeaa   25 hours ago   916MB
235050266187.dkr.ecr.ap-northeast-1.amazonaws.com/my-nginx   v1.0.0    6efc10a0510f   5 days ago     142MB
nginx                                                        latest    6efc10a0510f   5 days ago     142MB
ubuntu                                                       focal     1c5c8d0b973a   5 weeks ago    72.8MB
/fastcampus-devops/3-docker-kubernetes/3-dockerfile/nodejs-server$ cat Dockerfile.slim

#
# nodejs-server
#
# build:
#   docker build --force-rm -t nodejs-server .
# run:
#   docker run --rm -it --name nodejs-server nodejs-server
#

FROM node:16-slim
LABEL maintainer="FastCampus Park <fastcampus@fastcampus.com>"
LABEL description="Simple server with Node.js"

# Create app directory
WORKDIR /app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]

base 이미지만 기존 것과 다름
FROM node: 16 -> 16-slim

/fastcampus-devops/3-docker-kubernetes/3-dockerfile/nodejs-server$ docker build --force-rm -t nodejs-server:slim -f Dockerfile.slim .

$ docker images
REPOSITORY                                                   TAG       IMAGE ID       CREATED         SIZE
nodejs-server                                                slim      31b2055b3f8b   8 seconds ago   186MB
my-app                                                       v1        4a021aa4a38d   22 hours ago    408MB
my-ubuntu                                                    v1        7ebf1377db43   24 hours ago    72.8MB
nodejs-server                                                latest    1c6460fadeaa   25 hours ago    916MB
skybluelee/my-nginx                                          v1.0.0    1c6460fadeaa   25 hours ago    916MB
235050266187.dkr.ecr.ap-northeast-1.amazonaws.com/my-nginx   v1.0.0    6efc10a0510f   5 days ago      142MB
nginx                                                        latest    6efc10a0510f   5 days ago      142MB
ubuntu                                                       focal     1c5c8d0b973a   5 weeks ago     72.8MB

nodejs-server의 크기가 감소한 것을 확인할 수 있음

alpine

fastcampus-devops/3-docker-kubernetes/3-dockerfile/nodejs-server$ cat Dockerfile.alpine
#
# nodejs-server
#
# build:
#   docker build --force-rm -t nodejs-server .
# run:
#   docker run --rm -it --name nodejs-server nodejs-server
#

FROM node:16-alpine
LABEL maintainer="FastCampus Park <fastcampus@fastcampus.com>"
LABEL description="Simple server with Node.js"

# Create app directory
WORKDIR /app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]

$ docker build --force-rm -t nodejs-server:alpine -f Dockerfile.alpine .

$ docker images

REPOSITORY                                                   TAG       IMAGE ID       CREATED              SIZE
nodejs-server                                                alpine    e44b70158802   About a minute ago   124MB
nodejs-server                                                slim      31b2055b3f8b   5 minutes ago        186MB
my-app                                                       v1        4a021aa4a38d   22 hours ago         408MB
my-ubuntu                                                    v1        7ebf1377db43   24 hours ago         72.8MB
nodejs-server                                                latest    1c6460fadeaa   25 hours ago         916MB
skybluelee/my-nginx                                          v1.0.0    1c6460fadeaa   25 hours ago         916MB
nginx                                                        latest    6efc10a0510f   5 days ago           142MB
235050266187.dkr.ecr.ap-northeast-1.amazonaws.com/my-nginx   v1.0.0    6efc10a0510f   5 days ago           142MB
ubuntu                                                       focal     1c5c8d0b973a   5 weeks ago          72.8MB

alpine-multi

/fastcampus-devops/3-docker-kubernetes/3-dockerfile/nodejs-server$ cat Dockerfile.alpine-multi

#
# nodejs-server
#
# build:
#   docker build --force-rm -t nodejs-server .
# run:
#   docker run --rm -it --name nodejs-server nodejs-server
#

FROM node:16-alpine AS base
LABEL maintainer="FastCampus Park <fastcampus@fastcampus.com>"
LABEL description="Simple server with Node.js"

# Create app directory
WORKDIR /app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./


FROM base AS build
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production


FROM base AS release
COPY --from=build /app/node_modules ./node_modules
# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]

FROM AS 를 사용하여 여러개의 임시 블록을 생성 -> base, build, release 블록이 존재
multi-stage 전략 사용
build때 의존성이 많아질 수록 multi-stage의 효과가 상승함.

$ docker build --force-rm -t nodejs-server:alpine-multi -f Dockerfile.alpine-multi .

$ docker images

REPOSITORY                                                   TAG            IMAGE ID       CREATED          SIZE
nodejs-server                                                alpine-multi   5bea2a2f1015   5 seconds ago    119MB
nodejs-server                                                alpine         e44b70158802   7 minutes ago    124MB
nodejs-server                                                slim           31b2055b3f8b   10 minutes ago   186MB
my-app                                                       v1             4a021aa4a38d   22 hours ago     408MB
my-ubuntu                                                    v1             7ebf1377db43   24 hours ago     72.8MB
nodejs-server                                                latest         1c6460fadeaa   25 hours ago     916MB
skybluelee/my-nginx                                          v1.0.0         1c6460fadeaa   25 hours ago     916MB
nginx                                                        latest         6efc10a0510f   5 days ago       142MB
235050266187.dkr.ecr.ap-northeast-1.amazonaws.com/my-nginx   v1.0.0         6efc10a0510f   5 days ago       142MB
ubuntu                                                       focal          1c5c8d0b973a   5 weeks ago      72.8MB

0개의 댓글