笺注:Docker的安装可参考 CentOS8_在Docker中使用Nginx的反向代理
查看Docker的版本信息:
[root@centos8 ~]# docker -v
Docker version 20.10.21, build baeda1f
[root@centos8 ~]#
已经设置开机自动启动Docker:
[root@centos8 ~]# systemctl is-enabled docker
enabled
[root@centos8 ~]#
Docker多容器+LNMP的架构图:
从上图可以看出,Nginx容器与PHP容器有直接互联,PHP容器与MariaDB容器有直接互联。
在创建容器时的思路是,先创建MariaDB容器,再创建PHP容器,PHP容器link到MariaDB容器;
最后创建Nginx容器,link到PHP容器。
######
从公网下载镜像:( 以下是下载MariaDB10.2的镜像 )
[root@centos8 ~]# docker pull mariadb:10.2
查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb 10.2 625e5b493bbb 6 months ago 338MB
[root@centos8 ~]#
使用镜像“mariadb:10.2”创建并启动容器“MariaDB_1”:
[root@centos8 ~]# docker run -itd --name MariaDB_1 -e MYSQL_ROOT_PASSWORD=888 mariadb:10.2
234d900aba91a09f1037f7441529a2c62909fbf571742266a96943fd1fe923b6
[root@centos8 ~]#
注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-e 环境配置 : 指定数据库管理员root@localhost的密码为888
mariadb:10.2 : 镜像名称:版本号
在宿主机查看正在运行的容器:
[root@centos8 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
234d900aba91 mariadb:10.2 "docker-entrypoint.s…" 30 seconds ago Up 28 seconds 3306/tcp MariaDB_1
[root@centos8 ~]#
注释:容器“MariaDB_1”打开了TCP 3306端口。
###
进入容器“MariaDB_1”:
[root@centos8 ~]# docker exec -it MariaDB_1 /bin/bash
root@234d900aba91:/#
查看容器“MariaDB_1”的系统版本信息:
root@234d900aba91:/# cat /etc/issue
Ubuntu 18.04.6 LTS \n \l
root@234d900aba91:/# uname -r
4.18.0-193.el8.x86_64
root@234d900aba91:/#
查看MariaDB的版本信息:
root@234d900aba91:/# mysql -V
mysql Ver 15.1 Distrib 10.2.44-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
root@234d900aba91:/#
MariaDB的主配置文件:(以下是初始状态)
root@234d900aba91:/# find / -name my.cnf
/etc/mysql/my.cnf
find: '/proc/1/map_files': Permission denied
root@234d900aba91:/#
root@234d900aba91:/# cat /etc/mysql/my.cnf |grep -v "^#" |grep -v "^$"
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc_messages_dir = /usr/share/mysql
lc_messages = en_US
skip-external-locking
max_connections = 100
connect_timeout = 5
wait_timeout = 600
max_allowed_packet = 16M
thread_cache_size = 128
sort_buffer_size = 4M
bulk_insert_buffer_size = 16M
tmp_table_size = 32M
max_heap_table_size = 32M
myisam_recover_options = BACKUP
key_buffer_size = 128M
table_open_cache = 400
myisam_sort_buffer_size = 512M
concurrent_insert = 2
read_buffer_size = 2M
read_rnd_buffer_size = 1M
query_cache_limit = 128K
query_cache_size = 64M
slow_query_log_file = /var/log/mysql/mariadb-slow.log
long_query_time = 10
expire_logs_days = 10
max_binlog_size = 100M
default_storage_engine = InnoDB
innodb_buffer_pool_size = 256M
innodb_log_buffer_size = 8M
innodb_file_per_table = 1
innodb_open_files = 400
innodb_io_capacity = 400
innodb_flush_method = O_DIRECT
[galera]
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[mysql]
[isamchk]
key_buffer = 16M
[mariadb]
skip-host-cache
skip-name-resolve
!includedir /etc/mysql/conf.d/
root@234d900aba91:/#
本地登录MariaDB数据库:(使用的是数据库管理员root@localhost,密码888)
root@234d900aba91:/# mysql -uroot -p"888"
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.44-MariaDB-1:10.2.44+maria~bionic mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
查看数据库里有哪些库:(以下3个库是默认就有的)
MariaDB [(none)]> Show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]>
查看所有数据库用户及其主机信息:(以下是初始状态)
MariaDB [(none)]> Select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | % |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)
MariaDB [(none)]>
创建一个数据库用户并授权:
授权数据库用户zhuohua@'%'(密码886),可以从任意IP进行访问,对所有的库有完全控制的权限:
MariaDB [(none)]> Grant all on *.* to zhuohua@'%' identified by '886';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> Select user,host from mysql.user;
+---------+-----------+
| user | host |
+---------+-----------+
| root | % |
| zhuohua | % |
| root | localhost |
+---------+-----------+
3 rows in set (0.00 sec)
MariaDB [(none)]>
查看数据库用户zhuohua@'%'的权限:
MariaDB [(none)]> Show grants for zhuohua@'%';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for zhuohua@% |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'zhuohua'@'%' IDENTIFIED BY PASSWORD '*F961C54AFEB4D281CE53D7CB8E7822890D86FFFC' |
+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>
###
容器“MariaDB_1”在宿主机的存放目录:
[root@centos8 ~]# cd /var/lib/docker/containers/
[root@centos8 containers]# ls
234d900aba91a09f1037f7441529a2c62909fbf571742266a96943fd1fe923b6
[root@centos8 containers]#
[root@centos8 containers]# cd 234d900aba91a09f1037f7441529a2c62909fbf571742266a96943fd1fe923b6/
[root@centos8 234d900aba91a09f1037f7441529a2c62909fbf571742266a96943fd1fe923b6]# ls
234d900aba91a09f1037f7441529a2c62909fbf571742266a96943fd1fe923b6-json.log hostconfig.json mounts
checkpoints hostname resolv.conf
config.v2.json hosts resolv.conf.hash
[root@centos8 234d900aba91a09f1037f7441529a2c62909fbf571742266a96943fd1fe923b6]#
容器“MariaDB_1”的文件config.v2.json中的一些信息:
容器ID : "Config":{"Hostname":"234d900aba91",
容器名 : "Name":"/MariaDB_1",
端口映射 : "Ports":{"3306/tcp":null},
容器“MariaDB_1”的文件hostconfig.json中的一些信息:
"PortBindings":{},
"Links":null,
######
从公网下载镜像:( 以下是下载PHP7.1的镜像 )
[root@centos8 ~]# docker pull php:7.1-fpm
查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb 10.2 625e5b493bbb 6 months ago 338MB
php 7.1-fpm 404422fc039e 3 years ago 389MB
[root@centos8 ~]#
使用镜像“php:7.1-fpm”创建并启动容器“PHP_1”:
[root@centos8 ~]# docker run -itd --name PHP_1 --link MariaDB_1:db1 -v /opt/share:/var/www/html php:7.1-fpm
db26e111d109b0caa44d735dca4961a214039317410e74d08451b331cb7241bd
[root@centos8 ~]#
注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
--link MariaDB_1:db1 : 实现容器互联,让容器“PHP_1”可以通过容器名或容器id对容器“MariaDB_1”进行网络访问;db1是容器“MariaDB_1”在link下的别名。
-v 挂载目录 : 这里是把宿主机的目录/opt/share挂载到容器的目录/var/www/html;假如目录不存在,就会自动创建。
php:7.1-fpm : 镜像名称:版本号
在宿主机查看正在运行的容器:
[root@centos8 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db26e111d109 php:7.1-fpm "docker-php-entrypoi…" 2 minutes ago Up 2 minutes 9000/tcp PHP_1
234d900aba91 mariadb:10.2 "docker-entrypoint.s…" 17 minutes ago Up 17 minutes 3306/tcp MariaDB_1
[root@centos8 ~]#
注释:容器“PHP_1”打开了TCP 9000端口。
###
进入容器“PHP_1”:
[root@centos8 ~]# docker exec -it PHP_1 /bin/bash
root@db26e111d109:/var/www/html#
root@db26e111d109:/var/www/html# pwd
/var/www/html
root@db26e111d109:/var/www/html# ls
root@db26e111d109:/var/www/html#
查看容器“PHP_1”的系统版本信息:
root@db26e111d109:~# cat /etc/issue
Debian GNU/Linux 10 \n \l
root@db26e111d109:~# more /etc/debian_version
10.2
root@db26e111d109:~# uname -r
4.18.0-193.el8.x86_64
root@db26e111d109:~#
查看PHP的版本信息:
root@db26e111d109:~# php -v
PHP 7.1.33 (cli) (built: Nov 22 2019 18:34:33) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
root@db26e111d109:~#
容器“PHP_1”可以通过容器名或容器id对容器“MariaDB_1”进行网络访问:
root@db26e111d109:~# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 db1 234d900aba91 MariaDB_1
172.17.0.3 db26e111d109
root@db26e111d109:~#
显示容器“PHP_1”对容器“MariaDB_1”的环境变量:
root@db26e111d109:~# env | grep -i "db1"
DB1_ENV_MARIADB_MAJOR=10.2
DB1_ENV_GOSU_VERSION=1.14
DB1_PORT_3306_TCP_PORT=3306
DB1_ENV_MARIADB_VERSION=1:10.2.44+maria~bionic
DB1_PORT=tcp://172.17.0.2:3306
DB1_ENV_MYSQL_ROOT_PASSWORD=888
DB1_PORT_3306_TCP=tcp://172.17.0.2:3306
DB1_NAME=/PHP_1/db1
DB1_PORT_3306_TCP_ADDR=172.17.0.2
DB1_PORT_3306_TCP_PROTO=tcp
root@db26e111d109:~#
注释:
在创建容器“PHP_1”时,使用了参数 --link
db1是容器“MariaDB_1”在link下的别名。
###
容器“PHP_1”在宿主机的存放目录:
[root@centos8 ~]# cd /var/lib/docker/containers/
[root@centos8 containers]# ls
234d900aba91a09f1037f7441529a2c62909fbf571742266a96943fd1fe923b6
db26e111d109b0caa44d735dca4961a214039317410e74d08451b331cb7241bd
[root@centos8 containers]#
[root@centos8 containers]# cd db26e111d109b0caa44d735dca4961a214039317410e74d08451b331cb7241bd/
[root@centos8 db26e111d109b0caa44d735dca4961a214039317410e74d08451b331cb7241bd]# ls
checkpoints hostconfig.json mounts
config.v2.json hostname resolv.conf
db26e111d109b0caa44d735dca4961a214039317410e74d08451b331cb7241bd-json.log hosts resolv.conf.hash
[root@centos8 db26e111d109b0caa44d735dca4961a214039317410e74d08451b331cb7241bd]#
容器“PHP_1”的文件config.v2.json中的一些信息:
容器ID : "Config":{"Hostname":"db26e111d109",
容器名 : "Name":"/PHP_1",
端口映射 : "Ports":{"9000/tcp":null},
容器“PHP_1”的文件hostconfig.json中的一些信息:
"PortBindings":{},
"Links":["MariaDB_1:db1"], ##可以在这里更改--link的信息
######
从公网下载镜像:( 以下是下载Nginx/1.14的镜像 )
[root@centos8 ~]# docker pull nginx:1.14
查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb 10.2 625e5b493bbb 6 months ago 338MB
php 7.1-fpm 404422fc039e 3 years ago 389MB
nginx 1.14 295c7be07902 3 years ago 109MB
[root@centos8 ~]#
使用镜像“nginx:1.14”创建并启动容器“Nginx_1”:
[root@centos8 ~]# docker run -itd --name Nginx_1 --link PHP_1 -p 80:80 -v /opt/share:/usr/share/nginx/html nginx:1.14
cd8ba2bbb5ac98c5547fc775937cc76fdada4f428f3f5e6bff0e1aa852d9eca9
[root@centos8 ~]#
注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 第一个80是宿主机的端口,暴露给外部直接访问;第二个80是容器的端口。
--link PHP_1 : 实现容器互联,让容器“Nginx_1”可以通过容器名或容器id对容器“PHP_1”进行网络访问。
nginx:1.14 : 镜像名称:版本号
在宿主机查看所有的容器:(无论是否正在运行)
[root@centos8 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cd8ba2bbb5ac nginx:1.14 "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp Nginx_1
db26e111d109 php:7.1-fpm "docker-php-entrypoi…" 36 minutes ago Up 36 minutes 9000/tcp PHP_1
234d900aba91 mariadb:10.2 "docker-entrypoint.s…" 51 minutes ago Up 51 minutes 3306/tcp MariaDB_1
[root@centos8 ~]#
注释:宿主机的TCP 80端口,映射到容器“Nginx_1”的TCP 80端口。
###
进入容器“Nginx_1”:
[root@centos8 ~]# docker exec -it Nginx_1 /bin/bash
root@cd8ba2bbb5ac:/#
查看容器“Nginx_1”的系统版本信息:
root@cd8ba2bbb5ac:/# cat /etc/issue
Debian GNU/Linux 9 \n \l
root@cd8ba2bbb5ac:/# more /etc/debian_version
9.8
root@cd8ba2bbb5ac:/# uname -r
4.18.0-193.el8.x86_64
root@cd8ba2bbb5ac:/#
容器“Nginx_1”可以通过容器名或容器id对容器“PHP_1”进行网络访问:
root@cd8ba2bbb5ac:/# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3 PHP_1 db26e111d109
172.17.0.4 cd8ba2bbb5ac
root@cd8ba2bbb5ac:/#
显示容器“Nginx_1”对容器“PHP_1”的环境变量:
root@cd8ba2bbb5ac:/# env | grep -i "PHP_1"
PHP_1_ENV_PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
PHP_1_ENV_PHPIZE_DEPS=autoconf dpkg-dev file g++ gcc libc-dev make pkg-config re2c
PHP_1_NAME=/Nginx_1/PHP_1
PHP_1_ENV_PHP_VERSION=7.1.33
PHP_1_PORT_9000_TCP_PORT=9000
PHP_1_PORT_9000_TCP=tcp://172.17.0.3:9000
PHP_1_ENV_PHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi
PHP_1_ENV_PHP_MD5=
PHP_1_PORT_9000_TCP_ADDR=172.17.0.3
PHP_1_ENV_PHP_URL=https://www.php.net/get/php-7.1.33.tar.xz/from/this/mirror
PHP_1_PORT=tcp://172.17.0.3:9000
PHP_1_ENV_PHP_LDFLAGS=-Wl,-O1 -Wl,--hash-style=both -pie
PHP_1_ENV_PHP_INI_DIR=/usr/local/etc/php
PHP_1_ENV_GPG_KEYS=A917B1ECDA84AEC2B568FED6F50ABC807BD5DCD0 528995BFEDFBA7191D46839EF9BA0ADA31CBD89E 1729F83938DA44E27BA0F4D3DBDB397470D12172
PHP_1_ENV_PHP_SHA256=bd7c0a9bd5433289ee01fd440af3715309faf583f75832b64fe169c100d52968
PHP_1_ENV_PHP_ASC_URL=https://www.php.net/get/php-7.1.33.tar.xz.asc/from/this/mirror
PHP_1_ENV_PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
PHP_1_PORT_9000_TCP_PROTO=tcp
root@cd8ba2bbb5ac:/#
注释:在创建容器“Nginx_1”时,使用了参数 --link
###
查看容器“Nginx_1”的Nginx版本:
root@cd8ba2bbb5ac:/# nginx -v
nginx version: nginx/1.14.2
root@cd8ba2bbb5ac:/#
root@cd8ba2bbb5ac:/# nginx -V
nginx version: nginx/1.14.2
built by gcc 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)
built with OpenSSL 1.1.0f 25 May 2017 (running with OpenSSL 1.1.0j 20 Nov 2018)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.14.2/debian/debuild-base/nginx-1.14.2=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-specs=/usr/share/dpkg/no-pie-link.specs -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
root@cd8ba2bbb5ac:/#
查看容器“Nginx_1”的Nginx的主配置文件:
root@cd8ba2bbb5ac:/# find / -name "nginx.conf"
/etc/nginx/nginx.conf
find: '/proc/7/map_files': Permission denied
root@cd8ba2bbb5ac:/#
root@cd8ba2bbb5ac:/# cat /etc/nginx/nginx.conf |grep -v "^#" |grep -v "^$"
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
root@cd8ba2bbb5ac:/#
查看Nginx的默认站点的默认首页文件:
root@cd8ba2bbb5ac:/# cd /etc/nginx/conf.d
root@cd8ba2bbb5ac:/etc/nginx/conf.d# ls
default.conf
root@cd8ba2bbb5ac:/etc/nginx/conf.d#
root@cd8ba2bbb5ac:~# cat /etc/nginx/conf.d/default.conf |grep -v '^$'
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# 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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
root@cd8ba2bbb5ac:~#
###
在宿主机上创建容器“Nginx_1”的Nginx的默认站点的默认首页文件:
[root@centos8 ~]# cd /opt/share/
[root@centos8 share]# echo 'Welcome to zhuohua.' > index.htm
[root@centos8 share]#
创建容器“Nginx_1”与容器“PHP_1”的PHP测试页:
[root@centos8 share]# cat test.php
<?php
phpinfo();
?>
[root@centos8 share]#
创建容器“PHP_1”与容器“MariaDB_1”的数据库连接测试页:(PHP7.x的语法)
[root@centos8 share]# cat mysql.php
<?php
header("Content-Type:text/html;charset=utf-8");
$con=mysqli_connect('MariaDB_1','zhuohua','886')
OR die('远程MariaDB连接失败。');
echo '远程MariaDB连接正常。';
?>
[root@centos8 share]#
注释:因为在创建容器“PHP_1”时,使用了参数“--link”互联到容器“MariaDB_1”,所以在这里可以直接使用容器“MariaDB_1”的容器名、容器id、在link下的别名。
###
在容器“Nginx_1”上查看Nginx的默认站点里的文件:
root@cd8ba2bbb5ac:~# cd /usr/share/nginx/html
root@cd8ba2bbb5ac:/usr/share/nginx/html# ls
index.htm mysql.php test.php
root@cd8ba2bbb5ac:/usr/share/nginx/html#
###
在容器“PHP_1”上查看目录/var/www/html里的文件:
root@db26e111d109:/var/www/html# ls
index.htm mysql.php test.php
root@db26e111d109:/var/www/html#
注释:
因为在创建容器“Nginx_1”的时候,把宿主机的目录/opt/share挂载到容器的目录/usr/share/nginx/html;
因为在创建容器“PHP_1”的时候,把宿主机的目录/opt/share挂载到容器的目录/var/www/html;
所以这三个目录里的数据是共享的。
###
在宿主机的防火墙firewalld打开TCP 80端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
查看防火墙所有打开的端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
80/tcp
[root@centos8 ~]#
###
在客户端访问容器“Nginx_1”的Nginx的默认站点的默认首页文件:
http://192.168.168.154/
在客户端访问容器“Nginx_1”的Nginx的默认站点里的PHP测试页:
http://192.168.168.154/test.php
笺注:这是因为容器“Nginx_1”的Nginx本身无法解析PHP文件。
解决方法:(在宿主机上修改容器“Nginx_1”的Nginx的主配置文件)
[root@centos8 ~]# docker cp Nginx_1:/etc/nginx/nginx.conf /root
[root@centos8 ~]# vi nginx.conf
插入以下代码:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
# #当请求容器“Nginx_1”默认站点下PHP文件的时候,反向代理到容器“PHP_1”的php-fpm
location ~ \.php$ {
fastcgi_pass PHP_1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
}
注释:因为在创建容器“Nginx_1”时,使用了参数“--link”互联到容器“PHP_1”,所以在这里可以直接使用容器“PHP_1”的容器名或容器id。
如下图:
在宿主机上,往容器“Nginx_01”上传修改好的文件:
[root@centos8 ~]# docker cp /root/nginx.conf Nginx_1:/etc/nginx/nginx.conf
[root@centos8 ~]#
再次,查看容器“Nginx_1”的Nginx的主配置文件:
root@cd8ba2bbb5ac:/# cat /etc/nginx/nginx.conf |grep -v "^#" |grep -v "^$"
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
fastcgi_pass PHP_1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
}
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
root@cd8ba2bbb5ac:/#
在宿主机重启容器“Nginx_1”:
[root@centos8 ~]# docker restart Nginx_1
Nginx_1
[root@centos8 ~]#
再次,在客户端访问容器“Nginx_1”的Nginx的默认站点里的PHP测试页:
###
在客户端通过容器“Nginx_1”的反向代理,从而访问容器“PHP_1”与容器“MariaDB_1”的数据库连接测试页:
http://192.168.168.154/mysql.php
错误:
Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/mysql.php:3 Stack trace: #0 {main} thrown in /var/www/html/mysql.php on line 3
这是因为容器“PHP_1”的PHP没有安装相关的MySQL扩展。
进入容器“PHP_1”安装相关的MySQL扩展:
root@db26e111d109:~# docker-php-ext-install pdo_mysql
root@db26e111d109:~# docker-php-ext-install mysql
root@db26e111d109:~# docker-php-ext-install mysqli
root@db26e111d109:~# docker-php-ext-install pdo
在宿主机重启容器“PHP_1”:
[root@centos8 ~]# docker restart PHP_1
PHP_1
[root@centos8 ~]#
再次,在客户端通过容器“Nginx_1”的反向代理,从而访问容器“PHP_1”与容器“MariaDB_1”的数据库连接测试页:
###
容器“Nginx_1”在宿主机的存放目录:
[root@centos8 ~]# cd /var/lib/docker/containers/
[root@centos8 containers]# ls
cd8ba2bbb5ac98c5547fc775937cc76fdada4f428f3f5e6bff0e1aa852d9eca9
[root@centos8 containers]#
[root@centos8 containers]# cd cd8ba2bbb5ac98c5547fc775937cc76fdada4f428f3f5e6bff0e1aa852d9eca9/
[root@centos8 cd8ba2bbb5ac98c5547fc775937cc76fdada4f428f3f5e6bff0e1aa852d9eca9]# ls
cd8ba2bbb5ac98c5547fc775937cc76fdada4f428f3f5e6bff0e1aa852d9eca9-json.log hostconfig.json mounts
checkpoints hostname resolv.conf
config.v2.json hosts resolv.conf.hash
[root@centos8 cd8ba2bbb5ac98c5547fc775937cc76fdada4f428f3f5e6bff0e1aa852d9eca9]#
容器“Nginx_1”的文件config.v2.json中的一些信息:
容器ID : "Config":{"Hostname":"cd8ba2bbb5ac",
容器名 : "Name":"/Nginx_1",
端口映射 :
"Ports":{"80/tcp":[{"HostIp":"0.0.0.0","HostPort":"80"},{"HostIp":"::","HostPort":"80"}]},
容器“Nginx_1”的文件hostconfig.json中的一些信息:
"PortBindings":{"80/tcp":[{"HostIp":"","HostPort":"80"}]},
"Links":["PHP_1"],
######
宿主机重启后,启动容器的顺序:
[root@centos8 ~]# docker start Nginx_1
Error response from daemon: Cannot link to a non running container: /PHP_1 AS /Nginx_1/PHP_1
Error: failed to start containers: Nginx_1
[root@centos8 ~]#
[root@centos8 ~]# docker start PHP_1
Error response from daemon: Cannot link to a non running container: /MariaDB_1 AS /PHP_1/db1
Error: failed to start containers: PHP_1
[root@centos8 ~]#
注释:
因为在创建容器“Nginx_1”时,--link到容器“PHP_1”,所以容器“PHP_1”要早于
容器“Nginx_1”启动。
因为在创建容器“PHP_1”时,--link到容器“MariaDB_1”,所以容器“MariaDB_1”要早于容器“PHP_1”启动。
正确的启动顺序如下:
[root@centos8 ~]# docker start MariaDB_1
MariaDB_1
[root@centos8 ~]#
[root@centos8 ~]# docker start PHP_1
PHP_1
[root@centos8 ~]#
[root@centos8 ~]# docker start Nginx_1
Nginx_1
[root@centos8 ~]#
###
也可以让三个容器都随宿主机的启动而启动,就不用考虑容器的启动顺序:
[root@centos8 ~]# docker update --restart=always Nginx_1
Nginx_1
[root@centos8 ~]#
[root@centos8 ~]# docker update --restart=always PHP_1
PHP_1
[root@centos8 ~]#
[root@centos8 ~]# docker update --restart=always MariaDB_1
MariaDB_1
[root@centos8 ~]#
相关文章:
CentOS8_在Docker中安装LNMP
CentOS8_在Docker中安装LAMP(使用参数--link)
CentOS8_在Docker中安装MariaDB
CentOS8_在Docker中限制容器可用的CPU个数和内存量
CentOS8安装LNMP+phpMyAdmin |