Các cách đóng gói images để optimize

 Cách 1: Cài đặt đầy đủ phần mềm, kể cả các trình biên dịch và thư viện để compile source.


Ưu điểm:

- Cài đặt đơn giản

- Build image nhanh vì sử dụng cơ chế cache layers.

Nhược điểm:

- Kích thước image lớn.

VD:
FROM ubuntu:16.04

#Install Nginx

RUN apt-get update \
    && apt-get install -y software-properties-common \
    && apt-add-repository -y ppa:nginx/stable \
    && apt-get update \
    && apt-get install -y nginx \
    && rm -rf /var/lib/apt/lists/*

ADD nginx/nginx.conf /etc/nginx/nginx.conf
ADD nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf

ADD data/www /data/www

RUN rm /etc/nginx/sites-enabled/default

RUN ln -sf /dev/stdout /var/log/nginx/access.log \
    && ln -sf /dev/stderr /var/log/nginx/error.log

Cách 2: Thực hiện cài đặt trình biên dịch và thư viện để compile mã nguồn. Sau đó remove trình biên dịch và thư viện.

Ưu điểm:

- Kích thước image nhỏ.

Nhược điểm:

- Không sử dụng được cơ chế cache layers nên thời gian build docker image lâu.

VD:
FROM ubuntu:16.04

RUN  bash /build-nginx.sh

# Make utf-8 enabled by default
ENV LANG en_US.utf8

COPY nginx-config /etc/nginx

# Define working directory
WORKDIR /etc/nginx

# Create mount point

# Expose port
EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]
----------------------------------------------------
--------------------- build-nginx.sh -------------


#!/bin/bash

apt-get update

# make utf-8 enabled by default
apt-get install -y locales \
&& localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8


DEBIAN_FRONTEND=noninteractive \
apt-get install -y wget \
    gcc g++ make \
    zlib1g zlib1g-dev \
    libpcre3 libpcre3-dev \
    libssl1.0.0 libssl-dev \
    libxslt1.1 libxslt1-dev \
    libxml2 libxml2-dev \
    libgd3 libgd-dev libgd2-xpm-dev \
    libgeoip1 libgeoip-dev
    #libgoogle-perftools-dev


mkdir -pv /tmp/build-nginx/
cd /tmp/build-nginx/


##### Download nginx stable version ######
# NGINX_STABLE_VERSION=1.7.7
NGINX_STABLE_VERSION=1.10.3

# wget http://172.17.0.1:8000/nginx/nginx-${NGINX_STABLE_VERSION}.tar.gz
wget http://nginx.org/download/nginx-${NGINX_STABLE_VERSION}.tar.gz \
    -O nginx-${NGINX_STABLE_VERSION}.tar.gz \
    && tar -xzvf nginx-${NGINX_STABLE_VERSION}.tar.gz \
    && rm nginx-${NGINX_STABLE_VERSION}.tar.gz

#dir nginx-1.7.7


##### Download module ngx_echo #####
ECHO_NGINX_MODULE_VERSION=v0.56

# wget http://172.17.0.1:8000/nginx/echo-nginx-module_${ECHO_NGINX_MODULE_VERSION}.tar.gz
wget https://github.com/openresty/echo-nginx-module/archive/${ECHO_NGINX_MODULE_VERSION}.tar.gz \
    -O echo-nginx-module_${ECHO_NGINX_MODULE_VERSION}.tar.gz \
    && tar -xzvf echo-nginx-module_${ECHO_NGINX_MODULE_VERSION}.tar.gz \
    && rm echo-nginx-module_${ECHO_NGINX_MODULE_VERSION}.tar.gz

#dir echo-nginx-module-0.56


##### Download module nginx-sticky #####
NGINX_STICKY_MODULE_NG_VERSION=1.2.5

# wget http://172.17.0.1:8000/nginx/ nginx-sticky-module-ng-${NGINX_STICKY_MODULE_NG_VERSION}.tar.gz
wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/${NGINX_STICKY_MODULE_NG_VERSION}.tar.gz \
    -O nginx-sticky-module-ng-${NGINX_STICKY_MODULE_NG_VERSION}.tar.gz \
    && tar -xzvf nginx-sticky-module-ng-${NGINX_STICKY_MODULE_NG_VERSION}.tar.gz \
    && rm nginx-sticky-module-ng-${NGINX_STICKY_MODULE_NG_VERSION}.tar.gz \
    && mv nginx-goodies-nginx-sticky-module-ng-* nginx-sticky-module-ng-${NGINX_STICKY_MODULE_NG_VERSION}

##### Download module pagespeed #####
#
#
NPS_VERSION=1.11.33.3

# wget http://172.17.0.1:8000/nginx/ngx_pagespeed_${NPS_VERSION}.tar.gz
    -O ngx_pagespeed_v${NPS_VERSION}-beta.tar.gz \
    && tar -xzvf ngx_pagespeed_v${NPS_VERSION}-beta.tar.gz \
    && rm ngx_pagespeed_v${NPS_VERSION}-beta.tar.gz \
    && mv ngx_pagespeed_* ngx_pagespeed-${NPS_VERSION}-beta/ \
    && cd ngx_pagespeed-${NPS_VERSION}-beta/ \
    && wget https://dl.google.com/dl/page-speed/psol/${PSOL_VERSION}.tar.gz \
        -O pagespeed-psol_${PSOL_VERSION}.gz \
    && tar -xzvf pagespeed-psol_${PSOL_VERSION}.tar.gz \
    && cd ..

# wget http://172.17.0.1:8000/nginx/pagespeed-psol_${PSOL_VERSION}.tar.gz
#dir ngx_pagespeed-1.9.32.2-beta

cd /tmp/build-nginx/nginx-${NGINX_STABLE_VERSION}/

./configure \
    --prefix=/usr/share/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/nginx.conf \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/access.log \
    --user=nginx \
    --group=nginx \
    --without-mail_pop3_module \
    --without-mail_imap_module \
    --without-mail_smtp_module \
    --without-http_fastcgi_module \
    --without-http_uwsgi_module \
    --without-http_scgi_module \
    --with-file-aio \
    --with-http_addition_module \
    --with-http_dav_module \
    --with-http_degradation_module \
    --with-http_geoip_module \
    --with-http_gzip_static_module \
    --with-http_image_filter_module \
    --with-http_realip_module \
    --with-http_secure_link_module \
    --with-http_spdy_module \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_sub_module \
    --with-http_xslt_module \
    --with-ipv6 \
    --with-pcre \
    --with-debug \
    --add-module=../echo-nginx-module-${ECHO_NGINX_MODULE_VERSION}/ \
    --add-module=../nginx-sticky-module-ng-${NGINX_STICKY_MODULE_NG_VERSION}/ \
    --add-module=../ngx_pagespeed-${NPS_VERSION}-beta/

make && make install

# configure nginx
useradd -r nginx

rm -rf /etc/nginx
tar xzf /tmp/build-nginx/nginx-config.tgz -C /etc/

# cleaning
DEBIAN_FRONTEND=noninteractive \
apt-get purge --auto-remove -y wget \
    gcc g++ make \
    zlib1g-dev  \
    libpcre3-dev \
    libssl-dev \
    libxslt1-dev \
    libxml2-dev \
    libgd-dev \
    libgd2-xpm-dev \
    libgeoip-dev

rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Cách 3: Biên dịch mã nguồn trước, đóng gói image sau. (Multi Stage Dockerfile )

Ưu điểm:

- Build image nhanh vì sử dụng cơ chế cache layers.

Chỉ đóng gói chương trình đã được biên dịch, không lấy các thư viện biên dịch -> Giảm kích thước image.

Nhược điểm:

- Cách build phức tạp, nhiều bước.

- Chỉ áp dụng được trong một số điều kiện nhất định. Ví dụ như program sau khi được compile có thể chạy độc lập, không cần thêm thư viện. Nếu không, khi build image, cần cài thêm các thư viện cho program. Trong nhiều trường hợp, việc này không đơn giản.

(java, php, golang ...)


VD:
# PHP Dependencies
#
FROM composer:1.7 as vendor
COPY database/ database/
COPY composer.json composer.json
COPY composer.lock composer.lock
RUN composer install \
    --ignore-platform-reqs \
    --no-interaction \
    --no-plugins \
    --no-scripts \
    --prefer-dist
#
# Frontend
#
FROM node:8.11 as frontend
RUN mkdir -p /app/public
COPY package.json webpack.mix.js yarn.lock /app/
COPY resources/assets/ /app/resources/assets/
WORKDIR /app
RUN yarn install && yarn production
#
# Application
#
FROM php:7.2-apache-stretch
COPY . /var/www/html
COPY --from=vendor /app/vendor/ /var/www/html/vendor/
COPY --from=frontend /app/public/js/ /var/www/html/public/js/
COPY --from=frontend /app/public/css/ /var/www/html/public/css/
COPY --from=frontend /app/mix-manifest.json /var/www/html/mix-manifest.json

Dockerfile Optimize

Reduce layers => tăng tốc độ và giảm kich thước image

     ENV name="kien" \
             domain="kienbv"


    ENV name="kien"
    ENV domain="kienbv"

Using Alpine => giảm kích thước image
   

Build Cache

Nếu có ý định đóng gói code vào docker, cần tìm cách để những thứ ít thay đổi, tĩnh, ví dụ như file thư viện thì chỉ cần COPY một layer đứng trước 
Những thứ thường xuyên thay đổi, như code nghiệp vụ thì cho vào layer khác.
=> khi đó, build docker image mới sẽ chỉ cập nhật những code mới, tiết kiệm được thời gian và không gian lưu trữ

cần chia code thành các khối riêng biệt

Nhận xét

Bài đăng phổ biến từ blog này

ActiveMQ 5.x

Redo and undo Log in MySQL transaction

[Kubernetes Series] - Bài 19 - Adding custom resource to Kubernetes