Docker environement stack with APACHE, MYSQL (MariaDB), PHP (FPM)
Requirement docker-compose
- Apache version: 2.4
- PHP version: 7.3
- MariaDB version: 10.1
- httpd:2.4-alpine
- php:7.3-fpm-alpine
- mariadb:10.1
docker-amp
│ .env
│ .gitignore
│ docker-compose.yml
│ LICENSE
│ Makefile
│ README.md
│
├───app
│ index.php
│ info.php
│
├───db
├───docker
│ ├───mariadb
│ │ │ Dockerfile
│ │ │
│ │ └───init
| | 000-operations.sql
│ │ dbapp.sql
| | zzz-operations.sql
│ │
│ ├───apache
│ │ │ app.conf
│ │ │ Dockerfile
│ │ │
│ │ └───certs
│ │
│ └───php
│ Dockerfile
│ php.ini
│ www.conf
│
└───logs
├───db
├───php
└───web
Edit or view this file before building the stack
Variable description
PROJECT_NAME
: Name of your docker stackVERSION_ID
: ID of your project stack. (Use it only if you want try more version of your project)XDEBUG
: Using1
if you want install xdegub module, otherwise using0
.COMPOSER
: Using1
if you want install php composer, otherwise using0
.TIME_ZONE
: Time zone, using form PHP and MySQL.APP_URL
: Url of your web app. Using for CMS/Frameweork like Wordpress, CodeIgniter, etc.HTTP_PORT_EXPOSED
: Exposed port of you're HTTP web app.HTTPS_PORT_EXPOSED
: Exposed port of you're HTTPS SSL web app. Required certificates.DB_PORT_EXPOSED
: Used only if you want connetc to database with external client. It's port number used for connetcting with external client like HeidiSQL or MySQL Workbench to create database and tables. Configure external client connetction using host127.0.0.1
, usernameroot
whereas password yourDB_ROOTPASS
and port yourDB_PORT_EXPOSED
as in environment file.DB_ROOTPASS
: MySQL root password. It's not safe use this method in production but for development it's acceptable.DB_USER
: User name for connetion database from your webapp.DB_USER
: User password for connetion database from your webapp.DB_NAME
: Database of your webapp. Will be created at the build. Otherwise you can create database from docker exec or external client.
This is you WebApp folder. Put here your PHP project (CMS/Framework/Sources).
For connetcting to databse with Framework or source code use getenv()
variables (view index.php
source).
$dbhost = getenv('DB_HOST');
$dbuser = getenv('DB_USER');
$dbpass = getenv('DB_PASS');
For install a CMS with wizard setup use:
- hostname:
db
- database: the environment variable
DB_NAME
as in environment file - username: the environment variable
DB_USER
as in environment file - password: the environment variable
DB_PASS
as in environment file - port:
3306
it's important to declare it!
If you haven't this folder, will be it create at the first build.
db
folder contains your project's database, don't modify the files inside it. If you need to move your project enviroment, remember to move this folder with its content.
If you don't want the databases to be inside the project folder use the docker-compose volumes.
Writing logs to files is disabled by default. To enable it, read the Note.
The stack has also been tested with PHP 7.1 and MySQL 5.7, MariaDB 10.*. You can change these variables directly in the Dockerfile.
The first time of build after finish if you not connect to database aptendm some minute because the container must be inizilize all system.
If you want to use persistent and encrypted databases storage, you can use the docker volumes
by editing the docker-compose.yml
as below.
# ...
services:
# ...
db:
# ...
volumes:
# - ./db:/var/lib/mysql
- database:/var/lib/mysql
# ...
# add after services
volumes:
database:
For enable DB logs edit docker/mariadb/Dockerfile
uncomment this command
RUN sed -i "s/#log-error/log-error/g" /etc/mysql/mysql.conf.d/mysqld.cnf
If you've a database dump you can add the .sql
, .gz
dump file in docker\mariadb\init
and uncomment the follow row. The database will be created at the first build.
# ...
services:
# ...
db:
# ...
volumes:
- ./docker/mariadb/init:/docker-entrypoint-initdb.d
# ...
Also, if you need to perform other operations on databases at the first build, you can edit the following files by entering the queries that you need:
docker\mariadb\init\000-operations.sql
docker\mariadb\init\zzz-operations.sql
These operations will only be performed on the first build
When stack is ready visit http://localhost
For show PHP info go to http://localhost/info.php
If you're on linux system you can run:
make install
- builds and init your stackmake start
- start your stackmake stop
- stop your stackmake remove
- remove your stack but mantain your databases and logsmake purge
- remove your stack and delete all databases and clear logs filesmake validate
- validate your docker-compose.yml file if you're do any changesmake bash-db
- enter with bash into db containermake bash-php
- enter with bash into php containermake bash-web
- enter with bash into web container
Command in nutshell for build, start, stop, remove and validate the docker stack.
-
docker-compose up -d --build
- builds and init your stack -
docker-compose down
- remove your stack but mantain your databases and logs -
docker-compose start
- start your stack -
docker-compose stop
- stop your stack -
docker-compose down --volumes
- like down but also removes volumes -
docker-compose config
- validate your stack -
docker-compose exec SERVICE_NAME bash
enters with bash terminal into container