Code Monkey home page Code Monkey logo

docker-ssh-tunnel's Introduction

Docker SSH Tunnel

This Docker creates a simple SSH tunnel to a remote server.

Usage

Docker

  1. First you should create a config file in your local directory. For simplicity you can create this file in ~/.ssh in your local machine.

  2. Inside ~/.ssh/config put these lines:

    Host mysql-tunnel # You can use any name
            HostName ssh-tunnel.corporate.tld # Tunnel 
            IdentityFile ~/.ssh/id_rsa # Private key location
            User cagatay.guertuerk # Username to connect to SSH service
            ForwardAgent yes
            TCPKeepAlive yes
            ConnectTimeout 5
            ServerAliveCountMax 10
            ServerAliveInterval 15
  1. Don't forget to put your private key (id_rsa) to ~/.ssh folder.

  2. Now in docker-compose.yml you can define the tunnel as follows:

    version: '2'
    services:
      mysql:
        image: cagataygurturk/docker-ssh-tunnel
        volumes:
          - $HOME/.ssh:/root/ssh:ro
        environment:
	  SSH_DEBUG: "-v"
          TUNNEL_HOST: mysql-tunnel
          REMOTE_HOST: tunneled-sql.corporate.internal.tld
          LOCAL_PORT: 3306
          REMOTE_PORT: 3306
  1. Run docker-compose up -d

After you start up docker containers, any container in the same container network will be able to access to tunneled mysql instance using tcp://mysql:3306. Of course you can also expose port 3306 to be able to access to tunneled resource from your host machine.

Kubernetes

It is perfectly possible to use this container in Kubernetes and actually the sidecar pattern is very suitable for Kubernetes. If your application requires connecting to a remote resource through a SSH tunnel, you can place this container as a sidecar container to your application and let your application connect to this resource securely.

In the example below, our application (container named "mariadb") is connecting to a remote MariaDB instance through SSH tunnel.

For that, first create the SSH key as a secret:

$ kubectl create secret generic ssh-key-secret --from-file=ssh-privatekey=$PATH_TO_SSH_KEY`

Later use this Kubernetes manifest:

apiVersion: v1
kind: ConfigMap
metadata:
  name: ssh-config
data:
  config: |
    Host mysql-tunnel # You can use any name
            HostName tunneled-sql.corporate.internal.tld # Tunnel 
            IdentityFile ~/.ssh/id_rsa # Private key location
            User root # Username to connect to SSH service
            ForwardAgent yes
            TCPKeepAlive yes
            ConnectTimeout 5
            ServerAliveCountMax 10
            ServerAliveInterval 15
---
apiVersion: batch/v1
kind: Job
metadata:
  name: mysql-tunnel
spec:
  template:
    spec:
      containers:
        - name: docker-ssh-tunnel
          image: cagataygurturk/docker-ssh-tunnel
          env:
            - name: SSH_DEBUG
              value: "-v"
            - name: TUNNEL_HOST
              value: mysql-tunnel
            - name: REMOTE_HOST
              value: tunneled-sql.corporate.internal.tld
            - name: LOCAL_PORT
              value: "3306"
            - name: REMOTE_PORT
              value: "3306"
          volumeMounts:
            - name: config-volume
              readOnly: true
              mountPath: /root/ssh/config
              subPath: config
            - name: secret-volume
              readOnly: true
              mountPath: /root/ssh/id_rsa
              subPath: ssh-privatekey
        - name: mariadb
          image: mariadb:10.2
          command:
            - mysql
            - -h
            - 127.0.0.1
            - -P
            - "3306"
            - -uUSERNAME
            - -pPASSWORD
            - -e SHOW databases;
      volumes:
        - name: config-volume
          configMap:
            name: ssh-config
        - name: secret-volume
          secret:
            secretName: ssh-key-secret
      restartPolicy: OnFailure
  backoffLimit: 4

docker-ssh-tunnel's People

Contributors

cagataygurturk avatar ilyaguy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

docker-ssh-tunnel's Issues

Shared volume doesn't exist when docker runs ssh command

The container is missing the shared volume /root/.ssh when it runs the ssh command.

This results in an error message: Bad owner or permissions on /root/.ssh/config

Did you ever get this to work? I'm looking into a similar setup.

Handling Passcodes?

Thank you so much for this idea. I had exactly this problem and this works great for me.

I told the rest of the team about it, and some users had issues.
The problem was ... when they have a passcode on their id_rsa the service stalls. In the logs you can see it is stuck failing to get the passcode from tty.

Locally using ssh to talk to the server the users don't need to enter a passcode, so it must be in the local keychain, but not visible to the docker container.

Any thoughts on how to solve this?
Cheers
Nigel

Make verbose logging optional

This image is super useful, but I find the massive amount of logs generated by ssh due to the -vv parameter to be quite annoying. Would it be possible to provide an environment variable to enable debugging? Something like DEBUG=1 to enable adding the parameter, or maybe just SSH_PARAMS=-vv to pass custom parameters to the command.

".ssh/ssh_auth_sock" no such file or directory

Following the guide used to work ok, but recently I started getting an error about ssh_auth_sock not being found.
Maybe its a recent ssh change, but it seems like ~/.ssh/ssh_auth_sock is now a symlink to a temp directory, which isn't resolved since its not mapped into the volume.

I don't know if it's the correct solution, but I was able to fix it by using the SSH_AUTH_SOCK variable to mount the temp file instead of the symlink, as follows:

    image: cagataygurturk/docker-ssh-tunnel
    volumes:
      - $HOME/.ssh:/root/ssh:ro
      - $SSH_AUTH_SOCK:/root/ssh/ssh_auth_sock
 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.