1.概述
(1)docker官网:https://www.docker.com/
仓库地址https://hub.docker.com/
2.安装docker
(1)检查是否有已经安装docker,安装yum工具
[root@localhost ~]# rpm -qa | grep docker
[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
更新yum软件包索引
yum makecache fast
安装docker
yum install docker-ce docker-ce-cli containerd.io
启动docker
systemctl start docker
查看版本
docker version
(2)启动一个容器
[root@localhost yum.repos.d]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
(3)查看下载的镜像
[root@localhost yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
2.镜像加速
[root@localhost docker]# sudo tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://4q9hlak8.mirror.aliyuncs.com"]
> }
> EOF
[root@localhost docker]# systemctl daemon-reload
[root@localhost docker]# systemctl restart docker
3.docker常用命令
a.查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
搜索镜像
[root@localhost ~]# docker search redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 10277 [OK]
sameersbn/redis 83 [OK]
grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2 80
rediscommander/redis-commander Alpine image for redis-commander - Redis man… 71 [OK]
拉取最新版本镜像
[root@localhost ~]# docker pull mysql
拉取指定版本镜像
[root@localhost ~]# docker pull mysql:5.7
删除镜像
[root@localhost ~]# docker rmi -f centos
交互式进入容器,退出容器容器就关闭了
[root@localhost ~]# docker run -it centos
[root@43670455796f /]#
查看启动的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a18a8feedaf3 centos "/bin/bash" 6 seconds ago Up 4 seconds reverent_kare
查看所有容器,正在启动的和已经停止的
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a18a8feedaf3 centos "/bin/bash" About a minute ago Up About a minute reverent_kare
43670455796f centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago youthful_golick
52c44c3034c5 centos "/bin/bash" 3 minutes ago Exited (0) 3 minutes ago peaceful_kapitsa
922c02bc7641 centos "/bin/bash" 3 minutes ago Exited (0) 3 minutes ago zealous_almeida
224b540aaa7e mysql "docker-entrypoint.s…" 5 minutes ago Exited (1) 5 minutes ago focused_cray
4a1b9abf49cd hello-world "/hello" About an hour ago Exited (0) About an hour ago heuristic_shamir
查看所有容器的编号
[root@localhost ~]# docker ps -aq
a18a8feedaf3
43670455796f
52c44c3034c5
922c02bc7641
224b540aaa7e
4a1b9abf49cd
删除容器,只能删除未启动的容器
[root@localhost ~]# docker rm 52c44c3034c5
52c44c3034c5
启动停止的容器
[root@localhost ~]# docker start 43670455796f
43670455796f
启动centos的一个容器
[root@localhost ~]# docker run -d centos /bin/bash
81816f5d03cfb88dac21efc220f0a1ce29fccc43d6b2a06896903daa0bac7f31
查看没有正在运行的容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
启动容器后,执行一个运行的命令,才能使容器正常运行
[root@localhost ~]# docker run -d centos /bin/bash -c "while true;do echo kuangshen;sleep 1; done"
5fcb0b52b14cbf3305c0c7ccdb3374f20739a4a2d484064d92a37ed2d77a95d9
[root@localhost ~]#
查看容器中进程信息
[root@localhost ~]# docker top 5fcb0b52b14c
查看镜像的元数据
[root@localhost ~]# docker inspect 5fcb0b52b14c
进入容器
[root@localhost ~]# docker exec -it 5fcb0b52b14c /bin/bash
[root@5fcb0b52b14c /]#
从容器拷贝文件到宿主机
[root@localhost ~]# docker cp 5fcb0b52b14c:/test.txt /root/
通过当前容器生成一个镜像
[root@localhost ~]# docker commit -m="wodejingixang" -a='zss' 5fcb0b52b14c centos:zss11
sha256:3694e132e30f118a410f46b3163234c2d7b5d87c1a3d083cc1772a6ee3bf8b89
4.数据卷
(1)通过命令直接挂载,主机数据和容器数据互相同步
[root@localhost ~]# docker run -it -v /home/ceshi:/home centos /bin/bash
[if !supportLists](2)[endif]启动一个mysql容器
[root@localhost ~]# docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data/:/bar/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
6a3fa55aa813af0d12f351d68e947a467033df258c768884912730726256be5f
(3)匿名挂载,只指定容器的目录
[root@localhost ~]# docker run -d -P --name nginx01 -v /etc/nginx nginx
[root@localhost ~]# docker volume ls
DRIVER VOLUME NAME
local 7941953ceadc743eaa237b990d46ae56e2a14aceaa15b5d477c2010f1864ccf5
local d5ca6059960b989fa290db3e51119e1caf876991390dff6b34f9457958224e57
local e947d3c189dd8236845233c3b42786298f82490327c2b2df39a7e0993d47d2aa
local f29dc23e945fd06d50860642a7dfaf89f94a67763fe861b469ecda9b7f9c0f58
[if !supportLists](3)[endif]具名挂载,给容器内挂在的卷一个名字
[root@localhost _data]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx
5e711594038da1350611573fc809d92fbc55b170b432e99d9e7a7ab3276dced5
[root@localhost _data]# docker volume ls
DRIVER VOLUME NAME
local 7941953ceadc743eaa237b990d46ae56e2a14aceaa15b5d477c2010f1864ccf5
local d5ca6059960b989fa290db3e51119e1caf876991390dff6b34f9457958224e57
local e947d3c189dd8236845233c3b42786298f82490327c2b2df39a7e0993d47d2aa
local f29dc23e945fd06d50860642a7dfaf89f94a67763fe861b469ecda9b7f9c0f58
local juming-nginx
(4)设置容器内目录,在容器内只读,只能在宿主机内可以读写
[root@localhost /]# docker run -d -P --name nginx03 -v juming-nginx03:/etc/nginx:ro nginx
4362e22a069e21cb262f1a40b97b16d196d98c9afc48bbef1783bb67a83f334d
5.dockerfile
(1)创建一个镜像
a.创建dockerfile文件
[root@localhost docker]# cat dockerfile
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "----end---"
CMD /bin/bash
b.创建镜像
[root@localhost docker]# docker build -f /docker/dockerfile -t kuangshen/centos:1.0 .
Sending build context to Docker daemon 2.048kB
Step 1/4 : FROM centos
---> 5d0da3dc9764
Step 2/4 : VOLUME ["volume01","volume02"]
---> Running in 112f3903cfc0
Removing intermediate container 112f3903cfc0
---> 6eef3fbff990
Step 3/4 : CMD echo "----end---"
---> Running in db09822d0987
Removing intermediate container db09822d0987
---> b8f7c584c041
Step 4/4 : CMD /bin/bash
---> Running in 127aedae2c89
Removing intermediate container 127aedae2c89
---> 785db3faa532
Successfully built 785db3faa532
Successfully tagged kuangshen/centos:1.0
c.查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kuangshen/centos 1.0 785db3faa532 5 minutes ago 231MB
centos 3.0 785db3faa532 5 minutes ago 231MB
centos zss11 3694e132e30f 47 minutes ago 231MB
tomcat latest 24207ccc9cce 6 days ago 680MB
[if !supportLists](2)[endif]再通过dockerfile构建一个镜像
[if !supportLists]a. [endif]dockerfile如下
[root@localhost docker]# cat dockerfile1
FROM centos
MAINTAINER kuangshen<11212.qq.com>
#ENV MYPATN /usr/local
#WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
#CMD echo $MYPATH
CMD echo "--end----"
CMD /bin/bash
b.执行创建命令
[root@localhost docker]# docker build -f dockerfile1 -t centoszss5:2.0 .
Step 7/7 : CMD /bin/bash
---> Running in 14b36d913ea3
Removing intermediate container 14b36d913ea3
---> c1be04b32731
Successfully built c1be04b32731
Successfully tagged centoszss5:2.0
c.查看镜像
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centoszss5 2.0 c1be04b32731 4 minutes ago 322MB
d.使用自己生成的镜像启动容器,已经有vim命令
[root@localhost docker]# docker run -it centoszss5:2.0
[root@5a97a37b6b22 /]# vi
vi view vigr vim vimdiff vimtutor vipw
e.使用原始镜像启动容器,没有vim命令
[root@localhost ~]# docker run -it centos
[root@6f4a04ee0613 /]# vim
bash: vim: command not found
f.查看镜像变更过程
[root@localhost docker]# docker history centoszss5:2.0
IMAGE CREATED CREATED BY SIZE COMMENT
c1be04b32731 9 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
9cacb36695f3 9 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
a423666676d8 9 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
1a2ef029a997 9 minutes ago /bin/sh -c yum -y install net-tools 27MB
18030e069303 9 minutes ago /bin/sh -c yum -y install vim 64.1MB
fc1a1ff71c13 12 minutes ago /bin/sh -c #(nop) MAINTAINER kuangshen<1121… 0B
5d0da3dc9764 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0… 231MB
(3)生成一个tomcat镜像
a.dockerfile如下
[root@localhost docker]# cat tomcatdockerfile
FROM centos
MAINTAINER kuangshen<11212.qq.com>
ADD jdk-8u311-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.22.tar.gz /usr/local/
RUN yum -y install vim
ENV MYPATH /usr/local/
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_311
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.22
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.22
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
CMD /usr/local/apache-tomcat-9.0.22/bin/startup.sh
CMD tail -f /var/log/btmp
[if !supportLists]b. [endif]创建镜像
[root@localhost docker]# docker build -f tomcatdockerfile -t tomcatzss:3.0 .
Removing intermediate container edb56713f6ee
---> d0278c8b43e2
Successfully built d0278c8b43e2
Successfully tagged tomcatzss:3.0
c.查看镜像
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcatzss 3.0 d0278c8b43e2 7 seconds ago 676MB
d.运行容器
[root@localhost docker]# docker run -it -d tomcatzss:3.0
6.数据卷容器
创建2个容器实现数据卷共享
[root@localhost docker]# docker run -d -p 3320:3306 -v /etc/mysql/conf.d -v /var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 mysql:5.7
0fbdab503dbdb1656275b43f001fe4968b3d02607a80360808b1f0f3587150de
[root@localhost docker]# docker run -d -p 3330:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql04 --volumes-from mysql02 mysql:5.7
fdf04c4f44a69737144fc65c116f5305233b3adbbf21ffd446a153527bd54dd1
7.镜像仓库中所有镜像版本查看
参考链接:https://www.cnblogs.com/qa-freeroad/p/14450386.html
https://www.cnblogs.com/qa-freeroad/p/13563013.html
(1)安装python3.6
yum install python36 -y
yum install python36-devel -y
python3-V
(2)安装pip
yum install python36-pip -y
pip3 -V
(3)命令行查看镜像仓库中某个镜像都有哪些版本
curl https://registry.hub.docker.com/v1/repositories/${docker_img}/tags | python3 -m json.tool | more
8.容器内安装命令
apt update
apt install -y iproute2
apt install iputils-ping
9.网络相关命令
(1)link,一个容器可以ping另一个容器的name
(2)实操link
a.[root@localhost ~]# docker exec -it tomcat4 ping tocmcat3
ping: tocmcat3: Name or service not known
[root@localhost ~]# docker run -d -P --name tomcat5 --link tomcat4 tomcatzss:1.0
c82b18cdf4237336345956227419e0818a9cfb10a8ce2b8f92220d104df8b785
[root@localhost ~]# docker exec -it tomcat5 ping tomcat4
PING tomcat4 (172.17.0.5) 56(84) bytes of data.
64 bytes from tomcat4 (172.17.0.5): icmp_seq=1 ttl=64 time=0.505 ms
b.link的本质,在hosts中增加了解析
[root@localhost ~]# docker exec -it tomcat5 cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.5 tomcat4 9a356af00866
172.17.0.6 c82b18cdf423
c.查看当前网络
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
02e562a54700 bridge bridge local
752264caac7a host host local
7fc4b2a46f7c none null local
d.创建一个自定义网络
[root@localhost ~]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
e5e23ceec16d7aea3d3053c4a5aa992791f6d8bd3d7aa9e66b181931feabef29
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
02e562a54700 bridge bridge local
752264caac7a host host local
e5e23ceec16d mynet bridge local
7fc4b2a46f7c none null local
c.通过自定义网络创建容器
[root@localhost ~]# docker run -d -P --name tomcat--mynet --net mynet tomcatzss:1.0
2bb33a4f838f8819cfac3e5dd2a1f52dd119a143d4bbbbeb05e23b815822eed4
[root@localhost ~]# docker run -d -P --name tomcat--mynet1 --net mynet tomcatzss:1.0
e6dfeb9e95af43dd17a55af3d550ab88a05de08b66b52bf687da206901cdc8ba
[root@localhost ~]# docker exec -it e6dfeb9e95af ping tomcat--mynet
PING tomcat--mynet (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat--mynet.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.275 ms
d.网络联通(将A网络的容器加入到B网络)
[root@localhost ~]# docker network connect mynet tomcat01
10.redis集群部署实战
a.写一个脚本,准备6个docker挂载的目录
[root@localhost docker]# cat redis.sh
#/bin/bash
for port in $(seq 1 6)
do
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat > /mydata/redis/node-${port}/conf/redis.conf << EOF
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
appendonly yes
EOF
done
b.准备容器创建脚本
[root@localhost docker]# cat redis-rongqi.sh
#/bin/bash
for port in $(seq 1 6)
do
docker run -p 637${port} -p 1637${port}:16379 --name redis-${port} -v /mydata/redis/node-${port}/data:/data -v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
done
c.进入redis容器,创建集群
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38
.0.15:6379 172.38.0.16:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: 9d7456de5d339019f61a871f117fa527f6d7ddd2 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: 1902a1a872a462847e80152b8a28df57f92108a3 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: 48fbf97aaa206f7e6c71a10918891ea8be96cf35 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: f5fc035a60177085dd6d878811f0ec71557321bd 172.38.0.14:6379
replicates 48fbf97aaa206f7e6c71a10918891ea8be96cf35
S: b1523ddce48fdbdc93299f9b626032805f748cb5 172.38.0.15:6379
replicates 9d7456de5d339019f61a871f117fa527f6d7ddd2
S: e04a1d1a2219d0cac0f21abb2db7a05bd9457117 172.38.0.16:6379
replicates 1902a1a872a462847e80152b8a28df57f92108a3
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
....
>>> Performing Cluster Check (using node 172.38.0.11:6379)
M: 9d7456de5d339019f61a871f117fa527f6d7ddd2 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: e04a1d1a2219d0cac0f21abb2db7a05bd9457117 172.38.0.16:6379
slots: (0 slots) slave
replicates 1902a1a872a462847e80152b8a28df57f92108a3
S: b1523ddce48fdbdc93299f9b626032805f748cb5 172.38.0.15:6379
slots: (0 slots) slave
replicates 9d7456de5d339019f61a871f117fa527f6d7ddd2
S: f5fc035a60177085dd6d878811f0ec71557321bd 172.38.0.14:6379
slots: (0 slots) slave
replicates 48fbf97aaa206f7e6c71a10918891ea8be96cf35
M: 48fbf97aaa206f7e6c71a10918891ea8be96cf35 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 1902a1a872a462847e80152b8a28df57f92108a3 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
d.查看集群信息
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:2
cluster_stats_messages_ping_sent:195
cluster_stats_messages_pong_sent:203
cluster_stats_messages_meet_sent:4
cluster_stats_messages_sent:402
cluster_stats_messages_ping_received:201
cluster_stats_messages_pong_received:199
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:402
127.0.0.1:6379> CLUSTER NODES
f5fc035a60177085dd6d878811f0ec71557321bd 172.38.0.14:6379@16379 slave 48fbf97aaa206f7e6c71a10918891ea8be96cf35 0 1639723274617 4 connected
b1523ddce48fdbdc93299f9b626032805f748cb5 172.38.0.15:6379@16379 slave 9d7456de5d339019f61a871f117fa527f6d7ddd2 0 1639723272614 5 connected
e04a1d1a2219d0cac0f21abb2db7a05bd9457117 172.38.0.16:6379@16379 myself,slave 1902a1a872a462847e80152b8a28df57f92108a3 0 1639723273000 6 connected
48fbf97aaa206f7e6c71a10918891ea8be96cf35 172.38.0.13:6379@16379 master - 0 1639723273615 3 connected 10923-16383
9d7456de5d339019f61a871f117fa527f6d7ddd2 172.38.0.11:6379@16379 master - 0 1639723274000 1 connected 0-5460
1902a1a872a462847e80152b8a28df57f92108a3 172.38.0.12:6379@16379 master - 0 1639723272513 2 connected 5461-10922