”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 如何使用 Docker 和 Encore 将后端应用程序部署到 DigitalOcean

如何使用 Docker 和 Encore 将后端应用程序部署到 DigitalOcean

发布于2024-11-07
浏览:520

How to deploy a backend application to DigitalOcean using Docker and Encore

? This guide shows you how to deploy an Encore application to DigitalOcean using the new encore build command, part of Encore's open source CLI.

This is handy if you prefer manual deployment over the automation offered by Encore's Cloud Platform.

Even when deploying manually, Encore simplifies the process by providing tools to build and configure your app.⚡️

Now let's take a look at how to deploy an Encore app to DigitalOcean's App Platform using Docker and encore build.?

Prerequisites

  • DigitalOcean Account: Make sure you have a DigitalOcean account. If not, you can sign up here.
  • Docker Installed: Ensure Docker is installed on your local machine. You can download it from the Docker website.
  • Encore CLI: Install the Encore CLI:
    • macOS: brew install encoredev/tap/encore
    • Linux: curl -L https://encore.dev/install.sh | bash
    • Windows: iwr https://encore.dev/install.ps1 | iex
  • DigitalOcean CLI (Optional): You can install the DigitalOcean CLI for more flexibility and automation, but it’s not necessary for this tutorial.

Step 1: Create an Encore App

  • Create a New Encore App:

    • If you haven’t already, create a new Encore app using the Encore CLI.
    • You can use the following command to create a new app:
    encore app create myapp
    
    • Select the Hello World template.
    • Follow the prompts to create the app.
  • Build a Docker image:

    • Build the Encore app to generate the docker image for deployment:
    encore build docker myapp  
    

Step 2: Push the Docker Image to a Container Registry

To deploy your Docker image to DigitalOcean, you need to push it to a container registry. DigitalOcean supports
its own container registry, but you can also use DockerHub or other registries. Here’s how to push the image to DigitalOcean’s registry:

  • Create a DigitalOcean Container Registry:

    • Go to the DigitalOcean Control Panel and create a new container registry.
    • Follow the instructions to set it up.
  • Login to DigitalOcean's registry:

    Use the login command provided by DigitalOcean, which will look something like this:

   doctl registry login

You’ll need the DigitalOcean CLI for this, which can be installed from DigitalOcean CLI documentation.

  • Tag your Docker image: Tag your image to match the registry’s URL.
   docker tag myapp registry.digitalocean.com/YOUR_REGISTRY_NAME/myapp:latest
  • Push your Docker image to the registry:
   docker push registry.digitalocean.com/YOUR_REGISTRY_NAME/myapp:latest

Step 3: Deploy the Docker Image to DigitalOcean App Platform

  • Navigate to the App Platform:
    Go to DigitalOcean's App Platform.

  • Create a New App:

    • Click on "Create App".
    • Choose the "DigitalOcean Container Registry" option.
  • Select the Docker Image Source:

    • Select the image you pushed earlier.
  • Configure the App Settings:

    • Set up scaling options: Configure the number of containers, CPU, and memory settings.
    • Environment variables: Add any environment variables your application might need.
    • Choose the region: Pick a region close to your users for better performance.
  • Deploy the App:

    • Click "Next", review the settings, and click "Create Resources".
    • DigitalOcean will take care of provisioning the infrastructure, pulling the Docker image, and starting the application.

Step 4: Monitor and Manage the App

  • Access the Application:
    • Once deployed, you will get a public URL to access your application.
    • Test the app to ensure it’s running as expected, e.g.
curl https://myapp.ondigitalocean.app/hello/world
  • View Logs and Metrics:

    • Go to the "Runtime Logs" tab in the App Platform to view logs
    • Go to the "Insights" tab to view performance metrics.
  • Manage Scaling and Deployment Settings:

    • You can change the app configuration, such as scaling settings, deployment region, or environment variables.

Step 5: Add a Database to Your App

DigitalOcean’s App Platform provides managed databases, allowing you to add a database to your app easily. Here’s how to set up a managed database for your app:

  • Navigate to the DigitalOcean Control Panel:

    • Go to DigitalOcean Control Panel.
    • Click on "Databases" in the left-hand sidebar.
  • Create a New Database Cluster:

    • Click "Create Database Cluster".
    • Choose PostgreSQL
    • Select the database version, data center region, and cluster configuration (e.g., development or production settings based on your needs).
    • Name the database and configure other settings if necessary, then click "Create Database Cluster".
  • Configure the Database Settings:

    • Once the database is created, go to the "Connection Details" tab of the database dashboard.
    • Copy the connection string or individual settings (host, port, username, password, database name). You will need these details to connect your app to the database.
    • Download the CA certificate
  • Create a Database

    • Connect to the database using the connection string provided by DigitalOcean.
   psql -h mydb.db.ondigitalocean.com -U doadmin -d mydb -p 25060
  • Create a database
    CREATE DATABASE mydb;
    ```


   - Create a table


   ```sql
     CREATE TABLE users (
        id SERIAL PRIMARY KEY,
        name VARCHAR(50)
     );
     INSERT INTO users (name) VALUES ('Alice');
  • Declare a Database in your Encore app:
    • Open your Encore app’s codebase.
    • Add mydb database to your app (Encore Database Documentation)
      const mydb = new SQLDatabase("mydb", {
         migrations: "./migrations",
      });

      export const getUser = api(
        { expose: true, method: "GET", path: "/names/:id" },
        async ({id}: {id:number}): Promise => {
          return await mydb.queryRow`SELECT * FROM users WHERE id = ${id}` as { id: number; name: string };
        }
      );
  • Create an Encore Infrastructure config
    • Create a file named infra.config.json in the root of your Encore app.
    • Add the CA certificate and the connection details to the file:
   {
      "$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"}
             }
         }
      }]   
   }
  • Set Up Environment Variables (Optional):

    • Go to the DigitalOcean App Platform dashboard.
    • Select your app.
    • In the "Settings" section, go to "App-Level Environment Variables"
    • Add the database password as an encrypted environment variable called DB_PASSWORD.
  • Build and push the Docker image:

    • Build the Docker image with the updated configuration.
   encore build docker --config infra.config.json myapp
  • Tag and push the Docker image to the DigitalOcean container registry.
   docker tag myapp registry.digitalocean.com/YOUR_REGISTRY_NAME/myapp:latest
   docker push registry.digitalocean.com/YOUR_REGISTRY_NAME/myapp:latest
  • Test the Database Connection:
    • Redeploy the app on DigitalOcean to apply the changes.
    • Test the database connection by calling the API
    curl https://myapp.ondigitalocean.app/names/1

Troubleshooting Tips

  • Deployment Failures: Check the build logs for any errors. Make sure the Docker image is correctly tagged and pushed to the registry.
  • App Not Accessible: Verify that the correct port is exposed in the Dockerfile and the App Platform configuration.
  • Database Connection Issues: Ensure the database connection details are correct and the database is accessible from the app.

Conclusion

That’s it! You’ve successfully deployed an Encore app to DigitalOcean’s App Platform using Docker.?

You can now scale your app, monitor its performance, and manage it easily through the DigitalOcean dashboard.

? Try it yourself

  • Learn about building apps using Encore with these Tutorials.?
  • Find inspiration on what to build with these Open Source App Templates.?

Wrapping up

  • ⭐️ Support the project by starring Encore on GitHub.
  • ? If you have questions or want to share your work, join the developers hangout in Encore's community on Discord.
版本声明 本文转载于:https://dev.to/encore/how-to-deploy-a-backend-application-to-digitalocean-using-docker-and-encore-1eh0?1如有侵犯,请联系[email protected]删除
最新教程 更多>
  • 如何配置Pytesseract以使用数字输出的单位数字识别?
    如何配置Pytesseract以使用数字输出的单位数字识别?
    Pytesseract OCR具有单位数字识别和仅数字约束 在pytesseract的上下文中,在配置tesseract以识别单位数字和限制单个数字和限制输出对数字可能会提出质疑。 To address this issue, we delve into the specifics of Te...
    编程 发布于2025-04-07
  • 如何从Google API中检索最新的jQuery库?
    如何从Google API中检索最新的jQuery库?
    从Google APIS 问题中提供的jQuery URL是版本1.2.6。对于检索最新版本,以前有一种使用特定版本编号的替代方法,它是使用以下语法:获取最新版本:未压缩)While these legacy URLs still remain in use, it is recommended ...
    编程 发布于2025-04-07
  • 找到最大计数时,如何解决mySQL中的“组函数\”错误的“无效使用”?
    找到最大计数时,如何解决mySQL中的“组函数\”错误的“无效使用”?
    如何在mySQL中使用mySql 检索最大计数,您可能会遇到一个问题,您可能会在尝试使用以下命令:理解错误正确找到由名称列分组的值的最大计数,请使用以下修改后的查询: 计数(*)为c 来自EMP1 按名称组 c desc订购 限制1 查询说明 select语句提取名称列和每个名称...
    编程 发布于2025-04-07
  • 如何使用Java.net.urlConnection和Multipart/form-data编码使用其他参数上传文件?
    如何使用Java.net.urlConnection和Multipart/form-data编码使用其他参数上传文件?
    使用http request 上传文件上传到http server,同时也提交其他参数,java.net.net.urlconnection and Multipart/form-data Encoding是普遍的。 Here's a breakdown of the process:Mu...
    编程 发布于2025-04-07
  • 如何正确使用与PDO参数的查询一样?
    如何正确使用与PDO参数的查询一样?
    在pdo 中使用类似QUERIES在PDO中的Queries时,您可能会遇到类似疑问中描述的问题:此查询也可能不会返回结果,即使$ var1和$ var2包含有效的搜索词。错误在于不正确包含%符号。通过将变量包含在$ params数组中的%符号中,您确保将%字符正确替换到查询中。没有此修改,PDO...
    编程 发布于2025-04-07
  • 如何同步迭代并从PHP中的两个等级阵列打印值?
    如何同步迭代并从PHP中的两个等级阵列打印值?
    同步的迭代和打印值来自相同大小的两个数组使用两个数组相等大小的selectbox时,一个包含country代码的数组,另一个包含乡村代码,另一个包含其相应名称的数组,可能会因不当提供了exply for for for the uncore for the forsion for for ytry...
    编程 发布于2025-04-07
  • 如何使用“ JSON”软件包解析JSON阵列?
    如何使用“ JSON”软件包解析JSON阵列?
    parsing JSON与JSON软件包 QUALDALS:考虑以下go代码:字符串 } func main(){ datajson:=`[“ 1”,“ 2”,“ 3”]`` arr:= jsontype {} 摘要:= = json.unmarshal([] byte(...
    编程 发布于2025-04-07
  • 如何使用node-mysql在单个查询中执行多个SQL语句?
    如何使用node-mysql在单个查询中执行多个SQL语句?
    Multi-Statement Query Support in Node-MySQLIn Node.js, the question arises when executing multiple SQL statements in a single query using the node-mys...
    编程 发布于2025-04-07
  • 如何在Java中正确显示“ DD/MM/YYYY HH:MM:SS.SS”格式的当前日期和时间?
    如何在Java中正确显示“ DD/MM/YYYY HH:MM:SS.SS”格式的当前日期和时间?
    如何在“ dd/mm/yyyy hh:mm:mm:ss.ss”格式“ gormat 解决方案: args)抛出异常{ 日历cal = calendar.getInstance(); SimpleDateFormat SDF =新的SimpleDateFormat(“...
    编程 发布于2025-04-07
  • 在程序退出之前,我需要在C ++中明确删除堆的堆分配吗?
    在程序退出之前,我需要在C ++中明确删除堆的堆分配吗?
    在C中的显式删除 在C中的动态内存分配时,开发人员通常会想知道是否有必要在heap-procal extrable exit exit上进行手动调用“ delete”操作员,但开发人员通常会想知道是否需要手动调用“ delete”操作员。本文深入研究了这个主题。 在C主函数中,使用了动态分配变量(H...
    编程 发布于2025-04-07
  • 我可以将加密从McRypt迁移到OpenSSL,并使用OpenSSL迁移MCRYPT加密数据?
    我可以将加密从McRypt迁移到OpenSSL,并使用OpenSSL迁移MCRYPT加密数据?
    将我的加密库从mcrypt升级到openssl 问题:是否可以将我的加密库从McRypt升级到OpenSSL?如果是这样,如何?答案:是的,可以将您的Encryption库从McRypt升级到OpenSSL。可以使用openssl。附加说明: [openssl_decrypt()函数要求iv参...
    编程 发布于2025-04-07
  • 您可以使用CSS在Chrome和Firefox中染色控制台输出吗?
    您可以使用CSS在Chrome和Firefox中染色控制台输出吗?
    在javascript console 中显示颜色是可以使用chrome的控制台显示彩色文本,例如红色的redors,for for for for错误消息?回答是的,可以使用CSS将颜色添加到Chrome和Firefox中的控制台显示的消息(版本31或更高版本)中。要实现这一目标,请使用以下模...
    编程 发布于2025-04-07
  • 如何使用不同数量列的联合数据库表?
    如何使用不同数量列的联合数据库表?
    合并列数不同的表 当尝试合并列数不同的数据库表时,可能会遇到挑战。一种直接的方法是在列数较少的表中,为缺失的列追加空值。 例如,考虑两个表,表 A 和表 B,其中表 A 的列数多于表 B。为了合并这些表,同时处理表 B 中缺失的列,请按照以下步骤操作: 确定表 B 中缺失的列,并将它们添加到表的末...
    编程 发布于2025-04-07
  • 如何有效地转换PHP中的时区?
    如何有效地转换PHP中的时区?
    在PHP 利用dateTime对象和functions DateTime对象及其相应的功能别名为时区转换提供方便的方法。例如: //定义用户的时区 date_default_timezone_set('欧洲/伦敦'); //创建DateTime对象 $ dateTime = ne...
    编程 发布于2025-04-07
  • 如何使用PHP从XML文件中有效地检索属性值?
    如何使用PHP从XML文件中有效地检索属性值?
    从php $xml = simplexml_load_file($file); foreach ($xml->Var[0]->attributes() as $attributeName => $attributeValue) { echo $attributeName,...
    编程 发布于2025-04-07

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3