docker相关记录

相关文档:

  https://blog.csdn.net/weixin_39897267/article/details/111299278

  https://www.cnblogs.com/feipeng8848/p/10470655.html

 

docker中软件镜像的安装:

  docker pull  zookeeper

  docker pull wurstmeister/kafka

  docker pull mysql

 

镜像查看

  docker images

 

镜像的启动:

  docker run -d --name zookeeper -p 2181:2181 -d zookeeper:latest

  docker run -d --name kafka -p 9092:9092 -e KAFKA_BROKER_ID=0 -e KAFKA_ZOOKEEPER_CONNECT=本机ip:2181 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://本机ip:9092 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 -t wurstmeister/kafka

 

kafka容器测试消息

  • 进入容器:docker exec -it {container id} /bin/bash

  • 进入到opt/kafka_2.13-2.7.0/bin

  • 创建主题:./kafka-console-producer.sh --broker-list localhost:9092 --topic {topicName},然后发送消息

{"datas":[{"name":"jianshu","value":"10"}],"ver":"1.0"}

  • 运行consumer查看是否有消息:kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic {topicName} --from-beginning
zk相关信息查看:

 

 

 

mysql具体操作:

概念解释#

Docker镜像:可以理解成安装操作系统的镜像文件
Docker容器:可以理解为运行的操作系统。也有人比喻docker镜像为类,docker容器为对象

第零步,查看Docker MySQL文档#

MySQL文档地址:
https://hub.docker.com/_/mysql/

第一步,拉取MySQL镜像

docker pull mysql

之后docker会自动拉取(下载)MySQL镜像。
拉取成功后我们查看一下:

docker images

第二步,创建并启动一个MySQL容器

输入以下命令:

docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql

–name:给新创建的容器命名,此处命名为mysqlserver
-e:配置信息,此处配置mysql的root用户的登陆密码
-p:端口映射,表示在这个容器中使用3306端口(第二个)映射到本机的端口号也为3306(第一个)
-d:成功启动容器后输出容器的完整ID
最后一个mysql指的是mysql镜像名字
到这里我们查看容器运行状态:

docker ps

上图可以看到容器的简写ID,容器的源镜像,创建时间,状态,端口映射信息,容器名字等。

第三步,测试连接MySQL

1、命令连接

docker exec -it  mysqlserver /bin/bash

docker exec :在运行的容器中执行命令
语法
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
OPTIONS说明:
-d :分离模式: 在后台运行
-i :即使没有附加也保持STDIN 打开
-t :分配一个伪终端

 

 

 

mysql其他:

1.可以启动多个MySQL服务,因为我们启动的是容器,容器可以有多个,只要容器名字映射段端口不一样就可以了,例如:

docker run --name mysqlserver2 -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 -d mysql

下图就是把mysqlserver2的3306绑定到了本机上的3307,使用navicat访问的时候只要填写成3307即可访问。

 

2.查看所有容器(启动状态或者关闭状态)

Copy
docker ps -a

3.启动和关闭容器
启动命令:

docker start mysqlserver   //通过指定容器名字sudo docker start 73f8811f669e  //通过指定容器ID

关闭命令:

docker stop mysqlserver   //通过指定容器名字
docker stop 73f8811f669e  //通过指定容器ID

3.修改MySQL配置文件有两种方法:
一是进入容器,修改容器里的MySQL的配置文件,然后重新启动容器,例如:

docker exec -it mysqlserver /usr/bin/bash

然后可以进入容器的命令行模式,接着修改 /etc/mysql/my.cnf 文件即可
二是挂载主机的mysql配置文件,官方文档如下:
The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.

If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):

docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.

 

posted @ 2021-11-29 14:47  修心而结网  阅读(27)  评论(0编辑  收藏  举报