@@ -4,39 +4,50 @@ near-zero downtime and easy rollback.
44
55![ Blue/Green] ( assets/bluegreen.png )
66
7- ## Network
7+ ## 1. Create a Docker Network
88
9- We'll have a few concerns:
9+ Blue/Green adds some complexity because where before there was just "the
10+ stack", now you have:
1011
11- 1 . A blue stack
12- 2 . A green stack
13- 3 . A front proxy
14- 4 . Postgres
12+ 1 . A ` blue ` stack
13+ 2 . A ` green ` stack
14+ 3 . A front proxy to direct traffic
15+ 4 . An external Postgres.
1516
1617Create a network to link them:
1718
1819``` sh
19- docker network create myapp
20+ docker network create app
2021```
2122
2223And add it to the Compose file:
2324
2425``` yaml title="compose.yaml"
2526networks :
2627 default :
27- name : myapp
28+ name : app
2829 external : true
2930` ` `
3031
31- ## 1 . Adjust the Compose file
32+ ## 2 . Adjust Caddy
3233
33- ### Remove exposed ports
34+ ### Name the Caddy containers
35+
36+ Naming the Caddy containers ` blue_caddy` and `green_caddy` allows the front
37+ proxy to direct traffic to the correct stacks :
38+
39+ ` ` ` yaml title="compose.yaml"
40+ caddy:
41+ container_name: ${STACK_NAME}_caddy
42+ ` ` `
3443
35- Remove the ` caddy` service's ` ports:` section in `compose.yaml`.
44+ # ## Remove exposed ports
3645
37- We'll no longer expose ports in the stacks, instead the front proxy will sit in
46+ We'll no longer expose ports in the stacks, instead a front proxy will sit in
3847front of the two stacks, proxying to them.
3948
49+ So remove the `caddy` service's `ports:` section in `compose.yaml`.
50+
4051# ## Serve HTTP-only in the stacks
4152
4253Set `CADDY_SITE_ADDRESS` to only `:80`, removing `:443` (leaving TLS
@@ -48,24 +59,14 @@ caddy:
4859 CADDY_SITE_ADDRESS: :80
4960` ` `
5061
51- # ## Set the Caddy container name explicitly
52-
53- This allows us to switch between the two stacks :
54-
55- ` ` ` yaml title="compose.yaml"
56- caddy:
57- container_name: ${STACK_NAME}_caddy
58- ` ` `
59-
60- # ## Move Postgres to its own unique container
62+ # # 2. Postgres
6163
62- Running two separate PostgreSQL instances and having them both simultaneously
63- access the same data directory within that shared volume is not recommended and
64- can lead to data corruption.
64+ It's not advised to run two separate PostgreSQL instances and having them both simultaneously
65+ access the same shared volume.
6566
6667So we need to move Postgres out of the compose file and start it separately.
6768
68- # ## Share volumes between the stacks
69+ # # 3. Volumes
6970
7071To share data between the two stacks (uploads, etc.), give volumes explicit
7172names :
@@ -76,7 +77,7 @@ volumes:
7677 name: user-data
7778` ` `
7879
79- # # 3 . Bring up two Stacks
80+ # # 4 . Bring up two Stacks
8081
8182Deploying is the same as [before](deploying.md), but now we're deploying the
8283_idle stack_. For this example, `green` is idle so that's the one we're
@@ -94,13 +95,13 @@ Shell into the server and bring up the idle stack:
9495` ` ` sh
9596cd green
9697docker compose pull
97- docker compose up -d
98+ STACK_NAME=green docker compose up -d
9899` ` `
99100
100101Docker will use the directory name `green` as the project name, creating
101102different containers, volumes and networks than the `blue` stack.
102103
103- # # 3 . Add a Front Proxy
104+ # # 5 . Add a Front Proxy
104105
105106The _front proxy_ is a single container that binds ports `80` and `443` on the
106107server and routes requests into either the Blue or Green stack.
@@ -135,7 +136,7 @@ docker run -d \
135136 caddy:2
136137` ` `
137138
138- # # 4 . Upgrading
139+ # # 6 . Upgrading
139140
140141# # Flip traffic
141142
0 commit comments