How to create a docker project with PHP-8.3 MySQL-8.0 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:8.3-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:
db:
image: mysql:8.0
container_name: "your_container_name"
ports:
- 3306:3306
environment:
MYSQL_DATABASE: "MYSQL_DATABASE"
MYSQL_USER: "MYSQL_USER"
MYSQL_PASSWORD: 'MYSQL_PASSWORD'
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql
wordpress:
build:
context: .
dockerfile: Dockerfile
ports:
- 80:80
- 443:443
depends_on:
- db
volumes:
- ./public_html:/var/www/html
volumes:
db:
You need to change the MySQL’s 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.