alibaba / funcraft

(have) Fun with Serverless(API Gateway & Function Compute)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fun local start can't connect to host DB with *docker.host.internal*

DingXuefeng opened this issue · comments

Software version

> fun --version
3.6.19
> docker --version
Docker version 19.03.13, build 4484c46d9d
> mysql --version
mysql  Ver 8.0.22 for osx10.15 on x86_64 (Homebrew)
> mysqld --version
/usr/local/Cellar/mysql/8.0.22/bin/mysqld  Ver 8.0.22 for osx10.15 on x86_64 (Homebrew)

Operating system

Catalina 10.15.7

Minimal reproducible example: file folder structure

- serverless-sandbox
-- test
--- fn
---- index.js
- template.yml
- Funfile

template.yml file content:

ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
  test:
    Type: 'Aliyun::Serverless::Service'
    Properties:
      Description: This is test service
    fn:
      Type: 'Aliyun::Serverless::Function'
      Properties:
        Handler: test/fn/index.handler
        Runtime: nodejs12
        Timeout: 60
        MemorySize: 512
        CodeUri: .
      Events:
        httpTrigger:
          Type: HTTP
          Properties:
            AuthType: ANONYMOUS
            Methods:
              - GET
              - POST

index.js file content:

var getRawBody = require('raw-body');
var getFormBody = require('body/form');
var body = require('body');
const mysql = require('mysql2');

module.exports.handler = function (req, resp, context) {
    console.log('hello world');

    var pool = mysql.createPool({ host: 'docker.host.internal', user: 'XUSER', password: 'XPASSWORD!', database: 'XDB' });
    pool.getConnection((err, conn) => {
        if(err) console.error(err);
        conn.query('select * from XX', (err, rows) => {
            console.log(rows[0]);
        });
        conn.release();
    });
}

Procedure to reproduce the problem

cd serverless-test
fun install
fun local start

Verifying that it is not a problem of the password or configuration of the host system

mysql -u XUSER -p
# with correct password I verify I can see the tables and elements.

expected output: in the console the first row of XX table in the XDB database should be shown.
actual output:

>fun local start
using template: template.yml
HttpTrigger httpTrigger of test/fn was registered
        url: http://localhost:8000/2016-08-15/proxy/test/fn
        methods: [ 'GET', 'POST' ]
        authType: ANONYMOUS


function compute app listening on port 8000!

skip pulling image aliyunfc/runtime-nodejs12:1.9.9...
FC Invoke Start RequestId: bXXXX
load code for handler:test/fn/index.handler
2020-11-13T02:30:41.636Z bXXXX [verbose] hello world
2020-11-13T02:30:51.648Z bXXXX [error] Error: connect ETIMEDOUT
    at _handleTimeoutError (/code/node_modules/mysql2/lib/connection.js:178:17)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7) {
  errorno: 'ETIMEDOUT',
  code: 'ETIMEDOUT',
  syscall: 'connect',
  fatal: true
}
2020-11-13T02:30:51.658Z bXXXX [error] TypeError: Cannot read property 'query' of undefined
    at (/code/test/fn/index.js:12:14)
    at (/code/node_modules/mysql2/lib/pool.js:60:18)
    at (/code/node_modules/mysql2/lib/connection.js:739:13)
    at onceWrapper (events.js:421:26)
    at emit (events.js:326:22)
    at _notifyError (/code/node_modules/mysql2/lib/connection.js:225:12)
    at _handleFatalError (/code/node_modules/mysql2/lib/connection.js:156:10)
    at _handleNetworkError (/code/node_modules/mysql2/lib/connection.js:169:10)
    at _handleTimeoutError (/code/node_modules/mysql2/lib/connection.js:182:10)
    at listOnTimeout (internal/timers.js:554:17)
2020-11-13T02:30:51.665Z bXXXX [error] Error: Connection lost: The server closed the connection.
    at (/code/node_modules/mysql2/lib/connection.js:91:31)
    at emit (events.js:314:20)
    at (net.js:676:12)
Fun Error:  
^C
received canncel request, stopping running containers.....
stopping container 8XXXX
begin to close server
all containers stopped

Update: I can connect from another container, so host.docker.internal itself is fine.

docker run --rm -it alpine sh

/ # apk add mysql-client
/ # mysql -h host.docker.internal -u XUSER -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> use wefit