alibaba / alibaba/funcraft

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

Open
#1,036 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
931
Forks
128
PR merge metrics
No merged PRs in 30d

Description

Software version
```bash
> 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
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the failure with `fun install` and `fun local start` using the provided `template.yml`, `Funfile`, and `test/fn/index.js`. Start by checking how the local runtime container resolves `docker.host.internal` and reaches the host MySQL service. Done means the function connects to the host database and logs the first row without the timeout or follow-on connection error.

Written by the indexing model from the issue text.

Assessment

Tech stack
docker, javascript, mysql
Domain
cli, databases, networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.