Skip to content

Commit

Permalink
add option for custom hostname via label + update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
speto committed Sep 13, 2018
1 parent 98af709 commit ebf3880
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 12 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
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"
54 changes: 48 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![latest 0.1.0](https://img.shields.io/badge/latest-0.1.0-green.svg?style=flat)
![latest 0.2.0](https://img.shields.io/badge/latest-0.2.0-green.svg?style=flat)
[![license](https://img.shields.io/github/license/webcore/snitcher.svg?maxAge=2592000)](https://opensource.org/licenses/MIT)

# Docker DB tunnel
Expand Down Expand Up @@ -26,17 +26,58 @@ Run shell script to create network, sshd container and connect all db containers
$ ./docker-db-tunnel.sh
Creating db tunnel network: db-tunnel-network
668e40197c800a612ea748b9778d3f0888333673f7588d4a0bb1e027bd5d22d4
Running db tunnel container with name: db-tunnel-sshd
Running db tunnel container db-tunnel-sshd on port 22666
164e5a3c3b446169f928a03c135594493843664fef5ffa3edf820dd5de06f0a1
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
Connecting project_mysql_1 to db-tunnel-network with hostname (alias) project_db_host
```

### SSH Tunnel options
The **root** password for SSH is "**root**". How to [change-root-password](https://github.com/sickp/docker-alpine-sshd#change-root-password).
### Docker-compose services example

docker-compose-project.yml
```yaml
services:
project_mysql:
container_name: project_mysql_1
image: mariadb
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
labels:
- db.network.tunnel.hostname=project_db_host
network_mode: "bridge"
```
docker-compose-symfony-demo.yml
```yaml
services:
symfony-demo_mariadb:
container_name: symfony-demo_mariadb_1
image: mariadb
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
network_mode: "bridge"
```
### SSH Tunnel settings
The **root** password for SSH is in `sickp/alpine-sshd` "**root**".
You can also [change default root password](https://github.com/sickp/docker-alpine-sshd#change-root-password).

### Connection settings example

```
MySQL host: project_mysql_1
# or via 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 connection details
![Sequel Pro screenshot](./docker-db-tunnel.png)
## Customize
Expand All @@ -45,6 +86,7 @@ It is easy to extend via your own .env file:
```dotenv
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
Expand Down
18 changes: 12 additions & 6 deletions docker-db-tunnel.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ RESTORE=$(echo -en '\033[0m')
test -f .env && source .env

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

# @todo consider filter by label (e.g. label=db.network.tunnel, label=database, label=mysql)
docker ps --filter "status=running" --filter "name=${DB_CONTAINER_NAME_PATTERN}" --format "{{.Names}} {{.Networks}}" \
docker ps --filter "status=running" --filter "name=${DB_CONTAINER_NAME_PATTERN}" --format '{{.Names}} {{.Networks}} {{.Label "'${DB_TUNNEL_NETWORK_HOSTNAME_LABEL}'"}}' \
| grep -v ${DB_TUNNEL_NETWORK} \
| cut -d ' ' -f 1 \
| while read container_name ; do
echo "Connecting ${YELLOW}${container_name}${RESTORE} to ${DB_TUNNEL_NETWORK}";
docker network connect ${DB_TUNNEL_NETWORK} ${container_name};
| cut -d ' ' -f 1,3 \
| while read container_name host_name; do
if [ "$host_name" == "" ]; then
echo "Connecting ${YELLOW}${container_name}${RESTORE} to ${DB_TUNNEL_NETWORK}";
docker network connect ${DB_TUNNEL_NETWORK} ${container_name};
else
echo "Connecting ${YELLOW}${container_name}${RESTORE} to ${DB_TUNNEL_NETWORK} with hostname (alias) ${YELLOW}${host_name}${RESTORE}";
docker network connect ${DB_TUNNEL_NETWORK} ${container_name} --alias ${host_name};
fi
done
12 changes: 12 additions & 0 deletions example/docker-compose-project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.7'

services:
# mysql:
project_mysql:
container_name: project_mysql_1
image: mariadb
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
labels:
- db.network.tunnel.hostname=project_db_host
network_mode: "bridge"
10 changes: 10 additions & 0 deletions example/docker-compose-symfony-demo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.7'

services:
# mariadb:
symfony-demo_mariadb:
container_name: symfony-demo_mariadb_1
image: mariadb
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
network_mode: "bridge"
10 changes: 10 additions & 0 deletions example/docker-compose-symfony-demo2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.7'

services:
# mariadb:
symfony-demo2_mariadb:
container_name: symfony-demo2_mariadb_1
image: mariadb
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
network_mode: "bridge"

0 comments on commit ebf3880

Please sign in to comment.