From 00d6a6efbd37cf5f25d3c537f5280282bc8f2333 Mon Sep 17 00:00:00 2001 From: Tchama Date: Thu, 18 Feb 2021 17:46:47 +0100 Subject: [PATCH] improve initial config --- README.md | 63 +++++++++++++++++++++------------- docker-compose.yml | 8 ++--- docker_build/php/Dockerfile | 68 +++++++++++++++++++++++++++++++------ 3 files changed, 101 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index f12d5c5..fb3ceb3 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,10 @@ 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 bash` - * Run symfony console, `docker-compose exec php bin/console` - * Open a postgresql shell, `docker-compose exec db psql --user admin postgres` + * Shell into the PHP container, `docker-compose exec --user 1000 php bash` + * Run symfony console, `docker-compose exec --user 1000 php bin/console` + * Open a postgresql shell, `docker-compose exec --user postgres db psql` + * or if user is not postgres: `docker-compose exec db psql --user admin postgres` # Docker general cheatsheet @@ -44,27 +45,43 @@ Once you're done, simply `cd` to your project and run `docker-compose up -d`. Th Disclaimer: This project has been generated on phpdocker.io -# Installation +# My 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`: +* Build containers: `docker-compose build` +* Run containers: `docker-compose up -d` +* Enter in php container `docker-compose exec --user 1000 php bash` + * Install Symfony: `composer create-project symfony/skeleton symfony` + * Move project in parent (app): `mv symfony/* . && mv symfony/.* . && rm -Rf symfony` + * Add Symfony requirements: + ``` + composer require annotations + composer require --dev profiler + composer require twig + composer require orm + composer require form + composer require form validator + composer require maker-bundle + composer require security-csrf + ``` +* Stop containers: `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" + +++ DATABASE_URL="postgresql://postgres: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` +* Run containers: `docker-compose up -d` +* Enter in php container `docker-compose exec --user 1000 php bash` + * Test psql connection: `psql --host db --user postgres --password postgres` + * Create database schema: `bin/console doctrine:schema:create` + +## Let's go ! + +* Let's create first object: + * Create entity: `bin/console make:entity Beer` + * Create CRUD on entity: `bin/console make:crud Beer` + * See sql: `bin/console doctrine:schema:update --dump-sql` + * Control migrations: `bin/console doctrine:migrations:status` + * Generate migration file: `bin/console doctrine:migrations:diff` + * See migration file: `cat migrations/Version20210218160541.php ` + * Migrate: `bin/console doctrine:migrations:migrate` + * Control migrations: `bin/console doctrine:migrations:status` diff --git a/docker-compose.yml b/docker-compose.yml index c71fb1d..d8b2654 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,12 +3,12 @@ services: db: image: postgres:12 - container_name: docker-sf5-postgres volumes: - #- ./data:/var/lib/postgresql/data + # comment to keep data in docker volumes + - ./data:/var/lib/postgresql/data - ./docker_build/db/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d environment: - - "POSTGRES_USER=admin" + - "POSTGRES_USER=postgres" - "POSTGRES_PASSWORD=secret" - "POSTGRES_DB=postgres" ports: @@ -16,7 +16,6 @@ services: nginx: image: nginx:latest - container_name: docker-sf5-nginx working_dir: /var/www/app volumes: - ./app:/var/www/app @@ -29,7 +28,6 @@ services: context: ./docker_build/php args: UID: 1000 - container_name: docker-sf5-php image: php-fpm_sf5:latest working_dir: /var/www/app volumes: diff --git a/docker_build/php/Dockerfile b/docker_build/php/Dockerfile index 2adfc2a..ee2f7e5 100644 --- a/docker_build/php/Dockerfile +++ b/docker_build/php/Dockerfile @@ -1,12 +1,60 @@ -FROM phpdockerio/php72-fpm:latest +FROM php:7.3-fpm-buster + +# Set the lifetime of a PHP session +ARG SESSION_LIFETIME=10800 + +# Set default UID for the PHP user +ARG UID=1000 +ARG GID=1000 + +# Install postgresql client +# and compile docker php extensions +RUN apt update \ + && apt -y --no-install-recommends install \ + wget \ + gnupg \ + && wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \ + && echo "deb http://apt.postgresql.org/pub/repos/apt/ buster-pgdg main" > /etc/apt/sources.list.d/pgdg.list \ + && apt update \ + && apt -y --no-install-recommends install \ + libicu-dev \ + g++ \ + libzip-dev \ + libzip4 \ + unzip \ + libfreetype6-dev \ + libpng-dev \ + libjpeg62-turbo-dev \ + libpq5 \ + libpq-dev \ + postgresql-12 \ + git \ + && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ + && docker-php-ext-install -j$(nproc) gd \ + && docker-php-ext-install intl pdo_pgsql mbstring zip bcmath sockets exif \ + && apt remove -y \ + wget \ + gnupg \ + libicu-dev \ + g++ \ + libzip-dev \ + libpq-dev \ + && apt autoremove -y \ + && apt purge -y + +# Set php timezone +RUN { \ + echo ""; \ + echo "[Date]"; \ + echo "date.timezone = Europe/Brussels"; \ + echo ""; \ + } >> /usr/local/etc/php/conf.d/date.ini + +# Install composer +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer + WORKDIR "/var/www/app" -# Install selected extensions and other stuff -RUN apt-get update \ - && apt-get -y --no-install-recommends install php7.2-mysql \ - && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* - -# 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/* +# Add users/groups with uid 1000 +RUN groupadd --gid ${GID} "group${GID}" \ + && useradd --uid ${UID} --gid ${GID} --create-home "user${UID}"