无法在 Docker 容器内克隆存储库。

huangapple go评论74阅读模式
英文:

unable to clone a repo inside a docker container

问题

以下是翻译好的部分:

我正在使用Docker创建自己的开发环境。
这是我的Docker文件:

FROM alpine:latest
WORKDIR /app
RUN apk update
RUN apk upgrade
RUN reboot
RUN apk add --no-cache make
RUN apk add
RUN git config --global user.name "用户名"
RUN git config --global user.email "电子邮件"
RUN git config --global init.defaultBranch main
RUN export GIT_DISCOVERY_ACROSS_FILESYSTEM=1
RUN reboot

另外,在我的原始文件中,我已经为git配置了正确的用户名和电子邮件。

现在,我使用以下命令从这个Docker文件创建了镜像:

docker build -t xyz .

然后,我使用以下命令创建了一个容器:

docker run --rm -it -v %cd%:/app xyz sh

但是当我尝试克隆一个目录时出现问题:

git clone https://github.com/coder/code-server.git

我收到以下错误:

Cloning into 'code-server'...
fatal: unable to access 'https://github.com/coder/code-server.git/': Could not resolve host: github.com

这个问题是来自Docker的网络部分吗(我需要手动定义DNS吗?)

我已经尝试过以下方法来解决这个问题:

  • 使用git config --global --unset https.proxy(尝试过,但不起作用)
  • 使用git config --global --unset http.proxy(尝试过,但不起作用)
  • 尝试使用SSH仍然不起作用(使用openssh-client)(我在Alpine上安装了openssh-client)
  • 我还尝试在除了挂载文件夹/app之外的其他文件夹上克隆存储库,但也没有成功。

我期望容器能够访问github.com,而我的互联网连接工作正常,我可以ping通google.com,但无法ping通github.com。

英文:

I was creating my own development env with docker
this is my docker file

FROM alpine:latest
WORKDIR /app
RUN apk update
RUN apk upgrade
RUN reboot
RUN apk add --no-cache make
RUN apk add 
RUN git config --global user.name "username"
RUN git config --global user.email "email"
RUN git config --global init.defaultBranch main
RUN export GIT_DISCOVERY_ACROSS_FILESYSTEM=1
RUN reboot

and one more thing in my original file I have given proper username and email for the git config

now I created image out of this Dockerfile with

docker build -t xyz .

created a container using

docker run --rm -it -v %cd%:/app xyz sh

but the problem comes when I try to clone a directory

git clone https://github.com/coder/code-server.git

the error I get

Cloning into 'code-server'...
fatal: unable to access 'https://github.com/coder/code-server.git/': Could not resolve host: github.com

is this problem from the docker networking part (do I have to define DNS manually ..?)
enter image description here
and my internet is working perfectly and I can ping google.com but not github.com
enter image description here

the things I have tried to solve this problem

git config --global --unset https.proxy (used this) (didn't work)
git config --global --unset http.proxy (used this) (didn't work)
tried to use ssh still didn't work (using openssh-client) (i installed openssh-client on the alpine)
and I more thing
I have tried to clone the repo on other folders also (other than the mounted folder /app)

I was expecting that the container can reach the github.com

答案1

得分: 0

这很可能是一个DNS问题,所以尝试按照这个答案中提到的方式运行你的Docker镜像,配置一个DNS IP:

docker run --rm -it --dns 10.0.0.2 -v %cd%:/app xyz sh

请务必添加 --dns 10.0.0.2 参数。

将 "10.0.0.2" 替换为你的DNS IP(可以使用命令 ipconfig --all 或 ipconfig /a 获取)。

英文:

Pretty sure this is a DNS issue, so try, as mentionned in this answer, to run your docker image with a dns ip configured :

docker run --rm -it --dns 10.0.0.2 -v %cd%:/app xyz sh

Be careful to add --dns 10.0.0.2

Replace "10.0.0.2" by your DNS IP (ipconfig --all or ipconfig /a)

huangapple
  • 本文由 发表于 2023年6月26日 22:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定