How to Create PHP Development Environments with Docker Compose
In this talk, presented at DockerCon Online 2020, I demonstrate how you can create a PHP development environment with multiple services using Docker Compose, and a Laravel demo application as case study.
multi-container application environments based on definitions set in a YAML file. It uses service definitions to build fully customizable environments with multiple containers that can share networks and data volumes. • Easy to set up and manage • Multiple composed envs on a single host • Preserves volume data • Variables to allow customization
"8000:80" This simple docker-compose.yml file creates a service container based on an "nginx" image, redirecting all requests on local port 8000 to port 80 in the container.
the application in our development environment. Database: MySQL The application uses a MySQL database to store places and mark them as "visited" or "to go". PHP-FPM PHP-FPM is required to parse the PHP content and return the results to Nginx.
extensions that should be installed on the application container. Artisan Commands We need the ability to run Artisan and Composer commands from the host machine. Composer PHP In order to install and update Laravel's dependencies, we'll need Composer installed on the application container.
container_name: travellist-app restart: unless-stopped working_dir: /var/www/ volumes: - ./:/var/www networks: - travellist This service will be responsible for parsing all PHP requests and command-line calls made with Composer and Artisan. It will build a new image based on the provided Dockerfile.
- 8000:80 volumes: - ./:/var/www - ./docker-compose/nginx:/etc/nginx/conf.d networks: - travellist This service will be responsible for serving the application via Nginx. A port redirection will allow us to access port 80 inside the container through port 8000 in the host.
MYSQL_DATABASE: ${DB_DATABASE} MYSQL_ROOT_PASSWORD: ${DB_PASSWORD} MYSQL_PASSWORD: ${DB_PASSWORD} MYSQL_USER: ${DB_USERNAME} SERVICE_TAGS: dev SERVICE_NAME: mysql ... This service will run a MySQL database to store the application's data. It will extract values from the .env Laravel file to create a new DB and user.
update && apt-get install -y \ git \ curl \ libpng-dev \ libonig-dev \ libxml2-dev \ zip \ unzip ... This Dockerfile will build a new image based on the php:7.4-fpm official PHP image. We start by installing some required system packages.
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd # Create system user to run Composer and Artisan Commands RUN useradd -G www-data,root -u $uid -d /home/$user $user RUN mkdir -p /home/$user/.composer && \ chown -R $user:$user /home/$user ... Variables are used to create a new user and grant them the same privileges as your current system user on the host, so that you don't run into permission issues with synced files.
# Set working directory WORKDIR /var/www USER $user We then install Composer, set the work dir and change to the new user, so that when we run commands via docker-compose exec, they are executed as the new system user.