前言
當(dāng)我們需要使用某個(gè)容器的時(shí)候塘慕,通常先從docker hub或其它源上下載對(duì)應(yīng)的鏡像皂吮,然后通過一條命令就可以將其部署到服務(wù)器上戒傻,這使得我們能夠快速地部署單個(gè)鏡像。但是蜂筹,當(dāng)某一個(gè)鏡像需要部署成多個(gè)不同用途的容器時(shí)需纳,那么這些容器的管理就會(huì)變得非常麻煩。舉例說艺挪,假設(shè)我現(xiàn)在有一個(gè)ubuntu鏡像不翩,需要改造它然后安裝nginx用來做代理,需要安裝tomcat用來做應(yīng)用服務(wù)器等等,如果是有很多鏡像需要部署口蝠,那么手工修改這些鏡像就會(huì)變得非常棘手器钟。為此,我們可以在ubuntu鏡像的基礎(chǔ)上妙蔗,創(chuàng)建一個(gè)nginx鏡像傲霸,再創(chuàng)建一個(gè)tomcat鏡像。這樣眉反,鏡像的功能就單一化了昙啄,更容易被部署,能節(jié)省大量的時(shí)間和精力禁漓。
而dockerfile就是解決類似問題的良藥跟衅。
什么是dockerfile
dockerfile是一個(gè)文本文件,定義了構(gòu)建docker鏡像的過程播歼。我們可以使用dockerfile來自定義鏡像伶跷。
當(dāng)發(fā)現(xiàn)現(xiàn)有的鏡像無法滿足項(xiàng)目需求時(shí),就可能需要?jiǎng)?chuàng)建一個(gè)dockerfile來自定義一個(gè)鏡像了秘狞。創(chuàng)建自定義鏡像的流程大同小異:
- 創(chuàng)建一個(gè)dockerfile文件叭莫,在文件內(nèi)定義構(gòu)建過程的指令和配置。
- 通過docker build命令將dockerfile的內(nèi)容創(chuàng)建成鏡像烁试。
- 通過docker run命令使用鏡像雇初。
初識(shí)dockerfile
以官方的python鏡像為例,鏡像地址是https://hub.docker.com/_/python减响,也可以直接在github上查看靖诗,地址是https://github.com/docker-library/python。先貼上dockerfile一睹真容
#
# NOTE: THIS DOCKERFILE IS GENERATED VIA "update.sh"
#
# PLEASE DO NOT EDIT IT DIRECTLY.
#
FROM buildpack-deps:buster
# ensure local python is preferred over distribution python
ENV PATH /usr/local/bin:$PATH
# http://bugs.python.org/issue19846
# > At the moment, setting "LANG=C" on a Linux system *fundamentally breaks Python 3*, and that's not OK.
ENV LANG C.UTF-8
# extra dependencies (over what buildpack-deps already includes)
RUN apt-get update && apt-get install -y --no-install-recommends \
libbluetooth-dev \
tk-dev \
uuid-dev \
&& rm -rf /var/lib/apt/lists/*
ENV GPG_KEY E3FF2839C048B25C084DEBE9B26995E310250568
ENV PYTHON_VERSION 3.9.4
RUN set -ex \
\
&& wget -O python.tar.xz "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz" \
&& wget -O python.tar.xz.asc "https://www.python.org/ftp/python/${PYTHON_VERSION%%[a-z]*}/Python-$PYTHON_VERSION.tar.xz.asc" \
&& export GNUPGHOME="$(mktemp -d)" \
&& gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys "$GPG_KEY" \
&& gpg --batch --verify python.tar.xz.asc python.tar.xz \
&& { command -v gpgconf > /dev/null && gpgconf --kill all || :; } \
&& rm -rf "$GNUPGHOME" python.tar.xz.asc \
&& mkdir -p /usr/src/python \
&& tar -xJC /usr/src/python --strip-components=1 -f python.tar.xz \
&& rm python.tar.xz \
\
&& cd /usr/src/python \
&& gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)" \
&& ./configure \
--build="$gnuArch" \
--enable-loadable-sqlite-extensions \
--enable-optimizations \
--enable-option-checking=fatal \
--enable-shared \
--with-system-expat \
--with-system-ffi \
--without-ensurepip \
&& make -j "$(nproc)" \
&& make install \
&& rm -rf /usr/src/python \
\
&& find /usr/local -depth \
\( \
\( -type d -a \( -name test -o -name tests -o -name idle_test \) \) \
-o \( -type f -a \( -name '*.pyc' -o -name '*.pyo' -o -name '*.a' \) \) \
\) -exec rm -rf '{}' + \
\
&& ldconfig \
\
&& python3 --version
# make some useful symlinks that are expected to exist
RUN cd /usr/local/bin \
&& ln -s idle3 idle \
&& ln -s pydoc3 pydoc \
&& ln -s python3 python \
&& ln -s python3-config python-config
# if this is called "PIP_VERSION", pip explodes with "ValueError: invalid truth value '<VERSION>'"
ENV PYTHON_PIP_VERSION 21.0.1
# https://github.com/pypa/get-pip
ENV PYTHON_GET_PIP_URL https://github.com/pypa/get-pip/raw/29f37dbe6b3842ccd52d61816a3044173962ebeb/public/get-pip.py
ENV PYTHON_GET_PIP_SHA256 e03eb8a33d3b441ff484c56a436ff10680479d4bd14e59268e67977ed40904de
RUN set -ex; \
\
wget -O get-pip.py "$PYTHON_GET_PIP_URL"; \
echo "$PYTHON_GET_PIP_SHA256 *get-pip.py" | sha256sum --check --strict -; \
\
python get-pip.py \
--disable-pip-version-check \
--no-cache-dir \
"pip==$PYTHON_PIP_VERSION" \
; \
pip --version; \
\
find /usr/local -depth \
\( \
\( -type d -a \( -name test -o -name tests -o -name idle_test \) \) \
-o \
\( -type f -a \( -name '*.pyc' -o -name '*.pyo' \) \) \
\) -exec rm -rf '{}' +; \
rm -f get-pip.py
CMD ["python3"]
通過觀察這份dockerfile支示,我們印證了上一節(jié)所說的幾個(gè)內(nèi)容:
- dockerfile是一個(gè)文本文件刊橘,包含了創(chuàng)建一個(gè)鏡像所需的命令行代碼
- dockerfile的每一行都是一個(gè)命令集
- docker提供了一系列的標(biāo)準(zhǔn)命令,比如FROM/COPY/RUN/ENV/EXPOSE/CMD等
- docker將通過這些命令自動(dòng)創(chuàng)建鏡像
命令詳解
這部分的內(nèi)容在網(wǎng)上非常多颂鸿,建議跟隨官方文檔https://docs.docker.com/engine/reference/builder/
最佳實(shí)踐
官方有相關(guān)的指導(dǎo)文檔https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
有如下幾點(diǎn)說明:
Create ephemeral containers(構(gòu)建無狀態(tài)的容器)
Understand build context(理解上下文促绵,不引入多余文件)
Pipe Dockerfile through stdin(無需上下文的情況,通過stdin構(gòu)建)
Exclude with .dockerignore(排除context中文件嘴纺,參見 .dockerignore文件)
Use multi-stage builds(多階段構(gòu)建败晴,并合理利用緩存:排序原則->最基礎(chǔ)的RUN放在前面)
Don’t install unnecessary packages(不安裝不必要的包,中間過程文件栽渴,可以刪除和clean:rm -rf src/* && yum clean all)
Decouple applications(為了更好管理容器尖坤,不推薦在一個(gè)容器中部署多個(gè)進(jìn)程)
Minimize the number of layers(減少層數(shù),合并RUN闲擦、COPY糖驴、ADD僚祷、LABEL)
Sort multi-line arguments(為了直觀,RUN參數(shù)較多時(shí)贮缕,建議分成多行并排序)
Leverage build cache(同上,多階段構(gòu)建合理利用緩存)
Dockerfile instructions(Dockerfile指令優(yōu)化)