【问题标题】:How to add ping command to my pod using dockerfile?如何使用 dockerfile 向我的 pod 添加 ping 命令?
【发布时间】:2022-08-09 15:19:39
【问题描述】:

这是我的 Dockerfile

# Build the manager binary
FROM golang:1.17 as builder

WORKDIR /workspace

# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum

# cache deps before building and copying source so that we don\'t need to re-download as much
# and so that source changes don\'t invalidate our downloaded layer
RUN go mod download

# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY controllers/ controllers/
COPY opt/ opt/
RUN ls -altr /workspace
RUN chmod 775 /workspace/opt

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go

FROM alpine as alpine
RUN apk add --no-cache bash

FROM scratch

COPY --from=alpine /lib/ld-musl-x86_64.so.1 /lib/ld-musl-x86_64.so.1
COPY --from=alpine /bin/ping ./ping

FROM registry.access.redhat.com/ubi8/ubi-minimal:latest
WORKDIR /
COPY --from=builder /workspace/manager .
COPY --from=builder /workspace/opt ./opt

RUN chgrp 0 /manager \\
    && chmod g=u /manager

RUN chgrp 0 /opt \\
    && chmod g=u /opt

ENTRYPOINT [\"/manager\",\"./ping\"]

构建映像后,我使用

kubectl exec -it pod/controller-manager-deploy-5748468c5f-f4xrk -n cdg -- /bin/bash

登录 pod 后,如果我运行 ping 命令

[root@controller-manager-deploy-5748468c5f-f4xrk /]# ping
bash: ping: command not found

我收到此错误消息。

在这个 pod 的 /usr/bin 文件夹中,我只能看到这些包

[root@controller-manager-deploy-5748468c5f-f4xrk bin]# ls
\'[\'           comm            db_verify        gapplication           gpgv2       md5sum               pwd           shred      tsort
 alias        command         dd               gawk                   grep        microdnf             read          shuf       tty
 arch         coreutils       df               gdbus                  groups      mkdir                readlink      sleep      type
 awk          cp              dir              gencat                 gsettings   mkfifo               realpath      sort       tzselect
 b2sum        csplit          dircolors        getconf                hash        mknod                rm            sotruss    ulimit
 base32       curl            dirmngr          getent                 head        mktemp               rmdir         split      umask
 base64       cut             dirmngr-client   getopts                hostid      modulemd-validator   rpm           sprof      unalias
 basename     date            dirname          gio                    iconv       mv                   rpm2archive   stat       uname
 bash         db_archive      du               gio-querymodules-64    id          nice                 rpm2cpio      stdbuf     unexpand
 bashbug      db_checkpoint   echo             glib-compile-schemas   info        nl                   rpmdb         stty       uniq
 bashbug-64   db_deadlock     egrep            gpg                    install     nohup                rpmkeys       sum        unlink
 bg           db_dump         env              gpg-agent              jobs        nproc                rpmquery      sync       update-ca-trust
 brotli       db_dump185      expand           gpg-connect-agent      join        numfmt               rpmverify     tac        users
 ca-legacy    db_hotbackup    expr             gpg-error              ld.so       od                   runcon        tail       vdir
 cat          db_load         factor           gpg-wks-server         ldd         p11-kit              sed           tee        wait
 catchsegv    db_log_verify   false            gpg-zip                link        paste                seq           test       watchgnupg
 cd           db_printlog     fc               gpg2                   ln          pathchk              sh            timeout    wc
 chcon        db_recover      fg               gpgconf                locale      pldd                 sha1sum       touch      who
 chgrp        db_replicate    fgrep            gpgme-json             localedef   pr                   sha224sum     tr         whoami
 chmod        db_stat         fmt              gpgparsemail           logname     printenv             sha256sum     true       xmlcatalog
 chown        db_tuner        fold             gpgsplit               ls          printf               sha384sum     truncate   xmllint
 cksum        db_upgrade      g13              gpgv                   makedb      ptx                  sha512sum     trust      yes

在这里,找不到 ping 命令。 我必须在 dockerfile 中使用哪些命令将 ping、openssl、uuidgen、jq、hostname、ip、free 添加到我的 pod,以便我可以在 pod 内使用它?任何帮助深表感谢。提前致谢!

  • 为什么您的应用程序需要发送 ICMP ECHO 数据包;它可以使用本机代码而不是外部工具来完成吗?在最终图像中,文件/opt/ping 是什么(它是指向busybox 的损坏的符号链接)?您可能会发现在最终映像中使用像 yum 这样的 OS 包管理器来安装软件比尝试复制二进制文件及其在不同发行版映像之间的依赖关系更直接。
  • @DavidMaze 如何将 apk / yum 包管理器添加到最终图像中?请帮助我提供一些参考资料,因为我是 docker 新手。我这里真的不需要 ping 包。我希望将其他软件包安装在我的 pod 中,以便我可以使用它。

标签: linux docker dockerfile alpine operator-sdk


【解决方案1】:

这基本上可以安装您想要的任何软件包,您只需在根据或者最后阶段,对于基于 Debian/Ubuntu 的映像,它看起来像这样:

FROM base AS final
WORKDIR /
COPY --from=builder /workspace/manager .
COPY --from=builder /workspace/opt ./opt
######################
# Installing ping
RUN apt-get update
RUN apt-get install -y iputils-ping # <- change this according to your image
######################
RUN chgrp 0 /manager \    
    && chmod g=u /manager

RUN chgrp 0 /opt \
    && chmod g=u /opt

ENTRYPOINT ["/manager","./ping"]

根据来自 linuxshelltips 的this article,这些是安装 ping 的方法:

$ sudo apt install iputils-ping    [On Debian, Ubuntu and Mint]
$ sudo yum install iputils         [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux]
$ sudo emerge -a net-misc/iputils  [On Gentoo Linux]
$ sudo pacman -S iputils           [On Arch Linux]
$ sudo zypper install iputils      [On OpenSUSE]   

我无法快速确定 RedHat UBI 映像基于哪个发行版,但我猜应该是 RHEL?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多