查看内核版本
1
2
3
4
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@localhost ~]# uname -r
3.10.0-1160.el7.x86_64
Install Docker Engine on CentOS
1
2
3
4
5
6
7
8
9
10
11
12
13
https://docs.docker.com/engine/install/centos/
[root@localhost ~]# yum -y install gc
[root@localhost ~]# yum -y install gcc-c++
[root@localhost ~]# yum install -y yum-utils
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# yum makecache fast
[root@localhost ~]# yum -y install docker-ce docker-ce-cli containerd.io
[root@localhost ~]# systemctl start docker
[root@localhost ~]# ps -ef | grep docker
[root@localhost ~]# docker version
[root@localhost ~]# docker run hello-world
Uninstall Docker Engine
1
2
3
4
[root@localhost ~]# systemctl stop docker
[root@localhost ~]# yum remove docker-ce docker-ce-cli containerd.io
[root@localhost ~]# rm -rf /var/lib/docker
[root@localhost ~]# rm -rf /var/lib/containerd
镜像加速
阿里云 容器镜像服务 镜像加速器 ,按照文档指示执行相关操作
[root@localhost ~]# docker run hello-world
帮助启动类命令
1
2
3
4
5
6
7
8
9
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl stop docker
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# systemctl status docker
[root@localhost ~]# systemctl enable docker
[root@localhost ~]# systemctl disable docker
[root@localhost ~]# docker info
[root@localhost ~]# docker --help
[root@localhost ~]# docker 具体命令 --help
镜像命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~]# docker images
[root@localhost ~]# docker images -a
[root@localhost ~]# docker images -q
[root@localhost ~]# docker search --limit 5 hello-world
[root@localhost ~]# docker pull hello-world
[root@localhost ~]# docker pull redis
[root@localhost ~]# docker pull ubuntu
[root@localhost ~]# docker pull redis:6.0.8
[root@localhost ~]# docker images -a
[root@localhost ~]# docker system df
[root@localhost ~]# docker rmi -f d2c94e258dcb
[root@localhost ~]# docker rmi -f $(docker images -qa) //慎用,删除所有镜像
容器命令
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[root@localhost ~]# docker run -it ubuntu /bin/bash
[root@localhost ~]# docker ps
[root@localhost ~]# docker run -it --name test ubuntu bash
[root@localhost ~]# docker ps -a
[root@localhost ~]# docker ps -l
[root@localhost ~]# docker ps -n 2
[root@localhost ~]# docker ps -q
exit 退出容器,容器停止
ctrl + p + q 退出容器,容器不停止
[root@localhost ~]# docker ps -a //罗列所有的容器
[root@localhost ~]# docker start 939c38739633
[root@localhost ~]# docker restart 939c38739633
[root@localhost ~]# docker kill 939c38739633
[root@localhost ~]# docker stop 939c38739633
[root@localhost ~]# docker rm 939c38739633
[root@localhost ~]# docker rm -f c1d6cbd56aba //强制删除
[root@localhost ~]# docker rm -f $(docker ps -aq) //删除所有容器 慎用
[root@localhost ~]# docker ps -a -q | xargs docker rm //删除所有容器 慎用
[root@localhost ~]# docker pull redis:6.0.8
[root@localhost ~]# docker run -it redis:6.0.8 //退出即停止
[root@localhost ~]# docker run -d redis:6.0.8 //后台运行
[root@localhost ~]# docker logs e4e30b34eb92
[root@localhost ~]# docker top e4e30b34eb92
[root@localhost ~]# docker inspect e4e30b34eb92
[root@localhost ~]# docker exec -it e4e30b34eb92 /bin/bash //退出后台运行
[root@localhost ~]# docker attach 8ba72b3c1377 //退出即停止
[root@localhost ~]# docker exec -it 76d16921efca redis-cli
[root@localhost ~]# docker cp 76d16921efca:/data/b.txt /root/bbb.txt
[root@localhost ~]# docker cp /root/a.txt 76d16921efca:/data/aaa.txt
[root@localhost ~]# docker export 76d16921efca > redis608.tar
[root@localhost ~]# cat redis608.tar | docker import - baoguo/redis:1.2
[root@localhost ~]# docker images
[root@localhost ~]# docker commit -m="vim cmd add ok" -a="baoguo" 0458cec3c25a baoguo/myubuntu:1.1
Registry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 公网Registry
可以把镜像上传到阿里云镜像服务器,具体操作,登录阿里云查看。 Docker Registry
# 私有的Registry
[root@localhost ~]# docker pull registry
[root@localhost ~]# docker run -d -p 5000:5000 -v /root/myregistry/:/tmp/registry --privileged=true registry
[root@localhost ~]# docker run -it ubuntu /bin/bash
root@22a1da75fcfa:/# apt-get update
root@22a1da75fcfa:/# apt-get install net-tools
[root@localhost ~]# docker commit -m="net-tools install" -a="baoguo" 22a1da75fcfa baoguo/myubuntu:2.2
[root@localhost ~]# docker images
[root@localhost ~]# curl -XGET http://172.16.52.128:5000/v2/_catalog
[root@localhost ~]# docker tag baoguo/myubuntu:2.2 172.16.52.128:5000/myubuntu:2.2
[root@localhost ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://86o6qk1h.mirror.aliyuncs.com"],
"insecure-registries": ["172.16.52.128:5000"]
}
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# systemctl status docker
[root@localhost ~]# docker images
[root@localhost ~]# docker push 172.16.52.128:5000/myubuntu:2.2
[root@localhost ~]# curl -XGET http://172.16.52.128:5000/v2/_catalog
[root@localhost ~]# docker rmi -f 172.16.52.128:5000/myubuntu:2.2
[root@localhost ~]# docker pull 172.16.52.128:5000/myubuntu:2.2
[root@localhost ~]# docker run -d -it 172.16.52.128:5000/myubuntu:2.2 /bin/bash
[root@localhost ~]# docker exec -it cb504591257e /bin/bash
容器数据卷
1
2
3
4
5
6
7
[root@localhost ~]# docker run -it --privileged=true -v /root:/tmp ubuntu
[root@localhost ~]# docker inspect ee6548a3810c
[root@localhost ~]# docker run -it --privileged=true -v /root:/tmp:rw ubuntu //默认包括rw
[root@localhost ~]# docker run -it --privileged=true -v /root:/tmp:ro ubuntu //read only
[root@localhost ~]# docker run -it --privileged=true -v /root:/tmp --name u1 ubuntu /bin/bash
[root@localhost ~]# docker run -it --privileged=true --volumes-from u1 --name u2 ubuntu /bin/bash
docker 常见软件的安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
搜索镜像
拉取镜像
查看镜像
启动镜像
停止容器
移除容器
[root@localhost ~]# docker pull tomcat
[root@localhost ~]# docker images tomcat
[root@localhost ~]# docker run -it -p 8080:8080 tomcat
http://172.16.52.128:8080
[root@localhost ~]# docker exec -it 3035ef34ac7b /bin/bash
root@3035ef34ac7b:/usr/local/tomcat# rm -rf webapps
root@3035ef34ac7b:/usr/local/tomcat# mv webapps.dist webapps
就可以访问页面了!!!
或者使用下面的方法
[root@localhost ~]# docker run -d -p 8080:8080 --name mytomcat8 billygoo/tomcat8-jdk8
[root@localhost ~]# docker pull mysql:5.7
[root@localhost ~]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
[root@localhost ~]# docker ps
[root@localhost ~]# docker exec -it 557cf4b7a683 /bin/bash
root@557cf4b7a683:/# mysql -uroot -p
mysql> SHOW VARIABLES LIKE 'character%';
[root@localhost ~]# docker run -d -p 3306:3306 --privileged=true -v /root/mysql/log:/var/log/mysql -v /root/mysql/data:/var/lib/mysql -v /root/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7
[root@localhost ~]# cd /root/mysql/conf/
[root@localhost conf]# vim my.cnf
[root@localhost conf]# cat my.cnf
[client]
default_character_set=utf8
[mysqld]
collation_server=utf8_general_ci
character_set_server=utf8
[root@localhost conf]# docker restart mysql
[root@localhost conf]# docker exec -it mysql bash
root@8d44a40748c4:/# mysql -uroot -p
mysql> SHOW VARIABLES LIKE 'character%';
[root@localhost ~]# docker pull redis:6.0.8
[root@localhost ~]# docker run -d -p 6379:6379 redis:6.0.8
[root@localhost ~]# docker exec -it c84f153a97de /bin/bash
root@c84f153a97de:/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>
[root@localhost redis]# docker run -p 6379:6379 --name myredis --privileged=true -v /root/redis/redis.conf:/etc/redis/redis.conf -v /root/redis/data:/data -d redis:6.0.8 redis-server /etc/redis/redis.conf
在这个地方 redis.conf的有些配置需要去调整一下,比如:
注释掉 # bind 127.0.0.1 -::1
daemonize no 设置成no
appendonly yes 设置成yes
databases 10 //通过调整数据的数量,来查看配置文件是否是正确的
127.0.0.1:6379> select 9
mysql 主从复制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
docker run -p 3307:3306 --name mysql-master \
-v /mydata/mysql-master/log:/var/log/mysql \
-v /mydata/mysql-master/data:/var/lib/mysql \
-v /mydata/mysql-master/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
[root@localhost redis]# vim /mydata/mysql-master/my.cnf
[root@localhost redis]# cat /mydata/mysql-master/my.cnf
[mysqld]
server_id=101
binlog-ignore-db=mysql
log-bin=mall-mysql-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
[root@localhost redis]# docker restart mysql-master
[root@localhost redis]# docker ps
[root@localhost redis]# docker exec -it mysql-master /bin/bash
root@6c054b36b6da:/# mysql -uroot -proot
mysql> CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'slave'@'%';
docker run -p 3308:3306 --name mysql-slave \
-v /mydata/mysql-slave/log:/var/log/mysql \
-v /mydata/mysql-slave/data:/var/lib/mysql \
-v /mydata/mysql-slave/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
[root@localhost conf]# vim /mydata/mysql-slave/conf/my.cnf
[root@localhost conf]# cat my.cnf
[mysqld]
server_id=102
binlog-ignore-db=mysql
log-bin=mall-mysql-slave1-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
relay_log=mall-mysql-relay-bin
log_slave_updates=1
read_only=1
[root@localhost conf]# docker restart mysql-slave
[root@localhost conf]# docker ps
[root@localhost conf]# docker exec -it mysql-master /bin/bash
root@6c054b36b6da:/# mysql -uroot -proot
mysql> show master status;
[root@localhost redis]# docker exec -it mysql-slave /bin/bash
root@77e8f7a776d0:/# mysql -uroot -proot
mysql> change master to master_host='172.16.52.128',master_user='slave',master_password='123456',master_port=3307, master_log_file='mall-mysql-bin.000001',master_log_pos=617,master_connect_retry=30;
mysql> show slave status \G;
Slave_IO_Running: No
Slave_SQL_Running: No
mysql> start slave;
mysql> show slave status \G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
---
master 操作
mysql> create database t1;
Query OK, 1 row affected (0.00 sec)
mysql> use t1;
Database changed
mysql> create table u1(id int, name varchar(32));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into u1 values (1,'z3');
Query OK, 1 row affected (0.02 sec)
slave 操作
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| t1 |
+--------------------+
5 rows in set (0.00 sec)
mysql> use t1;
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> show tables;
+--------------+
| Tables_in_t1 |
+--------------+
| u1 |
+--------------+
1 row in set (0.00 sec)
mysql> select * from u1;
+------+------+
| id | name |
+------+------+
| 1 | z3 |
+------+------+
1 row in set (0.00 sec)
可以看到在主表做了操作之后,从表也是可以看到的。
redis 集群
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
[root@localhost ~]# systemctl start docker
[root@localhost ~]# docker run -d --name redis-node-1 --net host --privileged=true -v /data/redis/share/redis-node-1:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381
docker run -d --name redis-node-2 --net host --privileged=true -v /data/redis/share/redis-node-2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382
docker run -d --name redis-node-3 --net host --privileged=true -v /data/redis/share/redis-node-3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383
docker run -d --name redis-node-4 --net host --privileged=true -v /data/redis/share/redis-node-4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384
docker run -d --name redis-node-5 --net host --privileged=true -v /data/redis/share/redis-node-5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385
docker run -d --name redis-node-6 --net host --privileged=true -v /data/redis/share/redis-node-6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386
[root@localhost ~]# docker exec -it redis-node-1 /bin/bash
root@localhost:/data# redis-cli --cluster create 172.16.52.128:6381 172.16.52.128:6382 172.16.52.128:6383 172.16.52.128:6384 172.16.52.128:6385 172.16.52.128:6386 --cluster-replicas 1
root@localhost:/data# redis-cli -p 6381
127.0.0.1:6381> keys *
(empty array)
127.0.0.1:6381> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:210
cluster_stats_messages_pong_sent:224
cluster_stats_messages_sent:434
cluster_stats_messages_ping_received:219
cluster_stats_messages_pong_received:210
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:434
127.0.0.1:6381> cluster nodes
0e7435f5ed13714e62eb0da0ddc0d9036939ebbf 172.16.52.128:6381@16381 myself,master - 0 1709019758000 1 connected 0-5460
6d0d278d9ef3e91a6041f8a71f90b13a0a1e1ed4 172.16.52.128:6384@16384 slave a06367dc5096ce29e3571337b7a15e7adf707441 0 1709019759000 3 connected
159ff4633faa99063c0a2233757150f2098186e3 172.16.52.128:6386@16386 slave 046c6f06943d71fae987219e67c9a60393d309e6 0 1709019759169 2 connected
046c6f06943d71fae987219e67c9a60393d309e6 172.16.52.128:6382@16382 master - 0 1709019761178 2 connected 5461-10922
a06367dc5096ce29e3571337b7a15e7adf707441 172.16.52.128:6383@16383 master - 0 1709019759000 3 connected 10923-16383
4be3c296f86b2ac0ee082e5386d547350acc7860 172.16.52.128:6385@16385 slave 0e7435f5ed13714e62eb0da0ddc0d9036939ebbf 0 1709019760000 1 connected
127.0.0.1:6381>
[root@localhost ~]# docker exec -it redis-node-1 /bin/bash
root@localhost:/data# redis-cli -p 6381 -c
172.16.52.128:6383> FLUSHALL
172.16.52.128:6383> set k1 v1
OK
172.16.52.128:6383> set k2 v2
-> Redirected to slot [449] located at 172.16.52.128:6381
OK
root@localhost:/data# redis-cli --cluster check 172.16.52.128:6381
宕机迁移
[root@localhost ~]# docker stop redis-node-1
[root@localhost ~]# docker exec -it redis-node-2 /bin/bash
root@localhost:/data# redis-cli -p 6385 -c
127.0.0.1:6385> cluster nodes
127.0.0.1:6385> get k1
[root@localhost ~]# docker start redis-node-1
172.16.52.128:6383> cluster nodes
扩容
docker run -d --name redis-node-7 --net host --privileged=true -v /data/redis/share/redis-node-7:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6387
docker run -d --name redis-node-8 --net host --privileged=true -v /data/redis/share/redis-node-8:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6388
[root@localhost ~]# docker ps
[root@localhost ~]# docker exec -it redis-node-7 /bin/bash
root@localhost:/data# redis-cli --cluster add-node 172.16.52.128:6387 172.16.52.128:6381
root@localhost:/data# redis-cli --cluster check 172.16.52.128:6381
root@localhost:/data# redis-cli --cluster reshard 172.16.52.128:6381
16384/master台数=4096
node ID放最新的master 的ID
然后选择all
root@localhost:/data# redis-cli --cluster check 172.16.52.128:6381
root@localhost:/data# redis-cli --cluster add-node 172.16.52.128:6388 172.16.52.128:6387 --cluster-slave --cluster-master-id 95857e6e15dcea12c7f6dd8e4f4dca16aed5e2b6
缩容
root@localhost:/data# redis-cli --cluster del-node 172.16.52.128:6388 57f3668e363000840c279560f5a3c140d812bcf7 //删除6388从节点
root@localhost:/data# redis-cli --cluster reshard 172.16.52.128:6381 //重新分配
注意:大小是4096,接受的是6381 ,发送的是6387
root@localhost:/data# redis-cli --cluster check 172.16.52.128:6381
root@localhost:/data# redis-cli --cluster del-node 172.16.52.128:6387 95857e6e15dcea12c7f6dd8e4f4dca16aed5e2b6 //删除6387主节点
root@localhost:/data# redis-cli --cluster check 172.16.52.128:6381
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
docs.docker.com/engine/reference/builder/
https://github.com/docker-library/tomcat
https://www.oracle.com/java/technologies/downloads/#java8
x64 Compressed Archive
https://mirrors.yangxingzhen.com/jdk/
[root@localhost myfile]# cat Dockerfile
FROM rockylinux:8
MAINTAINER baoguo<baoguo.ding@foxmail.com>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
RUN yum -y install glibc.i686
RUN mkdir /usr/local/java
ADD jdk-8u401-linux-x64.tar /usr/local/java/
ENV JAVA_HOME /usr/local/java/jdk1.8.0_401
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH
EXPOSE 80
CMD echo $MYPATH
CMD echo "success--------ok"
CMD /bin/bash
[root@localhost myfile]# docker build -t centosjava8:1.5 .
虚悬镜像
[root@localhost myfile2]# cat Dockerfile
from ubuntu
CMD echo 'action is success'
[root@localhost myfile2]# docker build .
[root@localhost myfile2]# docker image ls -f dangling=true //查看虚悬镜像
[root@localhost myfile2]# docker image prune //删除虚悬镜像
Docker 微服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost mydocker]# cat Dockerfile
FROM openjdk:11
MAINTAINER baoguo
VOLUME /tmp
ADD demo-0.0.1-SNAPSHOT.jar docker.jar
RUN bash -c 'touch /docker.jar'
ENTRYPOINT ["java", "-jar","/docker.jar"]
EXPOSE 6001
[root@localhost mydocker]# docker build -t baoguo_docker:1.6 .
[root@localhost mydocker]# docker run -d -it -p 6001:6001 baoguo_docker:1.6 /bin/bash
http://172.16.52.130:6001/order/index
curl http://172.16.52.130:6001/order/index
Docker 网络
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
b9e28be4a7b7 bridge bridge local
fba5a4d6518f host host local
1cdf2d4d2374 none null local
[root@localhost ~]# docker network create aa_network
e7a10f9eb533291274b6a875e60448e31510fdcd7172ce7300cfcc40b6ce98b5
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
e7a10f9eb533 aa_network bridge local
b9e28be4a7b7 bridge bridge local
fba5a4d6518f host host local
1cdf2d4d2374 none null local
[root@localhost ~]# docker network rm aa_network
aa_network
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
b9e28be4a7b7 bridge bridge local
fba5a4d6518f host host local
1cdf2d4d2374 none null local
[root@localhost ~]# docker network inspect b9e28be4a7b7
bridge模式:常用模式,每个Docker有自己独立的IP地址
host模式:公用主机网络
none模式:一般不用
container模式:使用另外一个容器的网络
[root@localhost ~]# docker inspect 7ae5d643d3c3 | tail -n 20
案例 - bridge
[root@localhost ~]# docker run -d -p 8081:8080 --name tomcat81 billygoo/tomcat8-jdk8
[root@localhost ~]# docker run -d -p 8082:8080 --name tomcat82 billygoo/tomcat8-jdk8
案例 - host
[root@localhost ~]# docker run -d --network host --name tomcat83 billygoo/tomcat8-jdk8
[root@localhost ~]# docker exec -it tomcat83 bash
root@localhost:/usr/local/tomcat# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:5c:03:00 brd ff:ff:ff:ff:ff:ff
inet 172.16.52.130/24 brd 172.16.52.255 scope global noprefixroute dynamic ens33
valid_lft 1303sec preferred_lft 1303sec
inet6 fe80::c192:bb34:da96:5f96/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:4e:60:ac brd ff:ff:ff:ff:ff:ff
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:4e:60:ac brd ff:ff:ff:ff:ff:ff
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:cf:05:8a:fc brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:cfff:fe05:8afc/64 scope link
valid_lft forever preferred_lft forever
42: vethb16d7ee@if41: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether 02:dd:bf:f8:b9:80 brd ff:ff:ff:ff:ff:ff link-netnsid 3
inet6 fe80::dd:bfff:fef8:b980/64 scope link
valid_lft forever preferred_lft forever
44: vethd6f2b8d@if43: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether ae:d1:72:55:74:bd brd ff:ff:ff:ff:ff:ff link-netnsid 4
inet6 fe80::acd1:72ff:fe55:74bd/64 scope link
valid_lft forever preferred_lft forever
和主机一模一样
http://172.16.52.130:8080
主机IP加port直接访问,记得关闭防火墙
None模式
[root@localhost ~]# docker run -d -p 8084:8080 --network none --name tomcat84 billygoo/tomcat8-jdk8
Container网络模式
[root@localhost ~]# docker run -it --name alpine1 alpine /bin/sh
[root@localhost ~]# docker run -it --network container:alpine1 --name alpine2 alpine /bin/sh
[root@localhost ~]# docker inspect alpine1 | tail -n 20
[root@localhost ~]# docker inspect alpine2 | tail -n 20
[root@localhost ~]# docker exec -it alpine1 /bin/sh
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
45: eth0@if46: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
/ # exit
[root@localhost ~]# docker exec -it alpine2 /bin/sh
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
45: eth0@if46: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
[root@localhost ~]# docker stop alpine1
alpine1
[root@localhost ~]# docker exec -it alpine2 /bin/sh
/ # ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
/ #
自定义网络
[root@localhost ~]# docker network create baoguo_network
[root@localhost ~]# docker run -d -p 8081:8080 --network baoguo_network --name tomcat81 billygoo/tomcat8-jdk8
1545ce812cab6a8d0119c997f541171e5414820b78d18825c18a67038033ffb9
[root@localhost ~]# docker run -d -p 8082:8080 --network baoguo_network --name tomcat82 billygoo/tomcat8-jdk8
82161d42aed6ef5c72f6b0731ad2cde064cbb2696cc06bf14ec131f0f7f92ce3
[root@localhost ~]# docker exec -it tomcat81 /bin/bash
root@1545ce812cab:/usr/local/tomcat# ping tomcat82
PING tomcat82 (172.19.0.3) 56(84) bytes of data.
64 bytes from tomcat82.baoguo_network (172.19.0.3): icmp_seq=1 ttl=64 time=0.119 ms
64 bytes from tomcat82.baoguo_network (172.19.0.3): icmp_seq=2 ttl=64 time=0.069 ms
root@1545ce812cab:/usr/local/tomcat#
Docker-compose 容器编排
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
https://docs.docker.com/compose/compose-file/compose-file-v3/
https://docs.docker.com/compose/install/
[root@localhost ~]# curl -SL https://github.com/docker/compose/releases/download/v2.24.6/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
[root@localhost ~]# mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose
[root@localhost ~]# docker-compose --version
Docker Compose version v2.24.6
docker-compose.yml
services:
microService:
image: baoguo_docker:1.6
container_name: ms01
ports:
- "6001:6001"
volumnes:
- /app/microService:/data
networks:
- baoguo_net
depends_on:
- redis
- mysql
redis:
image: redis:6.0.8
ports:
- "6379:6379"
volumes:
- /app/redis/redis.conf:/etc/redis/redis.conf
- /app/redis/data:/data
networks:
- baoguo_net
command: redis-server /etc/redis/redis.conf
mysql:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: '123456'
MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
MYSQL_DATABASE: 'db2024'
MYSQL_USER: 'root'
MYSQL_PASSWORD: '123456'
ports:
- "3306:3306"
volumes:
- /app/mysql/db:/var/lib/mysql
- /app/mysql/conf/my.cnf:/etc/my.cnf
- /app/mysql/init:/docker-entrypoint-initdb.d
networks:
- baoguo_net
command: --default-authentication-plugin=mysql_native_password
networks:
baoguo_net:
检验 yml 文件是否正确
[root@localhost ~]# docker-compose config -q
[root@localhost ~]# docker-compose up -d
[root@localhost ~]# docker-compose stop
Docker 轻量级可视化工具Portainer
1
2
3
4
5
6
7
8
9
10
用于监控和统计
https://www.portainer.io/
https://docs.portainer.io/v/ce-2.9/start/install/server/docker/linux
[root@localhost docker-compose]# docker run -d -p 8000:8000 -p 9000:9000 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest
http://172.16.52.131:9000/#!/init/admin
Docker 容器监控之 CAdvisor + InfluxDB + Granfana
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
相当于java 的ELK
CAdvisor 监控收集
InfluxDB 存储数据
Granfana 展示图标
[root@localhost ~]# docker stats
[root@localhost cig]# cat docker-compose.yml
version: '3.1'
volumes:
grafana_data: {}
services:
influxdb:
image: tutum/influxdb:0.9
restart: always
environment:
- PRE_CREATE_DB=cadvisor
ports:
- "8083:8083"
- "8086:8086"
volumes:
- ./data/influxdb:/data
cadvisor:
image: google/cadvisor
links:
- influxdb:influxsrv
command: -storage_driver=influxdb -storage_driver_db=cadvisor -storage_driver_host=influxsrv:8086
restart: always
ports:
- "8080:8080"
volumes:
- /:/rootfs:ro
- /var/run:/var/run:rw
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
grafana:
user: "104"
image: grafana/grafana
restart: always
links:
- influxdb:influxsrv
ports:
- "3000:3000"
volumes:
- grafana_data:/var/lib/grafana
environment:
- HTTP_USER=admin
- HTTP_PASS=admin
- INFLUXDB_HOST=influxsrv
- INFLUXDB_PORT=8086
- INFLUXDB_NAME=cadvisor
- INFLUXDB_USER=root
- INFLUXDB_PASS=root
[root@localhost cig]# docker-compose up
收集 http://172.16.52.131:8080/containers/
存储 http://172.16.52.131:8083
展现 http://172.16.52.131:3000/login admin-admin