【Docker笔记02】【常用软件安装】

一、前言

本系列是根据 B 站 尚硅谷 Docker 视频 学习记录笔记。因为没有视频课件,部分内容摘自 https://www.yuque.com/tmfl/cloud/dketq0

本系列仅为自身学习笔记记录使用,记录存在偏差,推荐阅读原视频内容或本文参考笔记。


本文主要介绍 Tomcat、Mysql、Redis 在 Docker 中的安装过程。

二、Tomcat 安装

bash 复制代码
[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 如下:

bash 复制代码
# 查看目录下内容
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 免修改版的镜像

powershell 复制代码
[root@192 ~] docker pull billygoo/tomcat8-jdk8

三、Mysql 安装

1. 简单安装

bash 复制代码
[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. 复杂安装

相较于上面需要解决两个问题:

  1. 数据库使用数据卷备份数据问题
  2. 数据库字符编码不对导致中文乱码问题
bash 复制代码
# 使用数据卷挂载,完成数据备份问题
[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. 基础安装

bash 复制代码
[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. 复杂安装

相较于上面需要解决两个问题:

  1. Redis 数据挂载在宿主机
  2. 使用指定的Redis 配置文件启动

具体过程如下:

  1. 宿主机新增redis.conf 文件并修改主要是以下几点:

    bash 复制代码
    # 新增:开启密码验证
    requirepass 123
    # 注释:允许外地连接
    # bind 127.0.0.1 -::1
    # daemonize 设置为 no 或者注释,否则与docker run 的 -d 冲突
    daemonize no
    # 开启 AOF 持久化, 可选
    appendonly yes
    # 保护模式关闭:可选
    protected-mode no
  2. 启动 docker 中的redis

    bash 复制代码
    [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 客户端工具连接查看

需要注意:

  1. 需要注意,不同版本的 Redis Conf 文件使用可能会有问题,建议使用同版本 Redis conf 文件

  2. 启动时返回容器id,容器却没启动成功可以通过 docker logs [容器id] 查看容器启动错误日志

    bash 复制代码
    [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

相关推荐
tntlbb1 小时前
Ubuntu20.4 VPN+Docker代理配置
运维·ubuntu·docker·容器
Rinai_R1 小时前
计算机组成原理的学习笔记(7)-- 存储器·其二 容量扩展/多模块存储系统/外存/Cache/虚拟存储器
笔记·物联网·学习
吃着火锅x唱着歌1 小时前
PHP7内核剖析 学习笔记 第四章 内存管理(1)
android·笔记·学习
ragnwang1 小时前
C++ Eigen常见的高级用法 [学习笔记]
c++·笔记·学习
Gabriel_liao1 小时前
Docker安装Neo4j
docker·容器·neo4j
胡西风_foxww2 小时前
【es6复习笔记】rest参数(7)
前端·笔记·es6·参数·rest
豆豆豆豆变4 小时前
Docker轻量级可视化工具Portainer
docker·容器·自动化运维
花晓木4 小时前
k8s etcd 数据损坏处理方式
容器·kubernetes·etcd
运维&陈同学4 小时前
【模块一】kubernetes容器编排进阶实战之基于velero及minio实现etcd数据备份与恢复
数据库·后端·云原生·容器·kubernetes·etcd·minio·velero
花晓木5 小时前
k8s备份 ETCD , 使用velero工具进行备份
容器·kubernetes·etcd