Master Docker Middleware: Build PHP-FPM and MySQL Images Step‑by‑Step
This article walks you through preparing Docker, building a csphere/php-fpm:5.4 image with custom configuration files, creating a csphere/mysql:5.5 image, using ONBUILD, managing volumes, and running containers to demonstrate persistent data across container lifecycles.
Docker Practical Preparation
The preparation involves four steps: log in to OSChina Git, fork the docker‑training project, connect to the server via SSH, and clone your forked repository.
Build csphere/php-fpm:5.4 Image
# cd docker-training/php-fpm/
# ls
Dockerfile nginx_nginx.conf supervisor_nginx.conf
nginx_default.conf php_www.conf supervisor_php-fpm.confKey configuration files:
nginx_nginx.conf – replaces the default nginx.conf
nginx_default.conf – replaces the default site config
php_www.conf – sets the PHP‑FPM user to nginx
supervisor_nginx.conf – starts nginx via Supervisor
supervisor_php-fpm.conf – starts php‑fpm via Supervisor
Dockerfile excerpt:
#
# MAINTAINER Carson,C.J.Zeong <[email protected]>
# DOCKER-VERSION 1.6.2
#
FROM csphere/centos:7.1
ENV APP_DIR /app
RUN yum -y install nginx php-cli php-mysql php-pear php-ldap php-mbstring php-soap php-dom php-gd php-xmlrpc php-fpm php-mcrypt && yum clean all
ADD nginx_nginx.conf /etc/nginx/nginx.conf
ADD nginx_default.conf /etc/nginx/conf.d/default.conf
ADD php_www.conf /etc/php-fpm.d/www.conf
RUN sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php.ini
RUN mkdir -p /app && echo "<?php phpinfo(); ?>" > ${APP_DIR}/info.php
EXPOSE 80 443
ADD supervisor_nginx.conf /etc/supervisor.conf.d/nginx.conf
ADD supervisor_php-fpm.conf /etc/supervisor.conf.d/php-fpm.conf
ONBUILD ADD . /app
ONBUILD RUN chown -R nginx:nginx /appThe ONBUILD instruction runs only when the image is used as a base for a child image.
Build and run the image:
docker build -t csphere/php-fpm:5.4 .
docker run -d -p 8080:80 --name website csphere/php-fpm:5.4Access the container via http://your_ip:8080/info.php and enter the running container with docker exec -it website /bin/bash.
Build csphere/mysql:5.5 Image
#
# MAINTAINER Carson,C.J.Zeong <[email protected]>
# DOCKER-VERSION 1.6.2
#
FROM csphere/centos:7.1
ENV DATA_DIR /var/lib/mysql
RUN yum install -y mariadb mariadb-server && yum clean all
ADD mysqld_charset.cnf /etc/my.cnf.d/
COPY scripts /scripts
RUN chmod +x /scripts/start
EXPOSE 3306
VOLUME ["/var/lib/mysql"]
ENTRYPOINT ["/scripts/start"]The VOLUME directive maps host directories to the container, preserving data after container removal. The ENTRYPOINT runs the /scripts/start script on each container start.
Start script ( /scripts/start) initializes MariaDB on first run and ensures the MySQL socket is clean before launching mysqld_safe.
Docker Volume for Persistent Data
Define VOLUME["/data"] in Dockerfile.
Run container with
docker run -d -v <host_dir>:<container_dir> …Case Study: Data Persistence
Run a MySQL container without a volume, create a database, stop and remove the container – data is lost.
Run a MySQL container with a volume, create a database, stop and remove the container – data remains in the host directory and can be re‑attached to a new container.
Commands used:
# docker run -d -p 3306:3306 --name dbserver csphere/mysql:5.5
# docker exec -it dbserver mysql -e "create database mydb;"
# docker stop dbserver && docker rm dbserver
# docker run -d -p 3306:3306 -v /var/lib/docker/vfs/dir/mydata:/var/lib/mysql csphere/mysql:5.5
# docker exec -it newdb mysql -e "show databases;"Result: The mydb database persists across container recreation, demonstrating that Docker volumes protect data from container deletion.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
