目录
一、Dockerfile 构建镜像
1.Dockerfile 构建 nginx镜像
1.1创建 nginx Dockerfile 目录
1.2编写 Dockerfile 文件
1.3构建nginx镜像
2.Dockerfile 构建 mysql 镜像
2.1创建 mysql Dockerfile 目录
2.2修改mysql配置文件
2.3编写 Dockerfile 文件
2.4构建mysql镜像
3.Dockerfile 构建 php 镜像
3.1创建 php Dockerfile 目录
3.2编写 Dockerfile 文件
3.3调整PHP配置文件
3.4构建php镜像
二、基于镜像创建容器
1.自定义网络
2.创建容器
3.创建wordpress数据库
4.验证
三、部署 wordpress
mkdir /opt/nginx
cd /opt/nginx
将源码包nginx-1.24.0.tar.gz上传到该目录
vim Dockerfile
#指定基础镜像文件
FROM centos:7
#ADD:将nginx压缩包复制到镜像中并且解压到/opt目录下
ADD nginx-1.24.0.tar.gz /opt
#RUN:在镜像中执行的命令。使用 && 将多条命令合并为1条,减少镜像大小
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make && \
cd /opt/nginx-1.24.0 && \
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install && \
yum clean all
#清除缓存,减少镜像大小的方法之一。
#多级构建镜像,FROM指定第二级基础镜像
FROM centos:7
#from=0:指定第一级镜像。将第一级镜像中的/usr/local/nginx 复制到当前阶段的/usr/local/nginx
COPY --from=0 /usr/local/nginx /usr/local/nginx
RUN useradd -M -s /sbin/nologin nginx
#暴露nginx的80号端口
EXPOSE 80
#指定nginx的启动命令
CMD ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
#自定义镜像名称,这里我设置的为 nginx:centos7
docker build -t nginx:centos7 .
#构建完成后,查看镜像
docker images
mkdir /opt/mysql
cd /opt/mysql
将mysql-boost-5.7.20.tar.gz上传到目录下
#查看系统可用内存,最好大于15G
df -TH
vim my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[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
vim Dockerfile
FROM centos:7
ADD mysql-boost-5.7.20.tar.gz /opt/
#安装依赖包并编译安装
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make ncurses ncurses-devel bison cmake
RUN cd /opt/mysql-5.7.20/ && \
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_EXTRA_CHARSETS=all \
-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 -j2 && make install
FROM centos:7
#将一级镜像中的/usr/local/mysql/保留下来
COPY --from=0 /usr/local/mysql/ /usr/local/mysql/
#mysql配置文件复制到镜像中
COPY my.cnf /etc/my.cnf
#设置镜像的环境变量
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
#创建mysql用户
RUN useradd -M -s /sbin/nologin mysql && \
#修改/usr/local/mysql/和/etc/my.cnf的权限
chown -R mysql:mysql /usr/local/mysql/ && \
chown mysql:mysql /etc/my.cnf && \
#初始化mysql数据库
/usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
#暴露mysql的端口号
EXPOSE 3306
#设置mysql启动命令
CMD ["/usr/local/mysql/bin/mysqld"]
docker build -t mysql:centos7 .
docker images
mkdir /opt/php
cd /opt/php
上传php-7.1.10.tar.bz2压缩包
vim Dockerfile
#设置一级镜像的别名为 first
FROM centos:7 AS first
ADD php-7.1.10.tar.bz2 /opt
RUN yum -y install gcc gcc-c++ make gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel && \
cd /opt/php-7.1.10 && \
./configure \
--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 && make install
COPY php.ini /usr/local/php/lib/php.ini
COPY php-fpm.conf /usr/local/php/etc/php-fpm.conf
COPY www.conf /usr/local/php/etc/php-fpm.d/www.conf
FROM centos:7
#--from=first:指定一级镜像
COPY --from=first /usr/local/php /usr/local/php
RUN useradd -M -s /sbin/nologin php && yum -y install gd libjpeg-devel libpng-devel freetype-devel libxml2-devel zlib-devel curl-devel openssl-devel
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm","-F"]
cd /opt/php
准备php有三个配置文件:
php.ini 主配置文件
php-fpm.conf 进程服务配置文件
www.conf 扩展配置文件
#调整主配置文件:
vim php.ini
--1170行--修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
--939行--取消注释,修改
date.timezone = Asia/Shanghai
#调整进程服务配置文件:
vim php-fpm.conf
--17行--去掉";"注释
pid = run/php-fpm.pid
#调整扩展配置文件:
vim www.conf
--23,24行--修改用户和组
user = php
group = php
--36行--取消注释,修改
listen = 172.18.0.30:9000
--62行--取消注释,修改
listen.allowed_clients = 172.18.0.10
docker build -t mysql:centos7 .
docker images
#创建自定义网络
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mylnmp
#查看自定义网络
docker network ls
#创建nginx容器
docker run -d --name nginx --network=mylnmp --ip=172.18.0.10 -p 80:80 -v /opt/nginx/www:/usr/local/nginx/html nginx:centos7
#创建mysql容器
docker run -d --name mysql --network=mylnmp --privileged --ip 172.18.0.20 -p 3306:3306 -v /usr/local/mysql mysql:centos7
#创建php容器
docker run -d --name php -p 9000:9000 --network=mylnmp --ip 172.18.0.30 --volumes-from nginx --volumes-from mysql php:centos7
#进入mysql容器
docker exec -it mysql bash
mysqladmin -u root password "123456"
mysql -u root -p123456
CREATE DATABASE wordpress;
GRANT all ON wordpress.* TO 'admin'@'%' IDENTIFIED BY '123456';
GRANT all ON wordpress.* TO 'admin'@'localhost' IDENTIFIED BY '123456';
flush privileges;
cd /opt/nginx/www
#验证php
vim index.php
#验证mysql
vim index.php #替换原来的测试页内容
Success!!";
else echo "Fail!!";
?>
cd /opt
上传 wordpress-4.9.4-zh_CN.tar.gz
tar xf wordpress-4.9.4-zh_CN.tar.gz -C /opt/nginx/www
网页访问 http://192.168.88.10/wordpress/index.php