Docker客户端和守护进程

Docker客户端和守护进程

Docker的C/S模式


Docker的守护进程运行在宿主机上(即Server端),使用者通过Docker的客户端命令行接口向Docker服务器发送命令,经过处理后将结果返回给客户端。

客户端使用Remote API向服务端发送命令

Docker的 C/S连接方式

Docker使用socket进行客户端和服务端的连接,提供了三种进行socket连接的模式

  1. unix:///var/run/docker.sock Unix端口,默认的连接方式

  2. tcp://host:port

  3. fd://socketfd

docker的客户端与服务端可以在同一宿主机,可以在不同主机上

Docker守护进程的配置和操作

使用ps命令可以查看docker 守护进程运行状况

[root@localhost system]# ps -ax |grep docker
15255 ?        Ssl    0:21 /usr/bin/dockerd -b=br0 --fixed-cidr=172.25.11.1/24 -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --iptables=true --icc=false
15261 ?        Ssl    0:06 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc
15293 pts/2    S+     0:00 grep --color=auto docker

docker的启动选项

docker启动配置文件:/lib/systemd/system/docker.service

运行相关:
    -D, --debug=false
    -e,--exec-driver="native"
    -p,--pidfile="/var/run/docker.pid"
服务器相关:
    -G,--group="docker"
    -H,--host=[]
    --tls=false
RemoteAPI相关:
    --api-enable-cors=false
存储相关:
    -S,--storage-driver=""
    --selinux-enabled=false
    --storage-opt=[]
网络设置相关:
    -b,--bridge="" 设置自定义网桥
    --bip=""
    --dns=[]
    --ip=0.0.0.0

docker的远程访问

默认情况下,Docker守护进程会生成一个socket(/var/run/docker.sock)文件来进行本地进程通信,而不会监听任何端口,因此只能在本地使用docker客户端或者使用Docker API进行操作。如果想在其他主机上操作Docker主机,就需要让Docker守护进程监听一个端口,这样才能实现远程通信。

修改docker守护进程启动选项

​ -H tcp://host:port

​ unix:///path/to/sockt //默认守护进程配置

​ fd://* or fd://socktfd

要实现远程访问,就要更改docker的-H选项,使用tcp模式,更改docker配置文件 */lib/systemd/system/docker.service*

[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock

在[Service]模块最后添加指定可以本地登陆和通过tcp登陆(远程),修改完配置后要重启服务

[root@localhost system]# systemctl daemon-reload 
[root@localhost system]# systemctl restart docker
[root@localhost system]# netstat -antple |grep 2375
tcp6       0      0 :::2375                 :::*                    LISTEN      0          129198     11458/dockerd     
[root@localhost system]# ps -axu |grep docker
root     11458  0.2  0.7 510800 28112 ?        Ssl  14:31   0:00 /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
root     11465  0.0  0.1 291948  7236 ?        Ssl  14:31   0:00 docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start-timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker-containerd-shim --runtime docker-runc
root     12646  0.0  0.0 112648   956 pts/0    S+   14:35   0:00 grep --color=auto docker

测试远程访问:

[root@localhost system]# docker -H 172.25.254.11:2375 images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
fsx/nginx           latest              b4f46238b357        47 hours ago        206 MB
ubuntu              14.04               8cef1fa16c77        4 weeks ago         223 MB
wordpress           4.4.2               e146b48c1da2        2 years ago         517 MB
ubuntu              latest              07c86167cdc4        2 years ago         188 MB

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值