Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save majidalaeinia/11affa239ae0d16005e04b0293b14a57 to your computer and use it in GitHub Desktop.

Select an option

Save majidalaeinia/11affa239ae0d16005e04b0293b14a57 to your computer and use it in GitHub Desktop.
Connect to Remote Database from Dockerized Localhost

Suppose you have such ssh config:

Host folan_server
  Port 22
  User root
  ForwardAgent yes
  ProxyCommand ssh -A -p 22 aaa@x.x.x.x -W y.y.y.y:22

You should do this on your terminal:

ssh -N -L 5959:127.0.0.1:3306 folan_server

On your GUI that you want to see the remote database (e.g. database in Intellij products):

Host: 127.0.0.1
Port: 5959
User: {{Remote Database User}}
Password: {{Remote Database Password}}
Database: {{Remote Database Name}}

Then connect to the database as usual.

On the docker-compose file of the APP you want to connect to the database, there must be something like this:

extra_hosts:
  - 'host.docker.internal:host-gateway'

Then on the .env file of your project, you should have this:

DB_USER='{{Remote Database User}}'
DB_PW='{{Remote Database Password}}'
DB_HOST='host.docker.internal'
DB_NAME='{{Remote Database Name}}'
DB_PORT='5959'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment