Deploy Your Elixir App on Digital Ocean

If you want to deploy your Elixir app to a Digital Ocean droplet (i.e. cloud-hosted virtual machine), you can use Docker to make the deployment process easier. We’ll cover the steps to start with a new Phoenix project, publish to Docker Hub, and then deploy to your droplet.

You can start with the Phoenix app from our guide Build an Elixir release with Docker to Deploy Anywhere. Once you’ve deployed your app locally using the Docker build process, you’re ready to deploy your production web server.

Overview

  1. Publish to Docker Hub
  2. Set Up the Droplet
  3. Deploy app to Droplet

Publish to Docker Hub

If you haven’t used Docker Hub, first, create an account here.

Then, click “Create Repository” from the dashboard while logged in, set the name of the repository to the name of the image you have created, give it a description, and set the visibility to either public or private.

DockerHub Dashboard
Create a Repository

Once your account is created, you will need to log in with the Docker CLI:

docker_phx $ docker login

Now retag the image we created earlier with your username and then push to docker hub:

docker_phx $ docker image tag docker_phx:0.1.0 username/docker_phx:0.1.0

docker_phx $ docker image push username/docker_phx:0.1.0
The push refers to repository [docker.io/username/docker_phx]
cd755f488c49: Pushed
9fcb45b75640: Pushed
2e74f073bd24: Pushed
d9becafa2fb8: Pushed
90d11a8877ba: Pushed
39db6acceed3: Layer already exists
0.1.0: digest: sha256:aa82561553dba0fe48faa0a60a3b5a04c94f978c6ae6950376f3292e778a644c size: 1575

If you run into issues, we’ve just followed the steps from Docker Get Started – Part 3.

Finally, update your docker-compose.yml file to use the image on Docker Hub:

# docker-compose.yml
services:
  app:
    image: username/docker_phx:0.1.0

Set Up the Droplet

The first step is to set up your Digital Ocean account. Once you set up your account, you will be redirected to your account dashboard.

From here you create a container-based app then select Docker server.

Digital Ocean select container-based app
Select a container-based app
deploy a docker server
Create a Docker server

You’ll be asked to choose your plan. You can choose the standard $5/mo plan. Select the server location then add a password for your root user. Finally create your droplet.

create a docker droplet on digital ocean
create droplet on digital ocean

Deploy app to Droplet

Once your droplet is ready, copy the public IP address and SSH into the remote server from your terminal.

Digital Ocean dashboard
docker_phx $ ssh root@<ip address of droplet>

You’ll be asked for the root password you set when creating your droplet. You can add your SSH pubkey to your droplet to log in without your password in the future.

Upgrade your droplet and create a directory for your configuration files in your production environment.

Note: Now you execute some commands from the target directory in your droplet droplet and some from your project folder on your local machine. If you look at the prompt, you should be able to tell where to call the command.

root@droplet $ apt-get upgrade -y
root@droplet $ mkdir -p ~/etc/docker_phx/config

Now that the directory has been created, copy the docker-compose.yml and docker.env config file from your project folder to the droplet.

docker_phx $ scp ./config/docker.env root@<ip_address of droplet>:~/etc/docker_phx/config/docker.env
docker_phx $ scp ./docker-compose.yml root@<ip_address of droplet>:~/etc/docker_phx/docker-compose.yml

The final step is to initialize Docker Swarm and deploy our stack from the droplet. Docker will install dependencies (e.g. install Erlang and Elixir).

root@droplet $ docker swarm init --advertise-addr <ip address of droplet>
root@droplet $ docker stack deploy -c ~/etc/docker_phx/docker-compose.yml docker_phx

If you run into issues, try calling docker-compose up from your project folder on the droplet. You’ll see more debugging info.

root@droplet $ cd etc/docker_phx
root@droplet etc/docker_phx $ docker-compose up

You should be able to visit your site on the production server to add a user at http://<ip_address_of_droplet>/users.

deploy elixir with docker success

Conclusion

Congratulations, you’ve learned how to deploy a Phoenix app to to a production server! For additional info on updating new versions of your application, check out the Distillery guide.

Next learn how to Set Up Continuous Deployment with GitHub Actions to automate your deployment tasks!

Want To Know How To Deploy Phoenix Apps Using A single Command?

This brand-new FREE training reveals the most powerful new way to reduce your deployment time and skyrocket your productivity… and truly see your programming career explode off the charts!