简体   繁体   中英

Docker: Connect via SSH to Remote MySQL server

I'm using Docker for my Gitlab CI, but need to connect to a remote MySQL server with SSH and then map port 3306 to 127.0.0.1

I use the following command to do the SSH port forward ssh -L 3306:127.0.0.1:3306 user@remoteserver

I've tried viewing the SSH keys in Docker, but can't seem to "ls" into the root/.ssh folder where I believe they're stored, but nothing seems to happen as I get this error: The command '/bin/sh -c cd root/.ssh' returned a non-zero code: 1

I have a public key that i'd like to use from the computer Docker runs on. Where do I copy the contents of this file to?

Any help with this would be great, I am currently testing this out using a dockerfile instead of triggering new pipelines on Gitlab if that makes any difference.

You can map your port with docker run command like that:

docker run -itd --name mysql -p 3306:3306 mysql

Then when your container is running do docker cp /path/to/your/public_key /path/where/you/want/to/drop/your_key and after that you can do a docker exec -it mysql bash and add your public key to your authorized key.

You should try some tutorials before trying to docker advanced things with Docker...

In order to connect to a remote server, you need to:

  • have a local private key on the client
  • authorize the client (identified by a public key) on the remote

You can therefore:

  1. generate a public/private key pair
  2. add the client to the authorized authorized_keys on the remote
  3. store the private key as CI variable in GitLab
  4. use the key in your job

I have already answered here, to a quite similar question: https://stackoverflow.com/a/42001323/5972308 , I think that corresponds to your need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM