no

Docker Compose with Apache Php MySQL and Wordpress

I. Introduction In this article, we will look at a particular set of use cases using docker-compose. II. Docker Compose Architecture We w...

I. Introduction

In this article, we will look at a particular set of use cases using docker-compose.

II. Docker Compose Architecture

We will start by going through this diagram.
 

We have the MariaDB database in the center where almost all the other services will depend. Adminer and Phymyadmin are both database management tools that we can use to execute crud operations into the database.

And then we will create a web page that runs PHP script deployed on an HTTP server. This script will simply connect to our database.

Finally, we will run a WordPress image that will connect to the same database that we have.

III. MariaDB Docker Container

In this section, we will configure our docker-compose file to start our MariaDB database container.

To do so, we need to set the required environment variables and make sure to map the /var/lib/mysql to a volume, so that the data will be persisted on container restart.

version: "3.3" 

volumes:
    maria_db:

services:
    db:
        image: mariadb:10.5.2-bionic
        restart: always
        ports: 
            - "3306:3306"
        environment:
            MYSQL_DATABASE: wordpress
            MYSQL_USER: kerri
            MYSQL_PASSWORD: kerri
            MYSQL_ROOT_PASSWORD: kerri
        volumes:
            - maria_db:/var/lib/mysql
You can start the container by executing > docker-compose up.

III. Adding the Phpmyadmin and Adminer Images

The first step would be to add the networks and then the images:
version: "3.3"

networks:
    frontend:
    backend:     

volumes:
    maria_db:

services:
    db:
        image: mariadb:10.5.2-bionic
        restart: always
        ports: 
            - "3306:3306"
        environment:
            MYSQL_DATABASE: wordpress
            MYSQL_USER: kerri
            MYSQL_PASSWORD: kerri
            MYSQL_ROOT_PASSWORD: kerri
        volumes:
            - maria_db:/var/lib/mysql
        networks: 
            - backend
    adminer:
        image: adminer
        depends_on: 
            - db
        ports:
            - 8001:8080
        networks:
            - frontend
            - backend
    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        depends_on: 
            - db
        ports:
            - 8002:80
        environment:
            PMA_HOST: db
            MYSQL_ROOT_PASSWORD: kerri
        networks:
            - frontend
            - backend
Now try running: docker-compose up again and load the following URLs in your browser:
HTTP://localhost:8001 - adminer
HTTP://localhost:8001 - PHPMyAdmin

IV. Running Apache, Php and MariaDB Images

Let's remove the PHPMyAdmin and adminer and add the apache and PHP images.
version: "3.3"

networks:
    frontend:
    backend:     

volumes:
    maria_db:

services:
    db:
        image: mariadb:10.5.2-bionic
        restart: always
        ports: 
            - "3306:3306"
        environment:
            MYSQL_DATABASE: wordpress
            MYSQL_USER: kerri
            MYSQL_PASSWORD: kerri
            MYSQL_ROOT_PASSWORD: kerri
        volumes:
            - maria_db:/var/lib/mysql
        networks: 
            - backend
    php:
        build: './php/'
        depends_on:
            - db
        networks: 
            - backend
        volumes:
            - ./apache/public_html:/var/www/html/
    web:
        build: './apache/'
        depends_on:
            - php
            - db
        ports: 
            - "8000:80"
        networks: 
            - frontend
            - backend
        volumes:
            - ./apache/public_html/:/var/www/html/

Inside the folder where the docker-compose.yml is located, add the following folders and files

+apache
++public_html
+++index.php
++Dockerfile
+local.apache.conf
+php
++Dockerfile

Load the URL HTTP://localhost:8000 in your browser and you should see the index.php file connecting to the database.

*You can find these files in the Github repository in the reference section.

V. Running Wordpress with MariaDB

In this final configuration, we will try to run a Wordpress website connected to the MariaDB instance that we have using docker containers.
version: "3.3"

networks:
    frontend:
    backend:     

volumes:
    wordpress:
    maria_db:

services:
    db:
        image: mariadb:10.5.2-bionic
        restart: always
        ports: 
            - "3306:3306"
        environment:
            MYSQL_DATABASE: wordpress
            MYSQL_USER: kerri
            MYSQL_PASSWORD: kerri
            MYSQL_ROOT_PASSWORD: kerri
        volumes:
            - maria_db:/var/lib/mysql
        networks: 
            - backend
    wordpress:
        image: wordpress
        restart: always
        depends_on:
          - db
        ports:
          - 8080:80
        environment:
          WORDPRESS_DB_HOST: db
          WORDPRESS_DB_USER: kerri
          WORDPRESS_DB_PASSWORD: kerri
          WORDPRESS_DB_NAME: wordpress
        volumes:
          - wordpress:/var/www/html
        networks: 
            - frontend
            - backend
We need to specify the database connection settings in Wordpress so that it can connect to the MariaDB instance.

Run docker-compose up and load the URL HTTP://localhost:8080 in your browser.

VI. Reminders

Of course, you can run all the services at once, by compiling them into 1 docker-compose file. I deliberately use non-overlapping ports so that you can do that.

*DONT: Use non-fpm PHP version otherwise it won't work.

VII. Youtube Videos

VIII. References

  • https://hub.docker.com/_/mariadb
  • https://hub.docker.com/_/php
  • https://hub.docker.com/_/httpd
  • https://hub.docker.com/r/phpmyadmin/phpmyadmin/
  • https://hub.docker.com/_/adminer/
  • https://github.com/czetsuya/docker-apache-php-mysql-wordpress

Related

coding 7182971130363499747

Post a Comment Default Comments

item