Application Containerization
Mikucat
WHY?
CTFd (學術部出題)
Ops (網管部維運)
找雲端相關工作
Benefits
Consistent
Portability
Agility
Container?
Container
Containerfile vs Image vs Container
Containerfile
建置 image 的一種腳本
FROM
COPY
RUN
CMD
設定容器啟動時執行的指令/可執行檔
WORKDIR
設定建置時在 image 裡的當前目錄
建立映像檔
-t
參數為 image 加上標籤,方便辨識與使用
tag 格式 user/name:version
docker build -t joinclub:2021 .
運行
docker run --name joinclub \
-d -p 8000:8000 joinclub:2021
Best Practice
Merge operation modify same files
Stateless
Flexbility
Multi-Stage build
Remove redundant tools
Merge operation modify same files
Docker/OCI image 是由多層 檔案變動紀錄 所構成
Containerfile 每一行指令都會形成一層
Stateless
設定、狀態藉由環境變數傳入或掛載等方式來維持
Flexbility
image 保持彈性可以包容未來的變動
Example
Example 1
RUN curl -fsSLO https://example.com/linux.zip
RUN unzip linux.zip
RUN rm linux.zip
RUN curl -fsSLO https://example.com/linux.zip \
&& unzip linux.zip \
&& rm linux.zip
RUN curl -fsSLO https://example.com/linux.zip
RUN unzip linux.zip
RUN rm linux.zip
RUN curl -fsSLO https://example.com/linux.zip \
&& unzip linux.zip \
&& rm linux.zip
Example 2
FROM debian:bullseye
RUN apt update && apt install -y openssh-server
RUN echo 'root:my_password' | chpasswd
RUN systemctl enable ssh
RUN mkdir /var/run/sshd
RUN sed -i 's/#PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's|session\s*required\s*pam_loginuid.so|session optional pam_loginuid.so|g' /etc/pam.d/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd" , "-D" ]
FROM debian:bullseye
RUN apt update && apt install -y openssh-server
RUN echo 'root:my_password' | chpasswd
RUN systemctl enable ssh
RUN mkdir /var/run/sshd
RUN sed -i 's/#PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's|session\s*required\s*pam_loginuid.so|session optional pam_loginuid.so|g' /etc/pam.d/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd" , "-D" ]
Example 3
FROM debian:bullseye
COPY npc /usr/bin/npc
CMD ["/usr/bin/npc" , "-server=1.2.3.4:5678" , "-vkey=123456" ]
FROM debian:bullseye
COPY npc /usr/bin/npc
CMD ["/usr/bin/npc" , "-server=1.2.3.4:5678" , "-vkey=123456" ]
Example 4
FROM --platform=$BUILDPLATFORM alpine:3.15 AS builder
ARG VERSION
WORKDIR /
RUN set -ex \
&& wget -qO geosite.dat "https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat" \
&& wget -qO geoip.dat "https://github.com/v2fly/geoip/releases/latest/download/geoip.dat"
RUN set -ex \
&& wget -qO xray.zip "https://github.com/XTLS/Xray-core/releases/download/v${VERSION} /Xray-linux-64.zip" \
&& unzip xray.zip xray \
&& echo '{}' > config.json
FROM scratch
COPY --from=builder geosite.dat geoip.dat xray config.json /
EXPOSE 443
CMD [ "/xray" ,"-config" ,"/config.json" ]
FROM --platform=$BUILDPLATFORM alpine:3.15 AS builder
ARG VERSION
WORKDIR /
RUN set -ex \
&& wget -qO geosite.dat "https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat" \
&& wget -qO geoip.dat "https://github.com/v2fly/geoip/releases/latest/download/geoip.dat"
RUN set -ex \
&& wget -qO xray.zip "https://github.com/XTLS/Xray-core/releases/download/v${VERSION} /Xray-linux-64.zip" \
&& unzip xray.zip xray \
&& echo '{}' > config.json
FROM scratch
COPY --from=builder geosite.dat geoip.dat xray config.json /
EXPOSE 443
CMD [ "/xray" ,"-config" ,"/config.json" ]
FROM --platform=$BUILDPLATFORM alpine:3.15 AS builder
ARG VERSION
WORKDIR /
RUN set -ex \
&& wget -qO geosite.dat "https://github.com/v2fly/domain-list-community/releases/latest/download/dlc.dat" \
&& wget -qO geoip.dat "https://github.com/v2fly/geoip/releases/latest/download/geoip.dat"
RUN set -ex \
&& wget -qO xray.zip "https://github.com/XTLS/Xray-core/releases/download/v${VERSION} /Xray-linux-64.zip" \
&& unzip xray.zip xray \
&& echo '{}' > config.json
FROM scratch
COPY --from=builder geosite.dat geoip.dat xray config.json /
EXPOSE 443
CMD [ "/xray" ,"-config" ,"/config.json" ]
Other Build Tools
Docker buildkit
buildah
kaniko
Conclusion
Resume presentation
Application Containerization Mikucat
{"metaMigratedAt":"2023-06-16T21:46:31.171Z","metaMigratedFrom":"YAML","title":"Application Containerization","breaks":true,"description":"How to containerize applications","contributors":"[{\"id\":\"d5ff3cff-6013-437e-ac74-58dddd547e5a\",\"add\":9199,\"del\":4960}]"}