简体   繁体   English

来自 Docker 容器的 SSH 隧道

[英]SSH Tunnel from Docker Container

I need to SSH Tunnel into my Universities network to work on a project through a MySQL database (as opposed to physically being at school and using the network).我需要通过 SSH 隧道连接到我的大学网络,才能通过 MySQL 数据库开展项目(而不是实际在学校和使用网络)。

I can access the server with the following instructions (or via VSCode remote explorer):我可以使用以下说明(或通过 VSCode 远程资源管理器)访问服务器:

ssh -L 4444:abc.university.ca:443 login.university.ca

ssh -p 4444 'your_username'@localhost

and am them prompted to enter my password.并提示他们输入我的密码。

I can also connect to the database from MySQL Workbench SSH Tunnel.我还可以从 MySQL Workbench SSH Tunnel 连接到数据库。

However I want to develop locally on my machine.但是我想在我的机器上本地开发。

I have set up a Docker Container with an Apache PHP server so I don't have to manually copy my files to the server via the terminal.我已经使用 Apache PHP 服务器设置了一个 Docker 容器,因此我不必通过终端手动将我的文件复制到服务器。

Dockerfile文件

FROM php:7.2-apache
# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
#get mysqli
RUN docker-php-ext-install mysqli
COPY src/ /var/www/html/

docker-compose.yml docker-compose.yml

version: "3.2"
services:
    web:
        build: php-apache
        container_name: php-apache-server
        ports:
          - "8080:80"

I am then trying to connect with the Database via connection.php然后我尝试通过 connection.php 与数据库连接

<?php 
/* DB Credentials */
$DB_SERVER = 'xxxxxxxxxxxx';
$DB_USERNAME = 'xxxxxxxxxx';
$DB_PASSWORD = 'xxxxxxxx';
$DB_NAME = 'xxxxxxxxx';
$PORT = xxxx;

/* Attempt to connect to MySQL database */
$link = mysqli_connect($DB_SERVER, $DB_USERNAME, $DB_PASSWORD, $DB_NAME);

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}
?>

My problem is when I establish the SSH tunnel via the terminal, my application within the Docker container can not connect to the MySQL database.我的问题是当我通过终端建立 SSH 隧道时,我在 Docker 容器中的应用程序无法连接到 MySQL 数据库。 I am getting:我正进入(状态:

PHP Warning:  mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /var/www/html/connection.php on line 11

My question is, how can I open the SSH tunnel from within my container, so my local apache-php server can connect to the database which is on my school's network?我的问题是,如何从我的容器内打开 SSH 隧道,以便我的本地 apache-php 服务器可以连接到我学校网络上的数据库?

Well, I think that you would probably prefer to use VPN, versus an "SSH tunnel," but in any case the network-connection is going to be environmental: that is to say, "specific to the host environment, not to the Docker illusion."好吧,我认为您可能更喜欢使用VPN,而不是“SSH 隧道”,但无论如何,网络连接都将是环境的:也就是说,“特定于主机环境,而不是 Docker错觉。”

If the tunnel exposes a specific known "host IP address," then the Docker bridge-network will be able to reach it.如果隧道公开了一个特定的已知“主机 IP 地址”,那么 Docker 桥接网络将能够访问它。

To help with multiple hops and forwarding, in your .ssh/config为了帮助多跳和转发,在你的 .ssh/config

host uni
   hostname login.university.ca
   user  some_user

host abc
   hostname abc.university.ca
   port 443
   ProxyJump uni
   LocalForward localhost:3306 /var/lib/mysql/mysql.sock

Then $DB_SERVER=127.0.0.1 ( localhost means local unix socket to mysql) to port 3306 from your docker container to utilize the local forwards to the remote server.然后$DB_SERVER=127.0.0.1localhost表示本地 unix socket 到 mysql)从你的 docker 容器到端口 3306 以利用本地转发到远程服务器。

Other config tips for .ssh/config for less authenticating: .ssh/config 的其他配置提示以减少身份验证:

ControlMaster auto
ControlPath ~/.ssh/control-%r@%h:%p
ControlPersist 600
ServerAliveInterval 39

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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