本章目录
1. 部署步骤与流程
1.1 关闭防火墙
1.2 安装 Docker
1.3 Docker 部署 Nginx
1.4 Docker 部署 MySQL
1.5 Docker 部署 PHP
1.6 测试访问网页
2. 备份恢复
2.1 操作步骤
- 环境准备:
CentOS 7:内核最好在 4 核以上;内存在 4G 以上;硬盘 60G。
否则很容易在创建镜像的过程中出错,提示内存不足。
1 systemctl stop firewalld 2 systemctl disable firewalld 3 setenforce 0
详见之前的博客:Blog.079 Docker 安装、基本管理与网络模式
(容器IP 为 172.18.0.10)
1 docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork #自定义一个网卡,网卡名为 docker1 ,网络模式 mynetwork, ip地址池172.18.0.0
可以用 ifconfig docker1 查看是否创建成功。
1 mkdir /opt/nginx 2 cd /opt/nginx/ 3 4 ##上传 nginx-1.12.0.tar.gz、wordpress-4.9.4-zh_CN.tar.gz 到 /opt/nginx/ 目录中 5 mkdir /opt/nginx/html 6 tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html
1 ##在 /opt/nginx 目录下解压软件包 2 3 tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/html
- 在 /opt/nginx 目录下创建
1 vim Dockerfile 2 3 FROM centos:7 4 MAINTAINER this is nginx image <abc> 5 RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make 6 RUN useradd -M -s /sbin/nologin nginx 7 ADD nginx-1.12.0.tar.gz /usr/local/src/ 8 WORKDIR /usr/local/src/nginx-1.12.0 9 RUN ./configure \ 10 --prefix=/usr/local/nginx \ 11 --user=nginx \ 12 --group=nginx \ 13 --with-http_stub_status_module && make && make install 14 ENV PATH /usr/local/nginx/sbin:$PATH 15 ADD nginx.conf /usr/local/nginx/conf/ 16 ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/ 17 RUN chmod 777 -R /usr/local/nginx/html/ 18 EXPOSE 80 19 EXPOSE 443 20 ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
- 在 /opt/nginx 目录下,传入其他已经装好的 nginx.conf 文件,并修改如下部分:
1 worker_processes 1; 2 events { 3 worker_connections 1024; 4 } 5 http { 6 include mime.types; 7 default_type application/octet-stream; 8 sendfile on; 9 keepalive_timeout 65; 10 server { 11 listen 80; 12 server_name localhost; 13 14 ##配置网页的字符集编码为utf8 15 16 charset utf8; 17 location / { 18 root html; 19 20 ##修改,配合首页文件有 index.php 21 22 index index.html index.php; 23 } 24 error_page 500 502 503 504 /50x.html; 25 location = /50x.html { 26 root html; 27 } 28 29 ##配置nginx支持php 30 31 location ~ \.php$ { 32 root html; 33 ##fastcgi_pass 配置php所在容器的ip以及端口 34 35 fastcgi_pass 172.18.0.30:9000; 36 fastcgi_index index.php; 37 38 ##配置传递脚本文件请求路径。 39 $fastcgi_script_name 前配置的是nginx的网页文件。 40 41 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; 42 include fastcgi_params; 43 } 44 } 45 }
- 构建镜像,success 成功后再创建容器
1 docker build -t nginx:lnmp . 2 3 docker run -d --name nginx -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html --net mynetwork --ip 172.18.0.10 nginx:lnmp
(容器IP 为 172.18.0.20)
mkdir /opt/mysqld cd /opt/mysqld 上传 mysql-boost-5.7.20.tar.gz 到 /opt/mysqld 目录中
- 在 /opt/mysqld 目录下
1 vim Dockerfile 2 3 FROM centos:7 4 MAINTAINER this is mysql image <lv> 5 RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make 6 RUN useradd -M -s /sbin/nologin mysql 7 ADD mysql-boost-5.7.20.tar.gz /usr/local/src/ 8 WORKDIR /usr/local/src/mysql-5.7.20/ 9 RUN cmake \ 10 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ 11 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ 12 -DSYSCONFDIR=/etc \ 13 -DSYSTEMD_PID_DIR=/usr/local/mysql \ 14 -DDEFAULT_CHARSET=utf8 \ 15 -DDEFAULT_COLLATION=utf8_general_ci \ 16 -DWITH_INNOBASE_STORAGE_ENGINE=1 \ 17 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ 18 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ 19 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ 20 -DMYSQL_DATADIR=/usr/local/mysql/data \ 21 -DWITH_BOOST=boost \ 22 -DWITH_SYSTEMD=1 && make -j4 && make install 23 RUN chown -R mysql:mysql /usr/local/mysql/ 24 ADD my.cnf /etc/my.cnf 25 RUN chown mysql:mysql /etc/my.cnf 26 ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH 27 WORKDIR /usr/local/mysql/ 28 RUN bin/mysqld \ 29 --initialize-insecure \ 30 --user=mysql \ 31 --basedir=/usr/local/mysql \ 32 --datadir=/usr/local/mysql/data 33 RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ 34 EXPOSE 3306 35 RUN systemctl enable mysqld 36 VOLUME [ "/usr/local/mysql" ] 37 CMD /usr/sbin/init
- 在 /opt/mysqld 目录下
1 vim my.cnf 2 [client] 3 port = 3306 4 socket = /usr/local/mysql/mysql.sock 5 6 [mysql] 7 port = 3306 8 socket = /usr/local/mysql/mysql.sock 9 10 [mysqld] 11 user = mysql 12 basedir = /usr/local/mysql 13 datadir = /usr/local/mysql/data 14 port = 3306 15 character_set_server=utf8 16 pid-file = /usr/local/mysql/mysqld.pid 17 socket = /usr/local/mysql/mysql.sock 18 server-id = 1 19 20 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
- 在 /opt/mysqld 目录下
1 vim run.sh 2 #!/bin/bash 3 /usr/local/mysql/bin/mysqld 4 systemctl enable mysqld
- 构建镜像,完成后创建容器
1 docker build -t mysql:lnmp . 2 3 docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp /usr/sbin/init
(容器IP 为 172.18.0.30)
1 mkdir /opt/php 2 cd /opt/php 3 4 上传 php-7.1.10.tar.bz2 到 /opt/php 目录中
- 在 /opt/php 目录下,修改好的 php.ini、php-fpm.conf 和 www.conf 也传入到此目录下
1 vim Dockerfile 2 3 FROM centos:7 4 MAINTAINER this is php image <wl> 5 RUN yum -y install gd \ 6 libjpeg libjpeg-devel \ 7 libpng libpng-devel \ 8 freetype freetype-devel \ 9 libxml2 libxml2-devel \ 10 zlib zlib-devel \ 11 curl curl-devel \ 12 openssl openssl-devel \ 13 gcc gcc-c++ make pcre-devel 14 RUN useradd -M -s /sbin/nologin nginx 15 ADD php-7.1.10.tar.bz2 /usr/local/src/ 16 WORKDIR /usr/local/src/php-7.1.10 17 RUN ./configure \ 18 --prefix=/usr/local/php \ 19 --with-mysql-sock=/usr/local/mysql/mysql.sock \ 20 --with-mysqli \ 21 --with-zlib \ 22 --with-curl \ 23 --with-gd \ 24 --with-jpeg-dir \ 25 --with-png-dir \ 26 --with-freetype-dir \ 27 --with-openssl \ 28 --enable-fpm \ 29 --enable-mbstring \ 30 --enable-xml \ 31 --enable-session \ 32 --enable-ftp \ 33 --enable-pdo \ 34 --enable-tokenizer \ 35 --enable-zip && make && make install 36 ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH 37 ADD php.ini /usr/local/php/lib/ 38 ADD php-fpm.conf /usr/local/php/etc/ 39 ADD www.conf /usr/local/php/etc/php-fpm.d/ 40 EXPOSE 9000 41 ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]
- 构建镜像,完成后创建容器
1 docker build -t php:lnmp . 2 docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp
1 ##启动容器,需要从nginx容器和mysql容器中获取数据 2 3 docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.18.0.30 php:lnmp 4 5 ##进入容器,进行用户授权 6 7 docker exec -it mysql /bin/bash 8 mysql -u root -p mysql 9 密码直接回车 10 11 ##进入 MySQL 12 13 mysql> create database wordpress; 14 Query OK, 1 row affected (0.00 sec) 15 16 mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456'; 17 Query OK, 0 rows affected, 1 warning (0.00 sec) 18 19 mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123'; 20 Query OK, 0 rows affected, 1 warning (0.00 sec) 21 22 mysql> flush privileges; 23 Query OK, 0 rows affected (0.01 sec)
1 ##使用主机地址测试 2 3 firefox http://192.168.23.103/wordpress/index.php
- 将所有容器快照
- 将镜像保存成 tar 包
- 删除容器和镜像
- 恢复
-