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.
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.
An Overview:
- Step 1: Create and Build Your Encore App
- Step 2: Push the Docker Image to the DigitalOcean Container Registry
- Step 3: Deploy the App on the DigitalOcean App Platform
- Step 4: Monitor and Manage the App
- Step 5: Add a Managed PostgreSQL Database
- Step 6: Connect the Database to Your Encore App
- Step 7: Build, Push, and Test
Step 1: Create and Build Your Encore App
- First, we will begin by creating a new Encore app using the Encore CLI:
encore app create myapp
Copy Code - Select the “Hello World” template when prompted. Then, follow the CLI steps to complete the app setup.
- Once the app is ready, you’ll need to build a Docker image for deployment:
encore build docker myapp
Copy CodeThis 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.
- Log in to the DigitalOcean Control Panel.
- Go to “Container Registry” and create a new registry.
- Now, follow the setup steps provided by DigitalOcean.
- Next, we need to log in to the Registry. We need the `doctl` CLI for this. Once installed, run:
doctl registry login
Copy Code - 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 CodeReplace `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
- To create the app, click Create App from the Control Panel.
- Choose “DigitalOcean Container Registry” as the source.
- Then, select the Docker image we just pushed.
- 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.
- 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/world
Copy 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.
- First, go to “Databases” in the Control Panel.
- Then, click “Create Database Cluster” and select PostgreSQL.
- Choose a version, region, and configuration.
- Click “Create Database Cluster”.
- Now, go to the “Connection Details” tab.
- Copy the connection string or individual values (host, port, username, etc.).
- Download the CA certificate to enable SSL.
- To connect via terminal:
psql -h mydb.db.ondigitalocean.com -U doadmin -d defaultdb -p 25060
Copy Code - 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/1
Copy Code
If everything is configured correctly, we should receive a JSON response like:
{ "id": 1, "name": "Bob" }
Copy Code
[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