[root@docker01 ~]# docker network create wordpress
2713a5e58333f4dc7f793487d6b98b6a88c970af534aa47b5c807601d2d264db
[root@docker01 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
b497bef69509 bridge bridge local
13ae75f26408 host host local
de7cb7a83412 none null local
2713a5e58333 wordpress bridge local
[root@docker01 ~]# mkdir wp_Dockerfile
[root@docker01 ~]# cd wp_Dockerfile/
[root@docker01 ~/wp_Dockerfile]# mkdir mysql
[root@docker01 ~/wp_Dockerfile]# mkdir nginx
[root@docker01 ~/wp_Dockerfile]# mkdir php
[root@docker01 ~/wp_Dockerfile]# rz -E
rz waiting to receive.
[root@docker01 ~/wp_Dockerfile]# ll
total 15380
drwxr-xr-x 2 root root 6 Mar 23 19:45 mysql
drwxr-xr-x 2 root root 121 Mar 23 09:47 nginx
drwxr-xr-x 2 root root 55 Mar 23 19:13 php
-rw-r--r-- 1 root root 15747536 Mar 23 18:25 wordpress-5.7.tar.gz
解压
[root@docker01 ~/wp_Dockerfile]# tar xf wordpress-5.7.tar.gz
[root@docker01 ~/wp_Dockerfile]# ll
total 15384
drwxr-xr-x 2 root root 6 Mar 23 19:45 mysql
drwxr-xr-x 2 root root 121 Mar 23 09:47 nginx
drwxr-xr-x 2 root root 55 Mar 23 19:13 php
drwxr-xr-x 5 nobody 65534 4096 Mar 10 04:19 wordpress
-rw-r--r-- 1 root root 15747536 Mar 23 18:25 wordpress-5.7.tar.gz
[root@docker01 ~/wp_Dockerfile/nginx]# vim Dockerfile
FROM nginx
RUN groupadd www -g 666 && \
useradd www -u 666 -g 666 -s /sbin/nologin -M
ADD linux.wp.com.conf /etc/nginx/conf.d/
ADD nginx.conf /etc/nginx/
RUN mkdir /code/wordpress/ -p
RUN rm -rf /etc/nginx/conf.d/default.conf
EXPOSE 80
WORKDIR /root
CMD ["nginx","-g","daemon off;"]
[root@docker01 ~/wp_Dockerfile/nginx]# vim nginx.conf
user www;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
[root@docker01 ~/wp_Dockerfile/nginx]# vim linux.wp.com.conf
server {
listen 80;
location / {
root /code/wordpress;
index index.php;
}
location ~* \.php$ {
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
include fastcgi_params;
}
}
#创建镜像
docker build -t nginx:v1 .
[root@docker01 ~/wp_Dockerfile/php]# cat Dockerfile
FROM php:7.4-fpm
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# Mysqli 扩展 自带 直接安装即可(当前数据库使用的mysqli查询的)
RUN docker-php-ext-install mysqli
RUN groupadd www -g 666 && \
useradd www -u 666 -g 666 -s /sbin/nologin -M
ADD www.conf /usr/local/etc/php-fpm.d/
ADD php.ini /usr/local/etc/
ADD php-fpm.conf /usr/local/etc/
EXPOSE 9000
CMD ["php-fpm","-F","-y","/usr/local/etc/php-fpm.d/www.conf"]
php扩展
FROM php:7.2.4-fpm
MAINTAINER xiexinyang <983600849@qq.com>
#docker中php扩展安装方式
#1、PHP源码文件目录自带扩展 docker-php-ext-install直接安装
#2、pecl扩展 因为一些扩展不包含在PHP源码文件中,PHP 的扩展库仓库中存在。用 pecl install 安装扩展,再用 docker-php-ext-enable 命令 启用扩展
#3、其他扩展 一些既不在 PHP 源码包,也不再 PECL 扩展仓库中的扩展,可以通过下载扩展程序源码,编译安装的方式安装
# 扩展版本号定义
#redis 扩展
ENV PHPREDIS_VERSION 4.0.0
#msgpack扩展
ENV MSGPACK_VERSION 2.0.3
#memcached扩展
ENV MEMCACHED_VERSION 3.1.3
#mongodb扩展
ENV MONGODB_VERSION 1.5.3
#xhprof扩展 https://github.com/longxinH/xhprof/releases(pecl 不支持php7 使用这里的)
ENV XHPROF_VERSION 2.0.5
#swoole安装 如果以后用到的话,不用再安装了,4.0之后性能更好
ENV SWOOLE_VERSION 4.0.3
#swoole依赖hiredis
ENV HIREDIS_VERSION 0.13.3
# 设置时间
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' > /etc/timezone
# 扩展依赖
RUN apt-get update \
&& apt-get install -y \
curl \
wget \
git \
zip \
libz-dev \
libssl-dev \
libnghttp2-dev \
libpcre3-dev \
libmemcached-dev \
zlib1g-dev \
&& apt-get clean \
&& apt-get autoremove
# Composer安装
RUN curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/composer \
&& composer self-update --clean-backups
# Mysqli 扩展 自带 直接安装即可(当前数据库使用的mysqli查询的)
RUN docker-php-ext-install mysqli
# PDO 扩展 自带 直接安装即可
RUN docker-php-ext-install pdo_mysql
# Bcmath 扩展 自带 直接安装即可
RUN docker-php-ext-install bcmath
# Redis 扩展下载 pecl本地安装 开启扩展
RUN wget http://pecl.php.net/get/redis-${PHPREDIS_VERSION}.tgz -O /tmp/redis.tgz \
&& pecl install /tmp/redis.tgz \
&& rm -rf /tmp/redis.tgz \
&& docker-php-ext-enable redis
# msgpack 扩展下载 pecl本地安装 开启扩展(延迟队列使用减少源数据占用空间)
RUN wget http://pecl.php.net/get/msgpack-${MSGPACK_VERSION}.tgz -O /tmp/msgpack.tgz \
&& pecl install /tmp/msgpack.tgz \
&& rm -rf /tmp/msgpack.tgz \
&& docker-php-ext-enable msgpack
# memcached 扩展下载 pecl本地安装 开启扩展 前面已经通过 apt-get安装了libmemcached-dev依赖
RUN wget http://pecl.php.net/get/memcached-${MEMCACHED_VERSION}.tgz -O /tmp/memcached.tgz \
&& pecl install /tmp/memcached.tgz \
&& rm -rf /tmp/memcached.tgz \
&& docker-php-ext-enable memcached
# mongodb 扩展下载 pecl本地安装 开启扩展 前面已经通过
RUN wget http://pecl.php.net/get/mongodb-${MONGODB_VERSION}.tgz -O /tmp/mongodb.tgz \
&& pecl install /tmp/mongodb.tgz \
&& rm -rf /tmp/mongodb.tgz \
&& docker-php-ext-enable mongodb
# xhprof github上下载支持php7的扩展 安装 开启扩展
RUN wget https://github.com/longxinH/xhprof/archive/v${XHPROF_VERSION}.tar.gz -O /tmp/xhprof.tar.gz \
&& mkdir -p /tmp/xhprof \
&& tar -xf /tmp/xhprof.tar.gz -C /tmp/xhprof --strip-components=1 \
&& rm /tmp/xhprof.tar.gz \
&& ( \
cd /tmp/xhprof/extension \
&& phpize \
&& ./configure \
&& make -j$(nproc) \
&& make install \
) \
&& rm -r /tmp/xhprof \
&& docker-php-ext-enable xhprof
# Hiredis依赖安装
RUN wget https://github.com/redis/hiredis/archive/v${HIREDIS_VERSION}.tar.gz -O /tmp/hiredis.tar.gz \
&& mkdir -p /tmp/hiredis \
&& tar -xf /tmp/hiredis.tar.gz -C /tmp/hiredis --strip-components=1 \
&& rm /tmp/hiredis.tar.gz \
&& ( \
cd /tmp/hiredis \
&& make -j$(nproc) \
&& make install \
&& ldconfig \
) \
&& rm -r /tmp/hiredis
# Swoole 扩展安装 开启扩展
RUN wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O /tmp/swoole.tar.gz \
&& mkdir -p /tmp/swoole \
&& tar -xf /tmp/swoole.tar.gz -C /tmp/swoole --strip-components=1 \
&& rm /tmp/swoole.tar.gz \
&& ( \
cd /tmp/swoole \
&& phpize \
&& ./configure --enable-async-redis --enable-mysqlnd --enable-openssl --enable-http2 \
&& make -j$(nproc) \
&& make install \
) \
&& rm -r /tmp/swoole \
&& docker-php-ext-enable swoole
[root@docker01 ~/wp_Dockerfile/php]# vim www.conf
....
user = www
group = www
listen = 9000
;listen.allowed_clients = 127.0.0.1
request_terminate_timeout = 0
....
[root@docker01 ~/wp_Dockerfile/php]# vim php.ini
...
upload_max_filesize = 200M
post_max_size = 200M
...
#创建镜像
docker build -t php:v1 .
[root@docker01 ~/wp_Dockerfile/mysql]# cat Dockerfile
FROM mysql:5.7
#设置免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
#添加所需文件
ADD setup.sh /mysql/setup.sh
ADD schema.sql /mysql/schema.sql
ADD privileges.sql /mysql/privileges.sql
#设置容器启动时执行的命令
CMD ["sh", "/mysql/setup.sh"]
[root@docker01 ~/wp_Dockerfile/mysql]# cat setup.sh
#!/bin/bash
set -e
#查看mysql服务的状态,方便调试,这条语句可以删除
echo `service mysql status`
echo '1.启动mysql....'
#启动mysql
service mysql start
sleep 3
echo `service mysql status`
echo '2.开始导入数据....'
#导入数据
mysql < /mysql/schema.sql
echo '3.导入数据完毕....'
sleep 3
echo `service mysql status`
#重新设置mysql密码
echo '4.开始修改密码....'
mysql < /mysql/privileges.sql
echo '5.修改密码完毕....'
#sleep 3
echo `service mysql status`
echo `mysql容器启动完毕,且数据导入成功`
tail -f /dev/null
这里是先导入数据,然后才是设置用户和权限,是因为mysql容器一开始为免密登录,Dockerfile中有如下设置:ENV MYSQL_ALLOW_EMPTY_PASSWORD yes
,此时执行导入数据命令不需要登录验证操作,如果是先执行权限操作,那么导入数据则需要登录验证,整个过程就麻烦了许多。
[root@docker01 ~/wp_Dockerfile/mysql]# cat schema.sql
create database wordpress;
[root@docker01 ~/wp_Dockerfile/mysql]# cat privileges.sql
use mysql;
grant all on wordpress.* to wp@'172.18.0.%' identified by '123';
SET PASSWORD=PASSWORD('123456');
flush privileges;
#创建镜像
docker build -t mysql:v1 .
docker run -d --name mysql --network=wordpress wp_mysql:v1
docker run -d --name php --network=wordpress -v /code/wordpress/:/code/wordpress/ wp_php:v1
docker run -d --name nginx --network=wordpress -v /code/wordpress/:/code/wordpress/ -p 80:80 wp_nginx:v1
上传
rz wordpress*.tar.gz
解压
tar -xvf wordpress*.tar.gz
mv wordpress /root/wp_Dockerfile/
创建用户
groupadd www -g 666 && \
useradd www -u 666 -g 666 -s /sbin/nologin -M
授权
chown -R www.www /root/wp_Dockerfile/wordpress