Setting up Docker for Laravel and MySQL application

This article will briefly show you how to use Docker for Laravel and MySQL application.

Create a .docker folder right inside the project’s root folder.

Then create a Dockerfile file with the following content.

FROM php:7.4.1-apacheUSER rootWORKDIR /var/www/htmlRUN apt-get update && apt-get install -y \libpng-dev \zlib1g-dev \libxml2-dev \libzip-dev \libonig-dev \zip \curl \unzip \&& docker-php-ext-configure gd \&& docker-php-ext-install -j$(nproc) gd \&& docker-php-ext-install pdo_mysql \&& docker-php-ext-install mysqli \&& docker-php-ext-install zip \&& docker-php-source deleteCOPY .docker/vhost.conf /etc/apache2/sites-available/000-default.confRUN curl -sS https://getcomposer.org/installer | php — — install-dir=/usr/local/bin — filename=composerRUN chown -R www-data:www-data /var/www/html \&& a2enmod rewrite

Then create a vhost.conf file with the following content.

<VirtualHost *:80>DocumentRoot /var/www/html/public<Directory “/var/www/html”>AllowOverride allRequire all granted</Directory>ErrorLog ${APACHE_LOG_DIR}/error.logCustomLog ${APACHE_LOG_DIR}/access.log combined</VirtualHost>

This is the example docker-compose.yaml file. You can change the variable names as you want.

version: ‘3’services:app:build:context: .dockerfile: .docker/Dockerfileimage: ‘laravelapp’ports:- 8081:80networks:- app-networkvolumes:- ./:/var/www/html- ./:/var/www/- ./ci:/var/www/ci:cached- ./vendor:/var/www/vendor:delegated- ./storage:/var/www/storage:delegated- ./node_modules:/var/www/node_modules:cached- ~/.ssh:/root/.ssh:cached- ./composer.json:/var/www/composer.json- ~/.composer/cache:/root/.composer/cache:delegateddb:container_name: app_dbimage: library/mariadb:10.4.11environment:MYSQL_ROOT_PASSWORD: rootMYSQL_DATABASE: appMYSQL_USER: appMYSQL_PASSWORD: secretvolumes:- app-data:/var/lib/mysqlnetworks:- app-networkports:- “33060:3306”networks:app-network:driver: “bridge”volumes:app-data:driver: “local”driver: “local”

That’s it. You can now spin up the environment by running the following command.

“docker-compose up — build -d”

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store