i trying following in nodejs application running on mac osx: (docker rest api's used interact docker. api reference: https://docs.docker.com/engine/reference/api/docker_remote_api_v1.23/)
create mysql docker container, mapping port 3306 1 of host port. docker container configuration post body:
{ 'env': ['mysql_root_password=root'], 'image': 'test-mysql', 'exposedports': { '3306/tcp': {} }, 'hostconfig': { 'portbindings': { '3306/tcp': [{}]} }
start newly created mysql docker container.
- fetch docker container port 3306 host mapping host config, inspecting container. (assume mapping maps mysql container port 3306 port 32789 of host)
create mysql connection using following mysql npm module https://www.npmjs.com/package/mysql. connection config is:
{ host: dockerhostip, // 192.168.99.100 -- docker host ip port: portmappingfromstep3, //32789 user: mysqluser, password: mysqluserpassword, database: 'mysql' }
running step sequentially throws error while setting mysql connection. produces following output:
{ [error: connect econnrefused 192.168.99.100:32789] code: 'econnrefused', errno: 'econnrefused', syscall: 'connect', address: '192.168.99.100', port: 32800, fatal: true }
but, if provide timeout of around 7-8 seconds before establishing mysql connection, connection establishes successfully.
not sure, happening on here. not able understand, why without timeout mysql connection failing. think related port mapping. port mapping taking time setup , providing timeout of 7seconds, covering mapping time.
i trying set end end test environment multiple mysql containers running in parallel. waiting time of 7-8 seconds connection setup big overhead.
any unravel mystery helpful. thanks