簡體   English   中英

如何使用 dockerfile 向我的 pod 添加 ping 命令?

[英]How to add ping command to my pod using dockerfile?

這是我的 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、主機名、ip,以便在我的 pod 中免費使用它? 任何幫助深表感謝。 提前致謝!

這基本上可以安裝您想要的任何 package,您可以簡單地在基礎最終階段添加 RUN 選項,對於基於 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的這篇文章,這些是安裝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?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM