本文主要是介绍6.5.Docker数据管理和端口映射应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 实战总体步骤
- 案例1:安装tomcat(端口映射使用)
- 案例2:安装mysql(数据卷应用)
- 测试版
- 实战版
- 案例3:安装Redis(数据卷应用)
实战总体步骤
-
搜索镜像
可以在Dockerhub中查看容器如何使用,包括拉取和启动等。
-
拉取镜像
-
查看镜像
-
启动镜像
-
停止镜像
-
移除镜像
案例1:安装tomcat(端口映射使用)
-
搜索镜像
docker hub上查找tomcat或通过命令搜索
[root@zyn01 ~]# docker search -f is-official=true tomcat NAME DESCRIPTION STARS OFFICIAL tomcat Apache Tomcat is an open source implementati… 3667 [OK] tomee Apache TomEE is an all-Apache Java EE certif… 115 [OK]
-
拉取镜像
从docker hub拉取镜像到本地
[root@zyn01 ~]# docker pull tomcat
-
查看镜像
[root@zyn01 ~]# docker images tomcat REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest b64abfdee99c 2 years ago 668MB
-
启动镜像
使用tomcat镜像创建容器实例
[root@zyn01 ~]# docker run -it -d -p 8080:8080 tomcat 7aa22c83581d11d6dc33e89511fe383b000fa4aa48309bbbc820ee9d1c1996b8 [root@zyn01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES db6b2e945248 tomcat "catalina.sh run" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp epic_jang
-p 小写,主机端口:docker容器端口
-P 大写,随机分配端口
-i:交互
-t:tty终端
-d:后台
❓访问localhost:8080显示404。
可能是没有映射端口;可能是没有关闭防火墙;
新版tomcat需要进行如下修改:
把webapps.dist目录换成webapps,再次访问localhost:8080,访问成功。
[root@zyn01 ~]# docker exec -it db6b2e945248 /bin/bash
root@db6b2e945248:/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
root@db6b2e945248:/usr/local/tomcat# rm webapps
rm: cannot remove 'webapps': Is a directory
root@db6b2e945248:/usr/local/tomcat# rm -r webapps
root@db6b2e945248:/usr/local/tomcat# mv webapps.dist/ webapps
-
免修改版本操作流程
docker pull billygoo/tomcat8-jdk8 docker run -d -p 8080:8080 --name mytomcat8 billygoo/tomcat8-jdk8
案例2:安装mysql(数据卷应用)
测试版
-
搜索镜像
docker hub上查找mysql或通过命令搜索。
[root@zyn01 ~]# docker search -f is-official=true mysql NAME DESCRIPTION STARS OFFICIAL mysql MySQL is a widely used, open-source relation… 15055 [OK]
-
拉取镜像
从docker hub拉取镜像到本地
[root@zyn01 ~]# docker pull mysql
-
查看镜像
[root@zyn01 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 96a1b6f60db8 Less than a second ago 592MB
-
启动镜像
如何启动?https://hub.docker.com/_/mysql
使用mysql镜像创建并启动容器实例
[root@zyn01 ~]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d --name mysql mysql f3da95267e1eba21959c7739bd5967fef956d5a44b37e763b80e1ce68e08d77d[root@zyn01 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f3da95267e1e mysql "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
-
进入容器测试mysql数据库,创建数据库和表
[root@zyn01 ~]# docker exec -it f3da95267e1e /bin/bashbash-5.1# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.4.0 MySQL Community Server - GPLCopyright (c) 2000, 2024, 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> create database db01; Query OK, 1 row affected (0.00 sec)mysql> use db01; Database changed mysql> create table aa(id int,name varchar(20)); Query OK, 0 rows affected (0.01 sec)mysql> insert into aa values(1,'zyn'); Query OK, 1 row affected (0.01 sec)mysql> select * from aa; +------+------+ | id | name | +------+------+ | 1 | zyn | +------+------+ 1 row in set (0.00 sec)
❓存在插入中文乱码的情况!
原因:docker上默认字符集编码隐患,使用mac本地客户端工具可以正常显示和插入中文,但是docker里面的mysql容器实例查看,内容如下:
mysql> SHOW VARIABLES LIKE 'character%'; +--------------------------+--------------------------------+ | Variable_name | Value | +--------------------------+--------------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | latin1 | | character_set_server | utf8mb4 | | character_set_system | utf8mb3 | | character_sets_dir | /usr/share/mysql-8.4/charsets/ | +--------------------------+--------------------------------+ 8 rows in set (0.00 sec)mysql> select * from aa; +------+------+ | id | name | +------+------+ | 1 | zyn | | 2 | ?? | +------+------+ 2 rows in set (0.00 sec)
接下来给出解决方案。容器删除后,数据将丢失。接下来通过挂在容器卷实现容器移除后再次重启容器数据维持不变。
实战版
-
新建mysql容器
docker run -d -p 3306:3306 --privileged=true -v /zzyyuse/mysql/log:/var/log/mysql -v /zzyyuse/mysql/data:/var/lib/mysql -v /zzyyuse/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql
-
新建my.cnf,通过容器卷同步给mysql容器实例
[root@zyn01 ~]# cd /zzyyuse/mysql/conf/ [root@zyn01 conf]# vim my.cn [client] default_character_set=utf8 [mysqld] collation_server = utf8_general_ci character_set_server = utf8
-
修改字符集操作后,重启mysql容器再次查看字符集编码并测试插入中文。
[root@zyn01 conf]# docker restart mysql mysql> [root@zyn01 ~]# docker exec -it mysql /bin/bash bash-5.1# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.4.0 MySQL Community Server - GPLCopyright (c) 2000, 2024, 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 | utf8mb3 | | character_set_connection | utf8mb3 | | character_set_database | utf8mb3 | | character_set_filesystem | binary | | character_set_results | utf8mb3 | | character_set_server | utf8mb3 | | character_set_system | utf8mb3 | | character_sets_dir | /usr/share/mysql-8.4/charsets/ | +--------------------------+--------------------------------+ 8 rows in set (0.01 sec)mysql> create database dn01; Query OK, 1 row affected (0.01 sec) mysql> use dn01; Database changed mysql> create table aa(id int,name varchar(20)); Query OK, 0 rows affected (0.01 sec)mysql> select * from aa; +------+--------+ | id | name | +------+--------+ | 1 | 张 三 | +------+--------+ 1 row in set (0.00 sec)
-
本次绑定了数据卷,因此移除容器后再次重启容器,数据依然存在
[root@zyn01 conf]# docker stop mysql mysql [root@zyn01 conf]# docker rm mysql mysql [root@zyn01 conf]# docker run -d -p 3306:3306 --privileged=true -v /zzyyuse/mysql/log:/var/log/mysql -v /zzyyuse/mysql/data:/var/lib/mysql -v /zzyyuse/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql 3fdcedd80ef699764860393a377048ebb0ecc4400cd15e6e4ab4159e407ff631 [root@zyn01 ~]# docker exec -it mysql /bin/bash bash-5.1# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.4.0 MySQL Community Server - GPLCopyright (c) 2000, 2024, 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> use dn01; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -ADatabase changed mysql> select * from aa; +------+--------+ | id | name | +------+--------+ | 1 | 张 三 | +------+--------+ 1 row in set (0.00 sec)
案例3:安装Redis(数据卷应用)
-
拉取redis镜像
docker pull redis:6.0.8
-
运行镜像
[root@zyn01 conf]# docker run -d -p 6379:6379 redis:6.0.8 d2cc16415883b0352c0d239692a17a0ac0ac7b7c04f16fea4060948769b64d4d [root@zyn01 conf]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d2cc16415883 redis:6.0.8 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp magical_edison [root@zyn01 conf]# [root@zyn01 conf]# docker exec -it d2cc16415883 /bin/bash root@d2cc16415883:/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> ping PONG
-
在宿主机下新建目录/app/redis,将redis.conf文件上传至该目录并进行如下修改
#1.开启redis验证 可选 requirepass 123 #2.允许redis外地连接 必须注释掉 # bind 127.0.0.1 #3.将daemonize yes注释起来或者daemonize no设置,因为该配置和docker run中-d参数冲突,会导致容器一直启动失败 daemonize no #4.开启redis数据持久化 可选 appendonly yes
-
停止和删除redis容器,重新创建进行测试
[root@zyn01 redis]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d2cc16415883 redis:6.0.8 "docker-entrypoint.s…" 25 minutes ago Up 25 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp magical_edison[root@zyn01 redis]# docker stop d2cc16415883 d2cc16415883 [root@zyn01 redis]# docker rm d2cc16415883 d2cc16415883 [root@zyn01 redis]# docker run -p 6379:6379 --name myr3 --privileged=true -v /app/redis/redis.conf:/etc/redis/redis.conf -v /app/redis/data:/data -d redis:6.0.8 redis-server /etc/redis/redis.conf 25d1f9ae4ddacea525fc00e4e8abba1e59c99557c9389b9f2b8780a51b4b1328[root@zyn01 redis]# docker exec -it myr3 /bin/bash root@25d1f9ae4dda:/data# redis-cli 127.0.0.1:6379> set k1 v1 (error) NOAUTH Authentication required. 127.0.0.1:6379> auth 123 OK 127.0.0.1:6379> set k1 v1 OK 127.0.0.1:6379> get k1 "v1"
需要认证说明配置生效,测试成功。
这篇关于6.5.Docker数据管理和端口映射应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!