如何使用NodeJS从另一个Docker容器访问sqlite3数据库? [英] How to access sqlite3 database using NodeJS from another Docker container?

查看:275
本文介绍了如何使用NodeJS从另一个Docker容器访问sqlite3数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的docker-compose.yml文件:

This is my docker-compose.yml file:

version: '3.2'
services:
  pa-portal:
    image: web_app_image
    container_name: pa-portal
    volumes:
      - productDB:/web_app/db
    ports:
      - "8080:8080"
  cypress:
    image: "cypress/included:4.4.0"
    depends_on:
      - pa-portal
    environment:
      - CYPRESS_baseUrl=http://pa-portal:8080
    working_dir: /cypress-testing

    volumes:
      - ./:/cypress-testing

volumes:
  productDB:

在我的Cypress任务之一中,我具有以下代码段:

In one of my Cypress tasks I have the following snippet of code:

  let db = new sqlite3.Database('pa-portal:8080/web_app/db/db.sqlite3', sqlite3.OPEN_READWRITE, (err) => {
    if (err) {
      console.error(err.message);
    }
    console.log('Connected to the database.');
  });


  db.run(`DELETE FROM table_name`, function (err) {
    if (err) {
      return console.error(err.message);
    }
    console.log(`Row(s) deleted ${this.changes}`);});

,但我收到的消息是

 Connected to the database.
cypress_1    | SQLITE_CANTOPEN: unable to open database file

我想我以错误的方式访问数据库,并且我需要一个Docker具体方法...

I am guessing I am accessing the database in the wrong way, and I need a Docker specific way of doing it...

推荐答案


该db.sqlite3文件是其他人的一部分Django Web应用程序

That db.sqlite3 file is part of someone else's Django web app

然后,Django应用程序应公开一个API以防止对数据库的未授权访问。

Then that Django app should be exposing an API to prevent unauthorized access to that Database.

如前所述,容器文件系统是相互隔离的

As mentioned, container filesystems are isolated from one another

这篇关于如何使用NodeJS从另一个Docker容器访问sqlite3数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆