Skip to content

Commit 98af709

Browse files
committed
Add first docker-db-tunnel version
1 parent 51286e5 commit 98af709

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

.env.dist

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DB_TUNNEL_NETWORK=db-tunnel-network
2+
DB_TUNNEL_CONTAINER_NAME=db-tunnel-sshd
3+
DB_TUNNEL_CONTAINER_PORT=22666
4+
DB_CONTAINER_NAME_PATTERN="mariadb|mysql"

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
![latest 0.1.0](https://img.shields.io/badge/latest-0.1.0-green.svg?style=flat)
2+
[![license](https://img.shields.io/github/license/webcore/snitcher.svg?maxAge=2592000)](https://opensource.org/licenses/MIT)
3+
4+
# Docker DB tunnel
5+
6+
Simple tool to avoid an exposed container port conflicts when using databases in multiple docker projects.
7+
Main idea is to use the ssh tunnel and connect all databases to one network with sshd container.
8+
9+
10+
Inspired by [nginx-proxy](https://github.com/jwilder/nginx-proxy) Automated Nginx Reverse Proxy for Docker.
11+
It uses [sickp/alpine-sshd](https://hub.docker.com/r/sickp/alpine-sshd/) A lightweight OpenSSH Docker Image built atop Alpine..
12+
13+
## Install
14+
15+
Download
16+
17+
```shell
18+
git clone https://github.com/speto/docker-db-tunnel
19+
```
20+
21+
## Usage
22+
23+
Run shell script to create network, sshd container and connect all db containers:
24+
25+
```shell
26+
$ ./docker-db-tunnel.sh
27+
Creating db tunnel network: db-tunnel-network
28+
668e40197c800a612ea748b9778d3f0888333673f7588d4a0bb1e027bd5d22d4
29+
Running db tunnel container with name: db-tunnel-sshd
30+
164e5a3c3b446169f928a03c135594493843664fef5ffa3edf820dd5de06f0a1
31+
Connecting symfony-demo_mariadb_1 to db-tunnel-network
32+
Connecting symfony-demo2_mariadb_1 to db-tunnel-network
33+
Connecting project_mysql_1 to db-tunnel-network
34+
```
35+
36+
### SSH Tunnel options
37+
The **root** password for SSH is "**root**". How to [change-root-password](https://github.com/sickp/docker-alpine-sshd#change-root-password).
38+
39+
### Sequel Pro connection details
40+
![Sequel Pro screenshot](./docker-db-tunnel.png)
41+
42+
## Customize
43+
44+
It is easy to extend via your own .env file:
45+
46+
```dotenv
47+
DB_TUNNEL_NETWORK=db-tunnel-network
48+
DB_TUNNEL_CONTAINER_NAME=db-tunnel-sshd
49+
DB_TUNNEL_CONTAINER_PORT=22666
50+
DB_CONTAINER_NAME_PATTERN="mariadb|mysql" #pattern for docker ps filtering
51+
```
52+
53+
### MIT license
54+
55+
Copyright (c) 2018, Štefan Peťovský

docker-db-tunnel.png

104 KB
Loading

docker-db-tunnel.sh

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env bash
2+
3+
GREEN=$(echo -en '\033[00;32m')
4+
YELLOW=$(echo -en '\033[00;33m')
5+
RESTORE=$(echo -en '\033[0m')
6+
7+
test -f .env && source .env
8+
9+
DB_TUNNEL_NETWORK=${DB_TUNNEL_NETWORK:-'db-tunnel-network'}
10+
DB_TUNNEL_CONTAINER_NAME=${DB_TUNNEL_CONTAINER_NAME:-'db-tunnel-sshd'}
11+
DB_TUNNEL_CONTAINER_PORT=${DB_TUNNEL_CONTAINER_PORT:-'22666'}
12+
DB_CONTAINER_NAME_PATTERN=${DB_CONTAINER_NAME_PATTERN:-'mariadb|mysql'}
13+
14+
docker network inspect ${DB_TUNNEL_NETWORK} &> /dev/null
15+
if [ $? -eq 1 ]
16+
then
17+
echo "Creating db tunnel network: ${GREEN}${DB_TUNNEL_NETWORK}${RESTORE}"
18+
docker network create ${DB_TUNNEL_NETWORK}
19+
fi
20+
21+
# return true/false or error if not exist
22+
IS_TUNNEL_CONTAINER_RUNNING=$(docker inspect -f "{{.State.Running}}" ${DB_TUNNEL_CONTAINER_NAME} 2> /dev/null)
23+
if [ "$IS_TUNNEL_CONTAINER_RUNNING" == "" ]; then
24+
echo "Running db tunnel container with name: ${GREEN}${DB_TUNNEL_CONTAINER_NAME}${RESTORE}"
25+
docker run -d -p ${DB_TUNNEL_CONTAINER_PORT}:22 --restart=always --name ${DB_TUNNEL_CONTAINER_NAME} --network ${DB_TUNNEL_NETWORK} sickp/alpine-sshd
26+
elif [ "$IS_TUNNEL_CONTAINER_RUNNING" == "false" ]; then
27+
echo "Starting existing db tunnel container with name: ${GREEN}${DB_TUNNEL_CONTAINER_NAME}${RESTORE}"
28+
docker start ${DB_TUNNEL_CONTAINER_NAME} 1> /dev/null
29+
fi
30+
31+
# @todo consider filter by label (e.g. label=db.network.tunnel, label=database, label=mysql)
32+
docker ps --filter "status=running" --filter "name=${DB_CONTAINER_NAME_PATTERN}" --format "{{.Names}} {{.Networks}}" \
33+
| grep -v ${DB_TUNNEL_NETWORK} \
34+
| cut -d ' ' -f 1 \
35+
| while read container_name ; do
36+
echo "Connecting ${YELLOW}${container_name}${RESTORE} to ${DB_TUNNEL_NETWORK}";
37+
docker network connect ${DB_TUNNEL_NETWORK} ${container_name};
38+
done

0 commit comments

Comments
 (0)