ubuntu 推送Docker镜像时连接被拒绝

s4n0splo  于 12个月前  发布在  Docker
关注(0)|答案(3)|浏览(210)

我将按照https://docs.docker.com/registry/deploying/设置一个本地注册表。

docker run -d -p 5000:5000 --restart=always --name reg ubuntu:16.04

字符串
当我尝试运行以下命令时:

$ docker push localhost:5000/my-ubuntu


我得到错误:

Get http://localhost:5000/v2/: dial tcp 127.0.0.1:5000: connect:connection refused


你知道吗?

daolsyd0

daolsyd01#

连接被拒绝通常意味着你试图连接的服务实际上没有正常运行。可能有其他原因,如this问题中所述,但本质上,对于你的情况,它只是意味着注册表还没有启动。
在执行其他操作之前,请等待注册表容器正确创建-docker run -d -p 5000:5000 --restart=always --name registry registry:2,它从official docker image创建本地注册表。
通过运行docker ps | grep registry确保registry容器已启动,然后继续下一步。

qf9go6mv

qf9go6mv2#

更多关于

*Kubenetes(K8s)/ Minikube酒店
*docker / image / registry,container

如果您使用Minikube,并希望从127.0.0.1:5000下拉图像,
那么你会遇到下面的错误:
无法提取图像“127.0.0.1:5000/nginx_operator:latest“:rpc错误:code = Unknown desc =来自守护进程的错误响应:Get http://127.0.0.1:5000/v2/:dial tcp 127.0.0.1:5000:connect:connection refused
完整日志:

$ kubectl describe pod/your_pod
...
Events:
  Type     Reason     Age                  From               Message
  ----     ------     ----                 ----               -------
  Normal   Scheduled  2m29s                default-scheduler  Successfully assigned tj-blue-whale-05-system/tj-blue-whale-05-controller-manager-6c8f564575-kwxdv to minikube
  Normal   Pulled     2m25s                kubelet            Container image "gcr.io/kubebuilder/kube-rbac-proxy:v0.5.0" already present on machine
  Normal   Created    2m24s                kubelet            Created container kube-rbac-proxy
  Normal   Started    2m23s                kubelet            Started container kube-rbac-proxy
  Normal   BackOff    62s (x5 over 2m22s)  kubelet            Back-off pulling image "127.0.0.1:5000/nginx_operator:latest"
  Warning  Failed     62s (x5 over 2m22s)  kubelet            Error: ImagePullBackOff
  Normal   Pulling    48s (x4 over 2m23s)  kubelet            Pulling image "127.0.0.1:5000/nginx_operator:latest"
  Warning  Failed     48s (x4 over 2m23s)  kubelet            Failed to pull image "127.0.0.1:5000/nginx_operator:latest": rpc error: code = Unknown desc = Error response from daemon: Get http://127.0.0.1:5000/v2/: dial tcp 127.0.0.1:5000: connect: connection refused
  Warning  Failed     48s (x4 over 2m23s)  kubelet            Error: ErrImagePull

字符串

可能的根本原因:

注册表必须在Minikube端而不是主机端设置。

  • 主机:注册表(127.0.0.1:5000)
  • minikube:没有注册表(K8找不到您的图像)

如何查看?

Step1:检查您的Minikube容器

$ docker ps -a
CONTAINER ID   IMAGE                                           ...   STATUS        PORTS                                                                                                      NAMES
8c6f49491dd6   gcr.io/k8s-minikube/kicbase:v0.0.15-snapshot4   ...   Up 15 hours   127.0.0.1:49156->22/tcp, 127.0.0.1:49155->2376/tcp, 127.0.0.1:49154->5000/tcp, 127.0.0.1:49153->8443/tcp   minikube

# your Minikube is under running
# host:49154 <--> minikube:5000
# where: 
#  - port 49154 was allocated randomly by the docker service
#  - port 22: for ssh
#  - port 2376: for docker service
#  - port 5000: for registry (image repository)
#  - port 8443: for Kubernetes

Step2:登录您的Minikube

$ minikube ssh

docker@minikube:~$ curl 127.0.0.1:5000
curl: (7) Failed to connect to 127.0.0.1 port 5000: Connection refused

# setup
# =====
# You did not setup the registry.
# Let's try to setup it.
docker@minikube:~$ docker run --restart=always -d -p 5000:5000 --name registry registry:2

# test
# ====
# test the registry using the following commands
docker@minikube:~$ curl 127.0.0.1:5000

docker@minikube:~$ curl 127.0.0.1:5000/v2
<a href="/v2/">Moved Permanently</a>.

docker@minikube:~$ curl 127.0.0.1:5000/v2/_catalog
{"repositories":[]}
# it's successful

docker@minikube:~$ exit
logout

Step 3:构建镜像,推送到Minikube注册表中

# Let's take nginx as an example. (You can build your own image)
$ docker pull nginx

# modify the repository (the source and the name)
$ docker tag nginx 127.0.0.1:49154/nginx_operator

# check the new repository (source and the name)
$ docker images | grep nginx
REPOSITORY                       TAG       IMAGE ID       CREATED        SIZE
127.0.0.1:49154/nginx_operator   latest    ae2feff98a0c   3 weeks ago    133MB

# push the image into the registry of your Minikube
$ docker push 127.0.0.1:49154/nginx_operator

Step 4:再次登录Minikube

$ minikube ssh

# check the registry
$ curl 127.0.0.1:5000/v2/_catalog
{"repositories":["nginx_operator"]}
# it's successful

# get the image info
$ curl 127.0.0.1:5000/v2/nginx_operator/manifests/latest

docker@minikube:~$ exit
logout

自定义Minikube的暴露端口

如果你想在主机端使用端口5000而不是49154(由docker服务随机分配)
即主机:5000 <-->minikube:5000
你需要重新创建一个带有--ports标志的minikube示例

# delete the old minikube instance
$ minkube delete

# create a new one (with the docker driver)
$ minikube start --ports=5000:5000 --driver=docker
# or 
$ minikube start --ports=127.0.0.1:5000:5000 --driver=docker
$ docker ps -a
CONTAINER ID   IMAGE                                           COMMAND                  CREATED              STATUS              PORTS                                                                                                                              NAMES
5d1e5b61a3bf   gcr.io/k8s-minikube/kicbase:v0.0.15-snapshot4   "/usr/local/bin/entr…"   About a minute ago   Up About a minute   0.0.0.0:5000->5000/tcp, 127.0.0.1:49162->22/tcp, 127.0.0.1:49161->2376/tcp, 127.0.0.1:49160->5000/tcp, 127.0.0.1:49159->8443/tcp   minikube

$ docker port minikube
22/tcp -> 127.0.0.1:49162
2376/tcp -> 127.0.0.1:49161
5000/tcp -> 127.0.0.1:49160
5000/tcp -> 0.0.0.0:5000
8443/tcp -> 127.0.0.1:49159

的字符串

  • 你可以看到:0.0.0.0:5000->5000/tcp

在Minikube中重新测试注册表

# in the host side
$ docker pull nginx
$ docker tag nginx 127.0.0.1:5000/nginx_operator
$ docker ps -a
$ docker push 127.0.0.1:5000/nginx_operator

$ minikube ssh
docker@minikube:~$ curl 127.0.0.1:5000/v2/_catalog
{"repositories":["nginx_operator"]}

# Great!

qco9c6ql

qco9c6ql3#

如果您使用的是macOS,请尝试使用0.0.0.0:5000/xxx标记图像,然后推送。另请参阅相关问题:https://github.com/docker/for-mac/issues/3611

相关问题