目录标题
- [docker学习笔记(一)【docker 介绍、卸载、安装、配置加速】]()
- docker学习笔记(二)docker常用命令
- docker学习笔记(三)镜像
- docker学习笔记(五)如何创建自己的阿里云镜像仓库(这是2021版的阿里云教程)
- 1、安装mysql
- 1.1、拉取mysql镜像
- 1.2、运行容器
- 1.2.1、查看容器运行状态
- 1.2.2、通过docker logs 容器id查看容器运行日志
- 1.2.3、查看[官网](https://hub.docker.com/_/mysql)使用教程
- 1.2.4、我们测试连接一下这个mysql
- 1.2.4.1、本地连接:
- 1.2.4.2、远程连接:
- 1.3、登录mysql后我们还需要更改mysql的一些默认配置
- 1.3.1、我们重新run一个mysql容器
- 2、安装redis
- 2.1、拉取镜像
- 2.2、查看[官网](https://hub.docker.com/_/redis)使用说明
- 2.3、先去redis的官方网站获取redis.conf配置文件
- 2.4、docker run redis
- 2.5、访问 redis
- 2.6、redis在容器中的安装目录
- 3、安装Nginx
- 3.1、拉取镜像
- 3.2、运行容器
- 3.3、在宿主主机创建挂载目录
- 3.4、从容器中拷贝配置文件
- 3.5、运行容器
- 4、安装zookeeper
- 4.1、搜索镜像
- 4.2、拉取镜像
- 4.3、docker run 镜像名 (运行得到一个容器)
- 4.4、启动并打开zk容器
- 4.5、运行zk
- 4.6、外部访问
1、安装mysql
1.1、拉取mysql镜像
[root@iz2zedg4ylq9iqtwm11wecz ~]# dokcer pull mysql:5.7
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/tomcat latest c43a65faae57 2 weeks ago 667 MB
docker.io/mysql 5.7 2c9028880e58 2 weeks ago 447 MB
docker.io/centos latest 300e315adb2f 5 months ago
1.2、运行容器
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker run --name mysql \
> -p 3316:3306 -d mysql:5.7
e66ce0a9d573fc7434946dd5ce28996769db7c23d888cb039b09ff69d4f9d730
1.2.1、查看容器运行状态
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e66ce0a9d573 mysql:5.7 "docker-entrypoint..." 42 seconds ago Exited (1) 41 seconds ago mysql
发现mysql已经停止运行
1.2.2、通过docker logs 容器id查看容器运行日志
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker logs e66ce0a9d573
2021-05-28 07:42:18+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.34-1debian10 started.
2021-05-28 07:42:18+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2021-05-28 07:42:18+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.34-1debian10 started.
2021-05-28 07:42:18+00:00 [ERROR] [Entrypoint]: Database is uninitialized and password option is not specified
You need to specify one of the following:
- MYSQL_ROOT_PASSWORD
- MYSQL_ALLOW_EMPTY_PASSWORD
- MYSQL_RANDOM_ROOT_PASSWORD
[root@iz2zedg4ylq9iqtwm11wecz ~]#
百度翻译得知 我们初始化启动mysql的时候,需要指定密码
1.2.3、查看官网使用教程
#我们需要这样run mysql
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker run --name mysql -p 3316:3306 -d -e MYSQL_ROOT_PASSWORD=123 mysql:5.7
0d2eb32cb4ae320e9f3e1365eea04a262667b711a878b661905ccb083706a949
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0d2eb32cb4ae mysql:5.7 "docker-entrypoint..." 7 seconds ago Up 6 seconds 33060/tcp, 0.0.0.0:3316->3306/tcp mysql
[root@iz2zedg4ylq9iqtwm11wecz ~]#
这个时候mysql就启动成功了
1.2.4、我们测试连接一下这个mysql
1.2.4.1、本地连接:
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker exec -it 0d2eb32cb4ae /bin/bash
root@0d2eb32cb4ae:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.34 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>
1.2.4.2、远程连接:
可以发现我们已经成功连接上使用docker安装的mysql
1.3、登录mysql后我们还需要更改mysql的一些默认配置
我们可以查看官网是如何更改默认配置的
我们先将MySQL的配置文件复制到宿主主机
[root@iz2zedg4ylq9iqtwm11wecz conf]# docker cp mysql:/etc/mysql/mysql.conf.d/mysqld.cnf /my/mysql/conf
[root@iz2zedg4ylq9iqtwm11wecz conf]# ls
mysqld.cnf
[root@iz2zedg4ylq9iqtwm11wecz conf]#
1.3.1、我们重新run一个mysql容器
在run mysql容器的时候我们需要挂载一些数据、日志和配置。防止容器停止运行和数据丢失
- 第一步是在我们的宿主主机创建好mysql存放数据、日志和配置的目录
[root@iz2zedg4ylq9iqtwm11wecz my]# mkdir -p mysql/conf
[root@iz2zedg4ylq9iqtwm11wecz my]# mkdir -p mysql/data
[root@iz2zedg4ylq9iqtwm11wecz my]# mkdir -p mysql/logs
[root@iz2zedg4ylq9iqtwm11wecz my]# ls
mysql
[root@iz2zedg4ylq9iqtwm11wecz my]# cd mysql
[root@iz2zedg4ylq9iqtwm11wecz mysql]# ls
conf data logs
[root@iz2zedg4ylq9iqtwm11wecz mysql]#
- 删除并重新run mysql
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker run --name mysql \
-p 3316:3306 \
-v /my/mysql/conf:/etc/mysql/mysql.conf.d/ \
-v /my/mysql/data:/var/lib/mysql \
-v /my/mysql/logs:/logs \
-e MYSQL_ROOT_PASSWORD=123 \
-d mysql:5.7
9d55c43331202d784d14e49f685aa633ddee525b59eb2c3067e607a335fbf739
- 更改默认配置文件
[root@iz2zedg4ylq9iqtwm11wecz conf]# pwd
/my/mysql/conf
[root@iz2zedg4ylq9iqtwm11wecz conf]# vim mysqld.cnf
更改默认的字符集**(注意更改配置文件后要重启容器才能生效)**
- 进入mysql查看字符集是否修改成功 (注意更改配置文件后需要重启容器)
mysql> create database m
-> ;
Query OK, 1 row affected (0.00 sec)
mysql> show create database m
-> ;
+----------+------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------+
| m | CREATE DATABASE `m` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+------------------------------------------------------------+
1 row in set (0.00 sec)
2、安装redis
2.1、拉取镜像
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker pull redis
Using default tag: latest
Trying to pull repository docker.io/library/redis ...
latest: Pulling from docker.io/library/redis
69692152171a: Already exists
a4a46f2fd7e0: Pull complete
bcdf6fddc3bd: Pull complete
b7e9b50900cc: Pull complete
5f3030c50d85: Pull complete
63dae8e0776c: Pull complete
Digest: sha256:365eddf64356169aa0cbfbeaf928eb80762de3cc364402e7653532bcec912973
Status: Downloaded newer image for docker.io/redis:latest
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/tomcat latest c43a65faae57 2 weeks ago 667 MB
docker.io/redis latest bc8d70f9ef6c 2 weeks ago 105 MB
docker.io/mysql 5.7 2c9028880e58 2 weeks ago 447 MB
docker.io/centos latest 300e315adb2f 5 months ago 209 MB
[root@iz2zedg4ylq9iqtwm11wecz ~]#
2.2、查看官网使用说明
2.3、先去redis的官方网站获取redis.conf配置文件
- 先下载redis
[root@iz2zedg4ylq9iqtwm11wecz my]# mkdir redis
[root@iz2zedg4ylq9iqtwm11wecz my]# ls
mysql redis
[root@iz2zedg4ylq9iqtwm11wecz my]# cd redis/
[root@iz2zedg4ylq9iqtwm11wecz redis]# pwd
/my/redis
[root@iz2zedg4ylq9iqtwm11wecz redis]# wget https://download.redis.io/releases/redis-6.2.3.tar.gz
--2021-05-28 18:30:37-- https://download.redis.io/releases/redis-6.2.3.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2456050 (2.3M) [application/octet-stream]
Saving to: ‘redis-6.2.3.tar.gz’
100%[=========================================================================>] 2,456,050 2.37MB/s in 1.0s
2021-05-28 18:30:39 (2.37 MB/s) - ‘redis-6.2.3.tar.gz’ saved [2456050/2456050]
[root@iz2zedg4ylq9iqtwm11wecz redis]#
- 解压
[root@iz2zedg4ylq9iqtwm11wecz redis]# ls
redis-6.2.3.tar.gz
[root@iz2zedg4ylq9iqtwm11wecz redis]# tar zxf redis-6.2.3.tar.gz
[root@iz2zedg4ylq9iqtwm11wecz redis]# ls
redis-6.2.3 redis-6.2.3.tar.gz
[root@iz2zedg4ylq9iqtwm11wecz redis]#
- 获取redis.conf配置文件
[root@iz2zedg4ylq9iqtwm11wecz redis]# cd redis-6.2.3
[root@iz2zedg4ylq9iqtwm11wecz redis-6.2.3]# ls
00-RELEASENOTES CONTRIBUTING INSTALL README.md runtest-cluster sentinel.conf TLS.md
BUGS COPYING Makefile redis.conf runtest-moduleapi src utils
CONDUCT deps MANIFESTO runtest runtest-sentinel tests
[root@iz2zedg4ylq9iqtwm11wecz redis-6.2.3]# mkdir /my/redis/conf
[root@iz2zedg4ylq9iqtwm11wecz redis-6.2.3]# cp redis.conf /my/redis/conf
[root@iz2zedg4ylq9iqtwm11wecz redis-6.2.3]# cd /my/redis/conf
[root@iz2zedg4ylq9iqtwm11wecz conf]# ls
redis.conf
[root@iz2zedg4ylq9iqtwm11wecz conf]#
- 修改配置文件
首先 vim redis.conf
然后,在normal模式(也就是不能写的模式下)下按下/即可进入查找模式 按n搜索下一条,N是上一条
搜索:recuirepass (将注释去掉)然后在后面填写密码。比如更改为 : requirepass 123
2.4、docker run redis
[root@iz2zedg4ylq9iqtwm11wecz redis]# docker run --name myredis \
> -p 6379:6379 \
> -v /my/redis/conf/redis.conf:/usr/local/etc/redis/redis.conf \
> -v /my/redis/data:/data \
> -d redis redis-server /usr/local/etc/redis/redis.conf
598fef497cec840fff08dd5532fbf30f671eeb6022f93c2276cc12b77bc5e548
[root@iz2zedg4ylq9iqtwm11wecz redis]#
2.5、访问 redis
2.5.1本地访问
[root@iz2zedg4ylq9iqtwm11wecz redis]# docker exec -it myredis /bin/bash
root@598fef497cec:/data# redis-cli
127.0.0.1:6379> set name lihua
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123
OK
127.0.0.1:6379> set name lihua
OK
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> key *
(error) ERR unknown command `key`, with args beginning with: `*`,
127.0.0.1:6379> keys *
1) "name"
2) "age"
127.0.0.1:6379>
2.5.2远程访问,由于没有安装redis就演示了
注意远程连接需要,在redis.conf配置远程连接。配置文件更改后需要重启容器
1、关闭只允许本机访问
2、关闭保护模式
2.6、redis在容器中的安装目录
root@0bee7c2d98cb:/usr/local/bin# pwd
/usr/local/bin
3、安装Nginx
3.1、拉取镜像
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/nginx latest d1a364dc548d 2 days ago 133 MB
3.2、运行容器
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker run --name mynginx -d nginx
66f66c0d71a6bc73b5af1efe29db0923e70ec80c211b5fba47edf1206f7e0d77
[root@iz2zedg4ylq9iqtwm11wecz ~]#
3.3、在宿主主机创建挂载目录
[root@iz2zedg4ylq9iqtwm11wecz ~]# cd /my/nginx/
[root@iz2zedg4ylq9iqtwm11wecz nginx]# ls
html
[root@iz2zedg4ylq9iqtwm11wecz nginx]# pwd
/my/nginx
[root@iz2zedg4ylq9iqtwm11wecz nginx]#
3.4、从容器中拷贝配置文件
[root@iz2zedg4ylq9iqtwm11wecz nginx]# docker cp mynginx:/etc/nginx/nginx.conf /my/nginx #主配置文件
[root@iz2zedg4ylq9iqtwm11wecz nginx]# ls
html nginx.conf
[root@iz2zedg4ylq9iqtwm11wecz nginx]#
[root@iz2zedg4ylq9iqtwm11wecz nginx]# docker cp mynginx:/etc/nginx/conf.d /my/nginx #拷贝nginx 导入虚拟主机配置文件
[root@iz2zedg4ylq9iqtwm11wecz nginx]# ls
conf.d html nginx.conf
[root@iz2zedg4ylq9iqtwm11wecz nginx]#
自定义nginx主页
[root@iz2zedg4ylq9iqtwm11wecz nginx]# echo welcome to nginx! > /my/nginx/html/index.html
[root@iz2zedg4ylq9iqtwm11wecz nginx]# cd html
[root@iz2zedg4ylq9iqtwm11wecz html]# cat index.html
welcome to nginx!
[root@iz2zedg4ylq9iqtwm11wecz html]#
3.5、运行容器
[root@iz2zedg4ylq9iqtwm11wecz nginx]# docker run --name mynginx \
-p 80:80 -p 443:443 \
-v /my/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /my/nginx/html:/usr/share/nginx/html:ro \
-v /my/nginx/conf.d:/usr/nginx/conf.d \
-d nginx
dfabf4da2bddc13ee408897d1dac6f3478d30dd6829eda10bb764b46c733de48
[root@iz2zedg4ylq9iqtwm11wecz nginx]#
访问一下
4、安装zookeeper
4.1、搜索镜像
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker search zookeeper
INDEX NAME DESCRIPTION STARS(热度) OFFICIAL(官方) AUTOMATED
docker.io docker.io/zookeeper Apache ZooKeeper is an open-source server ... 1123 [OK]
docker.io docker.io/jplock/zookeeper Builds a docker image for Zookeeper versio... 165 [OK]
docker.io docker.io/wurstmeister/zookeeper 152 [OK]
docker.io docker.io/mesoscloud/zookeeper ZooKeeper 73 [OK]
docker.io docker.io/digitalwonderland/zookeeper Latest Zookeeper - clusterable 23 [OK]
docker.io docker.io/mbabineau/zookeeper-exhibitor 23 [OK]
docker.io docker.io/tobilg/zookeeper-webui Docker image for using `zk-web` as ZooKeep... 15 [OK]
docker.io docker.io/debezium/zookeeper Zookeeper image required when running the ... 14 [OK]
docker.io docker.io/confluent/zookeeper [deprecated - please use confluentinc/cp-z... 13 [OK]
docker.io docker.io/31z4/zookeeper Dockerized Apache Zookeeper. 9 [OK]
docker.io docker.io/thefactory/zookeeper-exhibitor Exhibitor-managed ZooKeeper with S3 backup... 6 [OK]
docker.io docker.io/engapa/zookeeper Zookeeper image optimised for being used i... 3
docker.io docker.io/emccorp/zookeeper Zookeeper 2
docker.io docker.io/harisekhon/zookeeper Apache ZooKeeper (tags 3.3 - 3.4) 2 [OK]
docker.io docker.io/duffqiu/zookeeper-cli 1 [OK]
docker.io docker.io/josdotso/zookeeper-exporter ref: https://github.com/carlpett/zookeeper... 1 [OK]
docker.io docker.io/openshift/zookeeper-346-fedora20 ZooKeeper 3.4.6 with replication support 1
docker.io docker.io/paulbrown/zookeeper Zookeeper on Kubernetes (PetSet) 1 [OK]
docker.io docker.io/perrykim/zookeeper k8s - zookeeper ( forked k8s contrib ) 1 [OK]
docker.io docker.io/strimzi/zookeeper 1
docker.io docker.io/dabealu/zookeeper-exporter zookeeper exporter for prometheus 0 [OK]
docker.io docker.io/humio/zookeeper-dev zookeeper build with zulu jvm. 0
docker.io docker.io/midonet/zookeeper Dockerfile for a Zookeeper server. 0 [OK]
docker.io docker.io/phenompeople/zookeeper Apache ZooKeeper is an open-source server ... 0 [OK]
docker.io docker.io/pravega/zookeeper-operator Kubernetes operator for Zookeeper 0
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker pull docker.io/zookeeper
4.2、拉取镜像
版本:3.7.0
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker pull docker.io/zookeeper:3.7.0
Using default tag: latest
Trying to pull repository docker.io/library/zookeeper ...
latest: Pulling from docker.io/library/zookeeper
e1acddbe380c: Pull complete
427b7134c0c7: Pull complete
cea848f5155d: Pull complete
663bffc94428: Pull complete
474d1e69eb08: Pull complete
c1cecf1b8792: Pull complete
159250b3b625: Pull complete
e216e6f8b2db: Pull complete
Digest: sha256:265a3f1d4a619752c15076308ab0ae0c8ac0eeea6ce2bc64ba204d8b7bae0bc6
Status: Downloaded newer image for docker.io/zookeeper:latest
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/zookeeper latest c83b9da01e73 2 days ago 270 MB
registry.cn-shenzhen.aliyuncs.com/wuyujitang/dockerimages_wuyujitang v11.1 55e93b69ba15 2 months ago 667 MB
docker.io/nginx latest d1a364dc548d 2 months ago 133 MB
docker.io/tomcat latest c43a65faae57 3 months ago 667 MB
docker.io/redis latest bc8d70f9ef6c 3 months ago 105 MB
4.3、docker run 镜像名 (运行得到一个容器)
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker run -p 2181:2181 --name myzk -d c83b9da01e73
fdf44d05a275ab198f84aeeb95060f17d2d680aed7a1d975c199e335ec4c3751
4.4、启动并打开zk容器
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker start myzk
myzk
[root@iz2zedg4ylq9iqtwm11wecz ~]# docker exec -it myzk /bin/bash
root@fdf44d05a275:/apache-zookeeper-3.7.0-bin#
4.5、运行zk
root@fdf44d05a275:/apache-zookeeper-3.7.0-bin# cd bin
root@fdf44d05a275:/apache-zookeeper-3.7.0-bin/bin# ./zkCli.sh
测试:
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1]
4.6、外部访问
- 放行阿里云端口
- 放行服务器内部端口
# 查看防火墙是否运行
[root@iz2zedg4ylq9iqtwm11wecz ~]# firewall-cmd --state
running
# 查询端口号是否放行
[root@iz2zedg4ylq9iqtwm11wecz ~]# firewall-cmd --query-port=8080/tcp
yes
[root@iz2zedg4ylq9iqtwm11wecz ~]# firewall-cmd --query-port=2181/tcp
no
# 放行端口号, --zone=public 放行类型(public 全部人)port=2181/tcp(放行的端口号:和协议类型) --permanent(永久生效,重启服务器也生效)
[root@iz2zedg4ylq9iqtwm11wecz ~]# firewall-cmd --zone=public --add-port=2181/tcp --permanent
success
# 重新启动防火墙,
[root@iz2zedg4ylq9iqtwm11wecz ~]# systemctl restart firewalld.service
# 重新加载防火墙配置
[root@iz2zedg4ylq9iqtwm11wecz ~]# firewall-cmd --reload
success
[root@iz2zedg4ylq9iqtwm11wecz ~]# firewall-cmd --query-port=2181/tcp
yes
[root@iz2zedg4ylq9iqtwm11wecz ~]#