在 Azure 托管的 Node.js 服务器中配置 Docker 卷以进行 SVG 和 PNG 管理

问题描述 投票:0回答:1

我正在开发一个 Express.js 服务器,该服务器将被容器化并部署在 Azure 中。该服务器旨在存储 SVG 和 PNG 文件,我正在考虑使用 Docker Volumes 进行此存储。考虑到 Azure 中跨不同主机的容器部署的动态特性,我面临着跨不同实例或主机的 Docker 卷的可访问性和一致性的挑战。

我对使用 Azure Blob 存储作为替代方案持保留态度,因为我相信它会使我的服务变慢且不那么通用。以下是我的具体技术疑问:

  1. 使用 Node.js 时,如何确保 Docker 卷在多个 Azure 实例或主机之间可访问且一致?
  2. 在可扩展的 Azure 部署中有效管理数据持久性和可用性需要哪些必要的技术步骤或配置?
  3. 考虑到服务的性能和通用性质,与 Azure Blob 存储相比,在 Azure 中使用 Docker 卷进行文件存储时,我应该注意哪些限制?

我正在寻找解决这些问题的详细见解或配置示例,尤其是那些保持服务的通用性和性能性质的配置。

提前感谢您的帮助!

azure docker express microservices docker-volume
1个回答
0
投票

要访问 Docker 中的文件,可以使用 Azure 存储。与 Azure Blob 存储相比,在 Azure 中使用 Docker 卷进行文件存储的局限性在于性能和可扩展性。

  • 我将 Docker 卷与 Azure 文件存储一起使用。
  • 将数据上传到 Docker 容器

// app.js

const express = require('express');
const multer = require('multer');
const { BlobServiceClient, StorageSharedKeyCredential } = require('@azure/storage-blob');

// Set up Express
const app = express();
const port = 3000;

// Azure Storage configuration
const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME;
const accountKey =  process.env.AZURE_STORAGE_ACCOUNT_KEY;
const sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey);
const blobServiceClient = new BlobServiceClient(`https://${accountName}.blob.core.windows.net`, sharedKeyCredential);
const containerName = 'certificate122';

// Set up Multer for file uploads
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });

// Serve static files
app.use(express.static('public'));

// Upload route
// Upload route
app.post('/upload', upload.array('files'), async (req, res) => {
    try {
      const promises = req.files.map(async file => {
        const containerClient = blobServiceClient.getContainerClient(containerName);
        const blobName = `${Date.now()}-${file.originalname}`;
        const blockBlobClient = containerClient.getBlockBlobClient(blobName);
        await blockBlobClient.uploadData(file.buffer, { blobHTTPHeaders: { blobContentType: file.mimetype } });
        return blobName;
      });
      const uploadedFiles = await Promise.all(promises);
      console.log("Uploaded"); // Print "uploaded" if successfully uploaded
      res.json({ files: uploadedFiles });
    } catch (error) {
      res.status(500).json({ error: error.message });
    }
  });
  

// Display all files route
app.get('/files', async (req, res) => {
  try {
    const containerClient = blobServiceClient.getContainerClient(containerName);
    const files = [];
    for await (const blob of containerClient.listBlobsFlat()) {
      files.push(blob.name);
    }
    res.json({ files });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// Start the server
app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>File Upload</title>
</head>
<body>
  <h1>File Upload</h1>
  <form id="uploadForm" enctype="multipart/form-data">
    <input type="file" id="fileInput" name="files" multiple>
    <button type="submit">Upload</button>
  </form>

  <h2>Uploaded Files</h2>
  <ul id="fileList"></ul>

  <script>
    async function fetchFiles() {
      const response = await fetch('/files');
      const data = await response.json();
      const fileList = document.getElementById('fileList');
      fileList.innerHTML = '';
      data.files.forEach(file => {
        const li = document.createElement('li');
        li.textContent = file;
        fileList.appendChild(li);
      });
    }

    fetchFiles();

    document.getElementById('uploadForm').addEventListener('submit', async (e) => {
      e.preventDefault();
      const formData = new FormData(e.target);
      const response = await fetch('/upload', {
        method: 'POST',
        body: formData
      });
      const data = await response.json();
      if (response.ok) {
        const uploadedMessage = document.createElement('p');
        uploadedMessage.textContent = 'Uploaded';
        document.body.appendChild(uploadedMessage);
        fetchFiles();
      } else {
        console.error('Upload failed:', data.error);
      }
    });
  </script>
</body>
</html>

enter image description here 使用此 doc 使用 Azure Web App for Containers 服务启动 Dockerized Node.js 应用程序。

在包含代码存储库的目录中使用以下命令构建 Docker 映像:

       docker build -t demoacrjagord.azurecr.io/demo-app:latest .

将 Docker 映像推送到 Azure 容器注册表 (ACR):

  • 使用提供的凭据登录到 Azure 容器注册表:
      docker login demoacrjagord.azurecr.io --username demoacrjagord

将 Docker 镜像推送到 ACR:

 docker push demoacrjagord.azurecr.io/demo-app:latest

为 Azure 中的容器创建 Web 应用程序:

  • 登录到 Azure 门户并导航到 Azure Web App for Containers 部分。 - 提供所需信息,包括应用程序名称、订阅、资源组、操作系统 (Linux) 和应用程序服务计划,并在“配置容器”下选择 Azure 容器注册表,然后选择要配置的 ACR、映像名称和标签。部署。 - 单击“创建”开始部署过程。

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.