前言
使用容器化技术如 Docker 可以极大地简化应用程序的部署和管理过程,本文将介绍如何利用 Docker 构建 LNMP 环境,并通过部署 WordPress 来展示这一过程。
目录
一、环境准备
1. 项目需求
2. 安装包下载
3. 服务器环境
4. 规划工作目录
5. 创建自定义网络
二、Nginx 镜像创建
1. 建立工作目录
2. 编写 Dockerfile 文件
3. 准备 nginx.conf 配置文件
4. 生成镜像
5. 启动镜像容器
6. 验证 nginx
三、Mysql 镜像创建
1. 进入工作目录
2. 编写 Dockerfile 文件
3. 准备 my.cnf 文件
4. 生成镜像
5. 启动镜像容器
6. 验证 mysql
四、Php 镜像创建
1. 进入工作目录
2. 编写 Dockerfile 文件
3. 准备 php.ini、php-fpm.conf、www.conf 配置文件
3.1 php.ini
3.2 php-fpm.conf
3.3 www.conf
4. 生成镜像
5. 启动镜像容器
6. 验证 php
7. 进入容器登录 mysql 授权
8. 创建一个新的配置文件 wp-config.php
9. 浏览器访问 wordpress
五、Docker 镜像打包
1. 基本信息
2. 容器导出
一、环境准备
1. 项目需求
(1)使用 Docker 构建 LNMP 环境并运行 Wordpress 网站平台
(2)限制 Nginx 容器最多使用 512Mb 的内存和 1G 的 Swap
(3)限制 Mysql 容器写 /dev/sda 的速率为 10 MB/s
(4)将所有容器进行快照,然后将 Docker 镜像打包成 tar 包备份到本地
2. 安装包下载
wget https://cdn.mysql.com//archives/mysql-5.7/mysql-boost-5.7.20.tar.gz
wget http://nginx.org/download/nginx-1.12.0.tar.gz
wget https://www.php.net/distributions/php-7.1.10.tar.bz2
wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
3. 服务器环境
容器名 | 宿主机系统/配置 | IP地址 | Docker版本 | 基于镜像 | 安装软件 |
nginx | Centos 7.4;4C/8G | 172.19.8.10 | 20.10.17 | centos:7 | Docker-Nginx Docker-Wordpress |
mysql | Centos 7.4;4C/8G | 172.19.8.20 | 20.10.17 | centos:7 | Docker-Mysql |
php | Centos 7.4;4C/8G | 172.19.8.30 | 20.10.17 | centos:7 | Docker-php |
安装 docker-ce-20.10.17:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# 安装所需的依赖
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 添加Docker官方仓库,正常后面还是用国内阿里源更合适
sudo yum makecache fast
# 更新Yum缓存
sudo yum install docker-ce-20.10.17 docker-ce-cli-20.10.17 containerd.io
# 安装Docker CE 20.10.17
sudo systemctl start docker
sudo systemctl enable docker
# 启动并启用Docker服务
docker --version
# 验证安装
4. 规划工作目录
[root@localhost ~]# tree /lnmp/ -L 2
/lnmp/
├── images
│ ├── mysql_image.tar
│ ├── nginx_image.tar
│ └── php_image.tar
├── mysql
│ ├── Dockerfile
│ ├── my.cnf
│ └── mysql-boost-5.7.20.tar.gz
├── nginx
│ ├── Dockerfile
│ ├── html
│ ├── nginx-1.12.0.tar.gz
│ ├── nginx.conf
│ └── wordpress-4.9.4-zh_CN.tar.gz
└── php
├── Dockerfile
├── php-7.1.10.tar.bz2
├── php-fpm.conf
├── php.ini
└── www.conf
5. 创建自定义网络
创建了一个名为 mynetwork 的自定义 Docker 网络,子网为 172.19.0.0/16,并指定了桥接名称为 docker1。
[root@localhost ~]# docker network create --subnet=172.19.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork
d2b40d071ba33351aeb1d4a6a8d9738fb6fb7a01991bd26c8fdb7244fce3819f
[root@localhost ~]# docker network ls | grep mynetwork
d2b40d071ba3 mynetwork bridge local
[root@localhost ~]# docker inspect d2b40d071ba3 | grep -i subnet
"Subnet": "172.19.0.0/16"
# 可以看到网络的子网是172.19.0.0/16
二、Nginx 镜像创建
1. 建立工作目录
[root@localhost ~]# mkdir /lnmp/{nginx,mysql,php} -p
[root@localhost nginx]# ls
nginx-1.12.0.tar.gz wordpress-4.9.4-zh_CN.tar.gz # 准备安装包
[root@localhost nginx]# mkdir html/
[root@localhost nginx]# tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C /lnmp/nginx/html
[root@localhost nginx]# cd html/;ls
wordpress
[root@localhost html]# cd wordpress/;ls
index.php wp-admin wp-content wp-load.php wp-signup.php
license.txt wp-blog-header.php wp-cron.php wp-login.php wp-trackback.php
readme.html wp-comments-post.php wp-includes wp-mail.php xmlrpc.php
wp-activate.php wp-config-sample.php wp-links-opml.php wp-settings.php
# 准备 wordpress 网站文件
2. 编写 Dockerfile 文件
[root@localhost wordpress]# cd ../..
[root@localhost nginx]# vim Dockerfile
FROM centos:7 # 基于centOS:7镜像作为基础镜像
MAINTAINER nginx image <fql> # 指定镜像的维护者信息
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make # yum安装必要的软件包至镜像;
RUN useradd -M -s /sbin/nologin nginx # 创建nginx的系统用户,且无法登录系统
ADD nginx-1.12.0.tar.gz /usr/local/src/ # 将nginx文件解压到/usr/local/src/目录下
WORKDIR /usr/local/src/nginx-1.12.0 # 工作目录切换
RUN ./configure \ # 配置、编译并安装Nginx,指定安装路径、用户、用户组以及使用了 with-http_sub_status_module 模块
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make -j 4 && make instal
ENV PATH /usr/local/nginx/sbin:$PATH # 将Nginx可执行文件路径添加到环境变量中
ADD nginx.conf /usr/local/nginx/conf/ # 将宿主机当前目录nginx.conf配置文件复制到镜像指定目录
#ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html/
RUN chmod 777 -R /usr/local/nginx/html/ # 授权
EXPOSE 80 # 暴露容器的80端口,http
EXPOSE 443 # 暴露容器的443端口,https
VOLUME [ "/usr/local/nginx/html/" ] # 在Docker内创建挂载点
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
# 启动Nginx并以非守护进程方式运行
3. 准备 nginx.conf 配置文件
[root@localhost nginx]# egrep -v "^(.)*#(.)*$" nginx.conf | grep -v "^$"
# 从文件nginx.conf中过滤掉以#开头的注释行和空行,然后输出剩余的非注释非空行内容
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root html;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ { # 指定了匹配请求URI以.php结尾的条件,~表示使用正则表达式匹配
root html; # 查找文件的根目录为html
fastcgi_pass 172.19.0.30:9000; # Nginx将把.php文件发送到172.19.0.30上的FastCGI服务器的9000端口
fastcgi_index index.php; # 当请求URI以/结尾时,Nginx应该使用的默认文件
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
# 这个参数告诉FastCGI服务器要执行的PHP脚本的路径。$fastcgi_script_name是一个FastCGI变量,代表请求的文件路径
include fastcgi_params; # 包含了一个外部文件fastcgi_params
}
}
}
# 根据请求的URI找到对应的PHP文件,将请求发送给FastCGI服务器进行处理,并设置一些FastCGI参数。这样可以实现在Nginx服务器上运行PHP脚本的功能。
4. 生成镜像
[root@localhost nginx]# docker build -t nginx:lnmp .
[root@localhost nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx lnmp b0ab50b55dac 43 seconds ago 569MB
nginx latest 7383c266ef25 4 days ago 188MB
centos 7 eeb6ee3f44bd 2 years ago 204MB
5. 启动镜像容器
限制 Nginx 容器最多使用 512Mb 的内存和 1G 的 Swap:
[root@localhost nginx]# docker run -d --name nginx -p 80:80 -m 512M --memory-swap=1G -v /lnmp/nginx/html:/usr/local/nginx/html --net mynetwork --ip 172.19.0.10 nginx:lnmp
30bac7a9755929001177b15047c39680cc0a26ea8ddf17ea32fd31045b3c6aa1
[root@localhost nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
30bac7a97559 nginx:lnmp "/usr/local/nginx/sb…" 50 seconds ago Up 48 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp nginx
[root@localhost nginx]# docker inspect nginx
"Gateway": "172.19.0.1",
"IPAddress": "172.19.0.10",
[root@localhost nginx]# docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
30bac7a97559 nginx 0.00% 952KiB / 512MiB 0.18% 2.41kB / 0B 0B / 0B 2
6. 验证 nginx
在宿主机目录建立html文件:
[root@localhost nginx]# echo nginx welcome > html/index.html
三、Mysql 镜像创建
1. 进入工作目录
[root@localhost nginx]# cd ../mysql/
[root@localhost mysql]# ls
mysql-boost-5.7.20.tar.gz # 准备安装包
2. 编写 Dockerfile 文件
[root@localhost mysql]# vim Dockerfile
FROM centos:7
MAINTAINER mysql image <fql>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/ # 将MySQL源代码包解压缩到镜像/usr/local/src/目录下
WORKDIR /usr/local/src/mysql-5.7.20/ # 设置工作目录为MySQL源代码包的目录
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j 4 && make install
ADD my.cnf /etc/
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH # 将MySQL的可执行文件路径添加到环境变量中
RUN useradd -M -s /sbin/nologin mysql
RUN chown mysql:mysql /etc/my.cnf
RUN chown -R mysql:mysql /usr/local/mysql/
RUN /usr/local/mysql/bin/mysqld \ # 运行MySQL的初始化命令,初始化MySQL数据库
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"] # 启动MySQL数据库服务
3. 准备 my.cnf 文件
[root@localhost mysql]# cat my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock
auto-rehash
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
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
4. 生成镜像
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=container error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache
# 如果遇到类似dns问题,可修改dns、替换阿里源尝试解决
[root@localhost mysql]# docker build -t mysql:lnmp .
[root@localhost mysql]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql lnmp 2b328d4ecd04 About a minute ago 10.1GB
nginx lnmp b0ab50b55dac 2 hours ago 569MB
5. 启动镜像容器
限制 Mysql 容器写 /dev/sda 的速率为 10 MB/s:
[root@localhost mysql]# docker run --name=mysql -d --privileged --device-write-bps /dev/sda:10M -v /usr/local/mysql -P --net mynetwork --ip 172.19.0.20 mysql:lnmp
ddd452f38b116b7c01a85676837933d25cacb53e65e2a61f36e43e7725544bae
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
7da2cc6a302c nginx:lnmp "/usr/local/nginx/sb…" About an hour ago Up 15 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp nginx
817aedefb6f6 mysql:lnmp "/usr/local/mysql/bi…" About an hour ago Up 14 minutes 0.0.0.0:49153->3306/tcp, :::49153->3306/tcp mysql
测试IO:
[root@localhost mysql]# docker exec -it mysql /bin/bash
[root@ddd452f38b11 mysql-5.7.20]# dd if=/dev/zero of=test bs=20M count=5 oflag=direct
5+0 records in
5+0 records out
104857600 bytes (105 MB) copied, 9.97887 s, 10.5 MB/s
# 速率限制是近似值,并且在实际应用中可能会有一些波动
6. 验证 mysql
容器登录:
[root@localhost mysql]# docker exec -it mysql /bin/bash
[root@ddd452f38b11 mysql-5.7.20]# mysql -uroot -p # 默认没有密码,直接回车
mysql>
远程登录:
[root@slave01 ~]# mysql -u root -h 192.168.190.108 -P 49153 -p'abc123'
mysql>
四、Php 镜像创建
1. 进入工作目录
[root@localhost mysql]# cd ../php/
[root@localhost php]# ls
php-7.1.10.tar.bz2 # 准备安装包
2. 编写 Dockerfile 文件
[root@localhost php]# vim Dockerfile
FROM centos:7
MAINTAINER php image <fql>
RUN yum -y install gd \ # 安装一系列依赖包
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel
RUN useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/ # 将php文件解压并复制到/usr/local/src/目录下
WORKDIR /usr/local/src/php-7.1.10 # 设置工作目录
RUN ./configure \ # 配置PHP编译选项
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make -j 4 && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH # 将PHP可执行文件路径添加到系统PATH中
ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000 # 暴露容器的9000端口,用于PHP-FPM服务
ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ] # 启动PHP-FPM服务并以守护进程方式运行
3. 准备 php.ini、php-fpm.conf、www.conf 配置文件
- php.ini 用于配置 PHP 的运行时参数和特性
- php-fpm.conf 用于配置 PHP-FPM 的全局行为和进程管理
- www.conf 用于配置 PHP-FPM 的进程池参数,如进程数和管理方式
[root@localhost php]# ls
Dockerfile php-7.1.10.tar.bz2 php-fpm.conf php.ini www.conf
3.1 php.ini
[root@localhost php]# vim php.ini
939 date.timezone = Asia/Shanghai # 取消注释,修改
1170 mysqli.default_socket = /usr/local/mysql/mysql.sock
# MySQL数据库的默认socket文件路径,用于PHP连接MySQL数据库时寻找MySQL服务器的通信端口
[root@localhost php]# egrep -v "^;" php.ini | egrep -v "^$"
# 显示php.ini文件中的非注释和非空行内容,即显示出配置文件中的有效配置项
[PHP]
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = On
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
date.timezone = Asia/Shanghai
[filter]
[iconv]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket = /usr/local/mysql/mysql.sock
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = On
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = 1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
[curl]
[openssl]
3.2 php-fpm.conf
[root@localhost php]# vim php-fpm.conf
17 pid = run/php-fpm.pid # 删除注释符号“;”
[root@localhost php]# egrep -v "^;" php-fpm.conf | egrep -v "^$"
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf
3.3 www.conf
[root@localhost php]# vim www.conf
23 user = nginx
24 group = nginx
36 listen = 172.19.0.30:9000 # 修改监听IP和端口为容器IP:9000端口
[root@localhost php]# egrep -v "^;" www.conf | egrep -v "^$"
[www]
user = nginx
group = nginx
listen = 172.19.0.30:9000
listen.allowed_clients = 127.0.0.1,172.19.0.10
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
4. 生成镜像
[root@localhost php]# docker build -t php:lnmp .
[root@localhost php]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php lnmp 39e39b251e20 24 seconds ago 1.4GB
mysql lnmp 2b328d4ecd04 44 minutes ago 10.1GB
nginx lnmp b0ab50b55dac 2 hours ago 569MB
5. 启动镜像容器
[root@localhost php]# docker run --name=php -d -p 9000:9000 --volumes-from mysql --volumes-from nginx --net mynetwork --ip 172.19.0.30 php:lnmp
5e91e0162badda54ad70cb39240b3b7e633b5764ad12830edad17f6c152645d9
[root@localhost php]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e91e0162bad php:lnmp "/usr/local/php/sbin…" 5 seconds ago Up 3 seconds 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php
ddd452f38b11 mysql:lnmp "/usr/local/mysql/bi…" 43 minutes ago Up 43 minutes 3306/tcp mysql
30bac7a97559 nginx:lnmp "/usr/local/nginx/sb…" 2 hours ago Up About an hour 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp nginx
6. 验证 php
[root@localhost php]# docker exec -it php /bin/bash
[root@5e91e0162bad php-7.1.10]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 113028 7520 ? Ss 09:39 0:00 php-fpm: master process (/usr/loc
nginx 8 0.0 0.0 113028 3768 ? S 09:39 0:00 php-fpm: pool www
nginx 9 0.0 0.0 113028 3768 ? S 09:39 0:00 php-fpm: pool www
root 10 1.5 0.0 11828 1880 pts/0 Ss 09:46 0:00 /bin/bash
root 24 0.0 0.0 51732 1724 pts/0 R+ 09:46 0:00 ps -aux
7. 进入容器登录 mysql 授权
[root@localhost php]# docker exec -it mysql /bin/bash
[root@ddd452f38b11 mysql-5.7.20]# mysql
mysql> create database wordpress;
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abc123';
mysql> flush privileges;
8. 创建一个新的配置文件 wp-config.php
[root@localhost ~]# cd /lnmp/nginx/html/wordpress
[root@localhost wordpress]# cp wp-config-sample.php wp-config.php
[root@localhost wordpress]# vim wp-config.php
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', '123456');
# 修改WordPress数据库信息
9. 浏览器访问 wordpress
http://192.168.190.108/wordpress/index.php
五、Docker 镜像打包
1. 基本信息
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b4d901f2bccc php:lnmp "/bin/sh -c '/usr/lo…" 30 minutes ago Up 11 minutes 0.0.0.0:9000->9000/tcp, :::9000->9000/tcp php
7da2cc6a302c nginx:lnmp "/usr/local/nginx/sb…" About an hour ago Up 12 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp nginx
817aedefb6f6 mysql:lnmp "/usr/local/mysql/bi…" About an hour ago Up 11 minutes 0.0.0.0:49153->3306/tcp, :::49153->3306/tcp mysql
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php lnmp d36e706ae5d6 31 minutes ago 1.4GB
nginx lnmp a6b0e83b8ff8 About an hour ago 569MB
mysql lnmp 2b328d4ecd04 5 hours ago 10.1GB
2. 容器导出
[root@localhost ~]# mkdir /lnmp/images
[root@localhost ~]# cd /lnmp/images/
[root@localhost images]# docker export nginx > nginx_image.tar
[root@localhost images]# docker export mysql > mysql_image.tar
[root@localhost images]# docker export php > php_image.tar
[root@localhost images]# ll -h
总用量 9.4G
-rw-r--r--. 1 root root 7.6G 4月 28 22:11 mysql_image.tar
-rw-r--r--. 1 root root 503M 4月 28 22:06 nginx_image.tar
-rw-r--r--. 1 root root 1.3G 4月 28 22:13 php_image.tar