Bobcares

Deploy a Backend App to DigitalOcean with Managed Database Integration

PDF Header PDF Footer

Discover how to deploy a backend app to DigitalOcean with Docker and Encore. Our DigitalOcean Support team is ready to assist.

Deploy a Backend Application to DigitalOcean with Managed Database Integration

Encore is a powerful framework that simplifies backend development by combining infrastructure, APIs, and code into one seamless workflow.

Deploy a Backend Application to DigitalOcean with Managed Database Integration

Today, let’s explore how to deploy an Encore app to DigitalOcean’s App platform using Docker and add a fully managed PostgreSQL database to our application.

Step 1: Create and Build Your Encore App

  1. First, we will begin by creating a new Encore app using the Encore CLI:
    encore app create myappCopy Code
  2. Select the “Hello World” template when prompted. Then, follow the CLI steps to complete the app setup.
  3. Once the app is ready, you’ll need to build a Docker image for deployment:
    encore build docker myappCopy Code

    This generates a Docker image of your Encore app, ready for the cloud.

If you’re new to DigitalOcean and wondering where to start, learning how to create a droplet in DigitalOcean can help you understand the infrastructure side before diving into containerized deployment.

Step 2: Push the Docker Image to the DigitalOcean Container Registry

Before deploying to DigitalOcean, the Docker image has to be available in a container registry.

  1. Log in to the DigitalOcean Control Panel.
  2. Go to “Container Registry” and create a new registry.
  3. Now, follow the setup steps provided by DigitalOcean.
  4. Next, we need to log in to the Registry. We need the `doctl` CLI for this. Once installed, run:
    doctl registry loginCopy Code
  5. Then, tag and push your Docker image:
    
    docker tag myapp registry.digitalocean.com/REGISTRY_NAME/myapp:latest
    docker push registry.digitalocean.com/REGISTRY_NAME/myapp:latest
    Copy Code

    Replace `REGISTRY_NAME` with the name of the container registry.

If you encounter issues during authentication, check out this guide on DigitalOcean all authentication methods failed for quick resolution.

Step 3: Deploy the App on the DigitalOcean App Platform

  1. To create the app, click Create App from the Control Panel.
  2. Choose “DigitalOcean Container Registry” as the source.
  3. Then, select the Docker image we just pushed.
  4. Now, it is time to configure the app settings.
    • Scaling: Choose the number of containers, CPU, and memory.
    • Environment Variables: Add any variables your app needs.
    • Region: Select a data center close to the users.
  5. To deploy the app, click “Next”, review the settings, and click “Create Resources”.

DigitalOcean will provision the infrastructure, pull the Docker image, and launch the application.

If your image source isn’t detected automatically, you may run into the “No detected components” error. Here’s how to fix the DigitalOcean no detected components issue.

Step 4: Monitor and Manage the App

Once the app is deployed, we will receive a public URL like https://myapp.ondigitalocean.app/hello/world .

We can test it with:

curl https://myapp.ondigitalocean.app/hello/worldCopy Code

To view real-time logs, use the “Runtime Logs” tab. We can also use “Insights” for metrics like CPU, memory, and uptime.

Furthermore, we can update environment variables, scaling options, and region settings at any time from the dashboard.

Step 5: Add a Managed PostgreSQL Database

Encore works well with databases. Here’s how to integrate a DigitalOcean-managed PostgreSQL database.

  1. First, go to “Databases” in the Control Panel.
  2. Then, click “Create Database Cluster” and select PostgreSQL.
  3. Choose a version, region, and configuration.
  4. Click “Create Database Cluster”.
  5. Now, go to the “Connection Details” tab.
  6. Copy the connection string or individual values (host, port, username, etc.).
  7. Download the CA certificate to enable SSL.
  8. To connect via terminal:
    psql -h mydb.db.ondigitalocean.com -U doadmin -d defaultdb -p 25060Copy Code
  9. Then, create a database and table:
    
    CREATE DATABASE mydb;
    \c mydb
    CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(50)
    );
    INSERT INTO users (name) VALUES ('Bob');
    Copy Code

Step 6: Connect the Database to Your Encore App

In the Encore app, we need to declare the database like this:

const mydb = new SQLDatabase("mydb", {
migrations: "./migrations",
});
export const getUser = api(
{ expose: true, method: "GET", path: "/names/:id" },
async ({ id }: { id: number }): Promise<{ id: number; name: string }> => {
return await mydb.queryRow`SELECT * FROM users WHERE id = ${id}` as {
id: number;
name: string;
};
}
);
Copy Code

Then, create a file `infra.config.json` at the root of the project:

{
"$schema": "https://encore.dev/schemas/infra.schema.json",
"sql_servers": [
{
"host": "mydb.db.ondigitalocean.com:25060",
"tls_config": {
"ca": "-----BEGIN CERTIFICATE-----\n..."
},
"databases": {
"mydb": {
"username": "doadmin",
"password": { "$env": "DB_PASSWORD" }
}
}
}
]
}
Copy Code

Replace `”—–BEGIN CERTIFICATE—–\n…”` with the actual content of the downloaded CA certificate.

Finally, go to the DigitalOcean dashboard, App > Settings > App-Level Environment Variables.

Then, add `DB_PASSWORD` as a secure variable with the database password.

Step 7: Build, Push, and Test

To build and push:


```bash
encore build docker --config infra.config.json myapp
docker tag myapp registry.digitalocean.com/YOUR_REGISTRY_NAME/myapp:latest
docker push registry.digitalocean.com/YOUR_REGISTRY_NAME/myapp:latest
Copy Code

To redeploy the App, trigger a redeploy from the DigitalOcean dashboard or CLI.

Call the API to retrieve user data:

curl https://myapp.ondigitalocean.app/names/1Copy Code

If everything is configured correctly, we should receive a JSON response like:

{ "id": 1, "name": "Bob" }Copy Code
To enhance security, follow this guide on enabling encryption in DigitalOcean Spaces to keep your data secure and compliant.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

By following this guide, we get a production-ready deployment pipeline for Encore applications.

In short, our Support Engineers demonstrated how to deploy a backend app to DigitalOcean with Docker and Encore

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Get featured on the Bobcares blog and share your expertise with a global tech audience.

WRITE FOR US
Bobcares

Use your time to build great apps. Leave your servers to us.

Managing a server is time consuming. Whether you are an expert or a newbie, that is time you could use to focus on your product or service. Leave your server management to us, and use that time to focus on the growth and success of your business.

TALK TO US Or click here to learn more.
Docker Support

Spend time on your business, not on your servers.

Managing a server is time consuming. Whether you are an expert or a newbie, that is time you could use to focus on your product or service. Leave your server management to us, and use that time to focus on the growth and success of your business.

TALK TO US Or click here to learn more.

Speed issues driving customers away?
We’ve got your back!