【问题标题】:Connection refused on pushing a docker image推送 docker 映像时连接被拒绝
【发布时间】:2019-03-12 22:00:23
【问题描述】:

我将按照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

有什么想法吗?

【问题讨论】:

  • 根据您提供的链接,您需要运行docker run -d -p 5000:5000 --restart=always --name registry registry:2 来启动注册表。在本地主机上运行此命令后,您能否给出docker ps 的输出?
  • @gravetii eefbbf952a0e ubuntu:16.04 "/bin/bash" 8 分钟前 Restarting (0) 12 秒前 reg
  • 看起来这是你的 ubuntu 镜像。理想情况下,您的 docker ps 输出中应该有 registry:2 图像。 docker ps | grep registry 会给你任何输出吗?
  • @gravetii,不!
  • 在这种情况下,您的注册表未启动。再次运行命令docker run -d -p 5000:5000 --restart=always --name registry registry:2 并等待registry:2 容器启动,然后再执行其他任何操作。

标签: docker ubuntu


【解决方案1】:

连接被拒绝通常意味着您尝试连接的服务实际上并未按应有的方式启动和运行。 this 问题中概述的其他原因可能还有其他原因,但基本上,就您而言,这只是意味着注册表尚未启动。

等待正确创建注册表容器,然后再执行任何其他操作 - docker run -d -p 5000:5000 --restart=always --name registry registry:2official docker image 创建本地注册表。

通过运行docker ps | grep registry 确保registry 容器已启动,然后继续。

【讨论】:

    【解决方案2】:

    更多的cmets关于

    • Kubenetes(K8s) / Minikube
    • docker / image / registry, 容器

    如果你正在使用 Minikube,并且想从 127.0.0.1:5000 拉下一个图像,

    那么你会遇到以下错误:

    无法提取图像“127.0.0.1:5000/nginx_operator:latest”:rpc 错误:代码 = 未知 desc = 来自守护程序的错误响应:获取 http://127.0.0.1:5000/v2/:拨打 tcp 127.0.0.1:5000:连接:连接被拒绝

    完整日志:

    $ 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:没有注册表(K8s 找不到你的镜像)

    如何检查?

    第 1 步:检查您的 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
    

    第 2 步:登录您的 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
    

    第 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
    

    Step4:再次登录您的 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-&gt;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!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 2017-07-10
      • 2018-07-03
      • 2020-02-22
      相关资源
      最近更新 更多