本文主要是介绍【Docker笔记02】【常用软件安装】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、前言
本系列是根据 B 站 尚硅谷 Docker 视频 学习记录笔记。因为没有视频课件,部分内容摘自 https://www.yuque.com/tmfl/cloud/dketq0。
本系列仅为自身学习笔记记录使用,记录存在偏差,推荐阅读原视频内容或本文参考笔记。
本文主要介绍 Tomcat、Mysql、Redis 在 Docker 中的安装过程。
二、Tomcat 安装
[root@192 ~]# docker search tomcat
[root@192 ~]# docker pull tomcat
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest fb5657adc892 2 years ago 680MB
# 启动
[root@192 ~]# docker run -it -p 8080:8080 tomcat
# 查看
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
58f903e80a2b tomcat "catalina.sh run" 12 seconds ago Up 11 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp great_nash
需要注意:
Docker 版本的 Tomcat 无法访问默认Tomcat 页面,因为其webapps 目录下是空的,如果需要可以将容器内部的tomcat 目录下的webapps.dist 替换 webapps 如下:
# 查看目录下内容
root@58f903e80a2b:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
# 删除旧的 webapps 目录,替换新的
root@58f903e80a2b:/usr/local/tomcat# rm -rf webapps
root@58f903e80a2b:/usr/local/tomcat# mv webapps.dist/ webapps
root@58f903e80a2b:/usr/local/tomcat# ls
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs native-jni-lib temp webapps work
root@58f903e80a2b:/usr/local/tomcat# cd webapps/
root@58f903e80a2b:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
也可以通过如下命令下载tomcat8 免修改版的镜像
[root@192 ~] docker pull billygoo/tomcat8-jdk8
三、Mysql 安装
1. 简单安装
[root@192 ~]# docker pull mysql:5.7
[root@192 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest fb5657adc892 2 years ago 680MB
mysql 5.7 c20987f18b13 2 years ago 448MB
[root@192 ~]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
c54db5ffd8521af204b77fcd6fac10016b8b60d62d555950eafb4b3654d2589e
[root@192 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c54db5ffd852 mysql:5.7 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp lucid_murdock
58f903e80a2b tomcat "catalina.sh run" 24 minutes ago Up 24 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp great_nash
# 进入容器
[root@192 ~]# docker exec -it c54db5ffd852 /bin/bash
# 连接Mysql
root@c54db5ffd852:/usr/local# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
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.mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)# 需要注意,默认情况下Docker mysql 5.7 版本编码可能会出现问题,需要修正编码
# 如果使用外部建表工具(如 navicat )建表会按照 navicat 的字符集来创建表
# 如果使用命令直接建表则按照服务器字符集版本建表可能出现中文乱码
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.01 sec)
外界可以通过虚拟机的映射连接
2. 复杂安装
相较于上面需要解决两个问题:
- 数据库使用数据卷备份数据问题
- 数据库字符编码不对导致中文乱码问题
# 使用数据卷挂载,完成数据备份问题
[root@192 ~]# docker run -d -p 3306:3306 --privileged=true -v /mydocker/mysql/log:/var/log/mysql -v /mydocker/mysql/data:/var/lib/mysql -v /mydocker/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=root --name mysql mysql:5.7
fda0fd20ea3e73ce45aa4044a5b183c334502a21a595aa4031929d48bce083cb# 解决中文乱码: 新建并修改my.cnf 文件
[root@192 ~]# cd /mydocker/mysql/conf/
[root@192 conf]# ls
[root@192 conf]# vim my.cnf
[root@192 conf]# cat my.cnf
[client]
default_character_set=utf8
[mysqld]
collation_server=utf8_general_ci
character_set_server=utf8
[root@192 conf]#
[root@192 conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fda0fd20ea3e mysql:5.7 "docker-entrypoint.s…" 10 minutes ago Up 9 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
58f903e80a2b tomcat "catalina.sh run" 52 minutes ago Up 52 minutes 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp great_nash# 重启Mysql
[root@192 conf]# docker restart mysql
mysql
[root@192 conf]# docker exec -it fda0fd20ea3e /bin/bash
root@fda0fd20ea3e:/#
# 连接 Mysql
root@fda0fd20ea3e:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
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.
# 查看修改后的字符集
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
四、 Redis 安装
1. 基础安装
[root@192 /]# docker pull redis:6.0.8[root@192 /]# docker run -d -p 6379:6379 redis:6.0.8
b38f0175807ec031bbd5e9c4194311fb0bfc8486d982a7464a807db1ab962e46
[root@192 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b38f0175807e redis:6.0.8 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp heuristic_matsumoto
fda0fd20ea3e mysql:5.7 "docker-entrypoint.s…" 20 minutes ago Up 10 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
58f903e80a2b tomcat "catalina.sh run" About an hour ago Up About an hour 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp great_nash
[root@192 /]# docker exec -it b38f0175807e /bin/bash
root@b38f0175807e:/data# redis-cli
127.0.0.1:6379> set k v
OK
127.0.0.1:6379> get k
"v"
2. 复杂安装
相较于上面需要解决两个问题:
- Redis 数据挂载在宿主机
- 使用指定的Redis 配置文件启动
具体过程如下:
-
宿主机新增redis.conf 文件并修改主要是以下几点:
# 新增:开启密码验证 requirepass 123 # 注释:允许外地连接 # bind 127.0.0.1 -::1 # daemonize 设置为 no 或者注释,否则与docker run 的 -d 冲突 daemonize no # 开启 AOF 持久化, 可选 appendonly yes # 保护模式关闭:可选 protected-mode no
-
启动 docker 中的redis
[root@192 redis]# docker run -p 6379:6379 --name myredis --privileged=true -v /mydocker/redis/redis.conf:/etc/redis/redis.conf -v /mydocker/redis/data:/data -d redis:6.0.8 redis-server /etc/redis/redis.conf 9a0d7ee72fa10d8fd3a6ca13d7597efec767776a6b0b03c12f7ef6ec4b04283f [root@192 redis]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9a0d7ee72fa1 redis:6.0.8 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp myredis 58f903e80a2b tomcat "catalina.sh run" 3 hours ago Up 3 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp great_nash # 进入容器 [root@192 redis]# docker exec -it myredis /bin/bash root@9a0d7ee72fa1:/data# redis-cli # 执行命令没有权限 127.0.0.1:6379> set k1 v1 (error) NOAUTH Authentication required. 127.0.0.1:6379> exit# 使用密码重新连接后执行命令 root@9a0d7ee72fa1:/data# redis-cli -a 123 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> set k1 v1 OK
也可以通过Redis 客户端工具连接查看
需要注意:
- 需要注意,不同版本的 Redis Conf 文件使用可能会有问题,建议使用同版本 Redis conf 文件
- 启动时返回容器id,容器却没启动成功可以通过 docker logs [容器id] 查看容器启动错误日志
[root@192 redis]# docker run -p 6379:6379 --name myredis --privileged=true -v /mydocker/redis/redis.conf:/etc/redis/redis.conf -v /mydocker/redis/data:/data -d redis:6.0.8 /usr/local/bin/redis-server /etc/redis/redis.conf 9e92ef584f78a335495890f93be807ebefe1b9b713069ed4ddbbcc4f47c6d0dc [root@192 redis]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9e92ef584f78 redis:6.0.8 "docker-entrypoint.s…" 11 seconds ago Exited (1) 11 seconds ago myredis c54db5ffd852 mysql:5.7 "docker-entrypoint.s…" 2 hours ago Exited (0) 2 hours ago lucid_murdock 58f903e80a2b tomcat "catalina.sh run" 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp great_nash # 查看容器启动日志,发现是 redis.conf 配置文件问题 [root@192 redis]# docker logs 9e92ef584f78*** FATAL CONFIG FILE ERROR (Redis 6.0.8) *** Reading the configuration file, at line 343 >>> 'set-proc-title yes' Bad directive or wrong number of arguments
五、参考内容
B 站 尚硅谷 Docker 视频
https://www.yuque.com/tmfl/cloud/dketq0
这篇关于【Docker笔记02】【常用软件安装】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!