C
Coder.comโ€ข5d ago
Xan

SSH/Tunnel to database fails

Did something change recently with either port forwarding / tunneling, or anything that would prevent connecting to a database inside of Coder with TablePlus or SQL Pro Studio? From what I can tell we haven't changed anything in a while, but suddenly none of our connections to Coder work in any desktop database tool. They all got a little wonky when Coder felt like breaking all the ~/.ssh/config stuff but eventually we got around that... now it just fails for us again. It's weird, we can open the port using coder port-forward $1 --tcp 2222:22 which allows things like Tinkerwell to connect to Coder still, but every database tool fails. Just making sure nothing surprise changed with Coder in the last couple weeks again to try to eliminate one area of debugging lol
Solution:
I think its something to do with our mysql being only accessible through mysql container name and not 127.0.0.1 inside the image. I ended up fixing connecting our tools to it with a terminal function ```shell function tunnel-sql() {...
Jump to solution
4 Replies
Codercord
Codercordโ€ข5d ago
Codercord
Codercordโ€ข5d ago
What are you creating this issue for?
Solution
Xan
Xanโ€ข5d ago
I think its something to do with our mysql being only accessible through mysql container name and not 127.0.0.1 inside the image. I ended up fixing connecting our tools to it with a terminal function
function tunnel-sql() {
local PROJECT="$1"
local LOCAL_PORT=3306
local REMOTE_USER="user"
local REMOTE_HOST="127.0.0.1"
local REMOTE_PORT="2222"

if [[ -z "$PROJECT" ]]; then
echo "Usage: tunnel-mysql <coder_project_name>"
return 1
fi

echo "๐Ÿ”Œ Starting coder tunnel for project: $PROJECT"
tunnel "$PROJECT" &
local TUNNEL_PID=$!

echo "โณ Waiting for tunnel to initialize..."
sleep 4

echo "๐Ÿ” Fetching MySQL container IP..."
local CONTAINER_IP
CONTAINER_IP=$(ssh -p $REMOTE_PORT "$REMOTE_USER@$REMOTE_HOST" "getent hosts mysql | awk '{ print \$1 }'")

if [[ -z "$CONTAINER_IP" ]]; then
echo "โŒ Failed to get MySQL container IP"
kill $TUNNEL_PID
return 1
fi

echo "๐Ÿ“ก MySQL container IP: $CONTAINER_IP"
echo "๐Ÿš€ Starting SSH tunnel: localhost:$LOCAL_PORT โžœ $CONTAINER_IP:3306"
ssh -L "$LOCAL_PORT:$CONTAINER_IP:3306" "$REMOTE_USER@$REMOTE_HOST" -p "$REMOTE_PORT"

echo "๐Ÿงน Cleaning up coder tunnel (PID $TUNNEL_PID)"
kill $TUNNEL_PID 2>/dev/null
}
function tunnel-sql() {
local PROJECT="$1"
local LOCAL_PORT=3306
local REMOTE_USER="user"
local REMOTE_HOST="127.0.0.1"
local REMOTE_PORT="2222"

if [[ -z "$PROJECT" ]]; then
echo "Usage: tunnel-mysql <coder_project_name>"
return 1
fi

echo "๐Ÿ”Œ Starting coder tunnel for project: $PROJECT"
tunnel "$PROJECT" &
local TUNNEL_PID=$!

echo "โณ Waiting for tunnel to initialize..."
sleep 4

echo "๐Ÿ” Fetching MySQL container IP..."
local CONTAINER_IP
CONTAINER_IP=$(ssh -p $REMOTE_PORT "$REMOTE_USER@$REMOTE_HOST" "getent hosts mysql | awk '{ print \$1 }'")

if [[ -z "$CONTAINER_IP" ]]; then
echo "โŒ Failed to get MySQL container IP"
kill $TUNNEL_PID
return 1
fi

echo "๐Ÿ“ก MySQL container IP: $CONTAINER_IP"
echo "๐Ÿš€ Starting SSH tunnel: localhost:$LOCAL_PORT โžœ $CONTAINER_IP:3306"
ssh -L "$LOCAL_PORT:$CONTAINER_IP:3306" "$REMOTE_USER@$REMOTE_HOST" -p "$REMOTE_PORT"

echo "๐Ÿงน Cleaning up coder tunnel (PID $TUNNEL_PID)"
kill $TUNNEL_PID 2>/dev/null
}
This makes it so things work a little more like they used to
Phorcys
Phorcysโ€ข3d ago
ah yeah that totally makes sense so, either you want to run MySQL inside the main container (either via Docker in Docker or just plain MySQL) or, you can use the network_mode with a container: type so both containers share the same localhost, and therefore ports are available as if it were local (see https://discord.com/channels/747933592273027093/1288479155976933478/1298640103555600384) for an example (another thing you could do is deploy an agent in your MySQL container and connect through that, but that's not great)

Did you find this page helpful?