为什么别人的docker镜像很小?为什么我们的docker镜像很大?因为我们的Dockerfile编写姿势不对。比如这样一个Dockerfile:
FROM centos:7
#init
WORKDIR /home
RUN mkdir ffmpeg_sources ffmpeg_build
RUN yum -y update && yum install -y \
autoconf \
automake \
bzip2 \
bzip2-devel \
cmake \
freetype-devel \
gcc \
gcc-c++ \
git \
libtool \
make \
pkgconfig \
zlib-devel \
openssl \
openssl-devel \
python3
#ffmpeg extensions
#NASM
WORKDIR /home/ffmpeg_sources
RUN curl -O -L https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.bz2 && tar xjvf nasm-2.15.05.tar.bz2
WORKDIR nasm-2.15.05
RUN ./autogen.sh && ./configure --prefix="/home/ffmpeg_build" --bindir="/usr/local/bin" && make && make install
#Yasm
WORKDIR /home/ffmpeg_sources
COPY ./lib/yasm-1.3.0.tar.gz .
RUN tar xzvf yasm-1.3.0.tar.gz
WORKDIR yasm-1.3.0
RUN ./configure --prefix="/home/ffmpeg_build" --bindir="/usr/local/bin"
RUN make && make install
#libx264
WORKDIR /home/ffmpeg_sources
COPY ./lib/x264 ./x264
WORKDIR x264
ENV PKG_CONFIG_PATH="/home/ffmpeg_build/lib/pkgconfig"
RUN ./configure --prefix="/home/ffmpeg_build" --bindir="/usr/local/bin" --enable-static
RUN make && make install
#libx265
WORKDIR /home/ffmpeg_sources
COPY lib/x265_git ./x265_git
RUN cd ./x265_git/build/linux && cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="/home/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source && make && make install
#libfdk_aac
WORKDIR /home/ffmpeg_sources
COPY lib/fdk-aac ./fdk-aac
WORKDIR fdk-aac
RUN autoreconf -fiv
RUN ./configure --prefix="/home/ffmpeg_build"
RUN make && make install
#libmp3lame
WORKDIR /home/ffmpeg_sources
COPY lib/lame-3.100.tar.gz ./
RUN tar xzvf lame-3.100.tar.gz
WORKDIR lame-3.100
RUN ./configure --prefix="/home/ffmpeg_build" --bindir="/usr/local/bin" --enable-nasm
RUN make && make install
#libopus
WORKDIR /home/ffmpeg_sources
COPY lib/opus-1.3.1.tar.gz ./opus-1.3.1.tar.gz
RUN tar xzvf opus-1.3.1.tar.gz
WORKDIR opus-1.3.1
RUN ./configure --prefix="/home/ffmpeg_build" --disable-shared
RUN make && make install
#libvpx
WORKDIR /home/ffmpeg_sources
COPY lib/libvpx ./libvpx
WORKDIR libvpx
RUN ./configure --prefix="/home/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm
RUN make && make install
#ffmpeg
WORKDIR /home/ffmpeg_sources
COPY lib/ffmpeg-snapshot.tar.bz2 .
RUN tar xjvf ffmpeg-snapshot.tar.bz2
WORKDIR ffmpeg
ENV PATH="/usr/local/bin:$PATH" PKG_CONFIG_PATH="/home/ffmpeg_build/lib/pkgconfig"
RUN ./configure \
--prefix="/home/ffmpeg_build" \
--pkg-config-flags="--static" \
--extra-cflags="-I/home/ffmpeg_build/include" \
--extra-ldflags="-L/home/ffmpeg_build/lib" \
--extra-libs=-lpthread \
--extra-libs=-lm \
--bindir="/usr/local/bin" \
--enable-gpl \
--enable-libfdk_aac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libopus \
--enable-libvpx \
--enable-libx264 \
--enable-libx265 \
--enable-nonfree \
--enable-openssl
RUN make && make install
#clean
WORKDIR /home
RUN rm -rf /home/*
RUN pip3 install supervisor
COPY supervisord.conf /etc/
WORKDIR /home
CMD ["/bin/bash"]
虽然,最后做了清理工作,删除了home目录里的所有不需要的内容,但是最后编译的镜像依然很大
但我们如果把它分开:
FROM centos:7
#init
WORKDIR /home
RUN mkdir ffmpeg_sources ffmpeg_build
RUN yum -y update && yum install -y \
autoconf \
automake \
bzip2 \
bzip2-devel \
cmake \
freetype-devel \
gcc \
gcc-c++ \
git \
libtool \
make \
pkgconfig \
zlib-devel \
openssl \
openssl-devel
#ffmpeg extensions
#NASM
WORKDIR /home/ffmpeg_sources
RUN curl -O -L https://www.nasm.us/pub/nasm/releasebuilds/2.15.05/nasm-2.15.05.tar.bz2 && tar xjvf nasm-2.15.05.tar.bz2
WORKDIR nasm-2.15.05
RUN ./autogen.sh && ./configure --prefix="/home/ffmpeg_build" --bindir="/usr/local/bin" && make && make install
#Yasm
WORKDIR /home/ffmpeg_sources
COPY ./lib/yasm-1.3.0.tar.gz .
RUN tar xzvf yasm-1.3.0.tar.gz
WORKDIR yasm-1.3.0
RUN ./configure --prefix="/home/ffmpeg_build" --bindir="/usr/local/bin"
RUN make && make install
#libx264
WORKDIR /home/ffmpeg_sources
COPY ./lib/x264 ./x264
WORKDIR x264
ENV PKG_CONFIG_PATH="/home/ffmpeg_build/lib/pkgconfig"
RUN ./configure --prefix="/home/ffmpeg_build" --bindir="/usr/local/bin" --enable-static
RUN make && make install
#libx265
WORKDIR /home/ffmpeg_sources
COPY lib/x265_git ./x265_git
RUN cd ./x265_git/build/linux && cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="/home/ffmpeg_build" -DENABLE_SHARED:bool=off ../../source && make && make install
#libfdk_aac
WORKDIR /home/ffmpeg_sources
COPY lib/fdk-aac ./fdk-aac
WORKDIR fdk-aac
RUN autoreconf -fiv
RUN ./configure --prefix="/home/ffmpeg_build"
RUN make && make install
#libmp3lame
WORKDIR /home/ffmpeg_sources
COPY lib/lame-3.100.tar.gz ./
RUN tar xzvf lame-3.100.tar.gz
WORKDIR lame-3.100
RUN ./configure --prefix="/home/ffmpeg_build" --bindir="/usr/local/bin" --enable-nasm
RUN make && make install
#libopus
WORKDIR /home/ffmpeg_sources
COPY lib/opus-1.3.1.tar.gz ./opus-1.3.1.tar.gz
RUN tar xzvf opus-1.3.1.tar.gz
WORKDIR opus-1.3.1
RUN ./configure --prefix="/home/ffmpeg_build" --disable-shared
RUN make && make install
#libvpx
WORKDIR /home/ffmpeg_sources
COPY lib/libvpx ./libvpx
WORKDIR libvpx
RUN ./configure --prefix="/home/ffmpeg_build" --disable-examples --disable-unit-tests --enable-vp9-highbitdepth --as=yasm
RUN make && make install
#ffmpeg
WORKDIR /home/ffmpeg_sources
COPY lib/ffmpeg-snapshot.tar.bz2 .
RUN tar xjvf ffmpeg-snapshot.tar.bz2
WORKDIR ffmpeg
ENV PATH="/usr/local/bin:$PATH" PKG_CONFIG_PATH="/home/ffmpeg_build/lib/pkgconfig"
RUN ./configure \
--prefix="/home/ffmpeg_build" \
--pkg-config-flags="--static" \
--extra-cflags="-I/home/ffmpeg_build/include" \
--extra-ldflags="-L/home/ffmpeg_build/lib" \
--extra-libs=-lpthread \
--extra-libs=-lm \
--bindir="/usr/local/bin" \
--enable-gpl \
--enable-libfdk_aac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libopus \
--enable-libvpx \
--enable-libx264 \
--enable-libx265 \
--enable-nonfree \
--enable-openssl
RUN make && make install
FROM centos:7
RUN yum update -y && yum install -y python3
RUN pip3 install supervisor
COPY supervisord.conf /etc/
COPY --from=0 /usr/local/bin/ffmpeg /usr/local/bin
CMD ["/bin/bash"]
这样得到的镜像大大缩小了: