9,docker基础之---Compose理论+部署word press

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了9,docker基础之---Compose理论+部署word press相关的知识,希望对你有一定的参考价值。

一,安装docker-compose

介绍:高效便捷管理容器

#因为此环境是python搭建所以需要python支持检查虚拟机有无python环境
[root@docker ~]# python
Python 2.7.5 (default, Oct 14 2020, 14:45:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>#显示咱们有2.7版本的ok的

1,普通安装

安装插件:

[root@docker ~]# yum install python -y
[root@docker ~]# yum install epel-release -y
[root@docker ~]# yum install python-pip -y
#pip是python下载软件的插件也是下载compose的主要工具

进行更新pip:

[root@docker ~]# pip install --upgrade pip

检查pip版本;

[root@docker ~]# pip --version

安装docker compose,自行更改版本号:

[root@docker ~]# pip install -U docker-compose==1.24.1

检查docker compose版本:

[root@docker ~]# docker-compose version

如果安装docker-compose时出现报错则进行升级requests库:

[root@docker ~]# pip install -I requests==2.10.0

再次安装docker compose:

[root@docker ~]# pip install -U docker-compose==1.24.1

如果继续报错卸载python-pip

[root@docker ~]# sudo yum remove python-pip -y

下载get-pip.py文件:

[root@docker ~]# wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
[root@docker ~]# sudo python get-pip.py

再次进行更新:

[root@docker ~]# pip install --upgrade pip

然后进行安装docker-compose即可成功:

[root@docker ~]# pip install -U docker-compose==1.24.1

2,简易安装

安装docker-compose版本为1.24.1【上述方法不行采用下面连接方式进行下载下载速度比较缓慢】

[root@docker ~]# curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose


% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 15.4M 100 15.4M 0 0 47977 0 0:05:36 0:05:36 --:--:-- 70633
#赋予应有权限
[root@docker ~]# chmod +x /usr/local/bin/docker-compose
#进行查看版本
[root@docker ~]# docker-compose version
docker-compose version 1.24.1, build 4667896b
docker-py version: 3.7.3
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.1.0j 20 Nov 2018
[root@docker ~]#

二,compose开关查删

创建一个docker-compose目录(在里面创建yml文件注意要在主目录执行)

[root@docker ~]# mkdir /usr/local/docker-compose
[root@docker ~]# cd /usr/local/docker-compose/
[root@docker docker-compose]#

编写一个简单的yml文件;

version 3  #实际上有三个版本的但是建议还是用最新版本3
services: #容器命名
redis:
image: mycentos: redis

[root@docker docker-compose]# mkdir test
[root@docker docker-compose]# cd test/
[root@docker test]# vi docker-compose.yml
[root@docker test]# cat docker-compose.yml
version: 3
services:
redis:
image: mycentos: redis
[root@docker test]#

启动配置文件:

[root@docker test]# docker-compose up -d
Creating network "test_default" with the default driver
Creating test_redis_1 ... done
[root@docker test]#

查看容器:

[root@docker test]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------
test_redis_1 /bin/sh -c /usr/local/redi ... Up
[root@docker test]#

进入容器:

[root@docker test]# docker-compose exec redis bash
[root@97366f6ce4e7 /]#

停止容器:

[root@docker test]# docker-compose stop
Stopping test_redis_1 ... done
[root@docker test]# docker-compose ps
Name Command State Ports
--------------------------------------------------------------
test_redis_1 /bin/sh -c /usr/local/redi ... Exit 0
[root@docker test]#

启动容器:

[root@docker test]# docker-compose start
Starting redis ... done
[root@docker test]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------
test_redis_1 /bin/sh -c /usr/local/redi ... Up
[root@docker test]#

删除容器:

[root@docker test]# docker-compose down
Stopping test_redis_1 ... done
Removing test_redis_1 ... done
Removing network test_default
[root@docker test]# docker-compose ps
Name Command State Ports
------------------------------
[root@docker test]#

容器生成之后会自动生成一个卷如果删除容器之后还想干掉卷即可这样操作:

[root@docker test]# docker-compose down --volumes

三,compose核心技能

version: 3
services:
nginx:
image: mycentos:nginx
network_mode: "host"
volumes:
- /home:/usr/local/nginx/html
- /var/logs/nginx/logs:/usr/local/nginx/logs
command: /usr/local/nginx/sbin/nginx -g "daemon off;"
redis:
image: mycentos:redis
ports:
- "6380:6379"

[root@docker nginx]# cd /home/
[root@docker home]# ll
总用量 1708
-rw-r--r-- 1 root root 179 2 16 09:28 dockerfile
drwxrwxr-x 6 root root 309 3 27 2018 redis-4.0.9
-rw-r--r-- 1 root root 1737022 2 16 09:27 redis-4.0.9.tar.gz
-rw-r--r-- 1 root root 219 2 16 09:28 redis_install.sh
[root@docker home]# cat > index.html
docker-compose--nginx
^C
[root@docker home]#
[root@docker ~]# cd /usr/local/docker-compose/test/
[root@docker test]# mkdir -p /var/logs/nginx/
[root@docker test]# mkdir -p /var/logs/nginx/logs
[root@docker test]# mkdir nginx/
[root@docker test]# cd nginx/
[root@docker nginx]# cat docker-compose.yml
version: 3
services:
nginx:
image: mycentos:nginx
volumes:
- /home:/usr/local/nginx/html
- /var/logs/nginx/logs:/usr/local/nginx/logs
command: /usr/local/nginx/sbin/nginx -g "daemon off;"
redis:
image: mycentos:redis
ports:
- "6380:6379"
[root@docker nginx]#

进行启动:

[root@docker nginx]# docker-compose up -d
Creating network "nginx_default" with the default driver
Creating nginx_nginx_1 ... done
Creating nginx_redis_1 ... done
[root@docker nginx]#

查看启动:

[root@docker nginx]# docker-compose ps
Name Command State Ports
--------------------------------------------------------------------------------
nginx_nginx_1 /usr/local/nginx/sbin/ngin Up 80/tcp
...
nginx_redis_1 /bin/sh -c /usr/local/redi Up 0.0.0.0:6380->6379/tcp,:::
... 6380->6379/tcp
[root@docker nginx]#

四,compose部署wordpress

version: 3.3  #版本
services:
db: #采用镜像
image: mysql:5.7
volumes: #代表挂载文件路径
- db_data:/var/lib/mysql #
restart: always #自动重启保证服务在线
environment:
MYSQL_ROOT_PASSWORD: somewordpress #设置mysql密码
MYSQL_DATABASE: wordpress #库名称
MYSQL_USER: wordpress #账号
MYSQL_PASSWORD: wordpress #密码
wordpress: #只有等上面得mysql安装完毕才能执行以下
depends_on:
- db #相当于--link
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: #对应上面volumes文件路径

version: 3.3
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data:

#创建一个wordpress目录

[root@docker ~]# mkdir -p /usr/local/docker-compose/wordpress
[root@docker wordpress]# cat docker-compose.yml
version: 3.3
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data:
[root@docker wordpress]#执行启动
[root@docker wordpress]# docker-compose up -d
#进行访问8000端口

9,docker基础之---Compose理论+部署word

查看卷名称:

[root@docker wordpress]# docker volume ls
DRIVER VOLUME NAME
local 4ae86d65d3c9d303fbe30f4cdd57b70d874b2f2d8bbc9d1c7edb3e1fb5bf1cb2
local c257d582258d30274aae0e411c3d39d2ce9c70c8c771fe702da0d9c20dbfa9cd
local wordpress_db_data
[root@docker wordpress]# docker volume inspect wordpress_db_data
[

"CreatedAt": "2023-02-19T13:56:50+08:00",
"Driver": "local",
"Labels":
"com.docker.compose.project": "wordpress",
"com.docker.compose.version": "1.24.1",
"com.docker.compose.volume": "db_data"
,
"Mountpoint": "/var/lib/docker/volumes/wordpress_db_data/_data", #路径
"Name": "wordpress_db_data",
"Options": null,
"Scope": "local"

]
[root@docker wordpress]#进入上述路径
[root@docker wordpress]# cd /var/lib/docker/volumes/wordpress_db_data/_data
[root@docker _data]# ll 下面内容都是数据库对应得内容由此可以得知这个路径挂载到容器内部
总用量 188488
-rw-r----- 1 polkitd input 56 2 19 13:57 auto.cnf
-rw------- 1 polkitd input 1680 2 19 13:57 ca-key.pem
-rw-r--r-- 1 polkitd input 1112 2 19 13:57 ca.pem
-rw-r--r-- 1 polkitd input 1112 2 19 13:57 client-cert.pem
-rw------- 1 polkitd input 1676 2 19 13:57 client-key.pem
-rw-r----- 1 polkitd input 609 2 19 14:16 ib_buffer_pool
-rw-r----- 1 polkitd input 79691776 2 19 14:19 ibdata1
-rw-r----- 1 polkitd input 50331648 2 19 14:19 ib_logfile0
-rw-r----- 1 polkitd input 50331648 2 19 13:57 ib_logfile1
-rw-r----- 1 polkitd input 12582912 2 19 14:19 ibtmp1
drwxr-x--- 2 polkitd input 4096 2 19 13:57 mysql
drwxr-x--- 2 polkitd input 8192 2 19 13:57 performance_schema
-rw------- 1 polkitd input 1680 2 19 13:57 private_key.pem
-rw-r--r-- 1 polkitd input 452 2 19 13:57 public_key.pem
-rw-r--r-- 1 polkitd input 1112 2 19 13:57 server-cert.pem
-rw------- 1 polkitd input 1680 2 19 13:57 server-key.pem
drwxr-x--- 2 polkitd input 8192 2 19 13:57 sys
drwxr-x--- 2 polkitd input 4096 2 19 14:11 wordpress
[root@docker _data]#进入mysql数据库容器
[root@docker ~]# cd /usr/local/docker-compose/wordpress/
[root@docker wordpress]# ll
总用量 4
-rw-r--r-- 1 root root 591 2 19 13:56 docker-compose.yml
[root@docker wordpress]# docker-compose exec db bash
root@a891476f6167:/# cd /var/lib/mysql
root@a891476f6167:/var/lib/mysql# ll
bash: ll: command not found
root@a891476f6167:/var/lib/mysql# ls
auto.cnf client-key.pem ibdata1 private_key.pem sys
ca-key.pem ib_buffer_pool ibtmp1 public_key.pem wordpress
ca.pem ib_logfile0 mysql server-cert.pem
client-cert.pem ib_logfile1 performance_schema server-key.pem
root@a891476f6167:/var/lib/mysql#发现内容一样所以可得他们路径挂载到本机路径

以上是关于9,docker基础之---Compose理论+部署word press的主要内容,如果未能解决你的问题,请参考以下文章

docker基础之docker-compose搭建consul集群

docker基础之docker-compose搭建consul集群

docker基础之docker-compose搭建consul集群

小白学Docker之Compose

小白学Docker之Compose

Ansible之AWX安装部署