Skip to content

Commit 2bb3340

Browse files
committed
improve docker setup
fixes #13
1 parent 0a93129 commit 2bb3340

File tree

6 files changed

+48
-23
lines changed

6 files changed

+48
-23
lines changed

Makefile

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1+
# set user to "root" to run commands as root in docker
2+
USER=$$(whoami)
3+
# The docker command to execute commands directly in docker
4+
DOCKER=docker-compose exec --user=$(USER) backend-php
5+
# The PHP binary to use, you may add arguments to PHP here
6+
PHP=php
17

8+
9+
# default target lists general usage information
210
default:
311
@echo "The following commands are available:"
412
@echo ""
5-
@echo " make start start PHP built-in webserver"
6-
@echo " make stop stop PHP built-in webserver"
13+
@echo " make start start PHP built-in webserver"
14+
@echo " make stop stop PHP built-in webserver"
15+
@echo " make start-docker start docker environment"
16+
@echo " make stop-docker stop docker environment"
17+
@echo " make cli run bash in docker environment"
18+
@echo " make bash alias for 'make cli'"
19+
20+
21+
.PHONY: start stop start-docker stop-docker clean test bash cli
22+
23+
24+
## PHP runtime ##
725

826
# start PHP built-in webserver
9-
start:
27+
start: config/components-dev.local.php config/components-test.local.php backend/config/cookie-validation.key env.php
1028
@echo "Starting server for api"
1129
cd api && $(MAKE) start
1230
@echo "Starting server for backend"
@@ -17,27 +35,30 @@ stop:
1735
cd api && $(MAKE) stop
1836
cd backend && $(MAKE) stop
1937

20-
test:
21-
cd api && $(MAKE) test
2238

23-
clean: stop
39+
## Docker Runtime ##
2440

25-
.PHONY: start stop clean test
41+
# run bash inside docker container
42+
bash: cli
43+
cli:
44+
$(DOCKER) bash
2645

27-
docker-up: config/components-dev.local.php config/components-test.local.php env.php stop
46+
start-docker: config/components-dev.local.php config/components-test.local.php backend/config/cookie-validation.key env.php
2847
docker-compose up -d
29-
docker-compose exec backend-php sh -c 'cd /app && composer install'
48+
$(DOCKER) sh -c 'cd /app && composer install'
3049
@echo ""
3150
@echo "API: http://localhost:8337/"
3251
# @echo "API docs: http://localhost:8337/docs/index.html" # not yet :)
3352
@echo "Backend: http://localhost:8338/"
3453
@echo ""
3554

36-
cli:
37-
docker-compose exec backend-php bash
55+
stop-docker:
56+
docker-compose down
3857

3958
# copy config files if they do not exist
4059
config/components-%.local.php: config/components-ENV.local.php
4160
test -f $@ || cp $< $@
4261
env.php: env.php.dist
4362
test -f $@ || cp $< $@
63+
backend/config/cookie-validation.key:
64+
test -s $@ || php -r 'echo bin2hex(random_bytes(20));' > $@

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,16 @@ You need to adjust `config/components-dev.local.php` to configure your database
5555

5656
After that, run `make start` and `make stop` to control the PHP webservers for API and backend.
5757

58+
You may use `XDEBUGW=1` to enable xdebug for the webserver, e.g. `make XDEBUGW=1 start`.
59+
5860
You can now continue with [generating code](#generate-code).
5961

6062
### Using Docker
6163

6264
You need [Docker](https://docs.docker.com/install/) and [Docker Compose](https://docs.docker.com/compose/install/) installed.
6365
For the easiest way you need GNU make, then run:
6466

65-
make docker-up
67+
make start-docker
6668

6769
This uses `docker-compose` to start docker containers for the API and the backend including a database.
6870

@@ -76,8 +78,6 @@ You can now continue with [generating code](#generate-code).
7678

7779
## Generate Code <span id="generate-code"></span>
7880

79-
Note: If OpenAPI spec file is present locally on the file system, then for UNIX bases OS, `openApiPath` must start with `/` and should provide aboslute path instead of relative path from the current directory. Example: `/home/user/documents/MyProjectOpenAPISpec.yml`
80-
8181
### Console
8282

8383
> Tip: If you use Docker, run `make cli` before running any of these commands.
@@ -87,6 +87,9 @@ spec file. The following example will generate API code for the [OpenAPI petstor
8787

8888
./yii gii/api --openApiPath=https://raw.githubusercontent.com/OAI/OpenAPI-Specification/3.0.2/examples/v3.0/petstore-expanded.yaml
8989

90+
> Note: If the OpenAPI spec file is present locally on the file system, then `openApiPath` must be specified as an absolute path, relative paths will not work correctly.
91+
> On UNIX based OS it must start with `/`. Example: `/home/user/documents/MyProjectOpenAPISpec.yml`
92+
9093
Run `./yii gii/api --help` for a list of configuration options. You may also adjust the configuration in `config/gii-generators.php`.
9194

9295
Then set up the database:

api/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ XDEBUGW=0
88
WEBPORT=8337
99

1010
ifeq ($(XDEBUG),1)
11-
XDEBUGARGS=-dzend_extension=xdebug.so -dxdebug.remote_enable=1
11+
XDEBUGARGS=-dzend_extension=xdebug.so -dxdebug.mode=debug,develop -dxdebug.start_with_request=yes -dxdebug.discover_client_host=yes -dxdebug.remote_enable=1 -dxdebug.remote_autostart=1
1212
endif
1313
ifeq ($(XDEBUGW),1)
14-
XDEBUGARGSW=-dzend_extension=xdebug.so -dxdebug.remote_enable=1
14+
XDEBUGARGSW=-dzend_extension=xdebug.so -dxdebug.mode=debug,develop -dxdebug.start_with_request=yes -dxdebug.discover_client_host=yes -dxdebug.remote_enable=1 -dxdebug.remote_autostart=1
1515
endif
1616

1717

@@ -20,11 +20,11 @@ current_dir=$(shell pwd)
2020
default:
2121

2222
start: stop
23-
@echo "Starting PHP webserver at http://localhost:$(WEBPORT), see php.log for access log."
23+
@echo "Starting PHP webserver at http://localhost:$(WEBPORT), see logs/php-api.log for access log."
2424
@echo ""
2525
@echo "Run make stop to stop it."
2626
@echo ""
27-
YII_ENV=dev php $(PHPARGS) $(XDEBUGARGSW) -S localhost:$(WEBPORT) -t web $(current_dir)/router.php >php.log 2>&1 & echo "$$!" > php.pid
27+
YII_ENV=dev php $(PHPARGS) $(XDEBUGARGSW) -S localhost:$(WEBPORT) -t web $(current_dir)/router.php >../logs/php-api.log 2>&1 & echo "$$!" > php.pid
2828

2929
stop:
3030
@echo "Stopping PHP webserver..."

backend/Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ XDEBUGW=0
88
WEBPORT=8338
99

1010
ifeq ($(XDEBUG),1)
11-
XDEBUGARGS=-dzend_extension=xdebug.so -dxdebug.remote_enable=1
11+
XDEBUGARGS=-dzend_extension=xdebug.so -dxdebug.mode=debug,develop -dxdebug.start_with_request=yes -dxdebug.discover_client_host=yes -dxdebug.remote_enable=1 -dxdebug.remote_autostart=1
1212
endif
1313
ifeq ($(XDEBUGW),1)
14-
XDEBUGARGSW=-dzend_extension=xdebug.so -dxdebug.remote_enable=1
14+
XDEBUGARGSW=-dzend_extension=xdebug.so -dxdebug.mode=debug,develop -dxdebug.start_with_request=yes -dxdebug.discover_client_host=yes -dxdebug.remote_enable=1 -dxdebug.remote_autostart=1
1515
endif
1616

1717

@@ -20,11 +20,11 @@ current_dir=$(shell pwd)
2020
default:
2121

2222
start: stop
23-
@echo "Starting PHP webserver at http://localhost:$(WEBPORT), see php.log for access log."
23+
@echo "Starting PHP webserver at http://localhost:$(WEBPORT), see logs/php-backend.log for access log."
2424
@echo ""
2525
@echo "Run make stop to stop it."
2626
@echo ""
27-
YII_ENV=dev php $(PHPARGS) $(XDEBUGARGSW) -S localhost:$(WEBPORT) -t web $(current_dir)/router.php >php.log 2>&1 & echo "$$!" > php.pid
27+
YII_ENV=dev php $(PHPARGS) $(XDEBUGARGSW) -S localhost:$(WEBPORT) -t web $(current_dir)/router.php >../logs/php-backend.log 2>&1 & echo "$$!" > php.pid
2828

2929
stop:
3030
@echo "Stopping PHP webserver..."

backend/config/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/cookie-validation.key

backend/config/components.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
return [
88
'request' => [
9-
'cookieValidationKey' => 'api1337', // TODO this should be dynamic
9+
'cookieValidationKey' => file_get_contents(__DIR__ . '/cookie-validation.key'),
1010
],
1111

1212
'urlManager' => [

0 commit comments

Comments
 (0)