Home > Software engineering >  Docker multistage build "file not found"
Docker multistage build "file not found"

Time:01-04

I have this docker file:

FROM alpine/git AS git

RUN apk fix && apk --no-cache --update add  zip

VOLUME /root
WORKDIR /root

FROM minio/mc AS minio

COPY --from=git /usr/bin/git /usr/bin/git
COPY --from=git /usr/bin/zip /usr/bin/zip


ENTRYPOINT ["sh"]

Basically I need the following tools:

  • git
  • minio client
  • zip

Which, per the docs I read, the above should work - it builds without and error

But when I try to execute anything I get:

Sending build context to Docker daemon  8.192kB
Step 1/8 : FROM alpine/git AS git                         
 ---> 22d84a66cda4                                        
Step 2/8 : RUN apk fix && apk --no-cache --update add  zip
 ---> Using cache                
 ---> 5ce4d94085d9               
Step 3/8 : VOLUME /root          
 ---> Using cache                
 ---> 89329e40cbba               
Step 4/8 : WORKDIR /root         
 ---> Using cache                
 ---> 37f2c9216bb1               
Step 5/8 : FROM minio/mc AS minio
 ---> 396036e5ac42
Step 6/8 : COPY --from=git /usr/bin/git /usr/bin/git
 ---> 543676360b6d
Step 7/8 : COPY --from=git /usr/bin/zip /usr/bin/zip
 ---> 936165b36d2e
Step 8/8 : ENTRYPOINT ["sh"]
 ---> Running in 4ee0c4b491f9
Removing intermediate container 4ee0c4b491f9
 ---> 1279af2dd755
Successfully built 1279af2dd755
Successfully tagged fabric:latest
[centos@ip-10-6-5-12 ~]$ sudo docker run -it  fabric
sh-4.4# which zip
sh: which: command not found
sh-4.4# zip
sh: /usr/bin/zip: No such file or directory
sh-4.4# which git
sh: which: command not found
sh-4.4# git
sh: /usr/bin/git: No such file or directory
sh-4.4# git
sh: /usr/bin/git: No such file or directory
sh-4.4# apt
sh: apt: command not found
sh-4.4# ls -al /usr/bin/git 
-rwxr-xr-x. 1 root root 2911912 Oct 19 04:51 /usr/bin/git

So, the file is there. Root (me) owns it and yet it says No such file or directory ? I am perplexed

Anyone know what's up?

CodePudding user response:

Conclusion

Don't use copy to add git or zip, because they have dependencies, just copying git or zip is not enough.

Dockerfile

FROM minio/mc AS minio

RUN  \
     microdnf update --nodocs && \
     microdnf install git zip --nodocs && \
     microdnf clean all
     
ENTRYPOINT ["sh"]

Install Log

...
Installing: gzip;1.9-13.el8_5;x86_64;ubi-8-baseos-rpms
Installing: cracklib;2.9.6-15.el8;x86_64;ubi-8-baseos-rpms
Installing: cracklib-dicts;2.9.6-15.el8;x86_64;ubi-8-baseos-rpms
Installing: libpwquality;1.4.4-5.el8;x86_64;ubi-8-baseos-rpms
Installing: pam;1.3.1-22.el8;x86_64;ubi-8-baseos-rpms
Installing: util-linux;2.32.1-38.el8;x86_64;ubi-8-baseos-rpms
Installing: openssh;8.0p1-16.el8;x86_64;ubi-8-baseos-rpms
Installing: openssh-clients;8.0p1-16.el8;x86_64;ubi-8-baseos-rpms
Installing: less;530-1.el8;x86_64;ubi-8-baseos-rpms
Installing: git-core;2.31.1-2.el8;x86_64;ubi-8-appstream-rpms
Installing: git-core-doc;2.31.1-2.el8;noarch;ubi-8-appstream-rpms
Installing: perl-Git;2.31.1-2.el8;noarch;ubi-8-appstream-rpms
Installing: git;2.31.1-2.el8;x86_64;ubi-8-appstream-rpms
Installing: zip;3.0-23.el8;x86_64;ubi-8-baseos-rpms

From the installation log, you can see that git or zip has other dependent packages that need to be installed.

Run it

$ docker run -it  fabric
sh-4.4# git
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--super-prefix=<path>] [--config-env=<name>=<envvar>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone             Clone a repository into a new directory
   init              Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add               Add file contents to the index
   mv                Move or rename a file, a directory, or a symlink
   restore           Restore working tree files
   rm                Remove files from the working tree and from the index
   sparse-checkout   Initialize and modify the sparse-checkout

examine the history and state (see also: git help revisions)
   bisect            Use binary search to find the commit that introduced a bug
   diff              Show changes between commits, commit and working tree, etc
   grep              Print lines matching a pattern
   log               Show commit logs
   show              Show various types of objects
   status            Show the working tree status

grow, mark and tweak your common history
   branch            List, create, or delete branches
   commit            Record changes to the repository
   merge             Join two or more development histories together
   rebase            Reapply commits on top of another base tip
   reset             Reset current HEAD to the specified state
   switch            Switch branches
   tag               Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch             Download objects and refs from another repository
   pull              Fetch from and integrate with another repository or a local branch
   push              Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.
sh-4.4# zip
Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
  The default action is to add or replace zipfile entries from list, which
  can include the special name - to compress standard input.
  If zipfile and list are omitted, zip compresses stdin to stdout.
  -f   freshen: only changed files  -u   update: only changed or new files
  -d   delete entries in zipfile    -m   move into zipfile (delete OS files)
  -r   recurse into directories     -j   junk (don't record) directory names
  -0   store only                   -l   convert LF to CR LF (-ll CR LF to LF)
  -1   compress faster              -9   compress better
  -q   quiet operation              -v   verbose operation/print version info
  -c   add one-line comments        -z   add zipfile comment
  -@   read names from stdin        -o   make zipfile as old as latest entry
  -x   exclude the following names  -i   include only the following names
  -F   fix zipfile (-FF try harder) -D   do not add directory entries
  -A   adjust self-extracting exe   -J   junk zipfile prefix (unzipsfx)
  -T   test zipfile integrity       -X   eXclude eXtra file attributes
  -y   store symbolic links as the link instead of the referenced file
  -e   encrypt                      -n   don't compress these suffixes
  -h2  show more help
  
sh-4.4# exit

Dokcer image size

$ docker images
REPOSITORY                                                            TAG                               IMAGE ID       CREATED          SIZE
fabric                                                                1.0.0                             d2415a18eb37   8 minutes ago    264MB
fabric                                                                latest                            d2415a18eb37   8 minutes ago    264MB

CodePudding user response:

You've copied binaries that are linked against dynamic libraries that don't exist in your target image:

sh-4.4# type zip
zip is /usr/bin/zip
sh-4.4# ldd /usr/bin/zip
        linux-vdso.so.1 (0x00007ffed7553000)
        libc.musl-x86_64.so.1 => not found
sh-4.4# type git
git is /usr/bin/git
sh-4.4# ldd /usr/bin/git
        linux-vdso.so.1 (0x00007ffdd8da8000)
        libpcre2-8.so.0 => /lib64/libpcre2-8.so.0 (0x00007f56c752e000)
        libz.so.1 => /lib64/libz.so.1 (0x00007f56c7316000)
        libc.musl-x86_64.so.1 => not found
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f56c70f6000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f56c6d30000)
        /lib/ld-musl-x86_64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x00007f56c7aa6000)

When copying binaries into an image, they need to either be statically linked, or you need to ensure all of the libraries already exist in the target image. Linux package managers do this for you, which in the target image you've picked is microdnf:

microdnf install zip git
  • Related