Reading time: 3 minutes
In this tutorial, I will show you how to create a docker-compose.yml
file to set up a complete local development environment with PHP, NGINX, MariaDB, Nginx Proxy Manager for SSL, and PhpMyAdmin.
Getting Started
The first thing we need to do is to create a directory on our machine and create a docker-compose.yml
file inside it. You can use your favorite text editor to do this. In this tutorial, we will use nano
as an example:
mkdir my-app cd my-app nano docker-compose.yml
Configuring the docker-compose.yml File
Once inside the docker-compose.yml
file, we start by configuring our network:
version: '3.9' networks: app-network: driver: bridge
In this case, we are using version 3.9
of Docker Compose. We then create a network with the name app-network
. This network will be used to connect all the containers we will create later.
Next, we create the services we need. In this example, we create four services: php
, nginx
, db
, and nginx-proxy-manager
.
services: php: image: php:7.4-cli networks: - app-network nginx: image: nginx #ports: #- "80:80" #- "443:443" depends_on: - php volumes: - ./webdata:/usr/share/nginx/html networks: - app-network db: image: mariadb restart: always environment: MYSQL_ROOT_PASSWORD: example MYSQL_DATABASE: example MYSQL_USER: example MYSQL_PASSWORD: example networks: - app-network nginx-proxy-manager: image: 'jc21/nginx-proxy-manager:latest' restart: unless-stopped ports: - '80:80' - '81:81' - '443:443' volumes: - ./data:/data - ./letsencrypt:/etc/letsencrypt networks: - app-network
PHP
The php
service is responsible for running our PHP code. In this example, we use a custom image that we created earlier and specify it in the image
key. We also specify the network that the service should connect to.
NGINX
The nginx
service is responsible for running the NGINX web server and handling HTTP and HTTPS requests. We also specify the 80
and 443
ports to make the service accessible from the host. Additionally, we use the depends_on
key to ensure that the php
service starts before the nginx
service. This is necessary because the NGINX web server needs the php-fpm
module to process PHP requests.
MariaDB
The db
service is responsible for running the MariaDB database server. In this example, we are using the official MariaDB image. We also specify the necessary environment variables to create a sample database and user.
Nginx Proxy Manager
The nginx-proxy-manager
service handles SSL certificates for our website and redirects HTTPS traffic to our NGINX server. We use the official Nginx Proxy Manager image and specify the 81
, `
443
, and 81
ports to make the service accessible from the host. Additionally, we specify a volume to store the Nginx Proxy Manager configuration on our local machine.
Running the docker-compose.yml File
Once we have finished configuring our docker-compose.yml
file, we can start our services with the following command:
docker-compose up -d
This command starts the services defined in our docker-compose.yml
file in detached mode. After running this command, we can see the containers that have been created with the following command:
docker ps
Accessing Our Services
After running the docker-compose up -d
command, we can access our services as follows:
Web Server
We can access our NGINX web server by entering the address http://localhost
or https://localhost
in our web browser.
Nginx Proxy Manager
We can access Nginx Proxy Manager by entering the address http://localhost:81
in our web browser. Here, we can configure SSL certificates and redirections for our website.
Conclusion
In this tutorial, we have created a docker-compose.yml
file to set up a complete local development environment with PHP, NGINX, MariaDB, Nginx Proxy Manager for SSL, and PhpMyAdmin. We have also explained how each service works and how to run our docker-compose.yml
file. You can now use this file as a template to create your own projects with Docker Compose.
Here is how to configure Nginx Proxy Manager separately:
SSL Certificates (Let’s Encrypt) in Docker using Nginx Proxy Manager
Don’t miss more Docker tutorials https://devcodelight.com/category/docker/