-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathitkdev-docker-compose
More file actions
executable file
·873 lines (685 loc) · 22.7 KB
/
itkdev-docker-compose
File metadata and controls
executable file
·873 lines (685 loc) · 22.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
#!/usr/bin/env bash
VERSION=3.0.0
set -o errexit -o errtrace -o noclobber -o nounset -o pipefail
IFS=$'\n\t'
bold=$(tput -Txterm-256color bold)
normal=$(tput -Txterm-256color sgr0)
# @see https://stackoverflow.com/a/54839008
fg_black_8=$(tput setaf 0)
bg_green_8=$(tput setab 2)
function find_script_dir {
local script=${BASH_SOURCE[0]}
local script_dir=$(cd $(dirname "${script}") && pwd)
if [ -L $script ]; then
# Symlink detected.
script_dir=$(dirname $(readlink $script));
fi
echo $script_dir
}
script_dir=$(find_script_dir)
script_path=$script_dir/$(basename "${BASH_SOURCE[0]}")
function self_update {
project_dir=$(cd "$script_dir/.." && pwd)
cd "$project_dir"
if ! git diff --quiet; then
(>&2 echo "${bold}Local changes in $project_dir.${normal} Not updating.")
git diff --name-status
exit 1
fi
# TODO: Should we use the master branch?
branch=develop
echo "${bold}Updating branch $branch …${normal}"
git fetch
git checkout "$branch"
git pull
echo "${bold}Done. Please run traefik:pull to ensure you have the latest containers.${normal}"
echo
echo "Latest changes:"
git log --format="%C(auto) %h %s" -10
}
function template_install {
local force=0
local list=0
local list_compact=0
local name=""
for var in "$@"
do
case "$var" in
--force)
force=1
;;
--list)
list=1
;;
--list-compact)
list_compact=1
;;
*)
name="$var"
;;
esac
done
local templates_dir=$script_dir/../templates
local source_dir=$templates_dir/$name/
local target_dir=$PWD
if [ "$list_compact" -eq 1 ]; then
for name in $(find "$templates_dir" -mindepth 1 -maxdepth 1 -type d | sort --version-sort); do
echo -n "$(basename $name) "
done
echo
exit 0
fi
if [ "$list" -eq 1 ]; then
echo Templates:
for name in $(find "$templates_dir" -mindepth 1 -maxdepth 1 -type d | sort --version-sort); do
echo " $(basename $name)"
done
exit 0
fi
if [ -z "$name" ]; then
(>&2 echo "${bold}Missing template name${normal}")
exit 1
fi
if [ ! -d $source_dir ]; then
(>&2 echo "${bold}Invalid template name: $name (use --list to show valid template names)${normal}")
exit 1
fi
if [ "$force" -ne 1 ]; then
if [ -e $target_dir/docker-compose.yml ]; then
(>&2 echo "${bold}Target $target_dir/docker-compose.yml already exists (use --force to overwrite)${normal}")
exit 1
fi
if [ -e $target_dir/.docker ]; then
(>&2 echo "${bold}Target $target_dir/.docker already exists (use --force to overwrite)${normal}")
exit 1
fi
read -p "Are you sure you want to install the $name template in $target_dir (y|N)? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit
fi
fi
echo "${bold}Installing $name template${normal}"
# Use --copy-links to copy actual files rather than symlinks (cf.
# https://linux.die.net/man/1/rsync#:~:text=%2DL%2C-,%2D%2Dcopy%2Dlinks,-When%20symlinks%20are)
rsync --archive --copy-links "$source_dir" "$target_dir"
local project_name_default=$(basename $target_dir)
# Help user set up a .env file
if [ ! -e $target_dir/.env ]; then
local project_name
local domain
read -p "Project name ($project_name_default)? " -e project_name
if [ -z "$project_name" ]; then
project_name=$project_name_default
fi
default_domain="$project_name_default.local.itkdev.dk"
read -p "Domain [$default_domain]? " -e domain
cat > $target_dir/.env <<EOF
COMPOSE_PROJECT_NAME=$project_name
COMPOSE_DOMAIN=${domain:-$default_domain}
ITKDEV_TEMPLATE=$name
EOF
else
cat <<EOF
${bold}Edit .env and make sure that COMPOSE_PROJECT_NAME, COMPOSE_DOMAIN, and ITKDEV_TEMPLATE are set${normal}, e.g.
COMPOSE_PROJECT_NAME=$project_name_default
COMPOSE_DOMAIN=$project_name_default.local.itkdev.dk
ITKDEV_TEMPLATE=$name
EOF
fi
}
function template_update {
local force=0
for var in "$@"
do
case "$var" in
--force)
force=1
;;
esac
done
local target_dir=$PWD
if [ ! -e $target_dir/.env ]; then
(>&2 echo "${bold}Cannot read file $target_dir/.env")
exit 1
fi
source $target_dir/.env
if [ -z "${ITKDEV_TEMPLATE:-}" ]; then
(>&2 echo "${bold}ITKDEV_TEMPLATE not set in $target_dir/.env")
exit 1
fi
name="$ITKDEV_TEMPLATE"
if [ "$force" -ne 1 ]; then
read -p "Are you sure you want to update the $name template in $target_dir (y|N)? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit
fi
fi
template_install --force "$name"
}
bold=$(tput bold)
normal=$(tput sgr0)
function usage {
cat <<'EOF'
Usage: itkdev-docker-compose command [command arguments]
Commands:
url [service [port]]
Print url to site or a service
Protip: run
brew install jq
for improved handling of Traefik host names using jq (https://stedolan.github.io/jq/).
open [service [port]]
Open url in default browser
drush
Run drush command. If drush in installed via composer,
the vendor/bin/drush command is run inside the phpfpm
container. Otherwise, the drush container is used to
run the command.
self:update
Update this script and other stuff.
sync
Sync both database and files.
sync:db
Sync database base on 'REMOTE_DB_DUMP_CMD' in the env
file.
If 'SYNC_DB_POST_SCRIPT' is set, it will be eval'ed after
importing the remote database.
sync:files
Sync files base on 'REMOTE_PATH' or 'LOCAL_PATH' in
the env file.
sql:cli
Open MySQL client to the database (named `db`) in the database
container (`mariadb`).
Execute a SQL query from the command line:
itkdev-docker-compose sql:cli --table <<< 'SHOW TABLES'
Run a SQL script:
itkdev-docker-compose sql:cli < query.sql
sql:connect
Print mysql command for connecting to database (named
`db`) in the database container (`mariadb`).
Use `$(itkdev-docker-compose sql:connect)` to open the
database cli.
sql:log
Log SQL queries sent to database.
sql:open
Open database GUI and connect to MariaDB.
sql:port
Display the exposed MariaDB SQL server port.
template:install name [--force] [--list]
Install a named docker-composer template in the current
directory.
template:update [--force]
Update template installed in project.
traefik:start
Start traefik reverse proxy.
traefik:stop
Stop traefik reverse proxy
traefik:url
URL for the administrative UI for traefik.
traefik:open
Open the administrative UI for traefik.
traefik:logs
See traefik logs
traefik:pull
Pull latest traefik & socket-proxy containers
mail:url
URL for the test mail web interface.
mail:open
Open test mail web interface in default browser.
mailhog:url
URL for the mailhog web-interface.
mailhog:open
Open mailhog url in default browser
xdebug
Boot the containers with PHP xdebug support enabled.
xdebug3
Boot the containers with PHP xdebug support enabled.
Consider using `xdebug` for an improved developer experience.
hosts:insert
Insert the docker site url into the hosts file.
images:pull
Update/pull all docker images.
shell [service name]
Enter into /usr/bin/env sh inside container. E.g. itkdev-docker-compose shell phpfpm
composer
Run composer command inside phpfpm container
php
Run php command inside phpfpm container
bin/*
vendor/bin/*
Run command command inside phpfpm container
down
Stop and remove containers, networks, images, and volumes
version
Display this tool's current version
*
Pass command and arguments to `docker compose` and
hope for the best.
Configuration:
The environment variable COMPOSE_PROJECT_NAME must be set in the env file (`.env`).
These environment variables may be set in the env file:
COMPOSE_DOMAIN
The domain to use when generating urls
REMOTE_HOST
Host used when pulling data from remove site
REMOTE_DB_DUMP_CMD
Command used to dump database on remote host
REMOTE_PATH
File path on remote host
REMOTE_EXCLUDE
List of rsync exclude patterns to use when pulling files, e.g.
REMOTE_EXCLUDE=(ting styles advagg_*)
LOCAL_PATH
Local file path
Local overrides of these variables can be defined in `.env.local` which should
NOT be commited to Git.
EOF
}
function message_info() {
echo "${bg_green_8}${fg_black_8}"
echo
echo " ""$@"" "
echo "${normal}"
echo
}
# Start PHP service with Xdebug enabled, wait for kill signal and restart with
# Xdebug disabled.
function xdebug() {
cat <<'EOF' | sed "s|«project directory»|$PWD|"
================================================================================
Make sure that `PHP_IDE_CONFIG=serverName=localhost` is set in the `phpfpm`
service environment:
# docker-compose.yml
phpfpm:
image: …
environment:
…
- PHP_IDE_CONFIG=serverName=localhost
# PhpStorm
Open Preferences > PHP > Servers and define "localhost" as a server:
Name: localhost
Host: localhost
Port: 8080
Debugger: Xdebug
Path mapping: «project directory» → /app
You may also want to uncheck "Force break at first line when no path mapping
specified" and/or "Force break at first line when a script is outside the
project" in PhpStorm (Preferences > Languages & Frameworks > PHP > Debug).
# VS Code
Install https://marketplace.visualstudio.com/items?itemName=xdebug.php-debug
Add a configuration in `.vscode/launch.json` with `pathMappings` (Run > Add
Configuration… > "PHP: Listen for Xdebug"):
{
…
"configurations": [
…,
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/app": "${workspaceFolder}"
}
}
]
}
================================================================================
EOF
message_info "Starting Docker Compose with Xdebug enabled. Press Ctrl-C to disable."
# Needed to use `trap`
set +o errexit
# https://stackoverflow.com/a/11952947
trap " " INT
# Start the docker compose with Xdebug enabled
# @todo Should we disable logs, e.g. by sending it to /dev/null?
# Note: `DRUSH_ALLOW_XDEBUG=1` is set in our php*-fpm container to allow debugging Drush commands
# (cf. https://github.com/drush-ops/drush/blob/14.x/docs/commands.md#xdebug)
PHP_XDEBUG_MODE=debug PHP_XDEBUG_WITH_REQUEST=yes docker_compose up
trap - INT
# Disable Xdebug.
echo
echo "Starting Docker Compose with Xdebug disabled."
echo
docker_compose up --detach
}
# template:install must be run before we check for existence of compose file.
if [[ "$#" -gt "0" && "$1" == "template:install" ]]; then
shift
template_install "$@"
exit
elif [[ "$#" -gt "0" && "$1" == "template:update" ]]; then
shift
template_update "$@"
exit
fi
# self:update must be run before we check for existence of compose file.
if [[ "$#" -gt "0" && "$1" == "self:update" ]]; then
self_update "$@"
exit
fi
# Allow traefik:start without existence of .env
if [[ "$#" -gt "0" && "$1" == "traefik:start" ]]; then
docker network inspect frontend >/dev/null 2>&1 || docker network create --driver=bridge --attachable --internal=false frontend
$(cd ${script_dir}/../traefik/; docker compose up -d)
exit
fi
# Allow traefik:stop without existence of .env
if [[ "$#" -gt "0" && "$1" == "traefik:stop" ]]; then
$(cd ${script_dir}/../traefik/; docker compose down --volumes)
exit
fi
# Allow traefik:pull without existence of .env
if [[ "$#" -gt "0" && "$1" == "traefik:pull" ]]; then
$(cd ${script_dir}/../traefik/; docker compose pull)
exit
fi
# Allow version without existence of compose file.
if [[ "$#" -gt "0" && "$1" == "version" ]]; then
echo Version: ${VERSION}
exit
fi
# Allow completion without existence of compose file.
if [[ "$#" -gt "0" && "$1" == "completions" ]]; then
cat ${script_dir}/../completion/itkdev-docker-compose-completion.bash
exit
fi
# Allow help without existence of compose file.
if [[ "$#" -gt "0" && "$1" == "--help" ]]; then
usage
exit
fi
# @see https://unix.stackexchange.com/questions/13464/is-there-a-way-to-find-a-file-in-an-inverse-recursive-search/13474
upsearch () {
slashes=${PWD//[^\/]/}
directory="$PWD"
for (( n=${#slashes}; n>0; --n ))
do
test -e "$directory/$1" && echo "$directory/$1" && return
directory="$directory/.."
done
}
COMPOSE_FILE=${COMPOSE_FILE:-docker-compose.yml}
docker_compose_file=$(upsearch ${COMPOSE_FILE})
if [ -z "$docker_compose_file" ]; then
(>&2 echo "${bold}Cannot find ${COMPOSE_FILE} file${normal}")
exit 1
fi
docker_compose_dir=$(cd $(dirname "$docker_compose_file") && pwd)
dot_env_file="$docker_compose_dir/.env"
if [ -z "$dot_env_file" ]; then
(>&2 echo "${bold}$dot_env_file not found${normal}")
exit 1
fi
source $dot_env_file
if [ -e $dot_env_file.local ]; then
source $dot_env_file.local
fi
if [ -z "${COMPOSE_PROJECT_NAME:-}" ]; then
(>&2 echo "${bold}Environment variable COMPOSE_PROJECT_NAME not defined in $dot_env_file${normal}")
exit 1
fi
COMPOSE_DOMAIN=${COMPOSE_DOMAIN:-${COMPOSE_PROJECT_NAME}.docker.localhost}
if [ "$#" == "0" ]; then
usage
exit 1
fi
# Check if revers proxy is running and print warning if not.
set +o errexit
is_treafik_running=$(docker inspect -f '{{.State.Running}}' traefik 2>/dev/null)
set -o errexit
if [ ! "$is_treafik_running" == "true" ]; then
(>&2 echo "${bold}Reverse proxy has not been started. Hostname will not be resolved to containers and not all commands will work correctly.${normal}")
fi
# Helper function to call `docker compose` in the right context
docker_compose () {
# Note: Apparently, using --project-directory or --file options for `docker compose` will break use of `$PWD` in
# compose file. Therefore, we `cd` before running `docker compose` command.
(cd "$docker_compose_dir" && COMPOSE_DOMAIN=${COMPOSE_DOMAIN} docker compose "$@")
}
cmd="$1"
shift
case "$cmd" in
url)
service=nginx
port=8080
if [ "$#" -gt 0 ]; then
service="$1"
shift
if [ "$#" -gt 0 ]; then
port="$1"
shift
fi
fi
host=$(docker_compose port $service $port)
if [ $? -ne 0 ]; then
exit 1
elif [ -z "$host" ]; then
(>&2 echo "Cannot find host for service $service (port: $port)")
exit 1
fi
traefik_host=""
if command -v jq &> /dev/null; then
# Get traefik host from label
# https://stedolan.github.io/jq/manual/#test(val),test(regex;flags)
# https://stedolan.github.io/jq/manual/#capture(val),capture(regex;flags)
traefik_host=$(docker inspect --format '{{ json .Config.Labels }}' $(docker_compose ps -q $service) | jq --raw-output '. | [to_entries[] | select(.key | test("^traefik\\.http\\.routers\\..+\\.rule$")) | select(.value | test("^Host\\(`(?<host>.+)`\\)$"))] | first | .value//"" | capture("^Host\\(`(?<host>.+)`\\)$") | .host//""')
fi
if ! [ -z "$traefik_host" ]; then
host="$traefik_host"
elif [ "nginx" == "$service" ]; then
# Fall back to using COMPOSE_DOMAIN for nginx container when no Traefik host has been found.
host=${COMPOSE_DOMAIN}
fi
echo http://$host
;;
open)
open $($script_path url "$@")
;;
sync)
itkdev-docker-compose sync:db
itkdev-docker-compose sync:files
;;
sync:db)
if [ -z "${REMOTE_HOST:-}" ]; then
(>&2 echo "${bold}Environment variable REMOTE_HOST not defined in $dot_env_file${normal}")
exit 1
fi
if [ -z "${REMOTE_DB_DUMP_CMD:-}" ]; then
(>&2 echo "${bold}Environment variable REMOTE_DB_DUMP_CMD not defined in $dot_env_file${normal} see readme for more information about this setting")
exit 1
fi
if command -v pv >/dev/null 2>&1; then
ssh ${REMOTE_HOST} ${REMOTE_DB_DUMP_CMD} | pv | $script_path sql:cli
else
cat <<EOF
Protip: run
brew install pv
to show progress
EOF
ssh ${REMOTE_HOST} ${REMOTE_DB_DUMP_CMD} | $script_path sql:cli
fi
if [ ! -z "${SYNC_DB_POST_SCRIPT:-}" ]; then
eval "${SYNC_DB_POST_SCRIPT}"
fi
;;
sync:files)
if [ -z "${REMOTE_HOST:-}" ]; then
(>&2 echo "${bold}Environment variable REMOTE_HOST not defined in $dot_env_file${normal}")
exit 1
fi
if [ -z "${REMOTE_PATH:-}" ]; then
(>&2 echo "${bold}Environment variable REMOTE_PATH not defined in $dot_env_file${normal}")
exit 1
fi
if [ -z "${LOCAL_PATH:-}" ]; then
(>&2 echo "${bold}Environment variable LOCAL_PATH not defined in $dot_env_file${normal}")
exit 1
fi
REMOTE_PATH=${REMOTE_PATH%/}
LOCAL_PATH=${LOCAL_PATH%/}
excludes=''
if [ ! -z "${REMOTE_EXCLUDE:-}" ]; then
for i in "${REMOTE_EXCLUDE[@]}"
do
excludes+="--exclude='${i}' "
done
fi
eval rsync -avz ${excludes} ${REMOTE_HOST}:${REMOTE_PATH}/ ${docker_compose_dir}/${LOCAL_PATH}
;;
sql:cli)
db_service=mariadb
exec_args=()
# @see https://stackoverflow.com/a/2456870?
if ! [ -t 0 ]; then
exec_args+=("--no-TTY")
fi
docker compose exec "${exec_args[@]}" $db_service mysql --user=db --password=db --database=db "$@"
;;
sql:connect)
db_service=mariadb
echo docker compose exec $db_service mysql --user=db --password=db db
;;
sql:open)
address=$(docker_compose port mariadb 3306)
host=$(echo $address | cut -d: -f1)
port=$(echo $address | cut -d: -f2)
echo open mariadb://db:db@$host:$port/db?statusColor=F8F8F8\&enviroment=local\&name=${COMPOSE_DOMAIN}
open mariadb://db:db@$host:$port/db?statusColor=F8F8F8\&enviroment=local\&name=${COMPOSE_DOMAIN}
;;
sql:port)
docker_compose port mariadb 3306 | cut -d: -f2
;;
sql:log)
# @see https://mariadb.com/kb/en/general-query-log/
# Enable logging to the file mariadb.log (cf. https://mariadb.com/kb/en/general-query-log/#configuring-the-general-query-log-filename)
docker_compose exec --no-TTY mariadb sh -c 'mysql --user=root --password=$MYSQL_ROOT_PASSWORD db --verbose' <<< "SET GLOBAL general_log=1; SET GLOBAL general_log_file='mariadb.log';"
echo
echo "Press Ctrl-C to stop logging"
echo
# Needed to use `trap`
set +o errexit
# https://stackoverflow.com/a/11952947
trap " " INT
docker_compose exec mariadb tail -f /var/lib/mysql/mariadb.log
trap - INT
# Disable logging.
echo
docker_compose exec --no-TTY mariadb sh -c 'mysql --user=root --password=$MYSQL_ROOT_PASSWORD --verbose db' <<< "SET GLOBAL general_log=0;"
;;
traefik:open)
open $($script_path traefik:url)
;;
traefik:url)
label=$(docker inspect --format '{{ index .Config.Labels "traefik.http.routers.traefik.rule"}}' traefik)
url=$(echo "${label}" | sed -n 's/[^(]*(.\(.*\).)/\1/p')
echo http://${url}:8080;
;;
traefik:logs)
echo "docker logs --tail 20 traefik"
docker logs --tail 20 traefik
;;
mail:url)
url="http://${COMPOSE_DOMAIN}:$(docker_compose port mail 8025 | cut -d: -f2)"
echo $url
;;
mail:open)
open "$(itkdev-docker-compose mail:url)"
;;
mailhog:url)
echo "Deprecated: Use mail:url"
url=http://${COMPOSE_DOMAIN}:$(docker_compose port mailhog 8025 | cut -d: -f2)
echo $url
;;
mailhog:open)
echo "Deprecated: Use mail:open"
open $(itkdev-docker-compose mailhog:url)
;;
xdebug)
xdebug
;;
xdebug3)
# Note: `DRUSH_ALLOW_XDEBUG=1` is set in our php*-fpm container to allow debugging Drush commands
# (cf. https://github.com/drush-ops/drush/blob/14.x/docs/commands.md#xdebug)
PHP_XDEBUG_MODE=debug PHP_XDEBUG_WITH_REQUEST=yes docker_compose up --detach
cat <<'EOF' | sed "s|«project directory»|$PWD|"
================================================================================
Make sure that `PHP_IDE_CONFIG=serverName=localhost` is set in the `phpfpm`
service environment:
# docker-compose.yml
phpfpm:
image: …
environment:
…
- PHP_IDE_CONFIG=serverName=localhost
# PhpStorm
Open Preferences > PHP > Servers and define "localhost" as a server:
Name: localhost
Host: localhost
Port: 8080
Debugger: Xdebug
Path mapping: «project directory» → /app
You may also want to uncheck "Force break at first line when no path mapping
specified" and/or "Force break at first line when a script is outside the
project" in PhpStorm (Preferences > Languages & Frameworks > PHP > Debug).
# VS Code
Install https://marketplace.visualstudio.com/items?itemName=xdebug.php-debug
Add a configuration in `.vscode/launch.json` with `pathMappings` (Run > Add
Configuration… > "PHP: Listen for Xdebug"):
{
…
"configurations": [
…,
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/app": "${workspaceFolder}"
}
}
]
}
================================================================================
EOF
message_info "Consider using \`$(basename $script_path) xdebug\` for an improved developer experience."
;;
hosts:insert)
# Remove any existing entry
sudo sed -i '.itkdev-docker-compose' "/${COMPOSE_DOMAIN}/d" /etc/hosts
# Append the entry
echo "0.0.0.0 ${COMPOSE_DOMAIN} # itkdev-docker-compose" | sudo tee -a /etc/hosts
;;
images:pull)
docker images --format "{{.Repository}}:{{.Tag}}" | grep -v "<none>" | sort | uniq
read -p "Are you sure you want to update all images? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
docker images --format "{{.Repository}}:{{.Tag}}" | grep -v "<none>" | sort | uniq | xargs -L1 docker pull
fi
;;
drush)
docker_compose exec phpfpm vendor/bin/drush "$@"
;;
composer)
docker_compose exec phpfpm composer "$@"
;;
php)
docker_compose exec phpfpm php "$@"
;;
bin/*|vendor/bin/*)
docker_compose exec phpfpm /app/$cmd "$@"
;;
down)
docker_compose down --volumes --remove-orphans
;;
shell)
docker_compose exec "$@" /usr/bin/env sh
;;
*)
docker_compose "$cmd" "$@"
;;
esac