笺注:Docker的安装可参考 CentOS8_使用Docker安装Python3
查看Docker的版本信息:
[root@centos8 ~]# docker -v
Docker version 20.10.21, build baeda1f
[root@centos8 ~]#
[root@centos8 ~]# yum -y install net-tools
[root@centos8 ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:54:d0:7c:2c txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.168.154 netmask 255.255.255.0 broadcast 192.168.168.255
inet6 fe80::92ab:5fb4:5373:ad53 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:0e:fa:49 txqueuelen 1000 (Ethernet)
RX packets 83976 bytes 125015836 (119.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 28748 bytes 1718121 (1.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@centos8 ~]#
给Docker创建自定义虚拟网卡,并且指定网段:
[root@centos8 ~]# docker network create --subnet=192.168.80.0/24 zhuohua-docker
d3552d02aa6da0ee577c754a8e3626b66e4ac8b67e78fa44997124beb1dc71fc
[root@centos8 ~]#
备注:给Docker创建自定义虚拟网卡后,容器才能使用固定IP地址;
查看Docker所有的虚拟网卡:
[root@centos8 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
ac02a5c5ce9c bridge bridge local
ecf4d5fc9a71 host host local
f3a83278f2d8 none null local
d3552d02aa6d zhuohua-docker bridge local
[root@centos8 ~]#
备注:
网卡ID“d3552d02aa6d”是自动生成的;
网卡名“zhuohua-docker”是自定义的;
[root@centos8 ~]# ifconfig
br-d3552d02aa6d: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.80.1 netmask 255.255.255.0 broadcast 192.168.80.255
ether 02:42:d4:e2:ca:1e txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:54:d0:7c:2c txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.168.154 netmask 255.255.255.0 broadcast 192.168.168.255
inet6 fe80::92ab:5fb4:5373:ad53 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:0e:fa:49 txqueuelen 1000 (Ethernet)
RX packets 84119 bytes 125026032 (119.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 28792 bytes 1723441 (1.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@centos8 ~]#
######
从公网下载镜像:( 以下是下载Tomcat/8.5的镜像 )
[root@centos8 ~]# docker pull tomcat:8.5
查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 8.5 37ce4f4ab859 4 days ago 474MB
[root@centos8 ~]#
使用镜像“tomcat:8.5”创建并启动容器“Tomcat_1”:
[root@centos8 ~]# docker run -itd --name Tomcat_1 -p 80:8080 -v /opt/share:/usr/local/tomcat/webapps --net zhuohua-docker --ip 192.168.80.2 tomcat:8.5
913a9d4f04c0dc83ea183a3ed10f24b6879e9b39e3e542265754733af368e6e5
[root@centos8 ~]#
注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 80是宿主机的端口,暴露给外部直接访问;8080是容器的端口。
-v 挂载目录 : 这里是把宿主机的目录/opt/share挂载到容器的目录/usr/local/tomcat/webapps
--net zhuohua-docker --ip : 指定容器的固定IP地址(使用虚拟网卡“zhuohua-docker”)。
tomcat:8.5 : 镜像名称:版本号
在宿主机查看正在运行的容器:
[root@centos8 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
913a9d4f04c0 tomcat:8.5 "catalina.sh run" About a minute ago Up About a minute 0.0.0.0:80->8080/tcp, :::80->8080/tcp Tomcat_1
[root@centos8 ~]#
注释:宿主机的TCP 80端口,映射到容器“Tomcat_1”的TCP 8080端口。
###
进入容器“Tomcat_1”:
[root@centos8 ~]# docker exec -it Tomcat_1 /bin/bash
root@913a9d4f04c0:/usr/local/tomcat#
root@913a9d4f04c0:/usr/local/tomcat# pwd
/usr/local/tomcat
root@913a9d4f04c0:/usr/local/tomcat#
查看容器“Tomcat_1”的系统版本信息:
root@913a9d4f04c0:/usr/local/tomcat# cat /etc/issue
Ubuntu 22.04.2 LTS \n \l
root@913a9d4f04c0:/usr/local/tomcat# uname -a
Linux 77fd019138fe 4.18.0-193.el8.x86_64 #1 SMP Fri May 8 10:59:10 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
root@913a9d4f04c0:/usr/local/tomcat#
测试JDK:
root@913a9d4f04c0:/usr/local/tomcat# cd
root@913a9d4f04c0:~#
root@913a9d4f04c0:~# java -version
openjdk version "17.0.6" 2023-01-17
OpenJDK Runtime Environment Temurin-17.0.6+10 (build 17.0.6+10)
OpenJDK 64-Bit Server VM Temurin-17.0.6+10 (build 17.0.6+10, mixed mode, sharing)
root@913a9d4f04c0:~#
Tomcat会自动启动:
root@913a9d4f04c0:~# ps -ef |grep java |grep -v grep
root 1 0 3 19:18 pts/0 00:00:16 /opt/java/openjdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root@913a9d4f04c0:~#
Tomcat默认就是使用TCP 8080端口:
root@913a9d4f04c0:~# cat /usr/local/tomcat/conf/server.xml |grep 8080
Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
<Connector port="8080" protocol="HTTP/1.1"
port="8080" protocol="HTTP/1.1"
root@913a9d4f04c0:~#
Tomcat的默认首页文件:
root@913a9d4f04c0:~# cat /usr/local/tomcat/conf/web.xml |grep index.*
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
root@913a9d4f04c0:~#
###
Docker宿主机的防火墙其实可以不用打开TCP 80端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
[root@centos8 ~]#
###
在客户端访问容器“Tomcat_1”的Tomcat的默认站点的默认首页文件:
http://192.168.168.154/
解决方法:
root@913a9d4f04c0:/usr/local/tomcat# pwd
/usr/local/tomcat
root@913a9d4f04c0:/usr/local/tomcat# ls
bin conf lib logs NOTICE RELEASE-NOTES temp webapps.dist
BUILDING.txt CONTRIBUTING.md LICENSE native-jni-lib README.md RUNNING.txt webapps work
root@913a9d4f04c0:/usr/local/tomcat# ls webapps
root@913a9d4f04c0:/usr/local/tomcat#
root@913a9d4f04c0:/usr/local/tomcat# cp -rfp webapps.dist/* webapps
root@913a9d4f04c0:/usr/local/tomcat#
root@913a9d4f04c0:/usr/local/tomcat# ls webapps
docs examples host-manager manager ROOT
root@913a9d4f04c0:/usr/local/tomcat# ls webapps/ROOT/
asf-logo-wide.svg bg-middle.png bg-upper.png index.jsp tomcat.css WEB-INF
bg-button.png bg-nav.png favicon.ico RELEASE-NOTES.txt tomcat.svg
root@913a9d4f04c0:/usr/local/tomcat#
###
再次,在客户端访问容器“Tomcat_1”的Tomcat的默认站点的默认首页文件:
注释:容器“Tomcat_1”要打开TCP 8080端口。
###
在宿主机上往容器“Tomcat_1”的Tomcat的默认站点里创建JSP测试页:
[root@centos8 ~]# cd /opt/share/
[root@centos8 share]# ls
docs examples host-manager manager ROOT
[root@centos8 share]# cd ROOT/
[root@centos8 ROOT]# vi time.jsp
写入:
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>中文测试页</title>
</head>
<body>
<center>现在时间: <%=new java.util.Date()%></center>
</body>
</html>
root@913a9d4f04c0:~# cat /usr/local/tomcat/webapps/ROOT/time.jsp
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>中文测试页</title>
</head>
<body>
<center>现在时间: <%=new java.util.Date()%></center>
</body>
</html>
root@913a9d4f04c0:~#
注释:因为在创建容器“Tomcat_1”的时候,把宿主机的目录/opt/share挂载到容器的目录/usr/local/tomcat/webapps,所以这两个目录里的数据是双向同步的。
Windows客户端远程测试:
http://192.168.168.154/time.jsp
######
从公网下载镜像:( 以下是下载MariaDB10.4的镜像 )
[root@centos8 ~]# docker pull mariadb:10.4
查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat 8.5 37ce4f4ab859 4 days ago 474MB
mariadb 10.4 81825cbadf42 4 days ago 385MB
[root@centos8 ~]#
使用镜像“mariadb:10.4”创建并启动容器“MariaDB_1”:
[root@centos8 ~]# docker run -itd --name MariaDB_1 -e MYSQL_ROOT_PASSWORD=P@ssw0rd --net zhuohua-docker --ip 192.168.80.3 mariadb:10.4
6c1c3a22d51af9672bc9d7eb48ad4b01a22626c51a3b4dc78b3c705a3bbe09c2
[root@centos8 ~]#
注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-e 环境配置 : 指定数据库管理员root@localhost的密码为P@ssw0rd
--net zhuohua-docker --ip : 指定容器的固定IP地址(使用虚拟网卡“zhuohua-docker”)。
mariadb:10.4 : 镜像名称:版本号
在宿主机查看所有的容器:(无论是否正在运行)
[root@centos8 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c1c3a22d51a mariadb:10.4 "docker-entrypoint.s…" 3 minutes ago Up 2 minutes 3306/tcp MariaDB_1
913a9d4f04c0 tomcat:8.5 "catalina.sh run" 25 minutes ago Up 25 minutes 0.0.0.0:80->8080/tcp, :::80->8080/tcp Tomcat_1
[root@centos8 ~]#
注释:容器“MariaDB_1”打开了TCP 3306端口。
###
进入容器“MariaDB_1”:
[root@centos8 ~]# docker exec -it MariaDB_1 /bin/bash
root@6c1c3a22d51a:/#
查看容器“MariaDB_1”的系统版本信息:
root@6c1c3a22d51a:/# cat /etc/issue
Ubuntu 20.04.5 LTS \n \l
root@6c1c3a22d51a:/# uname -r
4.18.0-193.el8.x86_64
root@6c1c3a22d51a:/#
查看MariaDB的版本信息:
root@6c1c3a22d51a:/# mysql -V
mysql Ver 15.1 Distrib 10.4.28-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
root@6c1c3a22d51a:/#
MariaDB的主配置文件:(以下是初始状态)
root@6c1c3a22d51a:/# find / -name "my.cnf"
/etc/mysql/my.cnf
find: ‘/proc/1/map_files’: Permission denied
root@6c1c3a22d51a:/#
root@6c1c3a22d51a:/# 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
!include /etc/mysql/mariadb.cnf
!includedir /etc/mysql/conf.d/
root@6c1c3a22d51a:/#
本地登录MariaDB数据库:(使用的是数据库管理员root@localhost,密码P@ssw0rd)
root@6c1c3a22d51a:/# mysql -uroot -p"P@ssw0rd"
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.4.28-MariaDB-1:10.4.28+maria~ubu2004 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.001 sec)
MariaDB [(none)]>
切换到库mysql:
MariaDB [(none)]> Use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]>
在MariaDB下查看当前使用的是哪个库:
MariaDB [mysql]> Select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.000 sec)
MariaDB [mysql]>
查看所有数据库用户及其主机信息:(显示当前库mysql中的表user里的字段“user”、“host”的所有数据)
MariaDB [mysql]> Select user,host from user;
+-------------+-----------+
| User | Host |
+-------------+-----------+
| root | % |
| mariadb.sys | localhost |
| root | localhost |
+-------------+-----------+
3 rows in set (0.003 sec)
MariaDB [mysql]>
创建一个数据库用户并授权:
授权数据库用户zhuohua@'192.168.80.2'(密码168),仅仅在使用IP地址192.168.80.2时可以进行访问,对所有的库有完全控制的权限:
MariaDB [(none)]> Grant all on *.* to zhuohua@'192.168.80.2' identified by '168';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]>
显示库mysql中的表user里的所有数据:
MariaDB [(none)]> Select user,host from mysql.user;
+-------------+--------------+
| User | Host |
+-------------+--------------+
| root | % |
| zhuohua | 192.168.80.2 |
| mariadb.sys | localhost |
| root | localhost |
+-------------+--------------+
4 rows in set (0.001 sec)
MariaDB [(none)]>
查看数据库用户zhuohua@'192.168.80.2'的权限:
MariaDB [(none)]> Show grants for zhuohua@'192.168.80.2';
+----------------------------------------------------------------------------------------------------------------------------+
| Grants for zhuohua@192.168.80.2 |
+----------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO `zhuohua`@`192.168.80.2` IDENTIFIED BY PASSWORD '*242E46A1E8D30FE06F7CE37B55BFC25BA981D70C' |
+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)
MariaDB [(none)]>
###
在宿主机上往容器“Tomcat_1”的Tomcat的默认站点里创建连接远程MariaDB的JSP测试页:
[root@centos8 ~]# cd /opt/share/
[root@centos8 share]# ls
docs examples host-manager manager ROOT
[root@centos8 share]# cd ROOT/
[root@centos8 ROOT]# vi mm.jsp
写入:
<pre class="html" name="code"><%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>连接192.168.80.3的库mysql</title>
</head>
<body>
<%
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://192.168.80.3:3306/mysql?useUnicode=true&characterEncoding=UTF-8";
String dbUser = "zhuohua";
String dbPwd = "168";
con = DriverManager.getConnection(dbUrl,dbUser,dbPwd);
out.print("数据库MariaDB远程连接正常");
}catch(Exception ex)
{
out.print("数据库MariaDB远程连接失败<br>"+ex.toString());
}
%>
</body>
</html>
</pre>
Windows客户端远程测试:
http://192.168.168.154/mm.jsp
解决方法:
[root@centos8 ~]# docker cp mysql-connector-java-8.0.15.jar Tomcat_1:/root
[root@centos8 ~]#
再进入容器“Tomcat_1”:
[root@centos8 ~]# docker exec -it Tomcat_1 /bin/bash
root@913a9d4f04c0:/usr/local/tomcat#
root@913a9d4f04c0:/usr/local/tomcat# cd
root@913a9d4f04c0:~# pwd
/root
root@913a9d4f04c0:~#
root@913a9d4f04c0:~# ls
mysql-connector-java-8.0.15.jar
root@913a9d4f04c0:~#
root@913a9d4f04c0:~# cp mysql-connector-java-8.0.15.jar /usr/local/tomcat/lib/
root@913a9d4f04c0:~# ll /usr/local/tomcat/lib/mysql-connector-java-8.0.15.jar
-rw-r--r--. 1 root root 2134905 Mar 20 20:57 /usr/local/tomcat/lib/mysql-connector-java-8.0.15.jar
root@913a9d4f04c0:~#
在宿主机重启容器“Tomcat_1”:
[root@centos8 ~]# docker restart Tomcat_1
Tomcat_1
[root@centos8 ~]#
Windows客户端再次远程测试:
笺注:
Docker宿主机的防火墙其实可以不用打开TCP 3306端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
[root@centos8 ~]#
但容器“MariaDB_1”要打开TCP 3306端口。
相关文章:
CentOS8_在Docker中安装MariaDB
CentOS8_在Docker中安装Nagios
CentOS8_Tomcat8+JDK1.8+MySQL |