Welcome to weblogs.com.pk Sign in | Join | Help

Dockerizing PHP + MySQL Application Part 2

In the previous post we used mysql:5.6 official Docker Image for Database Container and created a Custom Dockerfile for the PHP Container. We had to expose the MySQL container’s ports and so we can connect to the database using the MySQL CLI to create the required WordPress database and execute the MySQL dump file. In production; on the Server or System Administrator’s machine these CLI tools might not be available and more importantly we dont want to expose the MySQL ports given that Web Container can be "linked" to it within Docker. We can sort this out by creating a “DB Helper” container that has the MySQL CLI and its job is to wait till MySQL “server” image spins up and then connect to it and create the database and run the dump SQL script.

For this we will create a Shell script having the following code

#!/bin/bash
while ! mysql -h db -u root --password=passwd -e 'quit'
    do sleep 10;
done;
mysql -h db -u root --password=passwd << EOF
create database wordpress;
use wordpress;
source wordpress.sql;
EOF

The Dockerfile for our “DB Helper” container will be

FROM ubuntu
RUN apt-get update
RUN apt-get install -y mysql-client
#RUN apt-get install -y nano

ADD wordpress.sql /tmp/wordpress.sql
ADD createdb.sh /tmp/createdb.sh

RUN chmod +x /tmp/createdb.sh
RUN sed -i -e 's/\r$//' /tmp/createdb.sh

WORKDIR /tmp
CMD /bin/bash createdb.sh

Given everything is “automated”; we can now create a docker-compose file using which we can define the whole environment. Here’s its contents

version: '2'

services:
    db:
        image: mysql:5.6
        restart: unless-stopped
        environment:
            MYSQL_ROOT_PASSWORD: passwd
    dbhelper:
        build:
            context: .
            dockerfile: Dockerfile.dbhelper
        image: wordpress/dbhelper
        links:
            - db
    web:
        build:
            context: .
            dockerfile: Dockerfile.web
        image: wordpress/web
        restart: unless-stopped
        links:
            - db
        ports:
            - "57718:80"

Once its in place; we can simply issue docker-compose up --build to spins up all the containers for the WordPress; the DB Helper container will create the required database and import the dump and our application will shortly be made available on the specified port; 57718 in this example

  • Use docker-compose up --build –d to launch the containers in the background in Production
  • Alternatively one can also use docker-compose build to create required containers and then docker-compose up to launch and docker-compose down to stop the imagesl
  • docker-compose doesn't get installed along the Docker Daemon; please refer to its installation instruction on how to install it on the Server
  • We can execute more SQL commands to change the host / site urls if we created the dump in the development / staging environment and want to have different host / site urls in the production using the DB Helper container.
Published Wednesday, January 04, 2017 4:45 PM by khurram
Filed under: , ,

Comments

No Comments

New Comments to this post are disabled