赞
踩
docker 官网 https://hub.docker.com/ 官网
yum update
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce 或者
yum install -y docker-ce
docker -v
systemctl start docker
systemctl stop docker
systemctl restart docker
systemctl status docker
systemctl enable docker
docker info
docker --help
docker images
docker images –q # 查看所用镜像的id
docker search 镜像名称
docker pull 镜像名称:版本号
docker rmi 镜像id # 删除指定本地镜像
docker rmi `docker images -q` # 删除所有本地镜像
docker ps # 查看正在运行的容器
docker ps –a # 查看所有容器
参数说明:
-i:保持容器运行。通常与 -t 同时使用。加入it这两个参数后,容器创建后自动进入容器中,退出容器后,容器自动关闭。
-t:为容器重新分配一个伪输入终端,通常与 -i 同时使用。
-d:以守护(后台)模式运行容器。创建一个容器在后台运行,需要使用docker exec 进入容器。退出后,容器不会关闭。
-it 创建的容器一般称为交互式容器,-id 创建的容器一般称为守护式容器
–name:为创建的容器命名。
–restart=always :容器自启
docker update --restart=always 容器ID/容器名称:运行的容器添加自启动
docker run 参数 Eg:docker run -id \ -p 3307:3306 \ --name=c_mysql \ --restart=always \ -v $PWD/conf:/etc/mysql/conf.d \ -v $PWD/logs:/logs \ -v $PWD/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=123456 \ mysql:5.6 参数说明: -p 3307:3306:将容器的 3306 端口映射到宿主机的 3307 端口。 -v $PWD/conf:/etc/mysql/conf.d:将主机当前目录下的 conf/my.cnf 挂载到容器的 /etc/mysql/my.cnf。配置目录 -v $PWD/logs:/logs:将主机当前目录下的 logs 目录挂载到容器的 /logs。日志目录 -v $PWD/data:/var/lib/mysql :将主机当前目录下的data目录挂载到容器的 /var/lib/mysql 。数据目录 **-e MYSQL_ROOT_PASSWORD=123456:**初始化 root 用户的密码
docker exec 参数 # 退出容器,容器不会关闭 Eg:docker exec –it c_mysql /bin/bash,或者 docker exec –it c_mysql bash
docker stop 容器名称
docker start 容器名称
docker rm 容器名称
//停止容器
docker stop +id
//再执行
//可以删除所有不在运行的容器
docker container prune
docker inspect 容器名称
//大哥的文章很实用
https://blog.csdn.net/arcsin_/article/details/123707618
docker run ... –v 宿主机目录(文件):容器内目录(文件) ...
Eg:docker run -id --name=c_mysql -v $PWD/conf:/etc/mysql/conf.d \
centos:7 /bin/bash
docker commit 容器id 镜像名称:版本号 从容器创建一个新的镜像
docker save -o 压缩文件名称 镜像名称:版本号 将指定镜像保存成 tar 归档文件
docker load –i 压缩文件名称 导入使用docker save命令导出的镜像
1、Docker logs 命令格式
docker logs 参数 容器名/容器id
参数说明:
–details 显示提供给日志的其他详细信息
–follow , -f 实时跟踪日志输出
–since 显示自某个timestamp之后的日志大于等于某个时间,或相对时间,如1h 就是1h)
–tail , -n all 从日志末尾显示的行数,默认值为all 全部
–timestamps , -t 日志每行显示日志时间戳
–until 显示自某个timestamp之前的日志小于等于某个时间,或相对时间,如30m(即30分钟)
用法示例
#通过容器名 docker logs tomcat8 #通过容器id docker logs fe734a934051 #通过容器id 显示时间戳 docker logs -t -n 5 fe734a934051 #查询最近1小时的日志最新10行日志 docker logs --tail=10 --since 1h tomcat8 #实时跟踪tomcat8日志每次显示最新15行日志 docker logs --follow --tail=15 tomcat8docker logs --f --tail=15 tomcat8 #查询tomcat8的日志内容包含error取最新5行,注意需要使用2>&1进行重定向 docker logs -n 5 tomcat8 2>&1 | grep 'error' #查询tomcat8的包含错误的日志并且写入 /usr/logs_error001.txt docker logs -t tomcat8 | grep 'error' >> /usr/logs_error001.txt #查询指定时间范围内的tomcat8的日志文件 取最新8行 docker logs -t -n 8 --since="2022-08-02T13:23:37" --until="2022-08-02T22:23:37" tomcat8
1.安装命令
docker run -d --name nginx-server -v /opt/nginx-server:/home/share/nginx/html:ro nginx:latest
2 获取安装好的nginx 访问地址
(其中的b73 是执行命令docker ps 后CONTAINER ID 下的前三个字母)
docker inspect b73 | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
3 .尝试访问
//如果访问不到说明/opt/nginx-server下没有index.html
curl http://172.17.0.2
//所以执行命令 添加一个index.html
echo "nginx is working" > /opt/nginx-server/index.html
//获取docker容器的元数据
docker inspect 26a
在docker host中暴露端口(80:80容器内部80端口映射到服务器80端口)
docker run -d -p 80:80 --name nginx-server-port -v /opt/nginx-server-port:/usr/share/nginx/html:ro nginx
echo "nginx is runing" > /opt/nginx-server-port/index.html
//查看防火墙状态
firewall-cmd --state
docker pull primetoninc/jdk:1.8
发现docker pull镜像慢的让人着急,我们来配置下镜像加速器(阿里云的容器镜像服务地址:容器镜像服务)
尝试进入/etc/docker目录,查看文件
发现没有daemon.json文件,那么我们创建该文件
touch daemon.json
写入配置
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxx.mirror.aliyuncs.com"]
}
EOF
刷新配置文件并重启docker服务
sudo systemctl daemon-reload
sudo systemctl restart docker
2.查看本地镜像
docker images
在上图中可以看到我们已经安装了jdk1.8 镜像
3.启动jdk容器(其中,–name=后的jdk1.8是为容器指定了一个别名,而最后的primetoninc/jdk:1.8那个指的是下载镜像时的名称。)该命令的意思为:使用docker镜像primetoninc/jdk:1.8以后台模式启动一个容器,并将容器命名为jdk1.8
docker run -it --name jdk1.8 -d primetoninc/jdk:1.8
4.查看docker进程是否启动成功
docker ps
4.进入jdk容器,查看是否安装正确(即查看安装之后的目录)
sudo docker exec -it jdk1.8 /bin/bash
查看jsk版本
java -version
[root@localhost ~]# docker exec -it jdk1.8 /bin/bash
[root@17169c7e0196 /]# java -version
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)
[root@17169c7e0196 /]#
//首先
docker pull tomcat:9.0
docker run -d --rm tomcat:9.0
//查看ip地址尝试访问
[root@localhost docker]# docker inspect e05 | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
[root@localhost docker]# curl http://172.17.0.2:8080
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.56</h3></body></html>[root@localhost docker]#
docker run -d -p 8888:8080 tomcat:9.0 [root@localhost docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7a3aa04a6d4f tomcat:9.0 "catalina.sh run" 48 seconds ago Up 47 seconds 0.0.0.0:8888->8080/tcp, :::8888->8080/tcp sleepy_almeida e05f35dff139 tomcat:9.0 "catalina.sh run" 14 minutes ago Up 13 minutes 8080/tcp inspiring_bell //获取宿主机ip [root@localhost docker]# ifconfig docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:5cff:fe4a:a4f5 prefixlen 64 scopeid 0x20<link> ether 02:42:5c:4a:a4:f5 txqueuelen 0 (Ethernet) RX packets 76 bytes 9308 (9.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 106 bytes 8018 (7.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.102 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::ed78:6afd:a940:d077 prefixlen 64 scopeid 0x20<link> inet6 2409:8a44:9f8:c250:cfca:9b21:23af:3049 prefixlen 64 scopeid 0x0<global> ether 94:de:80:fa:8d:5e txqueuelen 1000 (Ethernet) RX packets 1888638 bytes 2001631204 (1.8 GiB) RX errors 0 dropped 10561 overruns 0 frame 0 TX packets 1917946 bytes 353324158 (336.9 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 505515 bytes 2932976798 (2.7 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 505515 bytes 2932976798 (2.7 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 veth33f3bf2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::c8d3:93ff:feac:d2cc prefixlen 64 scopeid 0x20<link> ether ca:d3:93:ac:d2:cc txqueuelen 0 (Ethernet) RX packets 9 bytes 1312 (1.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 26 bytes 1911 (1.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 veth5edb12e: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet6 fe80::d0c5:62ff:fefb:340f prefixlen 64 scopeid 0x20<link> ether d2:c5:62:fb:34:0f txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 10 bytes 796 (796.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 //利用宿主机访问 [root@localhost docker]# curl http://192.168.1.102:8888 <!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.56</h3></body></html>[root@localhost docker]#
Execute a command in a running container [root@localhost local]# docker exec 588 ls /usr/local/tomcat BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs native-jni-lib temp webapps webapps.dist work
docker run -d -p 8087:8080 -v /opt/tomcat-server:/usr/local/tomcat/webapps/ROOT tomcat:9.0
// 查看容器中/usr/local/tomcat/webapps/下的Root是否创建
[root@localhost local]# docker exec 5cd ls /usr/local/tomcat/webapps/
ROOT
//查看宿主机下的/opt/的tomcat-server是否被创建
[root@localhost local]# ls /opt/
containerd gitlab nginx-server nginx-server-port redis-6.0.6 redis-6.0.6.tar.gz tomcat-server
//再次测试宿主机连接成功
[root@localhost ~]# curl http://192.168.1.102:8087
tomcat running
docker pull mysql:8.0
//并且创建挂载目录
mkdir -p /data/mysql/conf
mkdir -p /data/mysql/data
mkdir -p /data/mysql/logs
vi /data/mysql/conf/my.cnf
[client] port = 3308 default-character-set = utf8mb4 [mysql] port = 3308 default-character-set = utf8mb4 [mysqld] # bind-address = 0.0.0.0 # port = 3306 max_connections=10000 character-set-server = utf8mb4 collation-server = utf8mb4_unicode_ci # 设置时区和字符集 # default-time-zone='+8:00' character-set-client-handshake=FALSE init_connect='SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci' gtid-mode=ON enforce-gtid-consistency = ON
docker run --restart=always --name mysql8.0 -v /data/mysql/conf:/etc/mysql -v /data/mysql/data:/var/lib/mysql -v /data/mysql/log:/var/log -v /data/mysql/mysql-files:/var/lib/mysql-files -p 3308:3306 -e MYSQL_ROOT_PASSWORD='123456' -d mysql:8.0
docker exec -it mysql8.0 bash root@8e06ede28f4b:/# 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 22 Server version: 8.0.26 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>
mysql> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> SELECT Host, User, plugin from user; +-----------+------------------+-----------------------+ | Host | User | plugin | +-----------+------------------+-----------------------+ | % | root | caching_sha2_password | | localhost | mysql.infoschema | caching_sha2_password | | localhost | mysql.session | caching_sha2_password | | localhost | mysql.sys | caching_sha2_password | | localhost | root | caching_sha2_password | +-----------+------------------+-----------------------+ 5 rows in set (0.00 sec) mysql>
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
FLUSH PRIVILEGES;
yum -y install mariadb [root@localhost opt]# mysql -h 192.168.1.102 -uroot -p123456 -P 3308 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 12 Server version: 8.0.27 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>
4.1 mysql 主节点部署
docker run --restart=always --name mysql-master
-v /data/mysql-master/conf:/etc/mysql
-v /data/mysql-master/data:/var/lib/mysql
-v /data/mysql-master/log:/var/log
-v /data/mysql-master/mysql-files:/var/lib/mysql-files -p 3309:3306 -e MYSQL_ROOT_PASSWORD='123456' -d mysql:8.0
[root@localhost opt]# docker run --restart=always --name mysql-master -v /data/mysql-master/conf:/etc/mysql -v /data/mysql-master/data:/var/lib/mysql -v /data/mysql-master/log:/var/log -v /data/mysql-master/mysql-files:/var/lib/mysql-files -p 3309:3306 -e MYSQL_ROOT_PASSWORD='123456' -d mysql:8.0 496a562290d7a4d7efedc278c0397849949f94ee568f73ff6316a1805f570ece [root@localhost opt]# mysql -h 192.168.1.102 -uroot -p123456 -P 3309 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.102:3309' (111) [root@localhost opt]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 496a562290d7 mysql:8.0 "docker-entrypoint.s…" 2 minutes ago Up About a minute 33060/tcp, 0.0.0.0:3309->3306/tcp, :::3309->3306/tcp mysql-master a799539c27b9 mysql:8.0 "docker-entrypoint.s…" 38 minutes ago Up 34 minutes 33060/tcp, 0.0.0.0:3308->3306/tcp, :::3308->3306/tcp mysql8.0 5cdf674e135c tomcat:9.0 "catalina.sh run" 24 hours ago Up 22 hours 0.0.0.0:8087->8080/tcp, :::8087->8080/tcp charming_liskov 17169c7e0196 primetoninc/jdk:1.8 "/bin/bash" 24 hours ago Up 22 hours jdk1.8 43dcc8fa9c10 nginx "/docker-entrypoint.…" 37 hours ago Up 22 hours 0.0.0.0:80->80/tcp, :::80->80/tcp nginx-server-port [root@localhost opt]# mysql -h 192.168.1.102 -uroot -p123456 -P 3309 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 8 Server version: 8.0.27 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>
4.2 mysql 主节点配置
编写 # vim /data/mysql-master/conf.my.cnf # cat /data/mysql-master/conf.my.cnf [client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqlId] init_connect="set collation_connection = utf8_unicode_ci" init_connect='set NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake skip-name-resolve server_id=1 log-bin=mysql-bin read-only=0 binlog-do-db=kubemsb_test replicate-ignore-db=mysql replicate-ignore-db=sys replicate-ignore-db=information-schema replicae-ignore-db=performance-schema
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。