当前位置:   article > 正文

Docker 配置远程访问_docker 远程访问

docker 远程访问

Docker客户端通常通过Unix套接字在本地与守护程序通信 /var/run/docker.sock,或通过网络通过TCP套接字。 以下是启动时提供给Docker守护程序的选项的典型示例:

  1. # ps -ef |grep dockerd
  2. root 23438 1 0 00:41 ? 00:00:03 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  3. root 24354 24336 0 08:15 pts/0 00:00:00 grep --color=auto dockerd

Docker的客户端和服务端通信有三种方式

  • -H unix:// 指的是Docker使用本地的unix套接字 /var/run/docker.sock进行通信
  • -H tcp://0.0.0.0:2376使守护程序可以通过端口2376上的任何网络接口使用。需要在安全组中打开此端口(并且,如果可能的话,请将该端口限制为IP地址白名单),以便远程客户端可以访问守护程序,为了安全起见,一般不建议开启。
  • -H fd:// 这是在systemd内部运行Docker是使用的远程通信方式,由systemd创建套接字并激活Docker守护进程。

Linux 系统:

添加远程 API 访问接口

ubuntu:

编辑 docker 配置文件/lib/systemd/system/docker.service, 找到运行主命令的那行,其内容大致为"ExecStart=/usr/bin/dockerd -H fd:// … "的那一行,给dockerd命令加参数-H tcp://0.0.0.0:2375,意思是在 2375 端口开放 API 访问。

例如在我的设备上,配置文件相应的那一行原本为:

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

添加参数后变为

ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375  --containerd=/run/containerd/containerd.sock

配置后的信息如下:

  1. [Unit]
  2. Description=Docker Application Container Engine
  3. Documentation=https://docs.docker.com
  4. After=network-online.target firewalld.service containerd.service
  5. Wants=network-online.target
  6. Requires=docker.socket
  7. Wants=containerd.service
  8. [Service]
  9. Type=notify
  10. # the default is not to use systemd for cgroups because the delegate issues still
  11. # exists and systemd currently does not support the cgroup feature set required
  12. # for containers run by docker
  13. #ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
  14. ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
  15. ExecReload=/bin/kill -s HUP $MAINPID
  16. TimeoutSec=0
  17. RestartSec=2
  18. Restart=always
  19. # Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
  20. # Both the old, and new location are accepted by systemd 229 and up, so using the old location
  21. # to make them work for either version of systemd.
  22. StartLimitBurst=3
  23. # Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
  24. # Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
  25. # this option work for either version of systemd.
  26. StartLimitInterval=60s
  27. # Having non-zero Limit*s causes performance problems due to accounting overhead
  28. # in the kernel. We recommend using cgroups to do container-local accounting.
  29. LimitNOFILE=infinity
  30. LimitNPROC=infinity
  31. LimitCORE=infinity
  32. # Comment TasksMax if your systemd version does not support it.
  33. # Only systemd 226 and above support this option.
  34. TasksMax=infinity
  35. # set delegate yes so that systemd does not reset the cgroups of docker containers
  36. Delegate=yes
  37. # kill only the docker process, not all processes in the cgroup
  38. KillMode=process
  39. OOMScoreAdjust=-500
  40. [Install]
  41. WantedBy=multi-user.target

重新加载

  1. systemctl daemon-reload # 重新加载守护进程配置
  2. systemctl restart docker.service # 重启 docker 服务

测试:

centos

首先编辑docker的宿主机文件/lib/systemd/system/docker.service

 修改以ExecStart开头的行:(因为我的系统是centos 7的,所以修改为下面得)

ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

如果是centos7以下的话,就把ExecStart修改为:

ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375

修改后保存文件,然后通知docker服务做出的修改

systemctl daemon-reload

重启docker服务

service docker restart

接下来测试一下看是否能连接到docker api。上面的2375就是对应端口

curl http://localhost:2375/verion

Mac:

brew install socat

socat TCP-LISTEN:2375,reuseaddr,fork UNIX-CONNECT:/var/run/docker.sock &

TCP4-LISTEN:在本地建立的是一个TCP ipv4协议的监听端口;

reuseaddr:绑定本地一个端口; 

fork:设定多链接模式,即当一个链接被建立后,自动复制一个同样的端口再进行监听 

socat启动监听模式会在前端占用一个shell,因此需使其在后台执行。 

docker -H tcp://10.10.11.99:2375 version

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/302309
推荐阅读
相关标签
  

闽ICP备14008679号