【问题标题】:building inside Docker container在 Docker 容器内构建
【发布时间】:2018-07-17 15:36:26
【问题描述】:

在从 Docker 容器内运行 Gitea 的简单情况下,当前的 Dockerfile 假定您已经编译了 Gitea 并且可以使用它来创建映像。

就我而言,我的机器上目前没有安装 Gitea。我在 Mac OSX 上,我的想法是,如果我在这台机器上构建,但 Gitea Dockerfile 使用 Alpine Linux 作为其基础映像,在这种情况下会有一些冲突吗?有人可以澄清这是否是个问题吗?

我想到的解决方法是使用 Alpine Linux 在容器内直接构建 Gitea。另一种可能性是我在 Mac OSX 上编译,然后更改我想的基本映像。

这两种解决方法是否有意义,哪一种更可取?我们在镜像中使用的二进制文件是否也必须与基础镜像兼容?我想是的,但也欢迎您的意见。

编辑:我尝试在容器内编译,但遇到了问题。来自 DockerHub 的可用 Dockerfile 如下:

FROM alpine:3.7
LABEL maintainer="The Gitea Authors"

EXPOSE 22 3000

RUN apk --no-cache add \
    su-exec \
    ca-certificates \
    sqlite \
    bash \
    git \
    linux-pam \
    s6 \
    curl \
    openssh \
    gettext \
    tzdata \
    make 
RUN addgroup \
    -S -g 1000 \
    git && \
  adduser \
    -S -H -D \
    -h /data/git \
    -s /bin/bash \
    -u 1000 \
    -G git \
    git && \
  echo "git:$(dd if=/dev/urandom bs=24 count=1 status=none | base64)" | 
chpasswd

# Dockerfile assumes Gitea is already up and running
# In this case, copy the source to container and Compile Gitea in Alpine 
Environment
COPY gitea-master.zip /
RUN unzip gitea-master.zip
RUN    cd gitea-master
RUN    make generate all

您可以看到我添加了将 gitea 复制到容器中、解压缩、cd 到目录和构建的说明,但我得到以下输出:

Step 1/17 : FROM alpine:3.7
 ---> 3fd9065eaf02
Step 2/17 : LABEL maintainer="The Gitea Authors"
 ---> Using cache
 ---> b6bb053b3e89
Step 3/17 : EXPOSE 22 3000
 ---> Using cache
 ---> ade88a29df64
Step 4/17 : RUN apk --no-cache add     su-exec     ca-certificates     
sqlite     bash     git     linux-pam     s6     curl     openssh     
gettext     tzdata     make
 ---> Using cache
 ---> 460a5562c60f
Step 5/17 : RUN addgroup     -S -g 1000     git &&   adduser     -S -H -D     
-h /data/git     -s /bin/bash     -u 1000     -G git     git &&   echo 
"git:$(dd if=/dev/urandom bs=24 count=1 status=none | base64)" | chpasswd
 ---> Using cache
 ---> 07ff1f0b2d3e
Step 6/17 : COPY gitea-master.zip /
 ---> Using cache
 ---> 3ad1e3177659
Step 7/17 : RUN unzip gitea-master.zip
 ---> Using cache
 ---> 61a2beb19e1e
Step 8/17 : RUN    cd gitea-master
 ---> Using cache
 ---> 6985914927b9
Step 9/17 : RUN    make generate all
 ---> Running in 3e603f88c302
make: *** No rule to make target 'generate'.  Stop.
The command '/bin/sh -c make generate all' returned a non-zero code: 2

Makefile 清楚地表明这些目标是可用的,但这让我有点难过。有什么建议吗?

【问题讨论】:

    标签: docker dockerfile gitea


    【解决方案1】:

    为了在 OSX 上为 Linux 构建 Go 二进制文件(我检查了 gitea,它似乎是用 Go 构建的),您需要将环境变量 GOOS 设置为 linux。您可以尝试在 gitea makefile: https://github.com/go-gitea/gitea/blob/master/Makefile#L221 的这一行前面添加 GOOS=linux ,或者直接运行 GOOS=linux make generate build 。这应该创建一个可以在 Docker 映像中使用的 go 二进制文件。

    编辑以响应编辑:可能您将 gitea-master 解压缩到更深的目录中,在构建映像时检查 ls 的输出以查看您是否可以使用 Makefile(在构建映像时,您的密码中似乎没有 Makefile )。

    【讨论】:

    • 我实际上不在正确的目录中,感谢您指出这一点。
    • @fobius 如果回答对您有帮助,请考虑接受。
    猜你喜欢
    • 2022-06-13
    • 2017-07-05
    • 2023-03-14
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    相关资源
    最近更新 更多