目录
一、总体步骤
二、安装tomcat
1、docker hub上查找tomcat镜像
三、安装MySQL
1、查看MySQL镜像
2、拉取MySQL镜像到本地,本次拉取MySQL5.7
3、使用MySQL镜像创建容器
4、使用Windows数据库工具,连接MySQL实例
5、常见问题
6、创建MySQL容器实例
7、新建my.cnf通过容器卷同步给MySQL容器实例
8、重启下MySQL容器实例
9、进入MySQL
四、安装redis
1、拉取镜像
2、启动镜像创建容器实例
3、在宿主机新建redis目录,/usr/redis 作为docker容器的宿主机目录
4、启动redis镜像创建redis实例
5、测试redis连接
一、总体步骤
1、搜索镜像
2、拉去镜像
3、查看镜像
4、启动镜像
5、停止容器
6、移除容器
二、安装tomcat
1、docker hub上查找tomcat镜像
#查找镜像
[root@localhost tmp]# docker search tomcat
#拉去tomcat 镜像
[root@localhost tmp]# docker pull tomcat
#docker images 查看拉去的tomcat镜像
[root@localhost tmp]# docker images tomcat
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest fb5657adc892 2 years ago 680MB#使用tomcat镜像创建容器实例(也叫运行镜像)
[root@localhost tmp]# docker run -d -p 8080:8080 --name tomcat1 tomcat
e2770f6be6dc13015344e36c4e54769df600708b696e767690ada53f2c4972b5
[root@localhost tmp]#
[root@localhost tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2770f6be6dc tomcat "catalina.sh run" 7 seconds ago Up 6 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat1
[root@localhost tmp]#
#访问tomcat首页
http://192.168.153.128:8080/
可能会报错 404未找到
原因是tomcat新版本,对于首页的访问发生了些许的改变。属于正常。有以下原因
1、可能没有映射端口,或者没有关防火墙,我们刚才也映射了,防火墙也是关着的
2、把webapps.dist目录换成webapps,进入容器
[root@localhost tmp]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e2770f6be6dc tomcat "catalina.sh run" 5 minutes ago Up 5 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp tomcat1
[root@localhost tmp]#
[root@localhost tmp]# docker exec -it e2770f6be6dc /bin/bash
root@e2770f6be6dc:/usr/local/tomcat# root@e2770f6be6dc:/usr/local/tomcat# pwd
/usr/local/tomcatroot@e2770f6be6dc:/usr/local/tomcat# ls -l
total 132
-rw-r--r--. 1 root root 18994 Dec 2 2021 BUILDING.txt
-rw-r--r--. 1 root root 6210 Dec 2 2021 CONTRIBUTING.md
-rw-r--r--. 1 root root 60269 Dec 2 2021 LICENSE
-rw-r--r--. 1 root root 2333 Dec 2 2021 NOTICE
-rw-r--r--. 1 root root 3378 Dec 2 2021 README.md
-rw-r--r--. 1 root root 6905 Dec 2 2021 RELEASE-NOTES
-rw-r--r--. 1 root root 16517 Dec 2 2021 RUNNING.txt
drwxr-xr-x. 2 root root 4096 Dec 22 2021 bin
drwxr-xr-x. 1 root root 22 Jun 22 12:10 conf
drwxr-xr-x. 2 root root 4096 Dec 22 2021 lib
drwxrwxrwx. 1 root root 80 Jun 22 12:10 logs
drwxr-xr-x. 2 root root 159 Dec 22 2021 native-jni-lib
drwxrwxrwx. 2 root root 30 Dec 22 2021 temp
drwxr-xr-x. 2 root root 6 Dec 22 2021 webapps
drwxr-xr-x. 7 root root 81 Dec 2 2021 webapps.dist
drwxrwxrwx. 2 root root 6 Dec 2 2021 work
root@e2770f6be6dc:/usr/local/tomcat#
root@e2770f6be6dc:/usr/local/tomcat# rm -r webappsroot@e2770f6be6dc:/usr/local/tomcat# mv webapps.dist webapps
root@e2770f6be6dc:/usr/local/tomcat#
然后就可以访问了。
#tomcat10比较麻烦 还需要改配置,我们把它干掉,拉一个tomcat8就行
[root@localhost tmp]# docker run -d -p 8080:8080 --name tomcat8 billygoo/tomcat8-jdk8
就可以了。
三、安装MySQL
1、查看MySQL镜像
[root@localhost tmp]# docker search mysql
2、拉取MySQL镜像到本地,本次拉取MySQL5.7
[root@localhost tmp]# docker pull mysql:5.7
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
[root@localhost tmp]# [root@localhost tmp]# docker images mysql:5.7
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 c20987f18b13 2 years ago 448MB
[root@localhost tmp]#
[root@localhost tmp]#
3、使用MySQL镜像创建容器
[root@localhost tmp]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
166c10ed2b7414f791042a20a0018ba4a955d30583e54f7c316623cb2546e0d2
docker: Error response from daemon: driver failed programming external connectivity on endpoint infallible_einstein (590077e55eca8279f5c4162f540bcd44499fe3c4a80a5f5091af66b322964ebd): Error starting userland proxy: listen tcp4 0.0.0.0:3306: bind: address already in use.
#有个报错,原因是我Linux 服务器上安装了MySQL8.0,提示3306端口被占用。所以创建不了容器,
修改一下映射端口
[root@localhost tmp]# docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
505ed9996b3c74c0f0791454ad6352639d04990960bfc94283bb57a38a256d51
[root@localhost tmp]#
[root@localhost tmp]# docker ps
[root@localhost tmp]# docker exec -it 505ed9996b3c /bin/bash
root@505ed9996b3c:/#
#进入MySQL
root@505ed9996b3c:/# mysql -uroot -p123456
#成功通过MySQL镜像运行了一个实例
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)mysql>
mysql> create database test;
Query OK, 1 row affected (0.00 sec)mysql> use test;
Database changed
mysql>
mysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)mysql> insert into t1 values(1,'z3');
Query OK, 1 row affected (0.01 sec)mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | z3 |
+------+------+
1 row in set (0.00 sec)mysql>
没问题哈。
4、使用Windows数据库工具,连接MySQL实例
5、常见问题
(1)、插入中文报错
因为docker 上默认字符集编码没有修正
在docker 里边的MySQL容器实例查看
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)mysql>
有个疑问,就是如果哪天把容器实例删除了,怎么办?跑路?
所以,我们在创建MySQL实例时候,挂载数据卷,来解决数据丢失的问题。
把这个容器干掉,我们重新创建容器实例
6、创建MySQL容器实例
[root@localhost ~]# docker run -d -p 3307:3307 --privileged=true
-v /usr/mysql/log:/var/log/mysql
-v /usr/mysql/data:/var/lib/mysql
-v /usr/mysql/conf:/etc/mysql/conf.d
-e MYSQL_ROOT_PASSWORD=123456
--name=mysql
mysql:5.7
18a1e538c92be54d8192a992d43f4a1d4339e7d62403985a0fe7fb9d0c106511
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18a1e538c92b mysql:5.7 "docker-entrypoint.s…" 6 seconds ago Up 3 seconds 3306/tcp, 33060/tcp, 0.0.0.0:3307->3307/tcp, :::3307->3307/tcp mysql
7、新建my.cnf通过容器卷同步给MySQL容器实例
[root@localhost conf]# vim my.cnf
[root@localhost conf]#
[root@localhost conf]# cat my.cnf
[client]
user=root
password=xxxxxx
default-character-set=utf8[mysql]
prompt=(\\u@\\h) [\\d]>\\_[mysqld]
port = 3307
collation_server = utf8_general_ci
character-set-server=utf8
8、重启下MySQL容器实例
[root@localhost ~]# docker restart mysql
mysql
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
18a1e538c92b mysql:5.7 "docker-entrypoint.s…" 11 minutes ago Up 1 second 3306/tcp, 33060/tcp, 0.0.0.0:3307->3307/tcp, :::3307->3307/tcp mysql
9、进入MySQL
[root@localhost ~]# docker exec -it 18a1e538c92b /bin/bash
root@18a1e538c92b:/#
root@18a1e538c92b:/#
root@18a1e538c92b:/# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.(root@localhost) [(none)]>
查看字符集
(root@localhost) [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
然后建库,建表,添加数据
(root@localhost) [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)(root@localhost) [(none)]> use test;
Database changed
(root@localhost) [test]>
(root@localhost) [test]> create table test1(a int,b varchar(20));
Query OK, 0 rows affected (0.01 sec)(root@localhost) [test]> insert into test1 values(1,'wu');
Query OK, 1 row affected (0.00 sec)(root@localhost) [test]> select * from test1;
+------+------+
| a | b |
+------+------+
| 1 | wu |
+------+------+
1 row in set (0.00 sec)
#客户端插入中文演示
insert INTO test1 VALUES (2,'武');
(root@localhost) [test]> select * from test1;
+------+------+
| a | b |
+------+------+
| 1 | wu |
| 2 | 武 |
+------+------+
2 rows in set (0.01 sec)总结:docker 安装MySQL并run之后,建议先修改字符集编码后,在新建库表,和插入数据。
还有一定要挂载容器数据卷,以免那个坑货,把mysql容器删了,数据丢失。
四、安装redis
1、拉取镜像
[root@localhost ~]# docker pull redis:6.0.8
6.0.8: Pulling from library/redis
bb79b6b2107f: Pull complete
1ed3521a5dcb: Pull complete
5999b99cee8f: Pull complete
3f806f5245c9: Pull complete
f8a4497572b2: Pull complete
eafe3b6b8d06: Pull complete
Digest: sha256:21db12e5ab3cc343e9376d655e8eabbdbe5516801373e95a8a9e66010c5b8819
Status: Downloaded newer image for redis:6.0.8
docker.io/library/redis:6.0.8
[root@localhost ~]#
[root@localhost ~]# docker images redis
REPOSITORY TAG IMAGE ID CREATED SIZE
redis 6.0.8 16ecd2772934 3 years ago 104MB
[root@localhost ~]#
[root@localhost ~]#
2、启动镜像创建容器实例
#因为我宿主机有redis,6379端口被占用,所以我映射成6380端口
[root@localhost ~]# docker run -d -p 6380:6380 redis:6.0.8
5c4d11ca385f31015b86bb9985a7533261c359718ec390b2104e5258042bd09b
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c4d11ca385f redis:6.0.8 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 6379/tcp, 0.0.0.0:6380->6380/tcp, :::6380->6380/tcp charming_dirac
18a1e538c92b mysql:5.7 "docker-entrypoint.s…" 43 minutes ago Up 24 minutes 3306/tcp, 33060/tcp, 0.0.0.0:3307->3307/tcp, :::3307->3307/tcp mysql
[root@localhost ~]# [root@localhost ~]# docker exec -it 5c4d11ca385f /bin/bash
root@5c4d11ca385f:/data#
root@5c4d11ca385f:/data# redis-cli
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379>
是不是很简单,当你觉得简单时候,那就大错特错了
当你容器删除时候,数据怎么办。是不是还得考虑数据丢失的问题。那么就需要挂载数据卷
我们先把这个容器删除
[root@localhost ~]# docker rm -f 5c4d11ca385f
3、在宿主机新建redis目录,/usr/redis 作为docker容器的宿主机目录
[root@localhost usr]# mkdir redis
[root@localhost redis]# pwd
/usr/redis
把redis配置文件拷贝到这个目录。redis配置文件,找之前的
[root@localhost redis-6.2.6]# cp redis.conf /usr/redis/
[root@localhost redis-6.2.6]#
[root@localhost redis-6.2.6]# cd /usr/redis/
[root@localhost redis]#
[root@localhost redis]# ll
total 92
-rw-r--r--. 1 root root 93975 Jun 23 15:59 redis.conf
[root@localhost redis]#
修改配置文件,
daemonize no 改为no,默认是yes
port 6380端口改为6380
bind 0.0.0.0 注释掉 运行外部访问
4、启动redis镜像创建redis实例
[root@localhost ~]# docker run -p 6380:6380 --name=redis
--privileged=true
-v /usr/redis/redis.conf:/etc/redis/redis.conf
-v /usr/redis/data:/data
-d redis:6.0.8
redis-server /etc/redis/redis.conf
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e23744ebb4f4 redis:6.0.8 "docker-entrypoint.s…" 6 minutes ago Up 6 seconds 6379/tcp, 0.0.0.0:6380->6380/tcp, :::6380->6380/tcp redis
18a1e538c92b mysql:5.7 "docker-entrypoint.s…" 2 hours ago Up 2 hours 3306/tcp, 33060/tcp, 0.0.0.0:3307->3307/tcp, :::3307->3307/tcp mysql
[root@localhost ~]#
终于起来了,搞了半个小时呀,因为我是直接拷贝的原来的redis配置文件,之前版本是6.2多。
现在版本是6.0,可能好多参数都不兼容。导致容器起不来,通过观察日志。看到报错,一个一个处理了。
心累
5、测试redis连接
[root@localhost ~]# docker exec -it e23744ebb4f4 /bin/bash
root@e23744ebb4f4:/data#
#因为偶我映射的端口是6380,配置文件有指定密码,所以连接时候要制定端口和密码
root@e23744ebb4f4:/data# redis-cli -p 6380 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6380>
127.0.0.1:6380>
127.0.0.1:6380> ping
PONG
127.0.0.1:6380> 安装完成!