目录
任务需求:
一、准备:
二、部署nginx容器(172.18.0.10):
1.编写Dockerfile构建镜像:
2.准备nginx配置文件:
3.构建镜像,启动nginx容器:
三、部署mysql容器(172.18.0.20):
1. 编写Dockerfile构建镜像:
2.准备mysql配置文件:
3.构建镜像,启动mysql容器:
四、部署php容器(172.18.0.30):
1. 编写Dockerfile构建镜像:
2.构建镜像,启动php容器:
五、启动wordpress服务:
1. 登录到mysql容器中,授权:
2.网页访问:
(1)使用Docker构建LNMP环境并运行Wordpress网站平台。
(2)限制Nginx容器最多使用500Mb的内存和1G的Swap。
(3)限制Mysql容器写 /dev/sda 的速率为 10 MB/s。
(4)将所有容器进行快照,然后将Docker镜像打包成tar包备份到本地。
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
#关闭防火墙和核心防护
docker pull centos:7
#从docker Hub公共仓库下载基础镜像
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork
#设置自定义网络模式,模式为bridge模式,docker1的ip地址为172.18.0.1,显示的网络名称mynetwork。
docker network ls
#查看使用的网络模式
ifconfig
##查看是否成功
mkdir /opt/nginx
mkdir /opt/mysql
mkdir /opt/php ##创建工作目录
FROM centos:7 AS first
#使用centos:7镜像作为底层镜像
MAINTAINER this is nginx image
#管理员信息(可不写)
ADD nginx-1.24.0.tar.gz /opt/
#上传安装包至容器/opt目录
RUN yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make && \
useradd -M -s /sbin/nologin nginx && \
cd /opt/nginx-1.24.0 && \
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && \
make && \
make install
#编译安装nginx
FROM centos:7
COPY --from=first /usr/local/nginx/ /usr/local/nginx/
#多层构建,使用上层镜像作为底层镜像,减小镜像体积
RUN useradd -M -s /sbin/nologin nginx
#创建nginx用户
EXPOSE 80
EXPOSE 443
#开启80,443端口
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
#指定nginx前台启动
#user nobody;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.wzw.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html; #添加.php结尾的默认文件名
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ { #开启fastcgi接口
root html;
fastcgi_pass 172.18.0.30:9000; #指定php信息
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
docker build -t nginx:lnmp .
#构建nginx镜像(别忘记最后的点)
docker images
#查看镜像
docker run -d --name nginx -p 80:80 -v /opt/nginx/html/:/usr/local/nginx/html/ -v /opt/nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf -m 500m --memory-swap 1g --net mynetwork --ip 172.18.0.10 nginx:lnmp
#启动容器
----------------------------
-m 500: #表示该容器内存最大为500MB
--memory-swap: #表示内存+swap总共1G,那swap也是500MB
//相当于是限制了内存的使用量,避免所占资源太多
FROM centos:7 AS first
MAINTAINER this is mysql image
ADD mysql-boost-5.7.41.tar.gz /opt/
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make openssl-devel && \
cd /opt/mysql-5.7.41/ && \
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 2 && make install
FROM centos:7
COPY --from=first /usr/local/mysql/ /usr/local/mysql/
ADD my.cnf /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin mysql && \
chown mysql:mysql /etc/my.cnf && \
chown -R mysql:mysql /usr/local/mysql/ && \
/usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data #重置数据库
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"] #前台启动mysql
[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
docker build -t mysql:lnmp .
#生成镜像
docker run --name mysql -d --privileged --device-write-bps /dev/sda:10M -v /usr/local/mysql --net mynetwork --ip 172.18.0.20 mysql:lnmp
#启动镜像
-----------------------------------------
--privileged: #让容器内拥有root权限
--device-write-bps: #限制写入到/dev/sda中每秒最大10M的速度。
-v /usr/local/mysql: #共享这个目录,其它容器可以使用--volumes-from 指定读取本容器的共享目录。然后也会生成一样的目录。
--net mynetwork: #指定--net网络模式,mynetwork为自定义网络模式。
--ip 172.18.0.20: #指定ip地址,自定义网络模式也可以指定ip地址。
-----------------------------------------
docker ps -a
#查看容器
FROM centos:7 AS first
MAINTAINER this is php image
ADD php-7.1.10.tar.bz2 /opt/
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 && \
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 -j 2 && make install && \
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini && \
sed -i -e '939c date.timezone = Asia/Shanghai' -e '1170c mysqli.default_socket = /usr/local/mysql/mysql.sock' /usr/local/php/lib/php.ini && \
#更改php.ini配置
cd /usr/local/php/etc/ && \
cp php-fpm.conf.default php-fpm.conf && \
sed -i '17 s/^;//' php-fpm.conf && \
cd /usr/local/php/etc/php-fpm.d/ && \
cp www.conf.default www.conf && \
sed -i -e '23c user = nginx' -e '24c group = nginx' -e '36c listen = 172.18.0.30:9000' -e '62c listen.allowed_clients = 172.18.0.10' www.conf
#更改php-fpm.conf配置
FROM centos:7 #使用多级构建
COPY --from=first /usr/local/php/ /usr/local/php/
RUN yum -y install gd \
libjpeg-devel \
libpng-devel \
freetype-devel \
libxml2-devel \
zlib-devel \
curl-devel \
openssl-devel && \
useradd -M -s /sbin/nologin nginx && \
rm -rf /var/cache/yum/*
#清理容器中yum缓存,减小容器体积
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm", "-F"]
由于在Dockerfile中更改了配置文件内容,所以不需要自己准备配置文件
也可以自己准备好配置文件,使用copy复制到容器中
docker build -t php:lnmp .
#构建镜像
docker run -d --name php --net mynetwork --ip 172.18.0.30 -p 9000:9000 --volumes-from nginx --volumes-from mysql php:lnmp
#启动容器
-----------------------------------------------
--volumes-from nginx: #表示读取nginx容器的共享信息
--volumes-from mysql: #表示读取mysql容器的共享信息
-----------------------------------------------
docker ps -a
#查看容器
docker exec -it mysql bash
#登录到mysql容器
mysql
#登录到容器中
create database wordpress;
#创建一个数据库
grant all privileges on wordpress.* to 'wordpress'@'%' identified by 'abc123';
#授权
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;
#刷新
http://192.168.30.100/wordpress/wp-admin/setup-config.php