Wondering how to change the docker.sock file path?

What is Docker Socket?

Docker socket file is located at /var/run/docker.sock

It is used to communicate with the main docker daemon (process) by default. It is the entry point for a Docker API. This socket is used by Docker CLI by default to execute docker commands.

Let me show you how to change the location of the docker sock file.

Stop Docker

Firstly, if the docker service is running on your system, stop it. To confirm it has stopped, run the status command.

geekflare@geekflare:~$ sudo service docker stop
geekflare@geekflare:~$ 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.

Edit docker.conf and docker.socket

The following, I am changing the path from /var/run/docker.sock to /home/geekflare/docker.sock

  • Edit the docker.conf file inside /etc/init/ directory with the new location. You just need to edit one line with DOCKER_SOCKET path.
geekflare@geekflare:~$ 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
<strong>DOCKER_SOCKET=/home/geekflare/docker.sock
</strong>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
  • Edit the docker.socket file located at /lib/systemd/system/ with the updated docker.sock file location.
geekflare@geekflare:~$ sudo gedit /lib/systemd/system/docker.socket

[Unit]

Description=Docker Socket for the API

PartOf=docker.service

[Socket]

<strong>ListenStream=/home/geekflare/docker.sock
</strong>
SocketMode=0660

SocketUser=root

SocketGroup=docker

[Install]

WantedBy=sockets.target

Run the below command to update the changes (location of docker sock file).

geekflare@geekflare:~$ sudo systemctl daemon-reload

Start Docker

Let’s start the docker service, and check the status if it got started. You will see at the end of the status log printed on the terminal will give the updated path of docker.sock file.

geekflare@geekflare:~$ sudo service docker start
geekflare@geekflare:~$ 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 <strong>/home/geekflare/docker.sock</strong>"

Run the ls command at the path of docker.sock file, just to confirm that this file got created when you started the docker service.

geekflare@geekflare:~$ 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 <strong>docker.sock</strong>
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

You have successfully updated the location of docker sock file.

If you are looking to learn Docker but busy, then check out this crash course.