docker 练习 10 docker容器nginx &mysql& wordpress实战lnmp

来源:本站原创 Docker 超过140 views围观 0条评论

 

先运行一个测试容器,做准备脚本等东西

1新建容器
root@docker01:~# docker run -it –volumes-from /dbata  reg.ccie.wang/library/nginx:1.15.9 /bin/bash
2.将nginx 配置文件copy到 主机的目录
root@0ceda43a72e8:/# cp -a /etc/nginx/* /dbdata/
3.验证是否正确
root@0ceda43a72e8:/# ls /dbdata/
conf.d        koi-utf  mime.types  nginx.conf   uwsgi_params
fastcgi_params    koi-win  modules     scgi_params  win-utf

4.查看主机目录,并进入查看
root@docker01:~# docker inspect dbata |grep volumes
                "Source": "/var/lib/docker/volumes/59955b706aca5c81e18e30375d5f2993fb3119dfd9702fbb201b9cad4c09fab3/_data",

5.查看目录内容是否存在
root@docker01:~# ll /var/lib/docker/volumes/59955b706aca5c81e18e30375d5f2993fb3119dfd9702fbb201b9cad4c09fab3/_data
total 48
drwxr-xr-x 3 root root 4096 3月  19 18:23 ./
drwxr-xr-x 3 root root 4096 3月  19 18:16 ../
drwxr-xr-x 2 root root 4096 3月   5 12:34 conf.d/
-rw-r–r– 1 root root 1007 2月  26 22:13 fastcgi_params
-rw-r–r– 1 root root 2837 2月  26 22:13 koi-utf
-rw-r–r– 1 root root 2223 2月  26 22:13 koi-win
-rw-r–r– 1 root root 5231 2月  26 22:13 mime.types
lrwxrwxrwx 1 root root   22 2月  26 22:13 modules -> /usr/lib/nginx/modules
-rw-r–r– 1 root root  643 2月  26 22:13 nginx.conf
-rw-r–r– 1 root root  636 2月  26 22:13 scgi_params
-rw-r–r– 1 root root  664 2月  26 22:13 uwsgi_params
-rw-r–r– 1 root root 3610 2月  26 22:13 win-utf
root@docker01:~#

6.并文件copy到将要挂载的目录/opt/dockerfile/nginx_wordpress/etc/nginx
cp -a /var/lib/docker/volumes/59955b706aca5c81e18e30375d5f2993fb3119dfd9702fbb201b9cad4c09fab3/_data/* .
root@docker01:/opt/dockerfile/nginx_wordpress/etc/nginx# ll
total 48
drwxr-xr-x 3 root root 4096 3月  19 18:29 ./
drwxr-xr-x 3 root root 4096 3月  19 18:29 ../
drwxr-xr-x 2 root root 4096 3月   5 12:34 conf.d/
-rw-r–r– 1 root root 1007 2月  26 22:13 fastcgi_params
-rw-r–r– 1 root root 2837 2月  26 22:13 koi-utf
-rw-r–r– 1 root root 2223 2月  26 22:13 koi-win
-rw-r–r– 1 root root 5231 2月  26 22:13 mime.types
lrwxrwxrwx 1 root root   22 2月  26 22:13 modules -> /usr/lib/nginx/modules
-rw-r–r– 1 root root  643 2月  26 22:13 nginx.conf
-rw-r–r– 1 root root  636 2月  26 22:13 scgi_params
-rw-r–r– 1 root root  664 2月  26 22:13 uwsgi_params
-rw-r–r– 1 root root 3610 2月  26 22:13 win-utf
root@docker01:/opt/dockerfile/nginx_wordpress/etc/nginx#

正是开始

创建数据卷容器
docker run -it -v /dbdata –name dbata alpine

忘了数据库

1.先建数据库容器 并设置帐号等因为Wordpress要进行指定
docker run -itd -m 500M \
–volumes-from /dbata -v /opt/dockerfile/nginx_wordpress/mysql_data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-e MYSQL_DATABASE=wp \
-e MYSQL_USER=wpuser \
-e MYSQL_PASSWORD=111111 \
–name mysql_wp  reg.ccie.wang/library/mysql:5.7

2.创建 docker  wordpress 指定name 为–name wp001 ,
docker run -itd -m 500M -p 8080:80  \
–link  mysql_wp:mysql_wp \
–volumes-from /dbata -v /opt/dockerfile/nginx_wordpress/data:/data \
-e WORDPRESS_DB_HOST=mysql_wp:3306 \     #这时host就是上面的mysql名
-e WORDPRESS_DB_USER=wpuser \
-e WORDPRESS_DB_NAME=wp \
-e WORDPRESS_DB_PASSWORD=111111\
–name wp001  reg.ccie.wang/library/wordpress:latest
 
3.再创建 nginx 进行关联
docker run -itd -m 500M -p 80:80 \
–volumes-from /dbata -v /opt/dockerfile/nginx_wordpress/etc/nginx:/etc/nginx \
–name nginx_wp –link wp001:wp001 reg.ccie.wang/library/nginx:1.15.9

一定要按上面的顺序

验证
root@docker01:/opt/dockerfile/nginx_wordpress# docker stats 628b3cc50f10

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O           PIDS
628b3cc50f10        nginx_wp            0.00%               2.668MiB / 500MiB   0.53%               3.69kB / 1.27kB     8.19kB / 0B         2

nginx容器增加配置文件

root@docker01:/opt/dockerfile/nginx_wordpress/etc/nginx/conf.d# cat jeff.conf
upstream wp {
   server  wp001:80;
}

server {
    listen 80;
    server_name 192.168.10.67;
  location / {
           client_max_body_size   3m;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_pass http://wp;
}}

进入容器刷新配置

root@docker01:/opt/dockerfile/nginx_wordpress/etc/nginx/conf.d# cd
root@docker01:~# docker ps
CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS              PORTS                      NAMES
628b3cc50f10        reg.ccie.wang/library/nginx:1.15.9       "nginx -g ‘daemon of…"   9 minutes ago       Up 9 minutes        0.0.0.0:80->80/tcp         nginx_wp
c553107a4ec0        reg.ccie.wang/library/wordpress:latest   "docker-entrypoint.s…"   12 minutes ago      Up 12 minutes       0.0.0.0:8080->80/tcp       wp001
root@docker01:~# docker exec -it 628b3cc50f10 /bin/bash
root@628b3cc50f10:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@628b3cc50f10:/# nginx -s reload
2019/03/19 10:44:16 [notice] 14#14: signal process started
root@628b3cc50f10:/#

出来到其它机器上进行验证

[root@docker02 ~]# curl http://192.168.10.67/wp-admin/setup-config.php
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="robots" content="noindex,nofollow" />
    <title>WordPress &rsaquo; Setup Configuration File</title>
    <link rel=’stylesheet’ id=’buttons-css’  href=’http://192.168.10.67/wp-includes/css/buttons.min.css?ver=5.1′ type=’text/css’ media=’all’ />

出现以上信息即为成功

文章出自:CCIE那点事 http://www.jdccie.com/ 版权所有。本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源。 禁止全文转载。
本文链接:http://www.jdccie.com/?p=4098转载请注明转自CCIE那点事
如果喜欢:点此订阅本站
  • 相关文章
  • 为您推荐
  • 各种观点

暂时还木有人评论,坐等沙发!
发表评论

您必须 [ 登录 ] 才能发表留言!