Skip to content

Commit 58a042f

Browse files
committed
Setup a caching proxy for Docker Hub
1 parent baff681 commit 58a042f

File tree

7 files changed

+60
-6
lines changed

7 files changed

+60
-6
lines changed

TODO.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@ progress of longer tasks.
1212

1313
- Deploy fleet manager to home server
1414

15-
15+
<!--
1616
## Medium priority: quality of life
17-
18-
**deploy/**
19-
20-
- Deal with DockerHub rate limits: set up Docker proxy on the gateway?
17+
-->
2118

2219

2320
## Low priority: nice to have

build/template/gateway.yml

+13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@
3333
caddy_config: /etc/caddy/Caddyfile
3434
firewall_auto_config: no
3535
tasks:
36+
- name: create systemd service for Docker Hub proxy
37+
template:
38+
src: /etc/provision/gateway/dockerhub.service
39+
dest: /etc/systemd/system/dockerhub.service
40+
mode: '0644'
41+
owner: root
42+
- name: enable systemd service for Docker Hub proxy
43+
systemd:
44+
name: dockerhub.service
45+
enabled: yes
46+
state: started
47+
daemon_reload: yes
48+
3649
# HTTP reverse proxy (Caddy)
3750
# nginx is too quirky with dynamic reverse proxy destinations:
3851
# - requires static resolver IP
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# I know that systemd service will own only docker client process, not the
2+
# container process itself.
3+
#
4+
# Typically this is less than ideal (and this is where alternative tools like
5+
# Podman truly shine) but for current usecase it's acceptable. Gateway is not
6+
# expected to live long (it will be destroyed if there were no CI jobs within
7+
# last hour) and there is only a slight chance that we will encounter a
8+
# failure not handled either by systemd or by docker in that short time.
9+
10+
[Unit]
11+
Description=Docker Hub caching proxy
12+
After=docker.service
13+
Requires=docker.service
14+
15+
[Service]
16+
Environment=REMOTE_IMAGE=registry:2
17+
Environment=LOCAL_CONTAINER=dockerhub
18+
Environment=LOCAL_USER=1912:1954
19+
Environment=LOCAL_STORAGE=/dockerhub
20+
ExecStop=-/usr/bin/docker stop ${LOCAL_CONTAINER}
21+
ExecStop=-/usr/bin/docker rm ${LOCAL_CONTAINER}
22+
ExecStartPre=/bin/mkdir -p ${LOCAL_STORAGE}
23+
ExecStartPre=/bin/chown -R ${LOCAL_USER} ${LOCAL_STORAGE}
24+
ExecStartPre=-/usr/bin/docker stop ${LOCAL_CONTAINER}
25+
ExecStartPre=-/usr/bin/docker rm ${LOCAL_CONTAINER}
26+
ExecStart=/usr/bin/docker run \
27+
--name ${LOCAL_CONTAINER} \
28+
--env REGISTRY_PROXY_REMOTEURL=https://registry-1.docker.io \
29+
--volume ${LOCAL_STORAGE}:/var/lib/registry \
30+
--publish {{ inner_ip }}:5000:5000 \
31+
--user ${LOCAL_USER} \
32+
--restart unless-stopped \
33+
${REMOTE_IMAGE}
34+
TimeoutStartSec=0
35+
Restart=always
36+
37+
[Install]
38+
WantedBy=default.target

build/template/runner.yml

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
vars:
5454
new:
5555
storage-driver: overlay2
56+
registry-mirrors:
57+
- 'http://{{ gateway_ip }}:5000'
5658
old: '{{ docker_config.content|b64decode|from_json }}'
5759
copy:
5860
content: |

deploy/cloud-config/gateway.yml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ write_files:
99
hosts:
1010
localhost:
1111
inner_subnet: ${inner_subnet}
12+
inner_ip: ${inner_ip}
1213
1314
runcmd:
1415
- /usr/bin/make -C /etc/provision gateway

deploy/cloud-config/runner.yml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ write_files:
1111
all:
1212
hosts:
1313
localhost:
14+
gateway_ip: ${gateway_ip}
1415
gitlab_runner_token: ${gitlab_runner_token}
1516
gitlab_runner_tag: ${gitlab_runner_tag}
1617

deploy/servers.tf

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ resource "yandex_compute_instance" "gateway" {
1616
auto_delete = true
1717
initialize_params {
1818
image_id = yandex_compute_image.base[0].id
19-
size = 10
19+
size = 25
2020
}
2121
}
2222
network_interface {
@@ -31,6 +31,7 @@ resource "yandex_compute_instance" "gateway" {
3131
serial-port-enable = 1
3232
user-data = templatefile("cloud-config/gateway.yml", {
3333
inner_subnet = var.inner_cidr[0],
34+
inner_ip = local.gateway_ip,
3435
})
3536
}
3637
}
@@ -64,6 +65,7 @@ resource "yandex_compute_instance" "runner" {
6465
metadata = {
6566
serial-port-enable = 1
6667
user-data = templatefile("cloud-config/runner.yml", {
68+
gateway_ip = local.gateway_ip,
6769
gitlab_runner_tag = var.gitlab_runner_tag,
6870
gitlab_runner_token = var.gitlab_runner_token,
6971
})

0 commit comments

Comments
 (0)