Docker之docker容器网络
docker容器网络
Docker在安装后自动提供3种网络,可以使用docker network ls
命令查看
[root@docker ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
9418b36060ba bridge bridge local
e60478a9ce12 host host local
b1eca0cb44c6 none null local
Docker使用Linux桥接,在宿主机虚拟一个Docker容器网桥(docker0),Docker启动一个容器时会根据Docker网桥的网段分配给容器一个IP地址,称为Container-IP,同时Docker网桥是每个容器的默认网关。因为在同一宿主机内的容器都接入同一个网桥,这样容器之间就能够通过容器的Container-IP直接通信。
docker的4种网络模式
网络模式 | 配置 | 说明 |
---|---|---|
host | –network host | 容器和宿主机共享Network namespace |
container | –network container:NAME_OR_ID | 容器和另外一个容器共享Network namespace |
none | –network none | 容器有独立的Network namespace, 但并没有对其进行任何网络设置, 如分配veth pair 和网桥连接,配置IP等 |
bridge | –network bridge | 默认模式 |
bridge模式
当Docker进程启动时,会在主机上创建一个名为docker0的虚拟网桥,此主机上启动的Docker容器会连接到这个虚拟网桥上。虚拟网桥的工作方式和物理交换机类似,这样主机上的所有容器就通过交换机连在了一个二层网络中。
从docker0子网中分配一个IP给容器使用,并设置docker0的IP地址为容器的默认网关。在主机上创建一对虚拟网卡veth pair设备,Docker将veth pair设备的一端放在新创建的容器中,并命名为eth0(容器的网卡),另一端放在主机中,以vethxxx这样类似的名字命名,并将这个网络设备加入到docker0网桥中。可以通过brctl show命令查看。
bridge模式是docker的默认网络模式,不写–network参数,就是bridge模式。使用docker run -p时,docker实际是在iptables做了DNAT规则,实现端口转发功能。可以使用iptables -t nat -vnL查看。
bridge模式如下图所示:
假设上图的docker2中运行了一个nginx,大家来想几个问题:
- 同主机间两个容器间是否可以直接通信?比如在docker1上能不能直接访问到docker2的nginx站点?
- 在宿主机上能否直接访问到docker2的nginx站点?
- 在另一台主机上如何访问node1上的这个nginx站点呢?DNAT发布?
Docker网桥是宿主机虚拟出来的,并不是真实存在的网络设备,外部网络是无法寻址到的,这也意味着外部网络无法通过直接Container-IP访问到容器。如果容器希望外部访问能够访问到,可以通过映射容器端口到宿主主机(端口映射),即docker run创建容器时候通过 -p 或 -P 参数来启用,访问容器的时候就通过[宿主机IP]:[容器端口]访问容器。
[root@docker ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:d1:aa:86 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.150/24 brd 192.168.200.255 scope global dynamic noprefixroute ens33
valid_lft 1659sec preferred_lft 1659sec
inet6 fe80::3f7:eb08:5d5b:98f5/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:09:81:2b:62 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:9ff:fe81:2b62/64 scope link
valid_lft forever preferred_lft forever
[root@docker ~]# docker run -it --rm --name nginx centos:latest /bin/bash
[root@1b9ac7ed8eef /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
42: eth0@if43: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@docker ~]# docker run -it --rm --name centos -p 8080:80 centos:latest
[root@e1406efe54fc /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
44: eth0@if45: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:03 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.3/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@docker ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 25 0.0.0.0:514 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8080 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 25 [::]:514 [::]:*
LISTEN 0 128 [::]:8080 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@docker ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
11 572 DOCKER all -- * * 0.0.0.0/0 0.0.0.0/0 ADDRTYPE match dst-type LOCAL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
281 17532 MASQUERADE all -- * !docker0 172.17.0.0/16 0.0.0.0/0
0 0 MASQUERADE tcp -- * * 172.17.0.3 172.17.0.3 tcp dpt:80
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
2 120 DOCKER all -- * * 0.0.0.0/0 !127.0.0.0/8 ADDRTYPE match dst-type LOCAL
Chain DOCKER (2 references)
pkts bytes target prot opt in out source destination
0 0 RETURN all -- docker0 * 0.0.0.0/0 0.0.0.0/0
0 0 DNAT tcp -- !docker0 * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 to:172.17.0.3:80
[root@docker ~]# docker network inspect bridge
[
{
"Name": "bridge",
"Id": "9418b36060ba85ae4d7fb6c482efd428224596470e33d1603faa4587232d515c",
"Created": "2021-12-03T05:00:03.114108852-05:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"1b9ac7ed8eef6a7bb5475203e30c74f996e7c616d1b8d9a32b04bdc15ca8df6d": {
"Name": "nginx",
"EndpointID": "08c3a6b30071087cad81e4bb3379b2d20569c17e0ad109fc24ffb02db2aaef76",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
},
"e1406efe54fc4e379e03e84bedb695d8db76a449d2ef2ba169eaf9913ae27955": {
"Name": "centos",
"EndpointID": "4d02335ce5db639a93b5086368777c8ce5f425c47641ae3f8658792113f39def",
"MacAddress": "02:42:ac:11:00:03",
"IPv4Address": "172.17.0.3/16",
"IPv6Address": ""
}
},
"Options": {
"com.docker.network.bridge.default_bridge": "true",
"com.docker.network.bridge.enable_icc": "true",
"com.docker.network.bridge.enable_ip_masquerade": "true",
"com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
"com.docker.network.bridge.name": "docker0",
"com.docker.network.driver.mtu": "1500"
},
"Labels": {}
}
]
[root@docker ~]# brctl show
bridge name bridge id STP enabled interfaces
docker0 8000.024209812b62 no veth29a6b3a
veth65312e1
container模式
这个模式指定新创建的容器和已经存在的一个容器共享一个 Network Namespace,而不是和宿主机共享。新创建的容器不会创建自己的网卡,配置自己的 IP,而是和一个指定的容器共享 IP、端口范围等。同样,两个容器除了网络方面,其他的如文件系统、进程列表等还是隔离的。两个容器的进程可以通过 lo 网卡设备通信。
container模式如下图所示:
host模式
如果启动容器的时候使用host模式,那么这个容器将不会获得一个独立的Network Namespace,而是和宿主机共用一个Network Namespace。容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。但是,容器的其他方面,如文件系统、进程列表等还是和宿主机隔离的。
使用host模式的容器可以直接使用宿主机的IP地址与外界通信,容器内部的服务端口也可以使用宿主机的端口,不需要进行NAT,host最大的优势就是网络性能比较好,但是docker host上已经使用的端口就不能再用了,网络的隔离性不好。
Host模式如下图所示:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:d1:aa:86 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.150/24 brd 192.168.200.255 scope global dynamic noprefixroute ens33
valid_lft 941sec preferred_lft 941sec
inet6 fe80::3f7:eb08:5d5b:98f5/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:09:81:2b:62 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:9ff:fe81:2b62/64 scope link
valid_lft forever preferred_lft forever
[root@docker ~]# docker run -it --network host --rm --name nginx centos:latest /bin/sh
sh-4.4# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:d1:aa:86 brd ff:ff:ff:ff:ff:ff
inet 192.168.200.150/24 brd 192.168.200.255 scope global dynamic noprefixroute ens33
valid_lft 903sec preferred_lft 903sec
inet6 fe80::3f7:eb08:5d5b:98f5/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:09:81:2b:62 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:9ff:fe81:2b62/64 scope link
valid_lft forever preferred_lft foreverbash
none模式
使用none模式,Docker容器拥有自己的Network Namespace,但是,并不为Docker容器进行任何网络配置。也就是说,这个Docker容器没有网卡、IP、路由等信息。需要我们自己为Docker容器添加网卡、配置IP等。
这种网络模式下容器只有lo回环网络,没有其他网卡。none模式可以在容器创建时通过–network none来指定。这种类型的网络没有办法联网,封闭的网络能很好的保证容器的安全性。
应用场景:
- 启动一个容器处理数据,比如转换数据格式
- 一些后台的计算和处理任务
none模式如下图所示:
[root@docker ~]# docker run -it --rm --name nginx --network none centos:latest /bin/bash
[root@25502089fbe7 /]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
使用container模式搭建lnmp
nginx
//拉取centos镜像
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker ~]# docker run -it centos /bin/sh
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
sh-4.4#
exit
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
96c6ee05b624 centos "/bin/sh" 56 seconds ago Exited (127) 8 seconds ago intelligent_euler
//传包
[root@docker ~]# docker cp /usr/src/nginx-1.20.1.tar.gz 96c6ee05b624:/usr/src
//创建用户
sh-4.4# useradd -r -M -s /sbin/nologin nginx
//解压
[root@docker ~]# docker exec -it 96c6ee05b624 /bin/sh
sh-4.4# cd /usr/src/
sh-4.4# ls
debug kernels nginx-1.20.1.tar.gz
sh-4.4# tar xf nginx-1.20.1.tar.gz
sh-4.4# ls
debug kernels nginx-1.20.1 nginx-1.20.1.tar.gz
//安装依赖包
sh-4.4# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
//创建日志存放目录
sh-4.4# mkdir -p /var/log/nginx
//编译安装
sh-4.4# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log && make && make install
//设置自启动
sh-4.4# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
sh-4.4# . /etc/profile.d/nginx.sh
sh-4.4# nginx
sh-4.4# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
sh-4.4# echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
//修改配置文件
sh-4.4# vim /usr/local/nginx/conf/nginx.conf
.......
location / {
root html;
index index.php index.html index.htm;
}
.......
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $Document_Root$fastcgi_script_name;
include fastcgi_params;
}
......
sh-4.4# /usr/local/nginx/sbin/nginx -s reload
//添加镜像
[root@docker ~]# docker commit -p -c 'CMD ["/usr/local/nginx/bin/nginx"]' nginx linlusama/centos-nginx:v1.20.3
sha256:e045067acf3c30ed8a0dcb6eb47d5b297d2e9d4bc2f28aba70fa64eca6a203bd
mysql
//创建容器
[root@docker ~]# docker run -itd --name mysql --network container:
9d39e8bbb2fd centos:latest /bin/sh
41b947a83e5633de554b202902dc5514a2164865d2d059bfdc407601369c1296
//连接
[root@docker ~]# docker exec -it mysql /bin/sh
sh-4.4# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
//传包
[root@docker ~]# docker cp mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz mysql:/usr/src/
//下载依赖包
yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs numactl
//创建用户
sh-4.4# useradd -r -M -s /sbin/nologin mysql
//解压
sh-4.4# cd /usr/src/
sh-4.4# ls
debug kernels mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
sh-4.4# tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
sh-4.4# cd /usr/local/
sh-4.4# ls
bin include libexec share
etc lib mysql-5.7.31-linux-glibc2.12-x86_64 src
games lib64 sbin
sh-4.4# mv mysql-5.7.31-linux-glibc2.12-x86_64/ mysql
sh-4.4# chown -R mysql.mysql mysql/
//创建数据存放目录
sh-4.4# mkdir /opt/data
sh-4.4# chown -R mysql.mysql /opt/data/
//环境变量
sh-4.4# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
sh-4.4# source /etc/profile.d/mysql.sh
//初始化
sh-4.4# mysqld --initialize-insecure --user mysql --datadir /opt/data
2021-12-03T11:35:08.177132Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-12-03T11:35:08.352238Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-12-03T11:35:08.377721Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-12-03T11:35:08.433675Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 116e2a2b-542d-11ec-bed5-0242ac110002.
2021-12-03T11:35:08.434743Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-12-03T11:35:08.829029Z 0 [Warning] CA certificate ca.pem is self signed.
2021-12-03T11:35:08.965104Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
//写配置文件
sh-4.4# vim /etc/my.cnf
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
sh-4.4# vim /usr/local/mysql/support-files/mysql.server
.....
basedir=/usr/local/mysql
datadir=/opt/data
.....
//启动
sh-4.4# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
sh-4.4# /etc/init.d/mysql start
Starting MySQL.Logging to '/opt/data/9d39e8bbb2fd.err'.
SUCCESS!
sh-4.4# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 80 *:3306 *:*
//写启动脚本
[root@9d39e8bbb2fd local]# cd /
[root@9d39e8bbb2fd /]# cat start.sh
#! /bin/bash
/etc/init.d/mysql start
/bin/bash
sh-4.4# chmod +x start.sh
//生成镜像
[root@docker ~]# docker commit -p -c 'CMD ["/bin/bash","/start.sh"]' mysql linlusama/mysql:v5.7.31
sha256:20bb1f3eaf5272da5cf760e0fe3a4e6c90bd0d7a7e6a0429a517d4de9097bb19
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
linlusama/mysql v5.7.31 20bb1f3eaf52 6 seconds ago 2.44GB
linlusama/centos-nginx v1.20.1 11bd74ac7f9e 26 hours ago 549MB
centos latest 5d0da3dc9764 2 months ago 231MB
php
//创建容器
[root@docker ~]# docker run -itd --name php --network container:9d39e8bbb2fd centos:latest /bin/sh
1cf21354c0c005122c62d7451ef56a400f5b6d3c2aefbd231c7bdb187fa684f0
//连接容器
[root@docker ~]# docker exec -it php /bin/sh
sh-4.4# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
//下载依赖包
sh-4.4# yum -y install epel-release
sh-4.4# yum -y install http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
sh-4.4# yum -y install gcc gcc-c++ make libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel oniguruma libzip-devel libcurl-minimal.x86_64 libcurl-devel.x86_64
//传包
[root@docker ~]# docker cp php-8.0.10.tar.xz php:/usr/src/
//解压
sh-4.4# cd /usr/src/
sh-4.4# ls
debug kernels php-8.0.10.tar.xz
sh-4.4# tar xf php-8.0.10.tar.xz -C
sh-4.4# ls
debug kernels php-8.0.10 php-8.0.10.tar.xz
//编译安装
sh-4.4# ./configure --prefix=/usr/local/php8 --with-config-file-path=/etc --enable-fpm --disable-debug --disable-rpath --enable-shared --enable-soap --with-openssl --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --enable-exif --enable-ftp --enable-gd --with-jpeg --with-zlib-dir --with-freetype --with-gettext --enable-mbstring --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-readline --enable-shmop --enable-simplexml --enable-sockets --with-zip --enable-mysqlnd-compression-support --with-pear --enable-pcntl --enable-posix && make && make install
//配置环境变量
sh-4.4# echo "export PATH=/usr/local/php8/bin:$PATH" > /etc/profile.d/php.sh
sh-4.4# . /etc/profile.d/php.sh
//配置php
sh-4.4# cp /usr/src/php-8.0.10/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
sh-4.4# chmod +x /etc/init.d/php-fpm
sh-4.4# cd /usr/local/php8/
sh-4.4# cp etc/php-fpm.conf.default etc/php-fpm.conf
sh-4.4# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
//配置网页文件
sh-4.4# vim /var/www/html/index.php
sh-4.4# mkdir -p /var/www/html/
sh-4.4# vim /var/www/html/index.php
<?php
phpinfo();
?>
sh-4.4# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
//启动
sh-4.4# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 127.0.0.1:9000 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
//编写启动脚本
sh-4.4# vim start.sh
#! /bin/bash
/etc/init.d/php-fpm start
/bin/bash
sh-4.4# chmod +x start.sh
//生成镜像
[root@docker ~]# docker commit -p -c 'CMD ["/bin/bash","/start.sh"]' php linlusama/php:v8.2
sha256:ecc988f1e94b7982546b95b76a24d720f5467192257b2ba6aff6a48374ddc3a9
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
linlusama/php v8.2 ecc988f1e94b 4 seconds ago 1.55GB
linlusama/mysql v5.7.31 20bb1f3eaf52 4 minutes ago 2.44GB
linlusama/centos-nginx v1.20.1 11bd74ac7f9e 26 hours ago 549MB
centos latest 5d0da3dc9764 2 months ago 231MB
访问
[root@docker ~]# docker run -itd --name nginx -p 80:80 -v linlusama/centos-nginx:v1.20.2
//连接
[root@docker ~]# docker run -itd --name mysql --network container:bcf200203a3c linlusama/mysql:v5.7.31
c34307c4a8c624112a3799ef492bd5a996e8da42e80c3d3b678e045ecf287ca9
[root@docker ~]# docker run -itd --name php --network container:bcf200203a3c linlusama/php:v8
89700135c675a2284eb8656f36eae9d510abaaf27dbe4576c7a0b132681aa153