csphere/php-fpm:5.4
# cd docker-training/php-fpm/# ls Dockerfile nginx_nginx.conf supervisor_nginx.confnginx_default.conf php_www.conf supervisor_php-fpm.conf
各文件解释:
nginx_nginx.conf 替换默认的nginx.conf文件
nginx_default.conf 替换默认的default.conf文件
php_ 修改apache用户为nginx
supervisor_nginx.conf 添加启动nginx的supervisor文件
supervisor_php-fpm.conf 添加启动php-fpm的supervisor文件
# cat Dockerfile ## MAINTAINER Carson,C.J.Zeong# DOCKER-VERSION 1.6.2## Dockerizing php-fpm: Dockerfile for building php-fpm images#FROM csphere/centos:7.1MAINTAINER Carson,C.J.Zeong # Set environment variableENV APP_DIR /appRUN 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 allADD nginx_nginx.conf /etc/nginx/nginx.confADD nginx_default.conf /etc/nginx/conf.d/default.confADD php_www.conf /etc/php-fpm.d/www.confRUN sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/' /etc/php.iniRUN mkdir -p /app && echo " " > ${APP_DIR}/info.phpEXPOSE 80 443ADD supervisor_nginx.conf /etc/supervisor.conf.d/nginx.confADD supervisor_php-fpm.conf /etc/supervisor.conf.d/php-fpm.confONBUILD ADD . /appONBUILD RUN chown -R nginx:nginx /app
命令解析:
ONBUILD ADD . /app
ONBUILD
在生成当前docker镜像的时候不生效,在子镜像生效;ONBUILD
在产品发布时起着非常重要的作用!举例
A镜像中有
ONBUILD
指令,在构建A镜像时ONBUILD
指令不执行;B镜像FROM A
,在构建B镜像时ONBUILD
指令开始执行;
如何给docker镜像命名:
- registry-url: registry服务器的域名或者ip
- namespace:
- image-name: docker镜像的名字
- tag: docker镜像的版本号,推荐使用应用服务的版本号来命名,如
php-fpm:5.4
生成php-fpm镜像
docker build -t csphere/php-fpm:5.4 .
Step 12 : ONBUILD add . /app ---> Running in 9e21ede67350 ---> 7541483a5a76Removing intermediate container 9e21ede67350Step 13 : ONBUILD run chown -R nginx:nginx /app ---> Running in ab55fc7a46a1 ---> c61699e8c237Removing intermediate container ab55fc7a46a1Successfully built c61699e8c237
生成website容器:
docker run -d -p 8080:80 --name website csphere/php-fpm:5.4
da30b15d3518320f4150b20ef329e59432a65610968977277879578b5fd8f4f7
参数解释:
- -d 后台运行
- -p 8080:80 将宿主机的8080端口映射到容器的80端口
- --name website 给容器命名为website
- csphere/php-fpm:5.4 使用这个镜像镜像创建docker容器
使用浏览器访问:http://your_ip:8080/info.php
如何进入一个正在运行的docker容器?
docker exec -it website /bin/bash
# supervisorctl 查看当前容器中使用supervisor启动了哪些服务 nginx RUNNING pid 9, uptime 0:23:15php-fpm RUNNING pid 10, uptime 0:23:15
csphere/mysql:5.5
cat Dockerfile ## MAINTAINER Carson,C.J.Zeong# DOCKER-VERSION 1.6.2## Dockerizing Mariadb: Dockerfile for building Mariadb images#FROM csphere/centos:7.1MAINTAINER Carson,C.J.Zeong ENV DATA_DIR /var/lib/mysql# Install MariadbRUN yum install -y mariadb mariadb-server && \ yum clean allADD mysqld_charset.cnf /etc/my.cnf.d/COPY scripts /scriptsRUN chmod +x /scripts/startEXPOSE 3306VOLUME ["/var/lib/mysql"]ENTRYPOINT ["/scripts/start"]
命令解析:
VOLUME ["/var/lib/mysql"]
VOLUME
指令,宿主机文件目录和docker容器文件目录做映射
ENTRYPOINT ["/scripts/start"]
ENTRYPOINT
在每次启动docker容器时都会被执行,此例,是运行了一个shell脚本"/scripts/start"
每次启动都会运行/scripts/start
脚本,脚本内容如下:
# cat start #!/bin/bashset -e## When Startup Container script#if [[ -e /scripts/firstrun ]]; then # config mariadb /scripts/firstrun_maria rm /scripts/firstrunelse # Cleanup previous mariadb sockets if [[ -e ${DATA_DIR}/mysql.sock ]]; then rm -f ${DATA_DIR}/mysql.sock fifiexec /usr/bin/mysqld_safe
脚本解析:
-
set -e
脚本中只要有一行有错误,就会中断脚本执行 -
如果firstrun文件存在,执行firstrun_maria脚本,如果不存在,删除mysql.sock文件,并启动Mariadb
firstrun_maira脚本是初始化Mariadb,以及设置数据库用户和密码,详情内容请自行阅读
构建mysql docker镜像
docker build -t csphere/mysql:5.5 .
docker volume 保证删除容器后,数据不被删除
- 保存容器中的数据
- 数据共享
使用方法:
1.在Dockerfile中定义VOLUME["/data"]
2.通过docker run -d -v <host_dir>:<container_dir>
案例:
- 创建mysql容器,不挂载docker volume,删除后,数据是否存在
- 创建mysql容器,挂载docker volume,删除后,数据是否存在
运行不挂载docker volume的mysql容器
# docker run -d -p 3306:3306 --name dbserver csphere/mysql:5.5
0a3092395c1e6a84f0ecd5383799f210519c5aefc82cbb7ee2ed1a471fc463f5
删除docker容器,容器里面的数据都会随着容器被删除而删除
# docker rm dbserverError response from daemon: Cannot destroy container dbserver: Conflict, You cannot remove a running container. Stop the container before attempting removal or use -fError: failed to remove containers: [dbserver]
参数解释:
docker rm
删除状态为“Exited”的docker容器docker rm -f
强制删除docker容器
运行挂载docker volume的mysql容器
docker run -d -p 3306:3306 -v /var/lib/docker/vfs/dir/mydata:/var/lib/mysql csphere/mysql:5.5
f49165d5e081b8bd8af9cb9c0bbbeb6545d45f857c1a852646c105
docker exec -it f49 /bin/bash
登陆数据库创建mydb数据库
# mysql# show databases;# create database mydb;Query OK, 1 row affected (0.00 sec)# show databases;+--------------------+| Database |+--------------------+| information_schema || mydb || mysql || performance_schema || test |+--------------------+5 rows in set (0.00 sec)# exitexit
查看主机文件目录下,是否已生成mydb数据库目录文件
# ls /var/lib/docker/vfs/dir/mydata/aria_log.00000001 ibdata1 ib_logfile1 mysql performance_schemaaria_log_control ib_logfile0 mydb mysql.sock test
停止docker容器# docker stop f49165d5e081f49165d5e081删除docker容器,查看`mydb`目录是否被删除# docker rm f49165d5e081f49165d5e081# ls /var/lib/docker/vfs/dir/mydata/ 验证,挂载docker volume后,容器被删除掉,数据还在aria_log.00000001 ibdata1 ib_logfile1 mysql performance_schemaaria_log_control ib_logfile0 mydb mysql.sock test
新创建一个容器,挂载到刚才的数据目录下,是否可以把之前的数据库加载回来
docker run -d -p 3306:3306 --name newdb -v /var/lib/docker/vfs/dir/mydata:/var/lib/mysql csphere/mysql:5.5
29418b93d4d4a00a86169c568b6f952e71d25b155d7f6b8012d953022691b2b8
docker exec -it newdb /bin/bash
# mysqlMariaDB [(none)]> show databases;+--------------------+| Database |+--------------------+| information_schema || mydb || mysql || performance_schema || test |+--------------------+5 rows in set (0.00 sec)
验证结果: 只要保证数据在,重新创建一个容器挂载回之前的数据目录,业务即可恢复(容器可随意删除、创建)