Skip to content

Avoid an exposed container port conflicts when using databases in multiple docker projects

License

Notifications You must be signed in to change notification settings

PlatoCreative/docker-db-tunnel

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

latest master GitHub tags GitHub releases GitHub stars GitHub license Twitter

Docker DB tunnel

Simple tool to avoid an exposed container port conflicts when using databases in multiple docker projects. Main idea is to use the ssh tunnel and connect all databases to one network with sshd container.

Inspired by nginx-proxy Automated Nginx Reverse Proxy for Docker.
It uses sickp/alpine-sshd A lightweight OpenSSH Docker Image built atop Alpine..

Install

Download via

git clone https://github.com/speto/docker-db-tunnel

Usage

Start some example dummy mariadb containers from multiple compose files.
More details in ./example/ folder or next sectiion about docker-compose services example:

$ cd ./example
$ docker-compose up -d
Creating network "docker-db-tunnel-example_default" with the default driver
Creating symfony-demo_mariadb_1  ... done
Creating symfony-demo2_mariadb_1 ... done
Creating project_mysql_1         ... done

Run ./docker-db-tunnel.sh shell script to create sshd container, tunnel network and connect sshd and all (database) containers with name matching mariadb|mysql pattern (customizable by DB_CONTAINER_NAME_PATTERN):

$ ./docker-db-tunnel.sh
Running db tunnel container db-tunnel-sshd on port 22666
668e40197c800a612ea748b9778d3f0888333673f7588d4a0bb1e027bd5d22d4
Creating db tunnel network: db-tunnel-network
164e5a3c3b446169f928a03c135594493843664fef5ffa3edf820dd5de06f0a1
Connecting db-tunnel-sshd to db-tunnel-network
Connecting symfony-demo_mariadb_1 to db-tunnel-network
Connecting symfony-demo2_mariadb_1 to db-tunnel-network
Connecting project_mysql_1 to db-tunnel-network with hostname (alias) project_db_host

Docker-compose services example

Uses bianjp/mariadb-alpine Lightweight MariaDB docker image based on Alpine Linux.
Fully compatible with official MariaDB image

./example/docker-compose-project.yml

services:
  project_mysql:
    container_name: project_mysql_1
    image: bianjp/mariadb-alpine:latest
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
    labels:
      - db.network.tunnel.hostname=project_db_host
  • You can set the stable hostname via db.network.tunnel.hostname container label which is later used as the network connection alias

./example/docker-compose-symfony-demo.yml

services:
  symfony-demo_mariadb:
    container_name: symfony-demo_mariadb_1
    image: bianjp/mariadb-alpine:latest
    environment:
    - MYSQL_ALLOW_EMPTY_PASSWORD=yes

SSH Tunnel settings

The root password for SSH in sickp/alpine-sshd is "root".
You can also change default root password.

Database connection settings example

MySQL host: project_mysql_1
# or stable hostname via container label and network connection alias:  
MySQL host: project_db_host
Username: root
Port: 3306

SSH Host: 127.0.0.1
SSH User: root
SSH Password: root
SSH Port: 22666

Sequel Pro screenshot

Customize

It is easy to extend via your own .env file:

DB_TUNNEL_NETWORK=db-tunnel-network
DB_TUNNEL_NETWORK_HOSTNAME_LABEL=db.network.tunnel.hostname
DB_TUNNEL_CONTAINER_NAME=db-tunnel-sshd
DB_TUNNEL_CONTAINER_PORT=22666
DB_CONTAINER_NAME_PATTERN="mariadb|mysql" #pattern for docker ps filtering

MIT license

Copyright (c) 2018, Štefan Peťovský

About

Avoid an exposed container port conflicts when using databases in multiple docker projects

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Shell 100.0%