Docker入门详细教程-进阶使用
程序员文章站
2022-06-17 08:56:32
...
DockerFile
指令
- FROM: 设置基础镜像, 祖先为scratch
- RUN: 构建容器时运行命令
- EXPOSE: 容器对外暴露端口
- WORKDIR: 默认工作目录
- ADD: 将宿主机文件拷贝+压缩
- COPY: 拷贝文件
- VALUME: 设置容器数据卷
- CMD: 指定容器启动时运行命令, 可以多个但只有最后一个生效, 会被docker run之后的参数替换
- ENTRYPOINT: 指定容器启动时运行命令
- ONBUILD: 当构建一个被继承的Dockerfile时运行命令, 父镜像在被继承后触发
生成镜像
docker build -f Dockerfile -t 新镜像名:TAG
docker build -f /mydocker/Dockerfile -t soul/centos:1.0 .
docker run -it soul/centos
查看镜像变更历史
docker history 镜像ID
生成新镜像
#docker commit 提交容器副本成为一个新的镜像
docker commit -m="描述" -a="作者" 容器ID 镜像名:版本
docker commit -m="my gocentos" -a="soul" 04ac5ae5d5fb soul/gocentos:1.0
提交镜像到阿里云
登录阿里云Docker Registry
$ sudo docker login --username=uisoul registry.cn-shenzhen.aliyuncs.com
用于登录的用户名为阿里云账号全名,密码为开通服务时设置的密码。
您可以在访问凭证页面修改凭证密码。
从Registry中拉取镜像
$ sudo docker pull registry.cn-shenzhen.aliyuncs.com/uisoul/gocentos:[镜像版本号]
将镜像推送到Registry
$ sudo docker login --username=uisoul registry.cn-shenzhen.aliyuncs.com
$ sudo docker tag [ImageId] registry.cn-shenzhen.aliyuncs.com/uisoul/gocentos:[镜像版本号]
$ sudo docker push registry.cn-shenzhen.aliyuncs.com/uisoul/gocentos:[镜像版本号]
请根据实际镜像信息替换示例中的[ImageId]和[镜像版本号]参数。
自定义网络
查看帮助
docker network --help
默认带网络参数
# 以下两个命令效果相同
docker run -d -P --name tomcat01 tomcat
docker run -d -P --name tomcat01 --net bridge tomcat
创建自定义网络
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
使用自定义网络启动容器
docker run -d -P --name tomcat-net-01 --net mynet tomcat
docker run -d -P --name tomcat-net-02 --net mynet tomcat
网络连通
查看帮助
docker network connect --help
使容器连通自定义网络 (一个容器两个IP)
docker network connect mynet tomcat01
测试连接
docker exec -it tomcat01 ping tomcat-net-01
Redis集群
创建自定义网络
docker network create redis --subnet 172.18.0.0/16
通过脚本创建6个redis配置
for port in $(seq 1 6);\
do \
mkdir -p /data/redis/node-${port}/conf
touch /data/redis/node-${port}/conf/redis.conf
cat << EOF >> /data/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.18.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
启动6个redis服务
docker run -p 6371:6379 -p 16671:16379 --name redis-1 \
-v /data/redis/node-1/data:/data \
-v /data/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.18.0.11 redis:5.0.10 redis-server /etc/redis/redis.conf
# node-2
docker run -p 6372:6379 -p 16672:16379 --name redis-2 \
-v /data/redis/node-2/data:/data \
-v /data/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.18.0.12 redis:5.0.10 redis-server /etc/redis/redis.conf
# node-3
docker run -p 6373:6379 -p 16673:16379 --name redis-3 \
-v /data/redis/node-3/data:/data \
-v /data/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.18.0.13 redis:5.0.10 redis-server /etc/redis/redis.conf
# node-4
docker run -p 6374:6379 -p 16674:16379 --name redis-4 \
-v /data/redis/node-4/data:/data \
-v /data/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.18.0.14 redis:5.0.10 redis-server /etc/redis/redis.conf
# node-5
docker run -p 6375:6379 -p 16675:16379 --name redis-5 \
-v /data/redis/node-5/data:/data \
-v /data/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.18.0.15 redis:5.0.10 redis-server /etc/redis/redis.conf
# node-6
docker run -p 6376:6379 -p 16676:16379 --name redis-6 \
-v /data/redis/node-6/data:/data \
-v /data/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.18.0.16 redis:5.0.10 redis-server /etc/redis/redis.conf
通过脚本运行六个redis
for port in $(seq 1 6);\
docker run -p 637${port}:6379 -p 1667${port}:16379 --name redis-${port} \
-v /data/redis/node-${port}/data:/data \
-v /data/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.18.0.1${port} redis:6.0.9 redis-server /etc/redis/redis.conf
异常错误
WARNING: IPv4 forwarding is disabled. Networking will not work.
解决
[[email protected] /]# vi /usr/lib/sysctl.d/00-system.conf
#添加
net.ipv4.ip_forward=1
#重启
[[email protected] /]# systemctl restart network
创建集群
#进入其中一台
docker exec -it redis-1 /bin/sh
redis-cli --cluster create 172.18.0.11:6379 172.18.0.12:6379 172.18.0.13:6379 172.18.0.14:6379 172.18.0.15:6379 172.18.0.16:6379 --cluster-replicas 1
连接集群
# redis-cli -c
# 查看集群信息
127.0.0.1:6379> 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:175
cluster_stats_messages_pong_sent:180
cluster_stats_messages_sent:355
cluster_stats_messages_ping_received:175
cluster_stats_messages_pong_received:175
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:355
# 查看节点
127.0.0.1:6379> cluster nodes
3d8d3e954b26161832eb68d004aa4b625a13026d 172.18.0.14:[email protected] slave 8ccbabfefb754804d31ed4151f6322229a0fe04d 0 1607074040540 4 connected
d038a7540b3b9477a2a715ef7e9c9c6938dfcc48 172.18.0.16:[email protected] slave e5ccfb60ea29c6a8d3366685fb82ff5df246eac9 0 1607074041149 6 connected
e5ccfb60ea29c6a8d3366685fb82ff5df246eac9 172.18.0.12:[email protected] master - 0 1607074041000 2 connected 5461-10922
8ccbabfefb754804d31ed4151f6322229a0fe04d 172.18.0.13:[email protected] master - 0 1607074041000 3 connected 10923-16383
b839fc0c45b3bf7fcfcc2d949035e6e0b9cacd83 172.18.0.11:[email protected] myself,master - 0 1607074040000 1 connected 0-5460
f613bbd183cbd6bf6436179b609845085a0d1b16 172.18.0.15:[email protected] slave b839fc0c45b3bf7fcfcc2d949035e6e0b9cacd83 0 1607074041663 5 connected
# 设置一个值
127.0.0.1:6379> set aaa 100
-> Redirected to slot [10439] located at 172.18.0.12:6379
OK
172.18.0.12:6379> get aaa
"100"
停止master172.18.0.12
docker stop redis-2
重新链接查看状态
# redis-cli -c
127.0.0.1:6379> get aaa
-> Redirected to slot [10439] located at 172.18.0.16:6379
"100"
172.18.0.16:6379> cluster nodes
d038a7540b3b9477a2a715ef7e9c9c6938dfcc48 172.18.0.16:[email protected] myself,master - 0 1607074285000 7 connected 5461-10922
f613bbd183cbd6bf6436179b609845085a0d1b16 172.18.0.15:[email protected] slave b839fc0c45b3bf7fcfcc2d949035e6e0b9cacd83 0 1607074286081 5 connected
8ccbabfefb754804d31ed4151f6322229a0fe04d 172.18.0.13:[email protected] master - 0 1607074285000 3 connected 10923-16383
b839fc0c45b3bf7fcfcc2d949035e6e0b9cacd83 172.18.0.11:[email protected] master - 0 1607074286583 1 connected 0-5460
e5ccfb60ea29c6a8d3366685fb82ff5df246eac9 172.18.0.12:[email protected] master,fail - 1607074179446 1607074178223 2 connected
3d8d3e954b26161832eb68d004aa4b625a13026d 172.18.0.14:[email protected] slave 8ccbabfefb754804d31ed4151f6322229a0fe04d 0 1607074285000 4 connected
重新启动172.18.0.12
docker start redis-2
查看节点状态
172.18.0.16:6379> cluster nodes
d038a7540b3b9477a2a715ef7e9c9c6938dfcc48 172.18.0.16:[email protected] myself,master - 0 1607074348000 7 connected 5461-10922
f613bbd183cbd6bf6436179b609845085a0d1b16 172.18.0.15:[email protected] slave b839fc0c45b3bf7fcfcc2d949035e6e0b9cacd83 0 1607074348000 5 connected
8ccbabfefb754804d31ed4151f6322229a0fe04d 172.18.0.13:[email protected] master - 0 1607074348206 3 connected 10923-16383
b839fc0c45b3bf7fcfcc2d949035e6e0b9cacd83 172.18.0.11:[email protected] master - 0 1607074349288 1 connected 0-5460
e5ccfb60ea29c6a8d3366685fb82ff5df246eac9 172.18.0.12:[email protected] slave d038a7540b3b9477a2a715ef7e9c9c6938dfcc48 0 1607074349000 7 connected
3d8d3e954b26161832eb68d004aa4b625a13026d 172.18.0.14:[email protected] slave 8ccbabfefb754804d31ed4151f6322229a0fe04d 0 1607074347594 4 connected
172.18.0.16:6379>