How to create a docker project with PHP-5.6 MySQL Apache with SSL
Creating a Project Architecture is an art. There are many ways to make a docker project. I am adding the best approach I have found till now.
The Dockerfile
Create a file named Dockerfile
. Content should be:
FROM php:5.6-apache
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install mysqli
COPY ssl/server.key /etc/ssl/private/server.key
COPY ssl/server.crt /etc/ssl/certs/server.pem
RUN echo "ServerName localhost:80" >> /etc/apache2/apache2.conf
RUN sed -i 's/ssl-cert-snakeoil/server/' /etc/apache2/sites-available/default-ssl.conf
RUN a2enmod rewrite
RUN a2enmod headers
RUN a2enmod ssl
RUN a2ensite default-ssl
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
The docker-compose.yml file
Create a docker-compose.yml
file with the following content:
services:
mysqldb:
image: mysql:5.7.23
platform: linux/amd64
volumes:
- mysql_db_data:/var/lib/mysql
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: "MYSQL_ROOT_PASSWORD"
MYSQL_DATABASE: "MYSQL_DATABASE"
MYSQL_USER: "MYSQL_USER"
MYSQL_PASSWORD: "MYSQL_PASSWORD"
php-apache:
depends_on:
- mysqldb
build:
context: .
volumes:
- ./public_html:/var/www/html
ports:
- 80:80
- 443:443
volumes:
mysql_db_data: {}
You need to change the MySQL’s root password, database name, project-specific MySQL username and password in this file.
The SSL certificates
Create a folder in your project root named ssl
. And put your SSL certificates. You can see I had taken the SSL key as server.key
an SSL certificate as server.crt
in that folder. If you don’t have an SSL certificate, you can create a self-signed SSL certificate. I will make a separate article for that procedure.
The webroot
I have taken the public_html
folder to place the webroot folder in the project root. You need to change the database connection details in your code and use the credentials which you have chosen in the docker-compose.yml
file.
The project folder structure
Now your project root folder should look like this:
Project Root
|- public_html
|- ssl
|- server.key
|- server.crt
|- docker-compose.yml
|- Dockerfile
Start your docker container
Now it’s time to run your project. Open a terminal/command prompt and cd to your project root. Run the following command: docker compose up -d --build
. This command takes a few minutes to complete. When it will be completed, you can use your browser with http://localhost
or https://localhost
to run your website.