Skip to content

Commit 4b4bb72

Browse files
authored
Merge pull request #32 from aakb/feature/traefik-proxy
Global træfik proxy
2 parents 47d0220 + 4881bb5 commit 4b4bb72

File tree

11 files changed

+251
-88
lines changed

11 files changed

+251
-88
lines changed

completion/bash/itkdev-docker-compose-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ _idc_completions()
99
fi
1010

1111
# Keep the suggestions in a local variable
12-
local suggestions=($(compgen -W "url open drush mailhog:open mailhog:url sql:connect sql:port xdebug hosts:insert sync sync:db sync:files images:pull composer" -- "${COMP_WORDS[1]}"))
12+
local suggestions=($(compgen -W "url open drush traefik:start traefik:stop traefik:open traefik:url mailhog:open mailhog:url sql:connect sql:port xdebug hosts:insert sync sync:db sync:files images:pull composer" -- "${COMP_WORDS[1]}"))
1313

1414
COMPREPLY=("${suggestions[@]}")
1515

completion/zsh/_itkdev-docker-compose

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ _itkdev-docker-compose() {
3535
'sync\:files[Sync files base.]' \
3636
'sql\:connect[Print mysql command for connecting to database.]' \
3737
'sql\:port[Display the exposed MariaDB SQL server port.]' \
38+
'traefik\:start[Start reverse proxy]' \
39+
'traefik\:stop[Stop reverse proxy]' \
40+
'traefik\:open[Open reverse proxy web-interface]' \
41+
'traefik\:url[URL for the reverse proxy web-interface]' \
3842
'mailhog\:url[URL for the mailhog web-interface.]' \
3943
'mailhog\:open[Open mailhog url in default browser.]' \
4044
'xdebug[Boot the containers with PHP xdebug support enabled.]' \

scripts/itkdev-docker-compose

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@
22
set -o errexit -o errtrace -o noclobber -o nounset -o pipefail
33
IFS=$'\n\t'
44

5-
script_dir=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
5+
function find_script_dir {
6+
local script=${BASH_SOURCE[0]}
7+
local script_dir=$(cd $(dirname "${script}") && pwd)
8+
if [ -L $script ]; then
9+
# Symlink detected.
10+
script_dir=$(dirname $(readlink $script));
11+
fi
12+
13+
echo $script_dir
14+
}
15+
16+
script_dir=$(find_script_dir)
617
script_path=$script_dir/$(basename "${BASH_SOURCE[0]}")
718

819
bold=$(tput bold)
@@ -47,6 +58,18 @@ Commands:
4758
sql:port
4859
Display the exposed MariaDB SQL server port.
4960
61+
traefik:start
62+
Start træfik reverse proxy.
63+
64+
traefik:stop
65+
Stop træfik reverse proxy
66+
67+
traefik:url
68+
URL for the administrative UI for træfik.
69+
70+
traefik:open
71+
Open the administrative UI for træfik.
72+
5073
mailhog:url
5174
URL for the mailhog web-interface.
5275
@@ -85,9 +108,6 @@ Configuration:
85108
COMPOSE_DOMAIN
86109
The domain to use when generating urls
87110
88-
COMPOSE_URL
89-
The url to use when generating urls
90-
91111
REMOTE_HOST
92112
Host used when pulling data from remove site
93113
@@ -152,19 +172,24 @@ if [ "$#" == "0" ]; then
152172
exit 1
153173
fi
154174

175+
# Check if traefik is running and print warning if not
176+
if [ ! "$(docker inspect -f '{{.State.Running}}' traefik 2>/dev/null)" == "true" ]; then
177+
(>&2 echo "${bold}Traefik reverse proxy has not been started. Hostname will not be resolved to containers and not all commands will work correctly.${normal}")
178+
fi
179+
155180
# Helper function to call `docker-compose` in the right context
156181
docker_compose () {
157182
# Note: Apparently, using --project-directory or --file options for `docker-compose` will break use of `$PWD` in
158183
# docker-compose.yml. Therefore, we `cd` before running `docker-compose` command.
159-
(cd "$docker_compose_dir" && docker-compose "$@")
184+
(cd "$docker_compose_dir" && COMPOSE_DOMAIN=${COMPOSE_DOMAIN} docker-compose "$@")
160185
}
161186

162187
cmd="$1"
163188
shift
164189

165190
case "$cmd" in
166191
url)
167-
echo ${COMPOSE_URL:-http://${COMPOSE_DOMAIN}:$(docker_compose port reverse-proxy 80 | cut -d: -f2)}
192+
echo http://${COMPOSE_DOMAIN}
168193
;;
169194

170195
open)
@@ -238,9 +263,29 @@ EOF
238263
docker_compose port mariadb 3306 | cut -d: -f2
239264
;;
240265

266+
traefik:start)
267+
docker network inspect frontend >/dev/null 2>&1 || docker network create --driver=bridge --attachable --internal=false frontend
268+
$(cd ${script_dir}/../traefik/; docker-compose up -d)
269+
;;
270+
271+
traefik:stop)
272+
$(cd ${script_dir}/../traefik/; docker-compose down --volumes)
273+
;;
274+
275+
traefik:open)
276+
open $($script_path traefik:url)
277+
;;
278+
279+
traefik:url)
280+
label=$(docker inspect --format '{{ index .Config.Labels "traefik.http.routers.traefik.rule"}}' traefik)
281+
url=$(echo "${label}" | sed -n 's/.*\`\(.*\)\`.*/\1/p')
282+
echo http://${url}:8080;
283+
;;
284+
241285
mailhog:url)
242286
url=http://${COMPOSE_DOMAIN}:$(docker_compose port mailhog 8025 | cut -d: -f2)
243-
echo $url;;
287+
echo $url
288+
;;
244289

245290
mailhog:open)
246291
open $(itkdev-docker-compose mailhog:url)
Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
version: "3"
22

3+
networks:
4+
frontend:
5+
external: true
6+
app:
7+
driver: bridge
8+
internal: false
9+
310
services:
411
mariadb:
512
image: ding2/ding2-mysql
13+
networks:
14+
- app
615
ports:
716
- '3306'
817
environment:
@@ -13,6 +22,8 @@ services:
1322

1423
phpfpm:
1524
image: itkdev/php7.0-fpm:latest
25+
networks:
26+
- app
1627
environment:
1728
- PHP_XDEBUG=${PHP_XDEBUG:-0}
1829
- PHP_XDEBUG_REMOTE_AUTOSTART=${PHP_XDEBUG_REMOTE_AUTOSTART:-0}
@@ -21,7 +32,7 @@ services:
2132
- PHP_MAX_EXECUTION_TIME=300
2233
- PHP_MEMORY_LIMIT=512M
2334
# - PHP_MAIL=1 # Uncomment to enable mailhog.
24-
- DOCKER_HOST_DOMAIN=${COMPOSE_PROJECT_NAME}.docker.localhost
35+
- DOCKER_HOST_DOMAIN=${COMPOSE_DOMAIN}
2536
depends_on:
2637
- mariadb
2738
volumes:
@@ -30,32 +41,43 @@ services:
3041

3142
nginx:
3243
image: nginx:latest
44+
networks:
45+
- app
46+
- frontend
3347
depends_on:
3448
- phpfpm
3549
- memcached
36-
# ports:
37-
# - '80'
50+
ports:
51+
- '80'
3852
volumes:
3953
- ${PWD}/.docker/vhost.conf:/etc/nginx/conf.d/default.conf:ro
4054
- ./:/app:delegated
4155
labels:
42-
- "traefik.http.routers.frontend.rule=Host(`${COMPOSE_PROJECT_NAME}.docker.localhost`)"
56+
- "traefik.enable=true"
57+
- "traefik.docker.network=frontend"
58+
- "traefik.http.routers.${COMPOSE_PROJECT_NAME}.rule=Host(`${COMPOSE_DOMAIN}`)"
4359

4460
memcached:
4561
image: 'memcached:latest'
62+
networks:
63+
- app
4664
ports:
4765
- '11211'
4866
environment:
4967
- MEMCACHED_CACHE_SIZE=64
5068

5169
mailhog:
5270
image: mailhog/mailhog
71+
networks:
72+
- app
5373
ports:
5474
- "1025"
5575
- "8025"
5676

5777
drush:
5878
image: itkdev/drush6:latest
79+
networks:
80+
- app
5981
depends_on:
6082
- mariadb
6183
entrypoint:
@@ -66,18 +88,11 @@ services:
6688

6789
node:
6890
image: node:6
91+
networks:
92+
- app
6993
volumes:
7094
- .:/app:delegated
7195

72-
reverse-proxy:
73-
image: traefik:2.0 # The official Traefik docker image
74-
command: --api.insecure=true --providers.docker # Enables the web UI and tells Traefik to listen to docker
75-
ports:
76-
- "80"
77-
- "8080"
78-
volumes:
79-
- /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events
80-
8196
# Drush cache volume to persist cache between runs.
8297
volumes:
8398
drush-cache:
Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
version: "3"
22

3+
networks:
4+
frontend:
5+
external: true
6+
app:
7+
driver: bridge
8+
internal: false
9+
310
services:
411
mariadb:
512
image: itkdev/mariadb:latest
13+
networks:
14+
- app
615
ports:
716
- '3306'
817
environment:
@@ -14,6 +23,8 @@ services:
1423

1524
phpfpm:
1625
image: itkdev/php7.2-fpm:latest
26+
networks:
27+
- app
1728
environment:
1829
- PHP_XDEBUG=${PHP_XDEBUG:-0}
1930
- PHP_XDEBUG_REMOTE_AUTOSTART=${PHP_XDEBUG_REMOTE_AUTOSTART:-0}
@@ -22,40 +33,52 @@ services:
2233
- PHP_MAX_EXECUTION_TIME=30
2334
- PHP_MEMORY_LIMIT=256M
2435
# - PHP_MAIL=1 # Uncomment to enable mailhog.
25-
- DOCKER_HOST_DOMAIN=${COMPOSE_PROJECT_NAME}.docker.localhost
36+
- DOCKER_HOST_DOMAIN=${COMPOSE_DOMAIN}
2637
depends_on:
2738
- mariadb
2839
volumes:
2940
- .:/app:delegated
3041

3142
nginx:
3243
image: nginx:latest
44+
networks:
45+
- app
46+
- frontend
3347
depends_on:
3448
- phpfpm
3549
- memcached
36-
# ports:
37-
# - '80'
50+
ports:
51+
- '80'
3852
volumes:
3953
- ${PWD}/.docker/vhost.conf:/etc/nginx/conf.d/default.conf:ro
4054
- ./:/app:delegated
4155
labels:
42-
- "traefik.http.routers.frontend.rule=Host(`${COMPOSE_PROJECT_NAME}.docker.localhost`)"
56+
- "traefik.enable=true"
57+
- "traefik.docker.network=frontend"
58+
- "traefik.http.routers.${COMPOSE_PROJECT_NAME}.rule=Host(`${COMPOSE_DOMAIN}`)"
59+
4360

4461
memcached:
4562
image: 'memcached:latest'
63+
networks:
64+
- app
4665
ports:
4766
- '11211'
4867
environment:
4968
- MEMCACHED_CACHE_SIZE=64
5069

5170
mailhog:
5271
image: mailhog/mailhog
72+
networks:
73+
- app
5374
ports:
5475
- "1025"
5576
- "8025"
5677

5778
drush:
5879
image: itkdev/drush6:latest
80+
networks:
81+
- app
5982
depends_on:
6083
- mariadb
6184
entrypoint:
@@ -64,15 +87,6 @@ services:
6487
- drush-cache:/root/.drush
6588
- ./:/app
6689

67-
reverse-proxy:
68-
image: traefik:2.0 # The official Traefik docker image
69-
command: --api.insecure=true --providers.docker # Enables the web UI and tells Traefik to listen to docker
70-
ports:
71-
- "80"
72-
- "8080"
73-
volumes:
74-
- /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events
75-
7690
# Drush cache volume to persist cache between runs.
7791
volumes:
7892
drush-cache:

0 commit comments

Comments
 (0)