-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphoenix-on-digitalocean.html
355 lines (351 loc) · 12.6 KB
/
phoenix-on-digitalocean.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<!DOCTYPE html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="ie=edge" />
<title>
How to Deploy your Phoenix LiveView 1.6 app on Digital Ocean with Docker
</title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="stylesheet"
href="../styles/output.css"
type="text/css"
media="screen"
/>
<script
defer
data-domain="aswinmohan.me"
data-api="/stats/api/event"
src="/stats/js/script.js"
></script>
</head>
<body>
<header class="flex items-center justify-between">
<p><a href="/">back to home</a></p>
<p>2022-Jul-13</p>
</header>
<article>
<h1>
How to Deploy your Phoenix LiveView 1.6 app on Digital Ocean with Docker
</h1>
<p>
One of my clients had a requirement that the LiveView app that I was
building for them be hosted on Digital Ocean. I had never hosted an
entire app on Digital Ocean, although I had experience with Spaces an S3
alternative from Digital Ocean. Turns out, with the new App Platform it
is easy to wire up the github repo and deploy the app as a docker
container.
</p>
<p>
This post outlines the steps required to host your existing Phoenix
LiveView apps on the Digital Ocean App platform.
</p>
<h2>Setting up and Configuring Phoenix</h2>
<p>
Assuming you have a Phoenix 1.6 let's dockerize it and host it on the
<a href="https://www.digitalocean.com/products/app-platform"
>Digital Ocean App Platform</a
>. Phoenix 1.6 ships with a <code>phx.gen.release</code> mix task to
generate the files to dockerize the app, run migrations and start the
server.
</p>
<h3>0️⃣ Phoenix LiveView or Vanilla App</h3>
<p>
You should have an existing Phoenix 1.6 app. This guide works for both
Vanilla and Liveview based app.
</p>
<h3>1️⃣ Dockerize the application</h3>
<p>
In the root directory of the project run
<code>mix phx.gen.release --docker</code> which generates the required
files to dockerize the app.
</p>
<pre>
* creating rel/overlays/bin/server
* creating rel/overlays/bin/server.bat
* creating rel/overlays/bin/migrate
* creating rel/overlays/bin/migrate.bat
* creating lib/digital_ocean_deploy/release.ex
* creating Dockerfile
* creating .dockerignore
</pre
>
<ul>
<li><code>server</code> : Starts the server in production</li>
<li>
<code>migrate</code> : Runs the database migrations in production
</li>
<li>
<code>release.ex</code> : Once the application is compiled and built
we won't have access to mix tasks on the server. This file includes
code to performs migrations and rollbacks on the database without
invoking mix. This function is called in the
<code> migrate </code> file.
</li>
<li>
<code> Dockerfile </code> : Dockerfile to build and deploy the app as
a container.
</li>
<li>
<code> .dockerignore </code> : Standard files to be ignored by docker.
</li>
</ul>
<h3>2️⃣ Configure SSL connection to Database</h3>
<p>
DigitalOcean only allows encrypted SSL connection between the
application and the database. Ecto supports SSL based connection , but
it is disabled out of the box.
</p>
<p>
Open up <code>runtime.exs</code>. This file contains the runtime
configuration of the language. Configuration here is not compiled into
the release.
</p>
<pre>
maybe_ipv6 = if System.get_env("ECTO_IPV6"), do: [:inet6], else: []
config :digital_ocean_deploy, DigitalOceanDeploy.Repo,
ssl: true,
url: database_url,
pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
socket_options: maybe_ipv6
</pre>
<p>
Uncomment <code>ssl: true</code>. If you have
<code>socket_options: [:inet6]</code> instead of the
<code>maybe_ipv6</code> shown here, you have to replace it with
<code>socket_options: [ ]</code>. Doing so makes Ecto connect to the
database through SSL.
</p>
<p>
Since connections to the database happen over SSL, we have to make sure
SSL is started in the migrate function. In the
<code> release.ex </code> file.
</p>
<pre>
defp load_app do
Application.ensure_all_started(:ssl)
Application.load(@app)
end
</pre>
<h3>3️⃣ Run Migrations before Server start</h3>
<p>
In the current setup, we have to manually run the migration script after
pushing any updates to the server. This is tedious, error prone and
unneccessary. We will update the script to run the migration before
every server start.
</p>
<p>Replace the last line of <code>Dockerfile</code>.</p>
<pre>
USER nobody
CMD ["/app/bin/start"]
</pre>
<p>
Create a <code>start</code> script in
<code>rel/overlays/bin/start</code> to run the migration first and then
start the server.
</p>
<pre>
#!/bin/sh
cd -P -- "$(dirname -- "$0")"
./migrate && ./server
</pre>
<p>Now make it executable with</p>
<pre>
chmod +x rel/overlays/bin/start
</pre>
<h2>Setup Digital Ocean App</h2>
<p>
Now it's time to setup the server on Digital Ocean App platform. Unlike
bare metal servers, Apps are managed by Digital Ocean akin to Heroku. We
are going to create a new app that connects to our Github repo, builds
our app and releases a new version on every update.
</p>
<h3>4️⃣ Create a Github or Gitlab repo and upload your code</h3>
<p>
Upload your code to Github or Gitab. We are going to connect this repo
to Digital Ocean.
</p>
<h3>5️⃣ Create a new Digital Ocean App and deploy</h3>
<p>
Create a new app and link it to the repo we created in the last step.
</p>
<ul>
<li>
<p>
Go to
<a href="https://cloud.digitalocean.com/projects">Digital Ocean</a>
and click Apps button from the Create dropdown.
</p>
<img
src="./assets/images/create-app.jpg"
alt="Create a new app by clicking on Create, Apps link"
/>
</li>
<li>
<p>
Connect Github or Gitlab where you have hosted the repo.Give Digital
Ocean read access to the repo.
</p>
<img
src="./assets/images/manage-access.jpg"
alt="Create a new app by clicking on Create, Apps link"
/>
<img
src="./assets/images/github-connect.jpg"
alt="Create a new app by clicking on Create, Apps link"
/>
</li>
<li>
<p>
Set the branch which you want to build from. When building
production apps, I create two apps. One to deploy from the
<code>develop</code> branch for the staging environment, and
<code>master</code> for production. Make sure
<code>Autodeploy</code> is checked, so when you push to the repo the
app gets rebuilt automatically.
</p>
<img
src="./assets/images/setup-autodeploy.jpg"
alt="Create a new app by clicking on Create, Apps link"
/>
</li>
<li>
<p>
You can click the <code>Edit Plan</code> button to change the
pricing of the instance. If you're building a test system, you can
provision a smaller instance for $5.00. Click the
<code>Add Resource</code> button to create a database.
</p>
<img
src="./assets/images/add-db.jpg"
alt="Create a new app by clicking on Create, Apps link"
/>
</li>
<li>
<p>
Remember the name that you give your database, we'll need that
later. Create and Attach that database to our app.
</p>
<img
src="./assets/images/attach-db.jpg"
alt="Create a new app by clicking on Create, Apps link"
/>
</li>
<li>
<p>
We can skip configuring the Environment for now. This will cause our
first deployment to fail, but we'll fix it later. Click Next.
</p>
<img
src="./assets/images/skip-env-vars.jpg"
alt="Create a new app by clicking on Create, Apps link"
/>
</li>
<li>
<p>
You can change the region and the name of app here. Changing the
name changes the free subdomain Digital Ocean gives you. It's best
to host the app in the region closest to your users for best
performance.
</p>
<img
src="./assets/images/info.jpg"
alt="Create a new app by clicking on Create, Apps link"
/>
</li>
<li>
<p>
Once you have confirmed the cost of the app, click Create Resource.
Digital Ocean will then clone your repo and start building.
</p>
<img
src="./assets/images/create-resource.jpg"
alt="Create a new app by clicking on Create, Apps link"
/>
</li>
</ul>
<h3>6️⃣ Setup Environment</h3>
<p>
The first deployment of our app will fail since we haven't setup the
Environment variables.
</p>
<p>
Go to the settings page and click on the server component. Also note the
name of the database we have setup.
</p>
<img src="./assets/images/settings.jpg" alt="Settings page for the app" />
<p>
Scroll down to the Environment Variables section and click
<code>Edit</code>
</p>
<img
src="./assets/images/env-vars-empty.jpg"
alt="Settings page for the app"
/>
<p>
For a LiveView app to function properly we need three environment
secrets that we need to provide.
</p>
<ul>
<li>
<code>SECRET_KEY_BASE</code> : Used by Phoenix to sign your assets.
Generate the value by running <code>mix phx.gen.secret</code> in the
root of your project.
</li>
<li>
<code>DATABASE_URL</code> : Used by Phoenix to communicate with the
database. Since we are provisioning a database with the app from
Digital Ocean itself, we can connect it through Digital Ocean. In the
values field add <code>${db.DATABASE_URL}</code> replacing
<code>db</code> with the name of your database.
</li>
</ul>
<p>Click Save and wait for the app to finish building.</p>
<img
src="./assets/images/env-vars-filled.jpg"
alt="Settings page for the app"
/>
<p>
Once the App has finished buildling, it will be deployed with a Digitial
Ocean subdomain. You have to add that domain or your custom domain to
the <code>PHX_HOST</code> variable.
</p>
<img src="./assets/images/phx-host.jpg" alt="Settings page for the app" />
<p>
Without adding this, your LiveView Websocket will not be connected.
Adding a new variable will rebulid the site and redeploy it.
</p>
<h2>Wrapping Up</h2>
<p>
With Docker and managed platforms such as Digital Ocean it is really
easy to deploy your Phoenix app to the cloud easily.
</p>
<h3>Production Tips</h3>
<ul>
<li>
Upgrade Database to a Production version: Currently we are running a
development database with no backups and standby nodes. Digital Ocean
has managed Postgres databases.
</li>
<li>
Setup a Staging Environment: Create an identical app, but deploy it
from the <code>develop</code> branch to have a staging environment.
Keep <code>master</code> as the branch that holds the production code.
</li>
</ul>
<p>Happy Deploying.</p>
<h3>References</h3>
<ul>
<li>
<a
href="https://blog.miguelcoba.com/deploying-an-elixir-release-using-docker-on-digitalocean"
>
Deploying an Elixir Release using Docker on DigitalOcean
</a>
</li>
</ul>
</article>
</body>
</html>