cli icon indicating copy to clipboard operation
cli copied to clipboard

Make it easier to use pscale connect in a container based environment

Open jonico opened this issue 2 years ago • 0 comments

Problem description

By default, pscale connect binds to 127.0.0.1:

# starting up docker container
docker run -e PLANETSCALE_SERVICE_TOKEN -e PLANETSCALE_SERVICE_TOKEN_NAME -e $PLANETSCALE_ORG -e $PLANETSCALE_BRANCH -e $PLANETSCALE_DB --rm -it  -p 3306:3306  planetscale/pscale:latest connect $PLANETSCALE_DB $PLANETSCALE_BRANCH --org $PLANETSCALE_ORG
Secure connection to database brandnewdb and branch mybranch is established!.

Local address to connect your application: 127.0.0.1:3306 (press ctrl-c to quit)

If pscale was running in a Docker container, the loopback device connected to 127.0.0.1 will not accept any connections from outside the container and attempts to do so will result in an error message:

localcomputer# mysql -h 127.0.0.1
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

Posible workaround

pscale has a --host parameter to bind to a different interface, but this would require to know the host IP in advance before running the docker run command. A workaround would be to use a different entry point for the docker image and find out the host ip programmatically:

docker run -e PLANETSCALE_SERVICE_TOKEN=$PLANETSCALE_SERVICE_TOKEN -e PLANETSCALE_SERVICE_TOKEN_NAME -e PLANETSCALE_BRANCH -e PLANETSCALE_ORG -e PLANETSCALE_DB --rm -it -p 3306:3306/tcp --entrypoint '/bin/sh' planetscale/pscale:latest -c 'pscale connect --host `hostname -i | awk "{print $1}"` $PLANETSCALE_DB $PLANETSCALE_BRANCH --org $PLANETSCALE_ORG'

Secure connection to database brandnewdb and branch mybranch is established!.

Local address to connect your application: 172.17.0.2:3306 (press ctrl-c to quit)

In that case, connections from outside the container work:

mysql -h 127.0.0.1 -P 3306
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3277
Server version: 8.0.23-vitess 
...

The same approach (i.e. use a different Docker entry point) could be used for docker-compose or a Kubernetes based example, as shown in this gist.

However, without changing the entrypoint, it does seem very hard to use pscale connect in a container based environment.

Possible solutions

a) document workaround b) introduce an option for pscale connect like --network-interface where you can specify something like eth0 to use that device instead of the loopback device (lo) c) introduce an option like --bind-to-hostname to bind against the first ip returned by hostname -i d) your ideas here

jonico avatar Nov 08 '21 17:11 jonico