|
| 1 | +## The Docker voting app |
| 2 | + |
| 3 | +### Initialize project |
| 4 | + |
| 5 | +In this example, we will create an app from the existing docker sample `example-voting-app`. First download the project [here](https://github.com/dockersamples/example-voting-app). |
| 6 | + |
| 7 | +Initialize the project using `docker-app init voting-app -c example-voting-app/docker-stack.yml`. |
| 8 | + |
| 9 | +### Edit metadata |
| 10 | + |
| 11 | +Go to `voting-app.dockerapp/` and open `metadata.yml` and fill the following fields: |
| 12 | +- description |
| 13 | +- maintainers |
| 14 | + |
| 15 | +### Add variables to the compose file |
| 16 | + |
| 17 | +Open `docker-compose.yml` and start by changing the version to `3.2` (generated docker-compose are version 3.2+ compatible). Change constants you want by variables, e.g.: |
| 18 | + |
| 19 | +Change the images used, from: |
| 20 | +- `dockersamples/examplevotingapp_vote:before` to `${vote.image.name}:${vote.image.tag}` |
| 21 | +- `dockersamples/examplevotingapp_result:before` to `${result.image.name}:${result.image.tag}` |
| 22 | +- `dockersamples/examplevotingapp_worker:before` to `${worker.image.name}:${worker.image.tag}` |
| 23 | +- `dockersamples/examplevotingapp_visualizer:before` to `${visualizer.image.name}:${visualizer.image.tag}` |
| 24 | + |
| 25 | +Change exposed ports, from: |
| 26 | +- `<value:5000>` to `${vote.port}` |
| 27 | +- `<value:5001>` to `${result.port}` |
| 28 | +- `<value:8080>` to `${visualizer.port}` |
| 29 | + |
| 30 | +Change default replicas, from: |
| 31 | +- `<value:2>` to `${vote.replicas}` |
| 32 | +- `<value:1>` to `${result.replicas}` |
| 33 | +- `<value:1>` to `${worker.replicas}` |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +[voting-app.dockerapp/docker-compose.yml](voting-app.dockerapp/docker-compose.yml): |
| 38 | +```yml |
| 39 | +[...] |
| 40 | +vote: |
| 41 | + image: ${vote.image.name}:${vote.image.tag} |
| 42 | + ports: |
| 43 | + - ${vote.port}:80 |
| 44 | + networks: |
| 45 | + - frontend |
| 46 | + depends_on: |
| 47 | + - redis |
| 48 | + deploy: |
| 49 | + replicas: ${vote.replicas} |
| 50 | + update_config: |
| 51 | + parallelism: 2 |
| 52 | + restart_policy: |
| 53 | + condition: on-failure |
| 54 | + |
| 55 | + result: |
| 56 | + image: ${result.image.name}:${result.image.tag} |
| 57 | + ports: |
| 58 | + - ${result.port}:80 |
| 59 | + networks: |
| 60 | + - backend |
| 61 | + depends_on: |
| 62 | + - db |
| 63 | + deploy: |
| 64 | + replicas: ${result.replicas} |
| 65 | + update_config: |
| 66 | + parallelism: 2 |
| 67 | + delay: 10s |
| 68 | + restart_policy: |
| 69 | + condition: on-failure |
| 70 | + |
| 71 | + worker: |
| 72 | + image: ${worker.image.name}:${worker.image.tag} |
| 73 | + networks: |
| 74 | + - frontend |
| 75 | + - backend |
| 76 | + deploy: |
| 77 | + mode: replicated |
| 78 | + replicas: ${worker.replicas} |
| 79 | + labels: [APP=VOTING] |
| 80 | + restart_policy: |
| 81 | + condition: on-failure |
| 82 | + delay: 10s |
| 83 | + max_attempts: 3 |
| 84 | + window: 120s |
| 85 | + placement: |
| 86 | + constraints: [node.role == manager] |
| 87 | + |
| 88 | + visualizer: |
| 89 | + image: ${visualizer.image.name}:${visualizer.image.tag} |
| 90 | + ports: |
| 91 | + - ${visualizer.port}:8080 |
| 92 | + stop_grace_period: 1m30s |
| 93 | + volumes: |
| 94 | + - "/var/run/docker.sock:/var/run/docker.sock" |
| 95 | + deploy: |
| 96 | + placement: |
| 97 | + constraints: [node.role == manager] |
| 98 | +[...] |
| 99 | +``` |
| 100 | + |
| 101 | +### Give variables their default value |
| 102 | + |
| 103 | +Open `settings.yml` and add every variables with the default value you want, e.g.: |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +[voting-app.dockerapp/settings.yml](voting-app.dockerapp/settings.yml): |
| 108 | +```yml |
| 109 | +# Vote. |
| 110 | +vote: |
| 111 | + image: |
| 112 | + name: dockersamples/examplevotingapp_vote |
| 113 | + tag: latest |
| 114 | + port: 8080 |
| 115 | + replicas: 1 |
| 116 | + |
| 117 | +# Result. |
| 118 | +result: |
| 119 | + image: |
| 120 | + name: dockersamples/examplevotingapp_result |
| 121 | + tag: latest |
| 122 | + port: 8181 |
| 123 | + replicas: 1 |
| 124 | + |
| 125 | +# Visualizer. |
| 126 | +visualizer: |
| 127 | + image: |
| 128 | + name: dockersamples/visualizer |
| 129 | + tag: latest |
| 130 | + port: 8282 |
| 131 | + |
| 132 | +# Worker. |
| 133 | +worker: |
| 134 | + image: |
| 135 | + name: dockersamples/examplevotingapp_worker |
| 136 | + tag: latest |
| 137 | + replicas: 1 |
| 138 | +``` |
| 139 | +
|
| 140 | +Test your application by running `docker-app render`. |
| 141 | + |
| 142 | +### Add settings for production and development environments |
| 143 | + |
| 144 | +Create `settings/development.yml` and `settings/production.yml` and add your target-specific variables. |
| 145 | + |
| 146 | +--- |
| 147 | + |
| 148 | +[voting-app.dockerapp/settings/development.yml](voting-app.dockerapp/settings/development.yml): |
| 149 | +```yml |
| 150 | +# Vote. |
| 151 | +vote: |
| 152 | + image: |
| 153 | + name: vote |
| 154 | +
|
| 155 | +# Result. |
| 156 | +result: |
| 157 | + image: |
| 158 | + name: result |
| 159 | +``` |
| 160 | +--- |
| 161 | + |
| 162 | +[voting-app.dockerapp/settings/production.yml](voting-app.dockerapp/settings/production.yml): |
| 163 | +```yml |
| 164 | +# Vote. |
| 165 | +vote: |
| 166 | + port: 80 |
| 167 | + replicas: 3 |
| 168 | +
|
| 169 | +# Result. |
| 170 | +result: |
| 171 | + port: 80 |
| 172 | + replicas: 5 |
| 173 | +``` |
| 174 | + |
| 175 | +### Wrap everything in a Makefile |
| 176 | + |
| 177 | +Add a Makefile to simplify rendering, deploying and killing your app. |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +[voting-app.dockerapp/Makefile](voting-app.dockerapp/Makefile): |
| 182 | +```Makefile |
| 183 | +# Input. |
| 184 | +SETTINGS_DIR ?= settings |
| 185 | +APP_NAME := voting-app |
| 186 | +
|
| 187 | +# Output. |
| 188 | +DEVELOPMENT_DIR := build/development |
| 189 | +PRODUCTION_DIR := build/production |
| 190 | +PACK := $(APP_NAME).pack |
| 191 | +
|
| 192 | +# |
| 193 | +# Cleanup. |
| 194 | +# |
| 195 | +cleanup/production: |
| 196 | + @rm -rf $(PRODUCTION_DIR) |
| 197 | +
|
| 198 | +cleanup/development: |
| 199 | + @rm -rf $(DEVELOPMENT_DIR) |
| 200 | +
|
| 201 | +cleanup: cleanup/production cleanup/development |
| 202 | +
|
| 203 | +# |
| 204 | +# Render. |
| 205 | +# |
| 206 | +render/production: cleanup/production |
| 207 | + @mkdir -p $(PRODUCTION_DIR) |
| 208 | + docker-app render -f $(SETTINGS_DIR)/production.yml > $(PRODUCTION_DIR)/docker-compose.yml |
| 209 | +
|
| 210 | +render/development: cleanup/development |
| 211 | + @mkdir -p $(DEVELOPMENT_DIR) |
| 212 | + docker-app render -f $(SETTINGS_DIR)/development.yml > $(DEVELOPMENT_DIR)/docker-compose.yml |
| 213 | +
|
| 214 | +render: render/production render/development |
| 215 | +
|
| 216 | +# |
| 217 | +# Stop. |
| 218 | +# |
| 219 | +stop/production: |
| 220 | + docker stack rm ${APP_NAME} |
| 221 | +
|
| 222 | +stop/development: |
| 223 | + docker stack rm ${APP_NAME}-dev |
| 224 | +
|
| 225 | +stop: stop/production stop/development |
| 226 | +
|
| 227 | +# |
| 228 | +# Deploy. |
| 229 | +# |
| 230 | +deploy/production: render/production stop/production |
| 231 | + docker-app deploy -f $(SETTINGS_DIR)/production.yml |
| 232 | +
|
| 233 | +deploy/development: render/development stop/development |
| 234 | + docker-app deploy -f $(SETTINGS_DIR)/development.yml |
| 235 | +
|
| 236 | +# |
| 237 | +# Pack. |
| 238 | +# |
| 239 | +pack: |
| 240 | + docker-app pack -o $(PACK) |
| 241 | +
|
| 242 | +# |
| 243 | +# Helm. |
| 244 | +# |
| 245 | +helm/production: |
| 246 | + docker-app helm -f $(SETTINGS_DIR)/production.yml |
| 247 | +
|
| 248 | +helm/development: |
| 249 | + docker-app helm -f $(SETTINGS_DIR)/development.yml |
| 250 | +``` |
| 251 | + |
| 252 | +You can add more commands, depending on your needs. |
0 commit comments