本文概述
想知道如何更改docker.sock文件路径?
什么是Docker套接字?
Docker套接字文件位于/var/run/docker.sock
默认情况下, 它用于与主docker守护程序(进程)进行通信。它是Docker API的入口点。默认情况下, Docker CLI使用此套接字执行docker命令。
让我向你展示如何更改docker sock文件的位置。
停止Docker
首先, 如果你的系统上正在运行docker服务, 请停止它。要确认它已停止, 请运行status命令。
[email protected]:~$ sudo service docker stop
[email protected]:~$ sudo service docker status
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sat 2019-11-23 15:37:00 EST; 4s ago
Docs: https://docs.docker.com
Process: 1474 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock (code=exited, status=0/SUCCESS)
Main PID: 1474 (code=exited, status=0/SUCCESS)
Nov 10 01:56:49 geekflare dockerd[1474]: time="2019-11-10T01:56:49-05:00" level=error msg="2019/11/10 06:56:49 Entering go-plugins-helper
Nov 10 01:56:49 geekflare dockerd[1474]: time="2019-11-10T01:56:49-05:00" level=error msg="time=\"2019-11-10T06:56:49Z\" level=debug msg=
Nov 10 01:56:49 geekflare dockerd[1474]: time="2019-11-10T01:56:49-05:00" level=error msg="time=\"2019-11-10T06:56:49Z\" level=error msg=
Nov 10 01:57:23 geekflare dockerd[1474]: time="2019-11-10T01:57:23.635519865-05:00" level=info msg="Container 3141793b98f315dc90a57d81006
Nov 10 01:57:24 geekflare dockerd[1474]: time="2019-11-10T01:57:24.091797103-05:00" level=info msg="ignoring event" module=libcontainerd
Nov 23 15:36:56 geekflare systemd[1]: Stopping Docker Application Container Engine...
Nov 23 15:36:56 geekflare dockerd[1474]: time="2019-11-23T15:36:56.992795411-05:00" level=info msg="Processing signal 'terminated'"
Nov 23 15:36:58 geekflare dockerd[1474]: time="2019-11-23T15:36:58.234014533-05:00" level=info msg="ignoring event" module=libcontainerd
Nov 23 15:37:00 geekflare dockerd[1474]: time="2019-11-23T15:37:00.403572098-05:00" level=info msg="ignoring event" module=libcontainerd
Nov 23 15:37:00 geekflare systemd[1]: Stopped Docker Application Container Engine.
编辑docker.conf和docker.socket
以下, 我将路径从/var/run/docker.sock更改为/home/geekflare/docker.sock
- 使用新位置编辑/ etc / init /目录中的docker.conf文件。你只需要用DOCKER_SOCKET路径编辑一行即可。
[email protected]:~$ sudo gedit /etc/init/docker.conf
description "Docker daemon"
start on (filesystem and net-device-up IFACE!=lo)
stop on runlevel [!2345]
limit nofile 524288 1048576
# Having non-zero limits causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
limit nproc unlimited unlimited
respawn
kill timeout 20
pre-start script
# see also https://github.com/tianon/cgroupfs-mount/blob/master/cgroupfs-mount
if grep -v '^#' /etc/fstab | grep -q cgroup \
|| [ ! -e /proc/cgroups ] \
|| [ ! -d /sys/fs/cgroup ]; then
exit 0
fi
if ! mountpoint -q /sys/fs/cgroup; then
mount -t tmpfs -o uid=0, gid=0, mode=0755 cgroup /sys/fs/cgroup
fi
(
cd /sys/fs/cgroup
for sys in $(awk '!/^#/ { if ($4 == 1) print $1 }' /proc/cgroups); do
mkdir -p $sys
if ! mountpoint -q $sys; then
if ! mount -n -t cgroup -o $sys cgroup $sys; then
rmdir $sys || true
fi
fi
done
)
end script
script
# modify these in /etc/default/$UPSTART_JOB (/etc/default/docker)
DOCKERD=/usr/bin/dockerd
DOCKER_OPTS=
if [ -f /etc/default/$UPSTART_JOB ]; then
. /etc/default/$UPSTART_JOB
fi
exec "$DOCKERD" $DOCKER_OPTS --raw-logs
end script
# Don't emit "started" event until docker.sock is ready.
# See https://github.com/docker/docker/issues/6647
post-start script
DOCKER_OPTS=
DOCKER_SOCKET=
if [ -f /etc/default/$UPSTART_JOB ]; then
. /etc/default/$UPSTART_JOB
fi
if ! printf "%s" "$DOCKER_OPTS" | grep -qE -e '-H|--host'; then
DOCKER_SOCKET=/home/geekflare/docker.sock
else
DOCKER_SOCKET=$(printf "%s" "$DOCKER_OPTS" | grep -oP -e '(-H|--host)\W*unix://\K(\S+)' | sed 1q)
fi
if [ -n "$DOCKER_SOCKET" ]; then
while ! [ -e "$DOCKER_SOCKET" ]; do
initctl status $UPSTART_JOB | grep -qE "(stop|respawn)/" && exit 1
echo "Waiting for $DOCKER_SOCKET"
sleep 0.1
done
echo "$DOCKER_SOCKET is up"
fi
end script
- 使用更新的docker.sock文件位置编辑位于/ lib / systemd / system /的docker.socket文件。
[email protected]:~$ sudo gedit /lib/systemd/system/docker.socket
[Unit]
Description=Docker Socket for the API
PartOf=docker.service
[Socket]
ListenStream=/home/geekflare/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target
运行以下命令以更新更改(docker sock文件的位置)。
[email protected]:~$ sudo systemctl daemon-reload
启动Docker
让我们启动docker服务, 并检查其状态。你会在终端上打印的状态日志的结尾看到, 将提供docker.sock文件的更新路径。
[email protected]:~$ sudo service docker start
[email protected]:~$ sudo service docker status
docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2019-11-23 15:39:36 EST; 3s ago
Docs: https://docs.docker.com
Main PID: 8840 (dockerd)
Tasks: 17
Memory: 47.6M
CGroup: /system.slice/docker.service
└─8840 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Nov 23 15:39:35 geekflare dockerd[8840]: time="2019-11-23T15:39:35.091941184-05:00" level=warning msg="Your kernel does not support cgroup rt runtime"
Nov 23 15:39:35 geekflare dockerd[8840]: time="2019-11-23T15:39:35.093149218-05:00" level=info msg="Loading containers: start."
Nov 23 15:39:35 geekflare dockerd[8840]: time="2019-11-23T15:39:35.957842188-05:00" level=info msg="Default bridge (docker0) is assigned with an IP address 172.17.0.0/16. Daemon
Nov 23 15:39:36 geekflare dockerd[8840]: time="2019-11-23T15:39:36.078753190-05:00" level=info msg="Loading containers: done."
Nov 23 15:39:36 geekflare dockerd[8840]: time="2019-11-23T15:39:36.664727326-05:00" level=info msg="Docker daemon" commit=481bc77 graphdriver(s)=overlay2 version=18.09.6
Nov 23 15:39:36 geekflare dockerd[8840]: time="2019-11-23T15:39:36.817929464-05:00" level=error msg="cluster exited with error: error while loading TLS certificate in /var/lib/do
Nov 23 15:39:36 geekflare dockerd[8840]: time="2019-11-23T15:39:36.820439024-05:00" level=error msg="swarm component could not be started" error="error while loading TLS certific
Nov 23 15:39:36 geekflare dockerd[8840]: time="2019-11-23T15:39:36.820821712-05:00" level=info msg="Daemon has completed initialization"
Nov 23 15:39:36 geekflare systemd[1]: Started Docker Application Container Engine.
Nov 23 15:39:36 geekflare dockerd[8840]: time="2019-11-23T15:39:36.883382952-05:00" level=info msg="API listen on /home/geekflare/docker.sock"
在docker.sock文件的路径上运行ls命令, 只是为了确认启动docker服务时已创建此文件。
[email protected]:~$ ls -l
total 466832
-rw-r--r-- 1 geekflare geekflare 0 Oct 23 05:32 ]
drwxr-xr-x 9 tomcat tomcat 4096 Nov 18 14:30 apache-tomcat-9.0.27
-rw-r--r-- 1 geekflare geekflare 10982406 Oct 7 06:21 apache-tomcat-9.0.27.tar.gz
drwxr-xr-x 8 geekflare geekflare 4096 Oct 23 06:05 chef-repo
-rw-r--r-- 1 geekflare geekflare 252269838 Jul 1 15:16 chef-server-core_13.0.17-1_amd64.deb
-rw-r--r-- 1 geekflare geekflare 129713682 Dec 27 2018 chef-workstation_0.2.43-1_amd64.deb
drwxr-xr-x 2 geekflare geekflare 4096 Oct 23 2018 Desktop
-rw-r--r-- 1 geekflare geekflare 726 Jul 27 15:10 Dockerfile
srw-rw---- 1 root docker 0 Nov 23 15:39 docker.sock
drwxr-xr-x 2 geekflare geekflare 4096 Oct 23 2018 Documents
drwxr-xr-x 2 geekflare geekflare 4096 Jul 20 18:20 Downloads
-rw-r--r-- 1 geekflare geekflare 8980 Oct 23 2018 examples.desktop
你已成功更新docker sock文件的位置。
如果你想学习Docker但又很忙, 请查看此速成班。
评论前必须登录!
注册