diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6887b05 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +app +data diff --git a/README.md b/README.md index 89bc020..f12d5c5 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ -Docker environment for a Symfony4 project +Docker environment for a Symfony5 project ================================== # Add to your project -Move the `docker-compose.yml` and the folder named `phpdocker` containing nginx and php-fpm config for it to the root of your Symfony4 project. +Move the `docker-compose.yml` and the folder named `docker_build` containing nginx and php-fpm config for it to the root of your Symfony project. -Ensure the webserver config on `docker\nginx.conf` is correct for your project. For instance, for Symfony4 it should look for the `public/index.php`, instead of the `web/app.php` from Symfony2 and Symfony3 +Ensure the webserver config on `docker\nginx.conf` is correct for your project. For instance, for Symfony 4 or 5 it should look for the `public/index.php`, instead of the `web/app.php` from Symfony2 and Symfony3 Note: you may place the files elsewhere in your project. Make sure you modify the locations for the php-fpm dockerfile, the php.ini overrides and nginx config on `docker-compose.yml` if you do so. - + # How to run Dependencies: @@ -28,9 +28,9 @@ Once you're done, simply `cd` to your project and run `docker-compose up -d`. Th * Kill containers: `docker-compose kill` * View container logs: `docker-compose logs` * Execute command inside of container: `docker-compose exec SERVICE_NAME COMMAND` where `COMMAND` is whatever you want to run. Examples: - * Shell into the PHP container, `docker-compose exec php-fpm bash` - * Run symfony console, `docker-compose exec php-fpm bin/console` - * Open a mysql shell, `docker-compose exec mysql mysql -uroot -pCHOSEN_ROOT_PASSWORD` + * Shell into the PHP container, `docker-compose exec php bash` + * Run symfony console, `docker-compose exec php bin/console` + * Open a postgresql shell, `docker-compose exec db psql --user admin postgres` # Docker general cheatsheet @@ -41,6 +41,30 @@ Once you're done, simply `cd` to your project and run `docker-compose up -d`. Th * To clear volumes: `docker volume rm $(docker volume ls -q)` * To clear networks: `docker network rm $(docker network ls | tail -n+2 | awk '{if($2 !~ /bridge|none|host/){ print $1 }}')` - - Disclaimer: This project has been generated on phpdocker.io + + +# Installation + +* `docker-compose up -d` +* in php container `docker-compose exec php bash` + * `composer create-project symfony/skeleton symfony` + * `mv symfony/* .` + * `mv symfony/.* .` + * `rm -Rf symfony` + * `composer require annotations` + * `composer require --dev profiler` + * `composer require twig` + * `composer require orm` + * `composer require form` + * `composer require form validator` + * `composer require maker-bundle` +* `docker-compose down` +* adapt querystring in `./app/.env`: + ``` + --- DATABASE_URL="postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=13&charset=utf8" + +++ DATABASE_URL="postgresql://admin:secret@db:5432/postgres?serverVersion=12&charset=utf8" + ``` +* `docker-compose up -d` +* in php container `docker-compose exec php bash` + * `bin/console doctrine:schema:create` diff --git a/docker-compose.yml b/docker-compose.yml index cd9fc3e..c71fb1d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,38 +1,37 @@ -############################################################################### -# Generated on phpdocker.io # -############################################################################### -version: "3.1" +version: "3.7" services: - mysql: - image: mysql:8.0 - container_name: docker-symfony4-mysql - working_dir: /application + db: + image: postgres:12 + container_name: docker-sf5-postgres volumes: - - .:/application + #- ./data:/var/lib/postgresql/data + - ./docker_build/db/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d environment: - - MYSQL_ROOT_PASSWORD=dbrootpw - - MYSQL_DATABASE=docker_symfony4 - - MYSQL_USER=dbuser - - MYSQL_PASSWORD=dbpw + - "POSTGRES_USER=admin" + - "POSTGRES_PASSWORD=secret" + - "POSTGRES_DB=postgres" ports: - - "8002:3306" + - "5432:5432" - webserver: - image: nginx:alpine - container_name: docker-symfony4-webserver - working_dir: /application + nginx: + image: nginx:latest + container_name: docker-sf5-nginx + working_dir: /var/www/app volumes: - - .:/application - - ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf + - ./app:/var/www/app + - ./docker_build/nginx/nginx.conf:/etc/nginx/conf.d/default.conf ports: - "8000:80" - php-fpm: - build: phpdocker/php-fpm - container_name: docker-symfony4-php-fpm - working_dir: /application + php: &php-fpm + build: + context: ./docker_build/php + args: + UID: 1000 + container_name: docker-sf5-php + image: php-fpm_sf5:latest + working_dir: /var/www/app volumes: - - .:/application - - ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini - + - ./app:/var/www/app + - ./docker_build/php/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini diff --git a/docker_build/db/docker-entrypoint-initdb.d/000_add_extensions.sql b/docker_build/db/docker-entrypoint-initdb.d/000_add_extensions.sql new file mode 100644 index 0000000..e69de29 diff --git a/phpdocker/nginx/nginx.conf b/docker_build/nginx/nginx.conf similarity index 65% rename from phpdocker/nginx/nginx.conf rename to docker_build/nginx/nginx.conf index e650838..b781316 100644 --- a/phpdocker/nginx/nginx.conf +++ b/docker_build/nginx/nginx.conf @@ -3,10 +3,10 @@ server { client_max_body_size 108M; - access_log /var/log/nginx/application.access.log; + access_log /var/log/nginx/app.access.log; - root /application/public; + root /var/www/app/public; index index.php; if (!-e $request_filename) { @@ -14,13 +14,13 @@ server { } location ~ \.php$ { - fastcgi_pass php-fpm:9000; + fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; - fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log"; + fastcgi_param PHP_VALUE "error_log=/var/log/nginx/app_php_errors.log"; fastcgi_buffers 16 16k; fastcgi_buffer_size 32k; include fastcgi_params; } - + } diff --git a/phpdocker/php-fpm/Dockerfile b/docker_build/php/Dockerfile similarity index 89% rename from phpdocker/php-fpm/Dockerfile rename to docker_build/php/Dockerfile index 353215a..2adfc2a 100644 --- a/phpdocker/php-fpm/Dockerfile +++ b/docker_build/php/Dockerfile @@ -1,5 +1,5 @@ FROM phpdockerio/php72-fpm:latest -WORKDIR "/application" +WORKDIR "/var/www/app" # Install selected extensions and other stuff RUN apt-get update \ @@ -9,4 +9,4 @@ RUN apt-get update \ # Install git RUN apt-get update \ && apt-get -y install git \ - && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* \ No newline at end of file + && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* diff --git a/phpdocker/php-fpm/php-ini-overrides.ini b/docker_build/php/php-ini-overrides.ini similarity index 100% rename from phpdocker/php-fpm/php-ini-overrides.ini rename to docker_build/php/php-ini-overrides.ini diff --git a/phpdocker/README.html b/phpdocker/README.html deleted file mode 100644 index 2c2a734..0000000 --- a/phpdocker/README.html +++ /dev/null @@ -1,125 +0,0 @@ - - - PHPDocker.io Readme - - - - - - -
-
-
-

PHPDocker.io generated environment

- -

Add to your project

- -

Simply, unzip the file into your project, this will create docker-compose.yml on the root of your project and a folder named phpdocker containing nginx and php-fpm config for it.

- -

Ensure the webserver config on docker\nginx.conf is correct for your project. PHPDocker.io will have customised this file according to the application type you chose on the generator, for instance web/app|app_dev.php on a Symfony project, or public/index.php on generic apps.

- -

Note: you may place the files elsewhere in your project. Make sure you modify the locations for the php-fpm dockerfile, the php.ini overrides and nginx config on docker-compose.yml if you do so.

- -

How to run

- -

Dependencies:

- - - -

Once you're done, simply cd to your project and run docker-compose up -d. This will initialise and start all the containers, then leave them running in the background.

- -

Services exposed outside your environment

- -

You can access your application via localhost, if you're running the containers directly, or through `` when run on a vm. nginx and mailhog both respond to any hostname, in case you want to add your own hostname on your /etc/hosts

- - - - - - - - - - - - - - - - - - -
ServiceAddress outside containers
Webserverlocalhost:8000
MySQLhost: localhost; port: 8002
- -

Hosts within your environment

- -

You'll need to configure your application to use any services you enabled:

- - - - - - - - - - - - - - - - - - - - - -
ServiceHostnamePort number
php-fpmphp-fpm9000
MySQLmysql3306 (default)
- -

Docker compose cheatsheet

- -

Note: you need to cd first to where your docker-compose.yml file lives.

- - - -

Recommendations

- -

It's hard to avoid file permission issues when fiddling about with containers due to the fact that, from your OS point of view, any files created within the container are owned by the process that runs the docker engine (this is usually root). Different OS will also have different problems, for instance you can run stuff in containers using docker exec -it -u $(id -u):$(id -g) CONTAINER_NAME COMMAND to force your current user ID into the process, but this will only work if your host OS is Linux, not mac. Follow a couple of simple rules and save yourself a world of hurt.

- - -
-
-
- - - - diff --git a/phpdocker/README.md b/phpdocker/README.md deleted file mode 100644 index 2d2d1e2..0000000 --- a/phpdocker/README.md +++ /dev/null @@ -1,58 +0,0 @@ -PHPDocker.io generated environment -================================== - -# Add to your project# - -Simply, unzip the file into your project, this will create `docker-compose.yml` on the root of your project and a folder named `phpdocker` containing nginx and php-fpm config for it. - -Ensure the webserver config on `docker\nginx.conf` is correct for your project. PHPDocker.io will have customised this file according to the application type you chose on the generator, for instance `web/app|app_dev.php` on a Symfony project, or `public/index.php` on generic apps. - -Note: you may place the files elsewhere in your project. Make sure you modify the locations for the php-fpm dockerfile, the php.ini overrides and nginx config on `docker-compose.yml` if you do so. - -# How to run - -Dependencies: - - * Docker engine v1.13 or higher. Your OS provided package might be a little old, if you encounter problems, do upgrade. See [https://docs.docker.com/engine/installation](https://docs.docker.com/engine/installation) - * Docker compose v1.12 or higher. See [docs.docker.com/compose/install](https://docs.docker.com/compose/install/) - -Once you're done, simply `cd` to your project and run `docker-compose up -d`. This will initialise and start all the containers, then leave them running in the background. - -## Services exposed outside your environment - -You can access your application via **`localhost`**, if you're running the containers directly, or through **``** when run on a vm. nginx and mailhog both respond to any hostname, in case you want to add your own hostname on your `/etc/hosts` - -Service | Address outside containers ------- | --------- -Webserver | [localhost:8000](http://localhost:8000) -MySQL | **host:** `localhost`; **port:** `8002` - -## Hosts within your environment - -You'll need to configure your application to use any services you enabled: - -Service|Hostname|Port number -------|---------|----------- -php-fpm|php-fpm|9000 -MySQL|mysql|3306 (default) - -# Docker compose cheatsheet# - -**Note:** you need to cd first to where your docker-compose.yml file lives. - - * Start containers in the background: `docker-compose up -d` - * Start containers on the foreground: `docker-compose up`. You will see a stream of logs for every container running. - * Stop containers: `docker-compose stop` - * Kill containers: `docker-compose kill` - * View container logs: `docker-compose logs` - * Execute command inside of container: `docker-compose exec SERVICE_NAME COMMAND` where `COMMAND` is whatever you want to run. Examples: - * Shell into the PHP container, `docker-compose exec php-fpm bash` - * Run symfony console, `docker-compose exec php-fpm bin/console` - * Open a mysql shell, `docker-compose exec mysql mysql -uroot -pCHOSEN_ROOT_PASSWORD` - -# Recommendations - -It's hard to avoid file permission issues when fiddling about with containers due to the fact that, from your OS point of view, any files created within the container are owned by the process that runs the docker engine (this is usually root). Different OS will also have different problems, for instance you can run stuff in containers using `docker exec -it -u $(id -u):$(id -g) CONTAINER_NAME COMMAND` to force your current user ID into the process, but this will only work if your host OS is Linux, not mac. Follow a couple of simple rules and save yourself a world of hurt. - - * Run composer outside of the php container, as doing so would install all your dependencies owned by `root` within your vendor folder. - * Run commands (ie Symfony's console, or Laravel's artisan) straight inside of your container. You can easily open a shell as described above and do your thing from there.