[docker] 构建镜像


FROM scratch

COPY ./root /

ENV PATH="/bin:/sbin:/usr/bin:/usr/sbin:"
#!/bin/bash

if [ -z "$*" ]; then
        echo "need arguments"
        exit 0
fi

for binfile in "$@"; do
        currdir=$(cd $(dirname $0)/; pwd)
        dstdir=$currdir/root/

        if [ ! -f "$binfile" ]; then
                tmpfile=$(which $binfile 2>/dev/null)
                if [ -n "$tmpfile" ]; then binfile=$tmpfile; fi
        fi

        libs=$(ldd $binfile | grep '/lib' | sed -e "s,^\s*[^/]\+[^/]*[/],/,g" -e "s,(.*),,g")
        echo "$libs" | while read line; do
                echo "... $line"
                libdir=$(dirname $line)
                mkdir -vp $dstdir/$libdir
                cp -vf $line $dstdir/$libdir/.
        done

        tmpdir=$(dirname $binfile)
        mkdir -vp $dstdir/$tmpdir
        cp -vf $binfile $dstdir/$tmpdir/.
done

docker build -t myimage .

./copybin.sh ls bash ping route ip
docker run -it --net=none myimage bash

myimage/
├── copybin.sh
├── Dockerfile
└── root
    ├── bin
    │   ├── bash
    │   └── ls
    ├── lib64
    │   ├── ld-linux-x86-64.so.2
    │   ├── libacl.so.1
    │   ├── libattr.so.1
    │   ├── libcap.so.2
    │   ├── libcrypto.so.10
    │   ├── libc.so.6
    │   ├── libdl.so.2
    │   ├── libidn.so.11
    │   ├── libm.so.6
    │   ├── libpcre.so.1
    │   ├── libpthread.so.0
    │   ├── libresolv.so.2
    │   ├── libselinux.so.1
    │   ├── libtinfo.so.5
    │   └── libz.so.1
    ├── sbin
    │   └── ip
    └── usr
        ├── bin
        │   └── ping
        └── sbin
            ├── ip
            └── route

posted @ 2021-04-10 11:36  TimLinux  阅读(49)  评论(0编辑  收藏  举报