Search code examples
javascriptnode.jsdockertarantool

Can't connect to Tarantool container inside Docker through Nodejs


I want to connect to the tarantool cotainer using this code:

import TarantoolConnection from 'tarantool-driver'
let connection = new TarantoolConnection('192.168.99.100:3301');
connection.ping().then((res) => {
   console.log(res);
});

Before that i started container:

docker run -p 3301:3301 -d tarantool/tarantool:1.6

But in result i get nothing.

If i try to create space or\and index for this space:

connection.eval("box.schema.space.create('myspace', {if_not_exists=true, temporary=true})").then((res) => {
    console.log(res);
});

I get this error:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: This socket is closed

or:

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: connection will be destroyed or already destroyed, create another one

As i see from the error, the needed socket is already closed, but i don't understand why.

Version of tarantool driver:

"tarantool-driver": "2.0.5",

How can i fix it?


Solution

  • You have two problems here:

    1. You should connect to localhost:3301 instead of 192.168.99.100:3301
    2. You have to use connection.connect() before connection.ping() or connection.eval()

    Here is the working code:

    const TarantoolConnection = require('tarantool-driver');
    
    let connection = new TarantoolConnection({port: 3301});
    
    connection.connect().then((res) => {
        console.log("Connected: " + res);
    
        connection.ping().then((res) => {
            console.log("Pong: " + res);
        });
    
        connection.eval("box.schema.space.create('myspace', {if_not_exists=true, temporary=true})").then((res) => {
            console.log("Space created");
        });
    });
    

    Just in case, I've used the following command to start tarantool docker instance:

    $ docker run --rm -p 3301:3301 -t -i tarantool/tarantool:1.6