docker 网络、wordpress+mysql搭建博客

[root@cka-master ~]# docker network list  #列出网络
NETWORK ID     NAME      DRIVER    SCOPE
58a540cc3504   bridge    bridge    local
30c1150c3a88   host      host      local
99c3a0788d3a   none      null      local

#briage 网络就相当于虚拟机的NAT网络,每个容器都有自己的网络空间
#host  与宿主机共享网络
#none 无网络,一般用于测试
#创建网络
exp:
docker network create \
                --driver=bridge \
                --subnet=172.28.0.0/16 \
                --ip-range=172.28.5.0/24 \
                --gateway=172.28.5.254 \
                br0  


[root@cka-master ~]# docker network create --subnet=10.0.0.0/8 mynet 
6cbb12bd826cbb5d4ca02628b6b97fe57013f81d6bd067ef823fe8bd1463c612
[root@cka-master ~]# docker network list 
NETWORK ID     NAME      DRIVER    SCOPE
58a540cc3504   bridge    bridge    local
30c1150c3a88   host      host      local
6cbb12bd826c   mynet     bridge    local
99c3a0788d3a   none      null      local


[root@cka-master ~]# docker network inspect  mynet #查看网卡详细信息

#创建容器指定网络
1.brige
[root@cka-master ~]# docker run -d --name nginx --network=mynet  nginx 
e5d37372df4c9888d12601dd3f47aadaa6c635fbfd3a46903b312348b4d6b663

[root@cka-master ~]# docker inspect nginx | grep -A 10 NetworkID
                    "NetworkID": "6cbb12bd826cbb5d4ca02628b6b97fe57013f81d6bd067ef823fe8bd1463c612",
                    "EndpointID": "cfe860cb1e67c2057a7ad14a3dbd8ac1156df3202a71425bcaa4cdc2ce535e6a",
                    "Gateway": "10.0.0.1",
                    "IPAddress": "10.0.0.2",
                    "IPPrefixLen": 8,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:0a:00:00:02",
                    "DriverOpts": null
}

2.host
[root@cka-master ~]# docker run -it  --name busybox --rm --network=host busybox 
/ # ifconfig  #查看到的是物理机的网络


3.none
[root@cka-master ~]# docker run -it  --name busybox --rm --network=none busybox 
/ # ifconfig
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

wordpress + mysql 实现个人博客

#拉取镜像
docker pull hub.c.163.com/library/wordpress
docker pull hub.c.163.com/library/mysql

#查看数据卷
[root@cka-node1 ~]# docker history  hub.c.163.com/library/wordpress:latest  | grep -i volume 
<missing>      4 years ago   /bin/sh -c #(nop)  VOLUME [/var/www/html]       0B        

[root@cka-node1 ~]# docker history  hub.c.163.com/library/mysql   | grep -i volume 
<missing>      4 years ago   /bin/sh -c #(nop)  VOLUME [/var/lib/mysql]      0B  

#运行数据库
[root@cka-node1 ~]# mkdir /data
[root@cka-node1 ~]# docker run -d --name db -- restart=always -v /data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=zhanghao -e MYSQL_DATABASE=wordpress hub.c.163.com/library/mysql

#运行wordpress,选一种方法即可
一、
查看数据库服务器地址
[root@cka-node1 ~]# docker inspect db   | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

[root@cka-node1 ~]# docker run -d --name blog --restart=always -p 80:80 -v /blog:/var/www/html -e WORDPRESS_DB_NAME=wordpress -e  WORDPRESS_DB_HOST=172.17.0.2 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=zhanghao   
二、
docker run -d --name blog --restart=always -p 80:80 -v /blog:/var/www/html --link=db:mysql  hub.c.163.com/library/wordpress

访问宿主机:http://192.168.4.61 (192.168.4.61是我宿主机的IP) ,此时wordpress就搭建出来了,如下图按照提示初始化即可

wordpress1.png

Q.E.D.