# Deploying an Elixir Release using Docker on DigitalOcean


Last time [we created a Docker image](https://blog.miguelcoba.com/deploying-a-phoenix-16-app-with-docker-and-elixir-releases) and ran a container based on it. Now we'll deploy our Elixir Release Docker image in DigitalOcean.

There are several approaches to this:

* Let DigitalOcean access directly your GitHub or GitLab repository
* Put the image in Docker public registry where DigitalOcean can fetch it
* Put the image in DigitalOcean own Container Registry

In this article we'll use the first approach 

## Create a repository

I'll show how to do it in GitHub but should be similarly easy in GitLab

### GitHub

Create a new repository

![Create new repository](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364364288/xmCZddtx0.png)

push the code:

```bash
git remote add origin git@github.com:miguelcoba/saturn.git
git branch -M main
git push -u origin main
```

### Prepare to deploy to DigitalOcean

DigitalOcean requires some changes to our code in order to correctly connect to the DB from our Docker application.

So far we have been establishing a plain, unencrypted connections between the application and the database. This won't work for DigitalOcean as they strictly enforce SSL when connecting to the database.

In our case, that means that we need to enable SSL on the application side every time we open a connection to the database.

First thing is to configure the Repo to use ssl. Open `runtime.ex` and uncomment the `ssl: true` line on the `Repo` section:

```elixir
  config :saturn, Saturn.Repo,
    ssl: true,
    # socket_options: [:inet6],
    url: database_url,
    pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")
```

This will enable SSL for all the connections in the pool that the repository uses to connect to the database.

We need to change the `Saturn.Release` module that we use to run the migrations, because that also opens a connection to the database and we need it to start the `ssl` application at the beginning.

Change the `load_app` function in the `Saturn.Release` module to be like this:

```elixir
  defp load_app do
    Application.ensure_all_started(:ssl)
    Application.load(@app)
  end
```

This will ensure the ssl application is started before trying to connect to the database to run the migrations.

We need to commit this changes to the repository. DigitalOcean works by deploying a specific branch from our repository and building the Docker image with what it finds in that branch. If you plan to use Docker as your production environment then is fine to commit this to the main branch and setup DigitalOcean to use the main branch for deployment.
For this example, I will use a different branch to commit these changes and configure DigitalOcean to use that branch. I'll use a branch named `digital-ocean-deployment`

```bash
git checkout -b digital-ocean-deployment
git add .
git commit -m "Setup for deploying to DigitalOcean"
git push -u origin digital-ocean-deployment
```

## Creating an App in DigitalOcean

Let's do the deployment to DigitalOcean.

Go to your DigitalOcean dashboard and create a new App:

![Create New App](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364401918/AlwF3dqrW.png)
 
Select GitHub on the "Choose Source" screen:

![Choose GitHub as a source](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364431638/B6lOjh7QX.png)

The first time you do this, GitHub will require you to authorize DigitalOcean to access your repositories. Follow the instructions and give access to the repository you just created

![Authorize DigitalOcean](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364462895/2R3Ll_oBG.png)

![Authorization granted](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364497138/_VheH383o.png)

You'll get back to the "Choose source" and you'll see the list of authorized repositories there

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364530678/qr2SwQoh6.png)

Select your repository and you'll see that the `main` branch is selected automatically

![Repository selected](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364562456/_xGYMrZ-4.png)

If you're using `main` that's fine. For this article, I'll change it to the `digital-ocean-deployment` branch:

![Branch selected](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364607779/H_4OpSTJh.png)

Click Next to move to the "Configure your app" page:

![Configure app](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364651854/f2k_l6_z1q.png)

We need to configure the environment variables that DigitalOcean will provide to our Docker container in runtime. We can omit the `PORT` environment variable as DigitalOcean automatically provides it. We have to create the `POOL_SIZE`, `DATABASE_URL`, and `SECRET_KEY_BASE`. Let's start with the last one. 

Click on the Edit link in the "Environment Variables" section. Then, in a terminal execute this command:

```bash
mix phx.gen.secret
```

Add a variable named SECRET_KEY_BASE and put the value you got from the command in it. Check the "Encrypt" option.

![SECRET_KEY_BASE environment variable](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364685029/fELShFoQJ.png)

Now create a `POOL_SIZE` with value 2 and a DATABASE_URL with value `${db.DATABASE_URL}`. These two don't need to be encrypted.

![Environment variables](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364713369/mlg2mTtJA.png)

We need to create a database. Click on the "Add a Database" button:

![Add a Database](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364776822/ksp1muN18.png)

Accept the default values for the database and now you're ready to create the App in DigitalOcean.

One thing to note: the DATABASE_URL environment variable is referring to this database we just created. It will be replaced with the correct value to connect to this database when the application starts. 

Click next to proceed to the "Name your web service" page:

![Name your web service](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364809034/6qYgDhO6Z.png)

Accept the defaults and click Next. You're now in the "Finalize and launch" page:

![Finalize and launch](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364842781/9YM0uyuSw.png)

Click the "Launch Basic App" and you'll see your app being created.

![App creation](https://cdn.hashnode.com/res/hashnode/image/upload/v1635364876486/IUDHBLC-1.png)

DigitalOcean will now:

1. Access your repository
2. Clone it,
3. Detect the `Dockerfile` in it
4. Build a Docker image from it
5. Push it to their own Container Registry
6. Provision the database
7. Set up environment variables
8. Provision a server to run the Docker container
9. Run the application

You can check the progress by inspecting the logs:

![Realtime logs](https://cdn.hashnode.com/res/hashnode/image/upload/v1635365839518/Kg9uqGhi3.png)

At the end you'll have your app deployed:

![Deployment successful](https://cdn.hashnode.com/res/hashnode/image/upload/v1635365869954/wDKPU1BGr.png)

## Run the migrations

You can access the elixir container console and run the migrations. Go to the "Console" tab and there evaluate the following command:

```bash
bin/saturn eval "Saturn.Release.migrate"
```

You'll see something like this:

![Run DB migrations](https://cdn.hashnode.com/res/hashnode/image/upload/v1635365900774/hMZxs9rZi.png)

Finally, visit the application by clicking the "Live App" button:

![Live App](https://cdn.hashnode.com/res/hashnode/image/upload/v1635366819591/R0QTIEYG6.png)

You'll see your app running in DigitalOcean's infrastructure:

![Image description](https://cdn.hashnode.com/res/hashnode/image/upload/v1635366863255/Hi5Ow9fdT.png)

We are done!

## Source code

The  [source code](https://github.com/miguelcoba/saturn/tree/digital-ocean-deployment)  for the saturn project is open source under the MIT license. Use the `digital-ocean-deployment` branch.

## About

I'm [Miguel Cobá](https://miguelcoba.com). I write about Elixir, Elm, Software Development, and eBook writing.

- Follow me on [Twitter](https://twitter.com/MiguelCoba_)
- Subscribe to my [newsletter](https://newsletter.miguelcoba.com)
- Read all my articles on my [blog](https://blog.miguelcoba.com)
- Get my books:
    - [100 Elixir Tips](https://store.miguelcoba.com/l/100elixirtips) [FREE]
    - [Deploying Elixir](https://store.miguelcoba.com/l/deployingelixir) [FREE]
    - [Deploying Elixir: Advanced Topics](https://store.miguelcoba.com/l/advancedtopics)
    - [eBook Writing Workflow for Developers](https://store.miguelcoba.com/l/ebookwriting)

Photo by [Kris-Mikael Krister](https://unsplash.com/@kmkr?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/datacenter-ocean?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)
  

