@@ -53,80 +53,6 @@ resource "coder_agent" "main" {
53
53
touch ~/.init_done
54
54
fi
55
55
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
-
130
56
# Add any commands that should be executed at workspace startup
131
57
# (e.g. install requirements, start a program, etc) here.
132
58
EOT
@@ -225,6 +151,15 @@ resource "coder_agent" "main" {
225
151
}
226
152
}
227
153
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
+
228
163
# See https://registry.coder.com/modules/coder/devcontainers-cli
229
164
module "devcontainers-cli" {
230
165
count = data. coder_workspace . me . start_count
0 commit comments