簡體   English   中英

如何使用 kingsquare 和 docker-compose 從本地 docker 獲得 SSH 隧道到遠程數據庫工作

[英]How may I gett an SSH tunnel from local docker to remote DB working, using kingsquare and docker-compose

我的新職責是將我們的項目移植到 docker 中。 這意味着每台開發人員機器上的本地代碼以及登台服務器上的測試數據。 目前,代碼位於同一台服務器上,因此使用本地主機(127.0.0.1)連接到數據庫。 docker 當前部署並可以運行單元測試,在不需要數據庫的情況下成功。

我嘗試使用此處提供的答案: https://github.com/phpmyadmin/docker/issues/99當時失敗了,並且通過各種不同的嘗試最終導致嘗試從容器內部創建 SSH 隧道( 如何完成從本地開發 docker 到臨時數據庫的 SSH 隧道)。 我已經重新嘗試使用該服務,因為其他選項似乎更加復雜或不可靠。

我已經恢復使用允許隧道的 kingsquare 圖像,但我不知道 ${SSH_AUTH_SOCK} 是什么或如何使用它。 我試過將它指向一個 SSH 鍵,但是(可能很明顯)失敗了。

我已經包含了整個 docker-compose.yml,因為我之前沒有注意到的一個錯誤是在我現有的 docker(應用程序)中不包括網絡引用。

version: '3'
services:
    tunnels:
        image: kingsquare/tunnel
        volumes:
            - '${SSH_AUTH_SOCK}:/ssh-agent'
        command: '*:3306:localhost:3306 -vvv user@[myserver->the IP of the machine hosting the DB?] -i /.ssh/openssh_ironman_justin  -p 2302'
        networks:
            mynetwork:
                aliases:
                    - remoteserver
    app:
        build:
            context: .
            dockerfile: .docker/Dockerfile
            args:
                APP_PATH: ${APP_PATH}
        image: laravel-docker
        env_file: .env
        ports:
            - 8080:80
            # We need to expose 443 port for SSL certification.
            - "443:443"
        volumes:
            - .:/var/www/jumbledown
        networks:
            - mynetwork
networks:
    mynetwork:
        driver: bridge

在 .env 文件中,每個開發人員都有以下內容,一旦 SSH 隧道完成,我需要對其進行更改,以便它使用隧道-DB 組合:

DB_HOST=127.0.0.1 # As per answer, this will change to the IP address of the server containing the database.  I'll leave the current localhost reference rather than displaying the IP address of the machine.
DB_PORT=3306
DB_DATABASE=[central database or sharded version for testing data changes]
DB_USERNAME=[username]
DB_PASSWORD=[password]

我希望能夠讓應用程序容器中的代碼能夠使用遠程服務器上的數據庫,同時盡可能減少部署后的復雜性。

更新我解決了一個端口問題。

如果我使用命令更新 2.5 command: '*:3306:localhost:3306 -vvv [username]@[IP of DB host] -i [location on my PC of key file]/openssh_dev -p 2302'然后它確實建立了連接但它被拒絕了:

tunnels_1  | debug1: Trying private key: /.ssh/openssh_ironman_justin
tunnels_1  | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
tunnels_1  | @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
tunnels_1  | @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
tunnels_1  | Permissions 0755 for '/.ssh/openssh_dev ' are too open.
tunnels_1  | It is required that your private key files are NOT accessible by others.
tunnels_1  | This private key will be ignored.

但是如何更改已掛載文件的權限? 可以通過 Dockerfile 完成,還是必須在開始之前已經存在?

但是如何更改已掛載文件的權限? 可以通過 Dockerfile 完成,還是必須在開始之前已經存在?

Dockerfile 用於創建圖像。 基於該映像的容器從您的主機安裝目錄並維護相同的主機權限。

您可以更改主機上文件的權限,Docker 將在容器中使用相同的權限。

對於您的 docker 容器127.0.0.1是它的本地主機。 要訪問主機,您需要將主機更改為0.0.0.0 另一方面,如果您想連接到遠程主機,那么它將是your-host-ip-or-domain.com

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM