top of page

Symfony/PHP/Apache avec Docker et Docker-compose

  • Photo du rédacteur: Jérémy Guyenot
    Jérémy Guyenot
  • 11 juil. 2024
  • 4 min de lecture

Pour un projet j'ai eu besoin de configurer un environement docker pour Symfony 5, PHP 8 sous Apache, du coup je vous propose ma config.


Création du fichier docker-compose.yml


version: "3.7"

x-common-env: &common-env
  TZ: Europe/Paris

services:

  redis:
    image: redis
    environment: *common-env

  mariadb:
    image: mariadb:10.6.4
    command: ["--wait_timeout=30"]
    environment:
      <<: *common-env
      MYSQL_ROOT_PASSWORD: R00t
    volumes:
      - mariadb-data:/var/lib/mysql
    ports: ["3306:3306"]

  adminer:
    image: adminer
    ports: ["8080:8080"]
    environment:
      <<: *common-env
      ADMINER_DEFAULT_SERVER: mariadb

  php:
    volumes:
      - composer-cache:/root/.composer/cache
      - .:/usr/src
      - ./docker/php/vhosts:/etc/apache2/sites-enabled
    environment:
      - XDEBUG_CONFIG=client_host=host.docker.internal client_port=9000
      - PHP_IDE_CONFIG=serverName=actuweb-php
    working_dir: /usr/src
    ports: ["8000:8000", "9003:9003"]
    restart: always
    build:
      context: ./docker/php
      dockerfile: Dockerfile
      args:
        UID: ${PHP_UID}
        GID: ${PHP_GID}
    links:
      - "mariadb"
      - "redis"

volumes:
  mariadb-data:
  composer-cache:

Creation du DockerFile PHP


FROM php:8.0-apache
LABEL maintainer="rfischer@grouperf.com"

ARG UID=1000
ARG GID=1000

USER root

RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf

RUN apt-get update \
    && apt-get install -y --no-install-recommends locales apt-utils git libicu-dev g++ libpng-dev libxml2-dev libzip-dev libonig-dev libxslt-dev

RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && \
    echo "fr_FR.UTF-8 UTF-8" >> /etc/locale.gen && \
    locale-gen

# Base and build tools
RUN apt-get update -yqq && \
    apt-get install -yqq \
        gnupg \
        curl \
        wget \
        zip \
        unzip \
        git \
        sshpass \
        rsync \
        build-essential \
        autoconf \
        zlib1g \
        zlib1g-dev \
        re2c \
        g++ \
    && true

### PHP extensions

# Install PHP GMP
RUN apt-get update -yqq && \
    apt-get install -yqq libgmp-dev && \
    case $PHP_VERSION in 5.6.*) ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h ;; esac && \
    docker-php-ext-install gmp

# Install PHP PDO
RUN docker-php-ext-install pdo

# Install PHP PDO-MySQL
RUN apt-get update -yqq && \
    apt-get install -yqq mariadb-client && \
    docker-php-ext-install pdo_mysql

# Install PHP MySQLi
RUN docker-php-ext-install mysqli

# Install PHP Zip
RUN apt-get update -yqq && \
    apt-get install -yqq libzip-dev && \
    case $PHP_VERSION in 7.2.*) docker-php-ext-configure zip --with-libzip ;; esac && \
    docker-php-ext-install zip

# Install PHP cURL
RUN apt-get update -yqq && \
    apt-get install -yqq libcurl4-gnutls-dev && \
    docker-php-ext-install curl

# Install PHP BCMath
RUN docker-php-ext-install bcmath

# Install PHP Sockets
RUN docker-php-ext-install sockets

# Install PHP Redis
RUN apt-get update -yqq && \
    apt-get install -yqq libzstd-dev libzstd1 && \
    case $PHP_VERSION in 5.*.*) pecl install -o -f igbinary-2.0.7 ;; *) pecl install igbinary ;; esac && \
    case $PHP_VERSION in 5.*.*) yes | pecl install -o -f redis-4.3.0 ;; *) yes | pecl install redis ;; esac && \
    docker-php-ext-enable igbinary && \
    docker-php-ext-enable redis

# Install PHP AMQP
#RUN apt-get update -yqq && \
#    apt-get install -yqq librabbitmq-dev && \
#    pecl install -o -f amqp$(case $PHP_VERSION in 8.*) printf "%s" "-beta" ;; esac) && \
#    docker-php-ext-enable amqp

# Install PHP GD
RUN apt-get update -yqq && \
    apt-get install -yqq libpng-dev libjpeg62-turbo-dev libfreetype6-dev && \
    case $PHP_VERSION in \
        7.[2-3].*) docker-php-ext-configure gd --with-gd --with-png-dir --with-zlib-dir --with-jpeg-dir --with-freetype-dir ;; \
        *) docker-php-ext-configure gd --enable-gd --with-jpeg --with-freetype ;; \
    esac && \
    docker-php-ext-install gd

# Install PHP BZ2
RUN apt-get update -yqq && \
    apt-get install -yqq libbz2-dev && \
    docker-php-ext-install -j$(nproc) bz2

# Install PHP Exif
RUN docker-php-ext-install -j$(nproc) exif
# Install PHP fileinfo
RUN docker-php-ext-install -j$(nproc) fileinfo
# Install PHP gettext
RUN docker-php-ext-install -j$(nproc) gettext

# SOAP / XMLRPC / XSL 
RUN apt-get update -yqq && \
    apt-get install -yqq libxml2-dev libssl-dev libxslt-dev && \
    docker-php-ext-install -j$(nproc) soap && \
#    docker-php-ext-install -j$(nproc) xmlrpc && \
    docker-php-ext-install -j$(nproc) xsl

# Install PHP Intl
RUN docker-php-ext-install -j$(nproc) intl

# Use dev recommended settings
RUN cp $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini

# Install Composer
RUN php -r "copy('https://getcomposer.org/installer', '/tmp/composer-setup.php');" && \
    test "$(php -r "echo hash_file('sha384', '/tmp/composer-setup.php');")" = "$(wget -q -O - https://composer.github.io/installer.sig)" && \
    php /tmp/composer-setup.php --2 --install-dir=/usr/local/bin --filename=composer

# Install XDebug
RUN pecl install xdebug$(case $PHP_VERSION in 7.*) printf "%s" "-2.9.8" ;; 5.*) printf "%s" "-2.5.5" ;; esac) && \
    docker-php-ext-enable xdebug

# Avoid future problems, see https://github.com/debuerreotype/debuerreotype/issues/10
RUN for i in $(seq 1 8) ; do mkdir -p /usr/share/man/man$i ; done

# Install Symfony
RUN wget https://get.symfony.com/cli/installer && \
    bash installer --install-dir=/usr/local/bin


# Set Timezone
RUN printf '[PHP]\ndate.timezone = "Europe/Paris"\n' > /usr/local/etc/php/conf.d/tzone.ini
RUN printf 'xdebug.mode = debug\nxdebug.start_with_request = trigger\nxdebug.client_port = 9003\nxdebug.discover_client_host = yes\n' > /usr/local/etc/php/conf.d/99-xdebug.ini

# Install NodeJS
RUN curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
    apt-get install -yqq nodejs && \
    node -v && \
    npm -v

# Set user
RUN groupadd -g $GID devgroup
RUN useradd -m -g $GID -u $UID -s /bin/bash devuser

USER devuser

ENTRYPOINT ["docker-php-entrypoint"]

Création du docker/php/vhosts/vhosts.conf


<VirtualHost *:8000>
    ServerName localhost

    DocumentRoot /usr/src/public
    DirectoryIndex /index.php

    <Directory /usr/src/public>
        AllowOverride None
        Order Allow,Deny
        Allow from All

        FallbackResource /index.php
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /usr/src/project>
    #     Options FollowSymlinks
    # </Directory>

    # optionally disable the fallback resource for the asset directories
    # which will allow Apache to return a 404 error when files are
    # not found instead of passing the request to Symfony
    <Directory /usr/src/public/bundles>
        FallbackResource disabled
    </Directory>
    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined

    # optionally set the value of the environment variables used in the application
    #SetEnv APP_ENV prod
    #SetEnv APP_SECRET <app-secret-id>
    #SetEnv DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name"
</VirtualHost>


Article d'aide: https://yoandev.co/un-environnement-de-developpement-symfony-5-avec-docker-et-docker-compose

 
 
 

Comments


Post: Blog2_Post

©2020 par Jérémy Guyenot. Créé avec Wix.com

bottom of page