Akemi

Docker-compose部署lnmp

2024/06/18

Docker Compose是一个用于定义和运行多个Docker容器的工具。它使用YAML文件来描述应用程序的服务、网络、存储等资源,并允许用户通过一个命令(如docker-compose up)来启动、停止、重启和管理整个应用程序。Docker Compose可以定义多个Docker容器,每个容器都可以使用自定义的Docker镜像,并可以指定容器之间的依赖关系和通信方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
docker-compose环境:
centos7.9
docker-compose最新版
192.168.10.1 lnmp

1.docker-compose节点初始化
hostnamectl set-hostname lnmp && bash
nmcli con modify ens18 ipv4.method manual ipv4.addresses 192.168.10.113/24 ipv4.gateway 192.168.10.1 ipv4.dns 192.168.1.1
nmcli con up ens18

yum install -y yum-utils device-mapper-persistent-data lvm2 wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack ntpdate telnet ipvsadm

yum -y install chrony
sed -i 's/^server/#server/g' /etc/chrony.conf
sed -i '1s/^/server cn.pool.ntp.org iburst\n/' /etc/chrony.conf
systemctl restart chronyd

sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
systemctl disable firewalld --now

modprobe br_netfilter
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl -p /etc/sysctl.d/k8s.conf

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce docker-compose -y
systemctl enable docker --now

2.常用命令
#启动并后台运行所有的服务
docker-compose up -d
#列出项目中目前的所有容器
docker-compose ps
#停止某个服务
docker-compose stop 服务名
#启动某个服务
docker-compose start 服务名
#停止并删除容器、网络、卷、镜像
docker-compose down

3.准备镜像与配置文件
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 2 years ago 141MB
mysql 5.6 dd3b2a5dcb48 2 years ago 303MB
php 7.3.29-fpm 4c013e5980cf 2 years ago 453MB

nginx配置文件
mkdir /root/lnmp/nginx/conf -p
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm index.php;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

location / {
index index.html index.htm index.php ;
try_files $uri $uri/ /index.php?$query_string;
autoindex on;
}

location ~ \.php$ {
#php73是容器命名
fastcgi_pass php73:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
}

}

docker-compose文件

#定义docker compose yml版本
version: "3"
#定义我们的服务对象
services:
#自定义的服务名称
nginx:
#镜像名称,默认拉取本地镜像,没有的话从远程获取
image: nginx:latest
#自定义容器的名称
container_name: c_nginx
#将宿主机的80端口映射到容器的80端口
ports:
- "80:80"
#将宿主机的~/lnmp/www目录和容器的/usr/share/nginx/html目录进行绑定,并设置rw权限
#将宿主机的~/lnmp/nginx/conf/default.conf和容器的/etc/nginx/conf.d/default.conf进行绑定
volumes:
- ~/lnmp/www/:/usr/share/nginx/html/:rw
- ~/lnmp/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf
#设置环境变量,当前的时区
environment:
TZ: "Asia/Shanghai"
#容器是否随docker服务启动重启
restart: always
#容器加入名为lnmp的网络
networks:
- lnmp
php:
image: php:7.3.29-fpm
container_name: php73
volumes:
- ~/lnmp/www/:/var/www/html/:rw
restart: always
cap_add:
- SYS_PTRACE
networks:
- lnmp

mysql:
image: mysql:5.6
container_name: mysql56
ports:
- "3306:3306"
volumes:
- ~/lnmp/mysql/data:/var/lib/mysql/:rw
restart: always
networks:
- lnmp
environment:
MYSQL_ROOT_PASSWORD: "123456"
TZ: "Asia/Shanghai"
networks:
#创建了一个自定义的网络叫做lnmp
lnmp:

---------------------------------------------------------------
4.启动docker-compose
docker-compose up -d
docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------
container_nginx /docker-entrypoint.sh ngin ... Up 0.0.0.0:80->80/tcp,:::80->80/tcp
mysql6 docker-entrypoint.sh mysqld Up 0.0.0.0:3306->3306/tcp,:::3306->3306/tcp
php73 docker-php-entrypoint php-fpm Up 9000/tcp

5.测试
(1)测试nginx作为http服务器是否可用
echo "wangsheng123" >> ~/lnmp/www/index.html

1
2
3
4
5
6
(2)测试nginx处理php请求
cat > ~/lnmp/www/a.php <<EOF
<?php
phpinfo();
?>
EOF

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
(3)测试php代码连接mysql
docker exec -it php73 /bin/bash
docker-php-ext-install pdo pdo_mysql mysqli # 安装插件
docker-php-ext-enable pdo pdo_mysql mysqli

vim ~/lnmp/www/mysql.php

<?php
// 创建连接
$conn = new mysqli('mysql6','root','123456');
if($conn->connect_error){
die("连接失败,错误:" . $conn->connect_error);
}
echo "mysql连接成功";

docker-compose restart

CATALOG