Skip to content

Commit ebf3880

Browse files
committed
add option for custom hostname via label + update examples
1 parent 98af709 commit ebf3880

6 files changed

+93
-12
lines changed

.env.dist

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
DB_TUNNEL_NETWORK=db-tunnel-network
2+
DB_TUNNEL_NETWORK_HOSTNAME_LABEL=db.network.tunnel.hostname
23
DB_TUNNEL_CONTAINER_NAME=db-tunnel-sshd
34
DB_TUNNEL_CONTAINER_PORT=22666
45
DB_CONTAINER_NAME_PATTERN="mariadb|mysql"

README.md

+48-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![latest 0.1.0](https://img.shields.io/badge/latest-0.1.0-green.svg?style=flat)
1+
![latest 0.2.0](https://img.shields.io/badge/latest-0.2.0-green.svg?style=flat)
22
[![license](https://img.shields.io/github/license/webcore/snitcher.svg?maxAge=2592000)](https://opensource.org/licenses/MIT)
33

44
# Docker DB tunnel
@@ -26,17 +26,58 @@ Run shell script to create network, sshd container and connect all db containers
2626
$ ./docker-db-tunnel.sh
2727
Creating db tunnel network: db-tunnel-network
2828
668e40197c800a612ea748b9778d3f0888333673f7588d4a0bb1e027bd5d22d4
29-
Running db tunnel container with name: db-tunnel-sshd
29+
Running db tunnel container db-tunnel-sshd on port 22666
3030
164e5a3c3b446169f928a03c135594493843664fef5ffa3edf820dd5de06f0a1
3131
Connecting symfony-demo_mariadb_1 to db-tunnel-network
3232
Connecting symfony-demo2_mariadb_1 to db-tunnel-network
33-
Connecting project_mysql_1 to db-tunnel-network
33+
Connecting project_mysql_1 to db-tunnel-network with hostname (alias) project_db_host
3434
```
3535

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).
36+
### Docker-compose services example
37+
38+
docker-compose-project.yml
39+
```yaml
40+
services:
41+
project_mysql:
42+
container_name: project_mysql_1
43+
image: mariadb
44+
environment:
45+
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
46+
labels:
47+
- db.network.tunnel.hostname=project_db_host
48+
network_mode: "bridge"
49+
```
50+
51+
docker-compose-symfony-demo.yml
52+
```yaml
53+
services:
54+
symfony-demo_mariadb:
55+
container_name: symfony-demo_mariadb_1
56+
image: mariadb
57+
environment:
58+
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
59+
network_mode: "bridge"
60+
```
61+
62+
### SSH Tunnel settings
63+
The **root** password for SSH is in `sickp/alpine-sshd` "**root**".
64+
You can also [change default root password](https://github.com/sickp/docker-alpine-sshd#change-root-password).
65+
66+
### Connection settings example
67+
68+
```
69+
MySQL host: project_mysql_1
70+
# or via alias
71+
# MySQL host: project_db_host
72+
Username: root
73+
Port: 3306
74+
75+
SSH Host: 127.0.0.1
76+
SSH User: root
77+
SSH Password: root
78+
SSH Port: 22666
79+
```
3880
39-
### Sequel Pro connection details
4081
![Sequel Pro screenshot](./docker-db-tunnel.png)
4182
4283
## Customize
@@ -45,6 +86,7 @@ It is easy to extend via your own .env file:
4586
4687
```dotenv
4788
DB_TUNNEL_NETWORK=db-tunnel-network
89+
DB_TUNNEL_NETWORK_HOSTNAME_LABEL=db.network.tunnel.hostname
4890
DB_TUNNEL_CONTAINER_NAME=db-tunnel-sshd
4991
DB_TUNNEL_CONTAINER_PORT=22666
5092
DB_CONTAINER_NAME_PATTERN="mariadb|mysql" #pattern for docker ps filtering

docker-db-tunnel.sh

+12-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ RESTORE=$(echo -en '\033[0m')
77
test -f .env && source .env
88

99
DB_TUNNEL_NETWORK=${DB_TUNNEL_NETWORK:-'db-tunnel-network'}
10+
DB_TUNNEL_NETWORK_HOSTNAME_LABEL=${DB_TUNNEL_NETWORK_HOSTNAME_LABEL:-'db.network.tunnel.hostname'}
1011
DB_TUNNEL_CONTAINER_NAME=${DB_TUNNEL_CONTAINER_NAME:-'db-tunnel-sshd'}
1112
DB_TUNNEL_CONTAINER_PORT=${DB_TUNNEL_CONTAINER_PORT:-'22666'}
1213
DB_CONTAINER_NAME_PATTERN=${DB_CONTAINER_NAME_PATTERN:-'mariadb|mysql'}
@@ -21,18 +22,23 @@ fi
2122
# return true/false or error if not exist
2223
IS_TUNNEL_CONTAINER_RUNNING=$(docker inspect -f "{{.State.Running}}" ${DB_TUNNEL_CONTAINER_NAME} 2> /dev/null)
2324
if [ "$IS_TUNNEL_CONTAINER_RUNNING" == "" ]; then
24-
echo "Running db tunnel container with name: ${GREEN}${DB_TUNNEL_CONTAINER_NAME}${RESTORE}"
25+
echo "Running db tunnel container ${GREEN}${DB_TUNNEL_CONTAINER_NAME}${RESTORE} on port ${GREEN}${DB_TUNNEL_CONTAINER_PORT}${RESTORE}"
2526
docker run -d -p ${DB_TUNNEL_CONTAINER_PORT}:22 --restart=always --name ${DB_TUNNEL_CONTAINER_NAME} --network ${DB_TUNNEL_NETWORK} sickp/alpine-sshd
2627
elif [ "$IS_TUNNEL_CONTAINER_RUNNING" == "false" ]; then
2728
echo "Starting existing db tunnel container with name: ${GREEN}${DB_TUNNEL_CONTAINER_NAME}${RESTORE}"
2829
docker start ${DB_TUNNEL_CONTAINER_NAME} 1> /dev/null
2930
fi
3031

3132
# @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+
docker ps --filter "status=running" --filter "name=${DB_CONTAINER_NAME_PATTERN}" --format '{{.Names}} {{.Networks}} {{.Label "'${DB_TUNNEL_NETWORK_HOSTNAME_LABEL}'"}}' \
3334
| 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};
35+
| cut -d ' ' -f 1,3 \
36+
| while read container_name host_name; do
37+
if [ "$host_name" == "" ]; then
38+
echo "Connecting ${YELLOW}${container_name}${RESTORE} to ${DB_TUNNEL_NETWORK}";
39+
docker network connect ${DB_TUNNEL_NETWORK} ${container_name};
40+
else
41+
echo "Connecting ${YELLOW}${container_name}${RESTORE} to ${DB_TUNNEL_NETWORK} with hostname (alias) ${YELLOW}${host_name}${RESTORE}";
42+
docker network connect ${DB_TUNNEL_NETWORK} ${container_name} --alias ${host_name};
43+
fi
3844
done

example/docker-compose-project.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: '3.7'
2+
3+
services:
4+
# mysql:
5+
project_mysql:
6+
container_name: project_mysql_1
7+
image: mariadb
8+
environment:
9+
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
10+
labels:
11+
- db.network.tunnel.hostname=project_db_host
12+
network_mode: "bridge"
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: '3.7'
2+
3+
services:
4+
# mariadb:
5+
symfony-demo_mariadb:
6+
container_name: symfony-demo_mariadb_1
7+
image: mariadb
8+
environment:
9+
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
10+
network_mode: "bridge"
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: '3.7'
2+
3+
services:
4+
# mariadb:
5+
symfony-demo2_mariadb:
6+
container_name: symfony-demo2_mariadb_1
7+
image: mariadb
8+
environment:
9+
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
10+
network_mode: "bridge"

0 commit comments

Comments
 (0)