Skip to content

Commit 6689bbd

Browse files
committed
move script to coder_script
1 parent ec80cb0 commit 6689bbd

File tree

2 files changed

+92
-74
lines changed

2 files changed

+92
-74
lines changed

examples/templates/docker-devcontainer/main.tf

Lines changed: 9 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -53,80 +53,6 @@ resource "coder_agent" "main" {
5353
touch ~/.init_done
5454
fi
5555
56-
if [ "$${CODER_AGENT_URL#*host.docker.internal}" != "$CODER_AGENT_URL" ]; then
57-
# If the access URL is host.docker.internal, we set up forwarding
58-
# to the host Docker gateway IP address, which is typically
59-
# 172.17.0.1, this will allow the devcontainers to access the
60-
# Coder server even if the access URL has been shadowed by a
61-
# "docker0" interface. This usually happens if docker is started
62-
# inside a devcontainer.
63-
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
64-
sudo iptables -t nat -A POSTROUTING -j MASQUERADE
65-
66-
# Get the IP address of the host Docker gateway, which is
67-
# typically 172.17.0.1 and set up port forwarding between this
68-
# workspace's Docker gateway and the host Docker gateway.
69-
host_ip=$(getent hosts host.docker.internal | awk '{print $1}')
70-
port="$${CODER_AGENT_URL##*:}"
71-
port="$${port%%/*}"
72-
case "$port" in
73-
[0-9]*)
74-
sudo iptables -t nat -A PREROUTING -p tcp --dport $port -j DNAT --to-destination $host_ip:$port
75-
echo "Forwarded port $port to $host_ip"
76-
;;
77-
*)
78-
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination $host_ip:80
79-
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination $host_ip:443
80-
echo "Forwarded default ports 80/443 to $host_ip"
81-
;;
82-
esac
83-
84-
# Start the docker service if it is not running, this will create
85-
# the "docker0" interface if it does not exist.
86-
sudo service docker start
87-
88-
# Since we cannot define "--add-host" for devcontainers, we define
89-
# a dnsmasq configuration that allows devcontainers to resolve the
90-
# host.docker.internal URL to this workspace, which is typically
91-
# 172.18.0.1. Note that we take the second IP address from
92-
# "hostname -I" because the first one is usually in the range
93-
# 172.17.0.0/16, which is the host Docker bridge.
94-
dns_ip=
95-
while [ -z "$dns_ip" ]; do
96-
dns_ip=$(hostname -I | awk '{print $2}')
97-
if [ -z "$dns_ip" ]; then
98-
echo "Waiting for hostname -I to return a valid second IP address..."
99-
sleep 1
100-
fi
101-
done
102-
103-
# Create a simple dnsmasq configuration to allow devcontainers to
104-
# resolve host.docker.internal.
105-
sudo apt-get update -y
106-
sudo apt-get install -y dnsmasq
107-
108-
echo "no-hosts" | sudo tee /etc/dnsmasq.conf
109-
echo "address=/host.docker.internal/$dns_ip" | sudo tee -a /etc/dnsmasq.conf
110-
echo "resolv-file=/etc/resolv.conf" | sudo tee -a /etc/dnsmasq.conf
111-
echo "no-dhcp-interface=" | sudo tee -a /etc/dnsmasq.conf
112-
echo "bind-interfaces" | sudo tee -a /etc/dnsmasq.conf
113-
echo "listen-address=127.0.0.1,$dns_ip" | sudo tee -a /etc/dnsmasq.conf
114-
115-
# Restart dnsmasq to apply the new configuration.
116-
sudo service dnsmasq restart
117-
118-
# Configure Docker to use the dnsmasq server for DNS resolution.
119-
# This allows devcontainers to resolve host.docker.internal to the
120-
# IP address of this workspace.
121-
echo "{\"dns\": [\"$dns_ip\"]}"| sudo tee /etc/docker/daemon.json
122-
123-
# Restart the Docker service to apply the new configuration.
124-
sudo service docker restart
125-
else
126-
# Start the docker service if it is not running.
127-
sudo service docker start
128-
fi
129-
13056
# Add any commands that should be executed at workspace startup
13157
# (e.g. install requirements, start a program, etc) here.
13258
EOT
@@ -225,6 +151,15 @@ resource "coder_agent" "main" {
225151
}
226152
}
227153

154+
resource "coder_script" "init_docker_in_docker" {
155+
count = data.coder_workspace.me.start_count
156+
agent_id = coder_agent.main.id
157+
display_name = "Initialize Docker-in-Docker"
158+
run_on_start = true
159+
icon = "/icon/docker.svg"
160+
script = file("${path.module}/scripts/init-docker-in-docker.sh")
161+
}
162+
228163
# See https://registry.coder.com/modules/coder/devcontainers-cli
229164
module "devcontainers-cli" {
230165
count = data.coder_workspace.me.start_count
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/sh
2+
set -e
3+
4+
if [ "${CODER_AGENT_URL#*host.docker.internal}" = "$CODER_AGENT_URL" ]; then
5+
# This is likely an external access URL, so we do not need to set up
6+
# port forwarding or DNS resolution for host.docker.internal.
7+
8+
# Start the docker service if it is not running.
9+
sudo service docker start
10+
11+
exit 0
12+
fi
13+
14+
# The access URL is host.docker.internal, so we must set up forwarding
15+
# to the host Docker gateway IP address, which is typically 172.17.0.1,
16+
# this will allow the devcontainers to access the Coder server even if
17+
# the access URL has been shadowed by a "docker0" interface. This
18+
# usually happens if docker is started inside a devcontainer.
19+
20+
# Enable IP forwarding to allow traffic to flow between the host and
21+
# the devcontainers.
22+
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
23+
sudo iptables -t nat -A POSTROUTING -j MASQUERADE
24+
25+
# Get the IP address of the host Docker gateway, which is
26+
# typically 172.17.0.1 and set up port forwarding between this
27+
# workspace's Docker gateway and the host Docker gateway.
28+
host_ip=$(getent hosts host.docker.internal | awk '{print $1}')
29+
port="${CODER_AGENT_URL##*:}"
30+
port="${port%%/*}"
31+
case "$port" in
32+
[0-9]*)
33+
sudo iptables -t nat -A PREROUTING -p tcp --dport $port -j DNAT --to-destination $host_ip:$port
34+
echo "Forwarded port $port to $host_ip"
35+
;;
36+
*)
37+
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination $host_ip:80
38+
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination $host_ip:443
39+
echo "Forwarded default ports 80/443 to $host_ip"
40+
;;
41+
esac
42+
43+
# Start the docker service if it is not running, this will create
44+
# the "docker0" interface if it does not exist.
45+
sudo service docker start
46+
47+
# Since we cannot define "--add-host" for devcontainers, we define
48+
# a dnsmasq configuration that allows devcontainers to resolve the
49+
# host.docker.internal URL to this workspace, which is typically
50+
# 172.18.0.1. Note that we take the second IP address from
51+
# "hostname -I" because the first one is usually in the range
52+
# 172.17.0.0/16, which is the host Docker bridge.
53+
dns_ip=
54+
while [ -z "$dns_ip" ]; do
55+
dns_ip=$(hostname -I | awk '{print $2}')
56+
if [ -z "$dns_ip" ]; then
57+
echo "Waiting for hostname -I to return a valid second IP address..."
58+
sleep 1
59+
fi
60+
done
61+
62+
# Create a simple dnsmasq configuration to allow devcontainers to
63+
# resolve host.docker.internal.
64+
sudo apt-get update -y
65+
sudo apt-get install -y dnsmasq
66+
67+
echo "no-hosts" | sudo tee /etc/dnsmasq.conf
68+
echo "address=/host.docker.internal/$dns_ip" | sudo tee -a /etc/dnsmasq.conf
69+
echo "resolv-file=/etc/resolv.conf" | sudo tee -a /etc/dnsmasq.conf
70+
echo "no-dhcp-interface=" | sudo tee -a /etc/dnsmasq.conf
71+
echo "bind-interfaces" | sudo tee -a /etc/dnsmasq.conf
72+
echo "listen-address=127.0.0.1,$dns_ip" | sudo tee -a /etc/dnsmasq.conf
73+
74+
# Restart dnsmasq to apply the new configuration.
75+
sudo service dnsmasq restart
76+
77+
# Configure Docker to use the dnsmasq server for DNS resolution.
78+
# This allows devcontainers to resolve host.docker.internal to the
79+
# IP address of this workspace.
80+
echo "{\"dns\": [\"$dns_ip\"]}" | sudo tee /etc/docker/daemon.json
81+
82+
# Restart the Docker service to apply the new configuration.
83+
sudo service docker restart

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy