5-- Dockerfile 搭建 部落格系統
阿新 • • 發佈:2021-07-30
目錄
安裝流程規劃
- 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
... ... ...
# 安裝