5-- Dockerfile 搭建 博客系统
安装流程规划
- wordpress装在宿主主机,相当于一台NFS
- php用yum安装在一台容器内
- nginx装在一个容器内
- 将容器挂载到wordpress站点目录以实现访问
- 宿主机内cpu要求四核四G
1.建站点,上传软件包
1、创建目录
[root@docter ~]# mkdir /blog/{php,nginx,mysql}
2、上传wordpress包并解压
[root@docker opt]# wget https://wordpress.org/latest.zip
[root@docker opt]# unzip -d /blog/ latest.zip
3、构建centos:7镜像
[root@docter ~]# docker run -it centos:7 bash
[root@docter ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 3c9be54d505f 7 months ago 204MB
4、创建一个网桥
[root@docter ~]# docker network create lnmp
[root@docter ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
e26ae49a68f1 bridge bridge local
b0a268c1d902 host host local
bb02f8b94a1a lnmp bridge local
0d214ca04d47 none null local
2.准备php文件+构建为镜像
#切换目录
[root@docker ~]# cd /blog/php
#编辑php配置文件
[root@docker php]# vim www.conf
[www]
user = www
group = www
listen = 9000
request_terminate_timeout = 0
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session
php_value[soap.wsdl_cache_dir] = /var/lib/php/wsdlcache
#创建PHP.repo
[root@docker php]# vim php.repo
[php-webtatic]
name = PHP Repo
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0
enable = 1
#编辑php Dockerfile
[root@docker php]# vim Dockerfile
FROM centos:7
ADD www.conf /etc/php-fpm.d/www.conf
ADD php.repo /etc/yum.repos.d/php.repo
RUN useradd www
RUN yum install -y php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb php71w-bcmath --skip-broken
EXPOSE 9000
CMD ["php-fpm "-F""]
#构建php镜像
[root@docker php]# docker build -t php:v1 .
# 启动容器(容器名为php,指定网桥,指定挂载目录,开辟一个窗口)
[root@docker php]# docker run -it --name php --network=lnmp -v /blog/wordpress/:/usr/share/nginx/html php:v1 bash
3.准备nginx文件+构建为镜像
1、编写nginx的dockerfile文件
[root@docter nginx]# cat Dockerfile
FROM centos:7
ADD nginx.repo /etc/yum.repos.d/
RUN yum -y install nginx
ADD default.conf /etc/nginx/conf.d/
ADD nginx.conf /etc/nginx/
WORKDIR /usr/share/nginx/
EXPOSE 80 443
RUN groupadd www -g 666 && \
useradd www -u 666 -g 666
CMD nginx -g "daemon off;"
2、编写nginx的default.conf 文件
[root@docter nginx]# cat default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
root /usr/share/nginx/html;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
3、编辑nginx配置文件
[root@docker1 nginx]# vim nginx.conf
user www;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
4、编写nginx的nginx.repo文件
[root@docter nginx]# cat nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
5、构建镜像
[root@docter nginx]# docker build -t nginx:v1 . #构建必须和dockerfile在同一个目录中
6、启动容器(容器名为nginx,绑定端口映射,指定网桥,指定挂载目录,开辟一个窗口)
[root@docker opt]# docker run -it --name nginx -p 80:80 --network=lnmp -v /blog/wordpress/:/usr/share/nginx/html nginx:latest bash
4.构建MYSQL镜像
# 启动容器(容器名为mysql,指定网桥,创建密码))
[root@docker blog]# docker run -d -e MYSQL_ROOT_PASSWORD=123 --network=lnmp --name mysql mysql:5.7
#进入mysql容器
[root@docker1 ~]# docker exec -it mysql bash
#进入数据库
root@9669bf1d0d99:/# mysql -uroot -p
Enter password: 123
#创建数据库
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
5.测试
#查看全部镜像
[root@m01 blog]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php v1 a1665bd3e348 About an hour ago 534MB
nginx latest 77c83387bf62 About an hour ago 335MB
mysql 5.7 09361feeb475 5 days ago 447MB
centos 7 8652b9f0cb4c 7 months ago 204MB
#查看全部容器是否启动成功
[root@m01 blog]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
deab5f483b44 mysql:5.7 "docker-entrypoint.s…" About an hour ago Up About an hour 3306/tcp, 33060/tcp mysql
94d5a2684033 php:v1 "bash" About an hour ago Up 50 minutes 9000/tcp php
60fb25477abb nginx:latest "bash" About an hour ago Up 55 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp nginx
#进入nginx容器执行nginx前台运行的命令
[root@60fb25477abb conf.d]# nginx -g "daemon off;"
#进入php容器执行php前台运行的命令
[root@94d5a2684033 /]# php-fpm -F
#IP访问测试
1、把 wp-config.php文件写到此目录中就ok
[root@docter wordpress]# pwd
/blog/wordpress
[root@docter wordpress]# vim wp-config.php
<?php
... ... ...
# 安装