title: Programatically create SSH tunnel inside of dockerized apache airflow python operator
body: <p>My program is unable to create an SSH tunnel while inside of my docker container running apache airflow. Only running the function on my local machine works fine. I have a list of servers which I use to create a tunnel, query the database, and close the connection. Normally, I'd do it the following way:</p>
  
  <pre><code>for server in servers:
      server_conn = sshtunnel.SSHTunnelForwarder(
          server,
          ssh_username=ssh_user,
          ssh_password=ssh_password,
          remote_bind_address=(localhost, db_port),
          local_bind_address=(localhost, localport)
      )
  </code></pre>
  
  <p>This works as expected and I can do whatever I need from there. However, within Docker, it does not work. I realize that docker runs and binds to a port and is not actually apart of the host system, so I used <code>network_mode="host"</code> to help mitigate this issue. However, this does not work because my containers lose the ability to communicate with one another. Here is my docker-compose file</p>
  
  <pre><code>    postgres:
          image: postgres:9.6
          environment:
              - POSTGRES_USER=airflow
              - POSTGRES_PASSWORD=airflow
              - POSTGRES_DB=airflow
              - PGDATA=/var/lib/postgresql/data/pgdata
          volumes:
              - ~/.whale/pgdata:/var/lib/postgresql/data/pgdata
              - ./dags/docker/sql/create.sql:/docker-entrypoint-initdb.d/init.sql
          ports:
            - "5432:5432"
  
      webserver:
          image: hawk
          build:
              context: .
              dockerfile: ./dags/docker/Dockerfile-airflow
          restart: always
          depends_on:
              - postgres
              # - redis
          environment:
              - LOAD_EX=n
              - FERNET_KEY=46BKJoQYlPPOexq0OhDZnIlNepKFf87WFwLbfzqDDho=
              - EXECUTOR=Local
          volumes:
              - ./dags:/usr/local/airflow/dags
              # Uncomment to include custom plugins
              # - ./plugins:/usr/local/airflow/plugins
          ports:
              - "8080:8080"
              - "52023:22"
          command: webserver
          healthcheck:
              test: ["CMD-SHELL", "[ -f /usr/local/airflow/airflow-webserver.pid ]"]
              interval: 30s
              timeout: 30s
              retries: 3
  
  </code></pre>
  
  <p>I also followed the instructions <a href="https://docs.docker.com/engine/examples/running_ssh_service/" rel="nofollow noreferrer">here</a> and got to the point where I can <code>docker exec</code> into my container and manually type the above python snippet and get a working connection.
  Additionally, I have read the airflow documentation <a href="https://airflow.readthedocs.io/en/latest/_modules/airflow/contrib/operators/ssh_operator.html" rel="nofollow noreferrer">here</a> which covers SSH connection operators, but those only support bash commands, I will need my python function to run. I am truly confused why the python code would work while <code>exec</code>-ed into the system, but not when I run it via my airflow DAG. At this time, I am unable to manually put all of the connections in because there will be > 100 once this system deploys. Any help would be greatly appreciated. If more depth is needed, please let me know.</p>