|
| 1 | ++++ |
| 2 | +title = "Docker Compose workflow" |
| 3 | ++++ |
| 4 | + |
| 5 | +Using Traefik together with Docker Compose and a Docker image registry makes it fairly easy to deploy services to the server. Here's an example of a setup. |
| 6 | + |
| 7 | +1. Make sure the Docker image is visible to Traefik by setting the proper labels: |
| 8 | + |
| 9 | + ```yaml |
| 10 | + # compose.yaml |
| 11 | + name: project |
| 12 | + services: |
| 13 | + service: |
| 14 | + build: |
| 15 | + context: . |
| 16 | + image: host/owner/repo:latest |
| 17 | + restart: unless-stopped |
| 18 | + networks: |
| 19 | + - traefik |
| 20 | + labels: |
| 21 | + - traefik.enable=true |
| 22 | + - traefik.docker.network=traefik |
| 23 | + networks: |
| 24 | + traefik: |
| 25 | + external: true |
| 26 | + ``` |
| 27 | +
|
| 28 | + Make sure the `traefik` network exists locally: |
| 29 | + |
| 30 | + ```bash |
| 31 | + docker network create traefik |
| 32 | + ``` |
| 33 | + |
| 34 | + See below how it is created on the server. |
| 35 | + |
| 36 | +2. On the server, set up Traefik. |
| 37 | + |
| 38 | + Layout: |
| 39 | + |
| 40 | + ``` |
| 41 | + $ tree -I certificates |
| 42 | + . |
| 43 | + ├── compose.yaml |
| 44 | + ├── dynamic |
| 45 | + │ └── service.yaml |
| 46 | + ├── traefik.env |
| 47 | + └── traefik.yaml |
| 48 | + ``` |
| 49 | +
|
| 50 | + The `traefik.env` file can contain the credentials for a [DNS provider](https://go-acme.github.io/lego/dns/), for example. |
| 51 | +
|
| 52 | + Docker Compose file: |
| 53 | +
|
| 54 | + ```yaml |
| 55 | + # compose.yaml |
| 56 | + name: traefik |
| 57 | + services: |
| 58 | + traefik: |
| 59 | + image: traefik:latest |
| 60 | + restart: unless-stopped |
| 61 | + ports: |
| 62 | + - 80:80 |
| 63 | + - 443:443 |
| 64 | + networks: |
| 65 | + - traefik |
| 66 | + env_file: ./traefik.env |
| 67 | + volumes: |
| 68 | + - ./certificates:/etc/traefik/certificates |
| 69 | + - ./dynamic:/etc/traefik/dynamic:ro |
| 70 | + - ./traefik.yaml:/etc/traefik/traefik.yaml:ro |
| 71 | + - /etc/localtime:/etc/localtime:ro |
| 72 | + - /var/run/docker.sock:/var/run/docker.sock:ro |
| 73 | + traefik-certs-dumper: |
| 74 | + image: ghcr.io/kereis/traefik-certs-dumper:latest |
| 75 | + restart: unless-stopped |
| 76 | + depends_on: |
| 77 | + - traefik |
| 78 | + networks: |
| 79 | + - traefik |
| 80 | + volumes: |
| 81 | + - ./certificates:/traefik:ro |
| 82 | + - ./certificates/dump:/output |
| 83 | + - /etc/localtime:/etc/localtime:ro |
| 84 | + networks: |
| 85 | + traefik: |
| 86 | + name: traefik |
| 87 | + enable_ipv6: true |
| 88 | + ipam: |
| 89 | + driver: default |
| 90 | + config: |
| 91 | + - subnet: 172.18.0.0/16 |
| 92 | + gateway: 172.18.0.1 |
| 93 | + - subnet: fdae:58bf:a627::/64 |
| 94 | + gateway: fdae:58bf:a627::1 |
| 95 | + ``` |
| 96 | + |
| 97 | + The manually specified subnets are a subnet in the Class B of the [private IPv4 address range](https://en.wikipedia.org/wiki/Private_network#Private_IPv4_addresses) and a subnet in the [private IPv6 address range](https://en.wikipedia.org/wiki/Private_network#Private_IPv6_addresses). The specific choice is not important. |
| 98 | + |
| 99 | + Config: |
| 100 | + |
| 101 | + ```yaml |
| 102 | + # traefik.yaml |
| 103 | + certificatesResolvers: |
| 104 | + letsencrypt: |
| 105 | + acme: |
| 106 | + |
| 107 | + storage: /etc/traefik/certificates/acme.json |
| 108 | + dnsChallenge: |
| 109 | + provider: provider |
| 110 | + propagation: |
| 111 | + delayBeforeChecks: 30s |
| 112 | + disableChecks: true |
| 113 | + entryPoints: |
| 114 | + http: |
| 115 | + address: :80 |
| 116 | + http3: {} |
| 117 | + https: |
| 118 | + address: :443 |
| 119 | + http3: {} |
| 120 | + http: |
| 121 | + tls: |
| 122 | + certResolver: letsencrypt |
| 123 | + providers: |
| 124 | + file: |
| 125 | + directory: /etc/traefik/dynamic |
| 126 | + docker: |
| 127 | + defaultRule: "" |
| 128 | + exposedByDefault: false |
| 129 | + ``` |
| 130 | +
|
| 131 | + Router: |
| 132 | +
|
| 133 | + ```yaml |
| 134 | + # dynamic/service.yaml |
| 135 | + http: |
| 136 | + routers: |
| 137 | + root: |
| 138 | + entrypoints: |
| 139 | + - https |
| 140 | + rule: Host(`example.com`) |
| 141 | + service: service-project@docker |
| 142 | + ``` |
| 143 | +
|
| 144 | +With the setup above, the workflow is fairly straightforward: |
| 145 | +
|
| 146 | +1. Build the image locally: |
| 147 | +
|
| 148 | + ```bash |
| 149 | + docker compose build |
| 150 | + ``` |
| 151 | + |
| 152 | +2. Push the image to the image registry: |
| 153 | + |
| 154 | + ```bash |
| 155 | + docker compose push |
| 156 | + ``` |
| 157 | + |
| 158 | +3. Pull the image on the server, start or restart the container: |
| 159 | + |
| 160 | + ```bash |
| 161 | + docker --context server compose up -d --pull always |
| 162 | + ``` |
| 163 | + |
| 164 | + The context can be created as follows: |
| 165 | + |
| 166 | + ```bash |
| 167 | + docker context create server --docker= "host=ssh://[email protected]" |
| 168 | + ``` |
| 169 | + |
| 170 | +The first two steps can be automated (e.g., via GitHub Actions) thus reducing the workflow to essentially one command. |
0 commit comments