安装到 alpine 容器的 bin 无法 运行,找不到 /gogs

bin mounted to alpine container fails to run with /gogs not found

我正在从源代码构建 gogs 并使用

COPY gogs /home/git/go/src/github.com/gogits/gogs

将包含构建的 gogs bin 及其文件的文件夹添加到我的容器中。

如果我 cd/home/git/go/src/github.com/gogits/gogs 我可以看到该文件,我可以 chmod +x 它, ls -l 它来查看权限和所有权等。但是我似乎看不到运行它。我尝试的每一种方式都得到:

/home/git/go/src/github.com/gogits/gogs # cat /var/log/gogs/stderr.log
/bin/ash: /home/git/go/src/github.com/gogits/gogs/gogs: not found
/bin/ash: /home/git/go/src/github.com/gogits/gogs/gogs: not found
/bin/ash: /home/git/go/src/github.com/gogits/gogs/gogs: not found

运行 ls -l . 在 gogs 目录中:

/home/git/go/src/github.com/gogits/gogs # ls -l .
total 33148
-rw-r--r--    1 git      nogroup        613 Aug 27 09:22 Dockerfile
-rw-r--r--    1 git      nogroup        913 Aug 27 09:22 Dockerfile.rpi
-rw-r--r--    1 git      nogroup       1054 Aug 27 09:22 LICENSE
-rw-r--r--    1 git      nogroup       1792 Aug 27 09:22 Makefile
-rw-r--r--    1 git      nogroup       7852 Aug 27 09:22 README.md
-rw-r--r--    1 git      nogroup       5331 Aug 27 09:22 README_ZH.md
drwxr-xr-x    2 git      nogroup       4096 Aug 28 08:33 cmd
drwxr-xr-x   10 git      nogroup       4096 Aug 28 08:33 conf
drwxr-xr-x    5 git      nogroup       4096 Aug 28 08:35 custom
drwxr-xr-x    2 git      nogroup       4096 Aug 28 08:33 data
drwxr-xr-x    4 git      nogroup       4096 Aug 28 08:33 docker
-rw-r--r--    1 git      nogroup       5359 Aug 27 09:22 glide.lock
-rw-r--r--    1 git      nogroup       1756 Aug 27 09:22 glide.yaml
-rwxr-xr-x    1 git      nogroup   33831440 Aug 27 10:12 gogs
-rw-r--r--    1 git      nogroup        839 Aug 27 09:22 gogs.go
drwxr-xr-x    2 git      nogroup       4096 Aug 28 08:33 log
drwxr-xr-x    4 git      nogroup       4096 Aug 28 08:33 models
drwxr-xr-x   34 git      nogroup       4096 Aug 28 08:33 modules
drwxr-xr-x    4 git      nogroup       4096 Aug 28 08:33 packager
drwxr-xr-x   14 git      nogroup       4096 Aug 28 08:33 public
drwxr-xr-x   14 git      nogroup       4096 Aug 28 08:33 routers
drwxr-xr-x   12 git      nogroup       4096 Aug 28 08:33 scripts
-rw-r--r--    1 root     root          2750 Aug 28 08:43 supervisord.log
-rw-r--r--    1 root     root             2 Aug 28 08:43 supervisord.pid
drwxr-xr-x   18 git      nogroup       4096 Aug 28 08:33 templates

我尝试过的其他东西:

/home/git/go/src/github.com/gogits/gogs # cp gogs /usr/bin
/home/git/go/src/github.com/gogits/gogs # /usr/bin/gogs
ash: /usr/bin/gogs: not found
/home/git/go/src/github.com/gogits/gogs # ls /usr/bin | grep gogs
gogs
/home/git/go/src/github.com/gogits/gogs # /usr/bin/gogs
ash: /usr/bin/gogs: not found
/home/git/go/src/github.com/gogits/gogs # ./usr/bin/gogs
ash: ./usr/bin/gogs: not found
/home/git/go/src/github.com/gogits/gogs # ls /usr/bin/gogs
/usr/bin/gogs

使用命令 sh 执行到 docker 容器中 一段时间后我开始出现奇怪的错误,这可能是在我将 gogs bin 添加到 PATH 之后发生的,记不太清了,不过以前有过

/home/git/go/src/github.com/gogits/gogs # gogs
sh: gogs: not found
/home/git/go/src/github.com/gogits/gogs # ./gogs
sh: ./gogs: not found
/home/git/go/src/github.com/gogits/gogs # export PATH=PATH:/home/git/go/src/github.com/gogits/gogs
/home/git/go/src/github.com/gogits/gogs # gogs
sh: gogs: not found
/home/git/go/src/github.com/gogits/gogs # cp gogs /usr/bin
sh: cp: not found
/home/git/go/src/github.com/gogits/gogs # cp gogs /usr/bin
sh: cp: not found
/home/git/go/src/github.com/gogits/gogs # cp
sh: cp: not found
/home/git/go/src/github.com/gogits/gogs # ls
sh: ls: not found

另外,当我尝试从容器内的源代码构建 gogs 时,我遇到了类似这样的分段错误(核心转储)。我用 valgrind 测试了它,得到了 Bad permissions for mapped region at address 0x1340F80

请求的信息:

FROM alpine:edge

MAINTAINER jonathan@saharacluster.com

ENV DEBIAN_FRONTEND noninteractive

COPY gogs /home/git/go/src/github.com/gogits/gogs

WORKDIR /home/git/go/src/github.com/gogits/gogs

RUN apk update && apk add --update \
    openssh \
    curl\
    git\
    supervisor ;\
    rm -rf /var/lib/apt/lists/* ;\
    adduser -S -g 'Gogs' git;\
    mkdir -p custom/conf \
    log\
    data\
    /var/log/gogs\
    /home/git/.ssh\
    /home/git/data\
    /var/run/sshd\
    /var/log/install\
    /var/log/sshd;\
    touch /home/git/.ssh/authorized_keys\
    /home/git/setup.sh \
    /home/git/data/gogs.db;\
    chmod 700 /home/git/.ssh && chmod 600 /home/git/.ssh/authorized_keys;\
    chown -R git: /var/log/gogs /var/log/sshd /home/git

COPY app.ini custom/conf/app.ini

COPY supervisord.conf /home/git/supervisord.conf

COPY docker-entrypoint.sh /home/git/docker-entrypoint.sh

COPY setup.sh /home/git/setup.sh

RUN chmod +x /home/git/docker-entrypoint.sh /home/git/setup.sh

EXPOSE 22 3000

ENV DEBIAN_FRONTEND dialog

ENTRYPOINT ["/home/git/docker-entrypoint.sh"]

环境

GOGS_USER_PASSWORD=mypassword
HOSTNAME=e11c1f1b65cd
SHLVL=1
HOME=/root
DOMAIN=abc.com
GOGS_USER_EMAIL=gogsuser@abc.com
APP_NAME=sc
API_USER_PASSWORD=mypassword
TERM=xterm
GOGS_USER=gogsuser
API_USER_EMAIL=apiuser@abc.com
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
DEBIAN_FRONTEND=dialog
PWD=/home/git/go/src/github.com/gogits/gogs
API_USER=apiuser

主要问题是您使用的图像没有所需的 Go 库。我怀疑您也没有为目标 OS.

编译 Go 应用程序
  1. 交叉编译

    如果您在 Mac 或 Windows 中编译您的应用程序,则必须通过 编译它。要针对64位linux(Debian/Ubuntu),是这样的

    GOOS=linux GOARCH=amd64 go build -o myapp_linux-amd64 .
    

    其他 OS 的标志在这里 https://golang.org/doc/install/source#environment

    用目标平台的名称 (_linux-amd64) 命名输出二进制文件是个好主意。

  2. 使用官方golang:1.7图片

    这是最简单的方法。该图像将已经包含所有需要的库。

稍后,您可以考虑优化图片大小。那时静态链接和 Alpine 变得有用,但它们有自己的陷阱(没有 libc,因此 DNS 可能无法正确解析,具体取决于您的应用程序)。它们可以解决,但现在可能不值得你花时间。