Skip to content

Commit 9390296

Browse files
committed
Fix host docker.sock shadowed by systemd /run tmpfs
Workspace runs systemd as PID 1; systemd remounts /run as a fresh tmpfs at boot, which shadows the host docker.sock bind mount placed at /var/run/docker.sock. Bind the host socket to /host-docker.sock (outside /run) and re-bind it onto /var/run/docker.sock via a systemd .mount unit after systemd-tmpfiles-setup creates the target file. coder-agent and web-shell now order After= and Require= the mount unit.
1 parent de552bf commit 9390296

7 files changed

Lines changed: 32 additions & 6 deletions

File tree

main.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,12 @@ resource "docker_container" "workspace" {
560560
read_only = false
561561
}
562562

563+
# Stash the host socket outside /run, which systemd remounts as a fresh
564+
# tmpfs at boot and would shadow a bind mount placed there. A systemd
565+
# .mount unit in the image re-binds /host-docker.sock onto
566+
# /var/run/docker.sock after /run is set up.
563567
volumes {
564-
container_path = "/var/run/docker.sock"
568+
container_path = "/host-docker.sock"
565569
host_path = "/var/run/docker.sock"
566570
read_only = false
567571
}

src/base/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ RUN printf '\nif [ -d /etc/profile.d ]; then\n for i in /etc/profile.d/*.sh; do
128128
RUN mkdir -p /etc/coder
129129
COPY src/base/coder-agent.service /etc/systemd/system/coder-agent.service
130130
COPY src/base/web-shell.service /etc/systemd/system/web-shell.service
131+
COPY src/base/var-run-docker.sock.mount /etc/systemd/system/var-run-docker.sock.mount
132+
COPY src/base/docker-sock.tmpfiles.conf /etc/tmpfiles.d/docker-sock.conf
131133
COPY --chmod=0755 src/base/web-shell-launch.sh /usr/local/bin/web-shell-launch
132134
RUN install -m 0644 /dev/null /var/log/web-shell.log && \
133135
chown coder:coder /var/log/web-shell.log && \
134-
systemctl enable coder-agent.service web-shell.service
136+
systemctl enable coder-agent.service web-shell.service var-run-docker.sock.mount
135137

136138
# Entrypoint claims fresh-volume mountpoints for the workspace user before
137139
# systemd starts. See entrypoint.sh for rationale.

src/base/coder-agent.service

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[Unit]
22
Description=Coder Agent
3-
After=network-online.target
3+
After=network-online.target var-run-docker.sock.mount
44
Wants=network-online.target
5+
Requires=var-run-docker.sock.mount
56
ConditionPathExists=/etc/coder/agent-init.sh
67

78
[Service]

src/base/docker-sock.tmpfiles.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f /var/run/docker.sock 0660 root docker -

src/base/entrypoint.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ fi
2626
# DooD: align the in-image `docker` group GID with the host socket's GID so
2727
# the workspace user (member of `docker` in the image) can talk to the
2828
# bind-mounted host daemon without a chmod 666 on the socket.
29-
if [ -S /var/run/docker.sock ]; then
30-
sock_gid=$(stat -c '%g' /var/run/docker.sock)
29+
if [ -S /host-docker.sock ]; then
30+
sock_gid=$(stat -c '%g' /host-docker.sock)
3131
cur_gid=$(getent group docker | cut -d: -f3 || true)
3232
if [ -n "$sock_gid" ] && [ "$sock_gid" != "$cur_gid" ]; then
3333
# If another group already owns the target GID, rename it out of the way

src/base/var-run-docker.sock.mount

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[Unit]
2+
Description=Bind-mount host Docker socket into /var/run
3+
DefaultDependencies=no
4+
After=systemd-tmpfiles-setup.service
5+
Requires=systemd-tmpfiles-setup.service
6+
ConditionPathExists=/host-docker.sock
7+
Before=sysinit.target shutdown.target
8+
Conflicts=shutdown.target
9+
10+
[Mount]
11+
What=/host-docker.sock
12+
Where=/var/run/docker.sock
13+
Type=none
14+
Options=bind
15+
16+
[Install]
17+
WantedBy=sysinit.target

src/base/web-shell.service

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
[Unit]
22
Description=web-shell
3-
After=network-online.target
3+
After=network-online.target var-run-docker.sock.mount
44
Wants=network-online.target
5+
Requires=var-run-docker.sock.mount
56

67
[Service]
78
Type=simple

0 commit comments

Comments
 (0)