docker icon indicating copy to clipboard operation
docker copied to clipboard

Connect to remote database using SSH Tunnel

Open valkirilov opened this issue 9 years ago • 7 comments

I wonder how to connect to remote DB for which I am using SSH Tunnel. When I have configured phpMyAdmin native on my system I am using the following config:

$i++;
$cfg['Servers'][$i]['verbose']       = 'Remote Server 1';// Change this to whatever you like.
$cfg['Servers'][$i]['host']          = '127.0.0.1';
$cfg['Servers'][$i]['port']          = '3307';
$cfg['Servers'][$i]['connect_type']  = 'tcp';
$cfg['Servers'][$i]['extension']     = 'mysqli';
$cfg['Servers'][$i]['compress']      = FALSE;
$cfg['Servers'][$i]['auth_type']     = 'cookie';

Then I open my SSH tunnel with the following command: ssh -L 3307:localhost:3306 user@server

But now, when I use a docker container I am not sure how to achieve my goal. I think that I should open the tunnel in the container or link the tunnel from my env to the container. Any ideas are welcome?

valkirilov avatar Dec 13 '16 09:12 valkirilov

Opening the tunnel in the container certainly sounds like a good approach. You can also make the ssh listed on the docker interface and let phpMyAdmin connect to it. It can be something like (depends on your Docker networking setup): ssh -L 172.17.0.1:3307:localhost:3306 user@server

Anyway if you figure out best approach, it would be great if you could contribute this to our documentation as I think more people will want such setup.

nijel avatar Dec 13 '16 10:12 nijel

Any luck with that?

nijel avatar Feb 06 '17 12:02 nijel

I couldn't achieve my goal because I realized that I should bind the SSH tunnel to the databse port but there aren't installed any MysQL packages (for example) in the container. Someone can try to link the phpmyadmin container with a database and share the results.

valkirilov avatar Feb 06 '17 19:02 valkirilov

Maybe it's better to use something like https://hub.docker.com/r/kingsquare/tunnel/ for that. It even includes examples for tunneling MySQL.

nijel avatar Feb 07 '17 09:02 nijel

Hi ! I know this issue is quite old but this might be useful in the future :

Here's my working phpMyAdmin + autossh tunnel Docker stack using kingsquare's tunnel image (thanks @nijel !) in order to allow phpmyadmin to connect to a remote server ('myserver' in this example) via ssh. It forwards the 3306, 3307 and 3308 ports but you can keep the 3306 forward only if you want (it won't conflict with the host 3306 port since all forwardings are kept inside the containers).

docker-compose.yml :

version: '3'
services:
  tunnels:
    image: kingsquare/tunnel
    volumes:
      - '${SSH_AUTH_SOCK}:/ssh-agent'
    command: '*:3306:localhost:3306 -L *:3307:localhost:3307 -L *:3308:localhost:3308 -vvv user@myserver'
    networks:
      mynetwork:
        aliases:
          - remoteserver
  pma:
    image: phpmyadmin/phpmyadmin
    ports:
      - '8080:80'
    volumes:
      - /sessions
      - './config.user.inc.php:/etc/phpmyadmin/config.user.inc.php'
    depends_on:
      - tunnels
    networks:
      - mynetwork
networks:
  mynetwork:
    driver: bridge

config.user.inc.php (server part only) :

/**
 * First port: 3306
 */
$i++;
$cfg['Servers'][$i]['verbose'] = 'Remote server 3306';
$cfg['Servers'][$i]['host'] = 'remoteserver';
$cfg['Servers'][$i]['port'] = '3306';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['AllowNoPassword'] = false;

/**
 * Second port: 3307
 */
$i++;
$cfg['Servers'][$i]['verbose'] = 'Remote server 3307';
$cfg['Servers'][$i]['host'] = 'remoteserver';
$cfg['Servers'][$i]['port'] = '3307';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['AllowNoPassword'] = false;

/**
 * Third port: 3308
 */
$i++;
$cfg['Servers'][$i]['verbose'] = 'Remote server 3308';
$cfg['Servers'][$i]['host'] = 'remoteserver';
$cfg['Servers'][$i]['port'] = '3308';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['extension'] = 'mysqli';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['AllowNoPassword'] = false;

theblackhole avatar Aug 30 '18 09:08 theblackhole

Just to add my experience, we've been using kingsquare/tunnel to handle connections from our dev environment to various database and other services very successfully for over a year now, the method recommended by @theblackhole works very well for us.

yaffol avatar Nov 12 '18 11:11 yaffol

:ping_pong: can I close this issue ?

williamdes avatar Dec 23 '19 13:12 williamdes