git install 在 Dockerfile 中失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50837605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:17:44  来源:igfitidea点击:

git install fails in Dockerfile

gitdocker

提问by dingo_d

I'm trying to pull the repository I'm working on while doing a docker build, so I followed this tutorialso I added in my Dockerfile

我正在尝试在进行 docker build 时拉出我正在处理的存储库,因此我遵循了本教程,因此我添加了Dockerfile

# this is our first build stage, it will not persist in the final image
FROM ubuntu as intermediate

# install git
RUN apt-get update \
    apt-get upgrade \
    apt-get install git

# add credentials on build
RUN mkdir ~/.ssh && ln -s /run/secrets/host_ssh_key ~/.ssh/id_rsa

# make sure your domain is accepted
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan github.org >> /root/.ssh/known_hosts

RUN git clone [email protected]:my-repo/myproject.git

FROM ubuntu
# copy the repository form the previous image
COPY --from=intermediate /your-repo /srv/your-repo

In my docker-compose.ymlI added

在我的docker-compose.yml我添加

secrets:
  host_ssh_key:
    file: ~/.ssh/id_rsa

And I'm adding

我正在添加

secrets:
  - host_ssh_key

In one of the service.

在其中一项服务。

Since this is only for my local use (I don't plan on deploying or using this for the production, I just want to test it out) it's fine to use it this way - if it'll work because currently the build fails at the git install stage with error

由于这仅适用于我的本地使用(我不打算部署或将其用于生产,我只想对其进行测试)以这种方式使用它很好 - 如果它可以工作,因为当前构建失败出现错误的 git install 阶段

E: The update command takes no arguments

ERROR: Service 'web' failed to build: The command '/bin/sh -c apt-get update apt-get upgrade apt-get install git' returned a non-zero code: 100

E:更新命令不带参数

错误:服务“web”无法构建:命令“/bin/sh -c apt-get update apt-get upgrade apt-get install git”返回非零代码:100

What am I missing?

我错过了什么?

回答by Alex Karshin

You are overanalyzing this. It's just a simple typo. Should be:

你过度分析了这一点。这只是一个简单的错字。应该:

RUN apt-get update && \
    apt-get upgrade -y && \
    apt-get install -y git

Those are 3 separate commands.

这些是 3 个单独的命令。

回答by emory

Some thoughts:

一些想法:

    1. Replace apt-get install gitwith apt-get install --assume-yes git. Without the --assume-yesit will prompt you for confirmation, which you are unable to give and it will be smart enough to figure that out and assume you meant "NO".
    1. You added the ssh key, but did you confirm it was 0600. I would just copy it and specifically chmod 0600 ~/.ssh/id_rsato be sure.
    1. 替换apt-get install gitapt-get install --assume-yes git。如果没有--assume-yes它,它会提示您进行确认,而您无法给出确认,并且它会很聪明地弄清楚并假设您的意思是“不”。
    1. 您添加了 ssh 密钥,但您确认它是0600. 我只是复制它,特别是chmod 0600 ~/.ssh/id_rsa为了确定。

Overall, your Dockerfile looks very clever and I got some fresh ideas from reading it.

总的来说,你的 Dockerfile 看起来非常聪明,我从阅读中得到了一些新的想法。

回答by Jairo Lozano

For those working with alpineimages, RUN apk add --no-cache gitdid the trick for me.

对于那些处理alpine图像的人,RUN apk add --no-cache git对我有用。