Docker

1.配置虚拟机IP

  1. # 编辑网络配置文件
  2. [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
  3. TYPE=Ethernet
  4. # 静态IP
  5. BOOTPROTO=static
  6. NAME=ens33
  7. DEVICE=ens33
  8. # 开机自启
  9. ONBOOT=yes
  10. # IP地址
  11. IPADDR=192.168.1.202
  12. # 子网掩码
  13. NETMASK=255.255.255.0
  14. # 网关
  15. GATEWAY=192.168.1.1
  16. # DNS
  17. DNS1=114.114.114.114
  18. DNS2=1.2.4.8

Docker - 图1

  1. # 重启网卡
  2. [root@localhost ~]# systemctl restart network

2.安装Docker引擎

  1. [root@localhost ~]# sudo yum install -y yum-utils # 安装yum-utils包
  2. # 设置仓库地址-阿里云
  3. [root@localhost ~]# sudo yum-config-manager \
  4. --add-repo \
  5. http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  6. # 安装docker引擎
  7. [root@localhost ~]# sudo yum install docker-ce docker-ce-cli containerd.io
  8. # 启动docker
  9. [root@localhost ~]# sudo systemctl start docker
  10. # 查看docker版本
  11. [root@localhost ~]# docker -v
  12. Docker version 20.10.9, build c2ea9bc
  13. # 通过run hello-world 验证 Docker Engine 是否已正确安装。
  14. [root@localhost ~]# sudo docker run hello-world

Docker - 图2

3.Doker的启动与停止命令

  1. [root@localhost ~]# sudo systemctl start docker # 启动docker
  2. [root@localhost ~]# sudo systemctl stop docker # 停止docker
  3. [root@localhost ~]# sudo systemctl restart docker # 重启docker
  4. [root@localhost ~]# sudo systemctl enable docker # 设置开机自启docker
  5. [root@localhost ~]# sudo systemctl status docker # 查看docker状态
  6. [root@localhost ~]# sudo docker info # 查看docker的概要信息
  7. [root@localhost ~]# sudo docker --help # 查看docker的帮助信息

4.配置镜像加速

Docker 从 Docker Hub 拉取镜像,因为是从国外获取,所以速度较慢
可以通过配置国内镜像源的方式,从国内获取镜像,提高拉取速度。

  1. # 编辑文件
  2. vim /etc/docker/daemon.json

Docker - 图3

设置163和中国科技大学的镜像

  • 重新加载配置信息及重启docker服务
    1. # 重新加载文件
    2. [root@localhost ~]# sudo systemctl daemon-reload
    3. # 重启docker
    4. [root@localhost ~]# sudo systemctl restart docker

5.镜像相关命令

1.查看镜像

  1. [root@localhost ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. hello-world latest feb5d9fea6a5 4 weeks ago 13.3kB
  • REPOSITORY : 镜像在仓库中的名称

  • TAG:镜像标签,latest代表最新版本

  • IMAGE ID:镜像ID

  • CREATED:镜像创建的日期

  • SIZE:镜像大小
    这些镜像都是存储在宿主机/etc/lib/docker目录下的

3.搜索镜像

语法:

  1. docker search 镜像名称

实例:

  1. [root@localhost ~]# docker search redis # 搜索名为redis的镜像
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. redis Redis is an open source key-value store that 10076 [OK]
  4. sameersbn/redis 83 [OK]
  5. grokzen/redis-cluster Redis cluster 3.0, 3.2, 4.0, 5.0, 6.0, 6.2 79
  6. rediscommander/redis-commander Alpine image for redis-commander - Redis man 66 [OK]
  7. redislabs/redisearch Redis With the RedisSearch module pre-loaded 40
  8. redislabs/redisinsight RedisInsight - The GUI for Redis 35
  9. oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Supp 31
  10. redislabs/redis Clustered in-memory database engine compatib 31
  11. redislabs/rejson RedisJSON - Enhanced JSON data type processi 29
  12. arm32v7/redis Redis is an open source key-value store that 25
  13. redislabs/redisgraph A graph database module for Redis 16 [OK]
  14. arm64v8/redis Redis is an open source key-value store that 16
  15. redislabs/rebloom A probablistic datatypes module for Redis 16 [OK]
  16. redislabs/redismod An automated build of redismod - latest Redi 15 [OK]
  17. webhippie/redis Docker image for redis 11 [OK]
  18. redislabs/redistimeseries A time series database module for Redis 10
  19. s7anley/redis-sentinel-docker Redis Sentinel 10 [OK]
  20. insready/redis-stat Docker image for the real-time Redis monitor 10 [OK]
  21. goodsmileduck/redis-cli redis-cli on alpine 9 [OK]
  22. centos/redis-32-centos7 Redis in-memory data structure store, used a 5
  23. clearlinux/redis Redis key-value data structure server with t 3
  24. wodby/redis Redis container image with orchestration 1 [OK]
  25. tiredofit/redis Redis Server w/ Zabbix monitoring and S6 Ove 1 [OK]
  26. xetamus/redis-resource forked redis-resource 0 [OK]
  27. centos/redis-5-centos7 Redis in-memory data structure store, used a 0
  28. [root@localhost ~]#

4.拉取镜像

  1. 语法:
  1. docker pull 镜像名称
  1. 假如我们要拉去centos镜像到本地,如果不声明镜像TAG标签信息则默认拉取latest版本,也可以通过hub.docker.com搜索该镜像,查看支持的TAG信息。
  1. [root@localhost ~]# docker pull redis
  2. Using default tag: latest
  3. latest: Pulling from library/redis
  4. 7d63c13d9b9b: Pull complete
  5. a2c3b174c5ad: Pull complete
  6. 283a10257b0f: Pull complete
  7. 7a08c63a873a: Pull complete
  8. 0531663a7f55: Pull complete
  9. 9bf50efb265c: Pull complete
  10. Digest: sha256:a89cb097693dd354de598d279c304a1c73ee550fbfff6d9ee515568e0c749cfe
  11. Status: Downloaded newer image for redis:latest
  12. docker.io/library/redis:latest
  13. [root@localhost ~]# docker images
  14. REPOSITORY TAG IMAGE ID CREATED SIZE
  15. redis latest 7faaec683238 12 days ago 113MB
  16. hello-world latest feb5d9fea6a5 4 weeks ago 13.3kB
  17. [root@localhost ~]# docker pull redis:5
  18. 5: Pulling from library/redis
  19. 7d63c13d9b9b: Already exists
  20. a2c3b174c5ad: Already exists
  21. 283a10257b0f: Already exists
  22. 54ac4e97e390: Pull complete
  23. 0d3ede1e63a5: Pull complete
  24. 878bf2d7168d: Pull complete
  25. Digest: sha256:8217ee751b6a72bc4b3ef757c18aa9619e939d5073d5a26ce2074905385000b0
  26. Status: Downloaded newer image for redis:5
  27. docker.io/library/redis:5
  28. [root@localhost ~]# docker images
  29. REPOSITORY TAG IMAGE ID CREATED SIZE
  30. redis 5 02fee89f17ad 12 days ago 110MB
  31. redis latest 7faaec683238 12 days ago 113MB
  32. hello-world latest feb5d9fea6a5 4 weeks ago 13.3kB
  33. [root@localhost ~]#

5.删除镜像

  1. docker rmi 镜像名/镜像id
  1. [root@localhost ~]# docker rmi redis
  2. Untagged: redis:latest
  3. Untagged: redis@sha256:a89cb097693dd354de598d279c304a1c73ee550fbfff6d9ee515568e0c749cfe
  4. Deleted: sha256:7faaec68323851b2265bddb239bd9476c7d4e4335e9fd88cbfcc1df374dded2f
  5. Deleted: sha256:e6deb90762475cda72e21895911f830ed99fd1cc6d920d92873270be91235274
  6. Deleted: sha256:2649acad13241d9c8d81e49357bc66cce459b352ded7f423d70ede7bd3bb7b89
  7. Deleted: sha256:64007bba5fc220df4d3da33cecdc2d55dd6a73528c138b0fa1acd79fd6a9c217
  8. [root@localhost ~]# docker rmi redis:5
  9. Untagged: redis:5
  10. Untagged: redis@sha256:8217ee751b6a72bc4b3ef757c18aa9619e939d5073d5a26ce2074905385000b0
  11. Deleted: sha256:02fee89f17adc8213b560b929d5ac585137612651f8eeb26423aadfe39dc3847
  12. Deleted: sha256:f92e4de257018a916bd0715da7b1dd39a3540fa393799c2219a82f0ce99d57e2
  13. Deleted: sha256:b13f2ab83d74b48551107876f35dcdaa7197a557fd4769cd4bfeb56bc4f031b6
  14. Deleted: sha256:58ca4126d8128345fc58de904cb9029f2a1deb1f88b1fa0ac3f3f0e707f099b9
  15. Deleted: sha256:b2cc2f1bf8b1cca8ba7c19e1697f7b73755903ad8f880b83673fd6a697aca935
  16. Deleted: sha256:fbd1283ab782925be4d990bd4bebe9ad5e5cf9a525abfb6fa87465e072da9d31
  17. Deleted: sha256:e8b689711f21f9301c40bf2131ce1a1905c3aa09def1de5ec43cf0adf652576e
  18. [root@localhost ~]# docker images
  19. REPOSITORY TAG IMAGE ID CREATED SIZE
  20. hello-world latest feb5d9fea6a5 4 weeks ago 13.3kB
  21. [root@localhost ~]#

6.容器相关命令

1.查看容器

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  • CONTAINER ID:容器ID
  • IMAGE:所属镜像
  • COMMAND
  • CREATED:创建时间
  • STATUS:状态
  • PORTS:端口
  • NAMES:容器名称

    查看所有容器(停止和运行)

  1. [root@localhost ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. a08d44d00165 hello-world "/hello" 27 hours ago Exited (0) 27 hours ago magical_buck
  1. 查看停止运行的容器
  1. [root@localhost ~]# docker ps -f status=exited
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. a08d44d00165 hello-world "/hello" 27 hours ago Exited (0) 27 hours ago magical_buck
  1. 查看最后一次运行的容器
  1. [root@localhost ~]# docker ps -l
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. a08d44d00165 hello-world "/hello" 2 days ago Exited (0) 2 days ago magical_buck
  1. 列出最近创建的n个容器
  1. [root@localhost ~]# docker ps -n 2
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. a08d44d00165 hello-world "/hello" 2 days ago Exited (0) 2 days ago magical_buck

2.创建与启动容器

  1. [root@localhost ~]# docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • -i:表示运行的容器
  • -t :表示容器启动后会进入其命令行。加入这两个参数后,容器创建就能登陆进去。即分配一个伪终端。
  • —name:为创建的容器命名。
  • -v:表示目录映射关系(前者是宿主机目录,后者是映射到宿主机上的目录),可以使用多个-v做多个目录或文件映射。注意:最好做目录映射,在宿主机上做修改,然后共享到容器上。
  • -d:在run后面加上-d参数,则会创建一个守护式容器在后台运行(这样创建容器后就不会自动登录容器,如果只加-i -t两个参数,创建容器后就会自动进容器里)。
  • -p:表示端口映射,前者是宿主机端口,后者是容器内的映射端口。可以使用多个-p做多个端口映射。
  • -P:随机使用宿主机的可用端口与容器暴露的端口映射。

    创建容器并进入

  1. [root@localhost ~]# docker run --name mynginx -P nginx # 创建一个nginx容器,自定义名称为mynginx,将nginx的端口随机映射到宿主机端口
  2. [root@localhost ~]# docker ps # 查看正在运行的容器,发现nginx映射到了宿主机的49153端口
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 12c76efcc8f7 nginx "/docker-entrypoint.…" 48 seconds ago Up 47 seconds 0.0.0.0:49153->80/tcp, :::49153->80/tcp mynginx
  1. 守护方式创建容器
  1. [root@localhost ~]# docker run -di --name mynginx -p 80:80 nginx
  2. a0a190090a232b9d3ed1a0882d77088c198d710b129ec764d9b66cedc960f096
  3. [root@localhost ~]#
  4. [root@localhost ~]# docker ps
  5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  6. a0a190090a23 nginx "/docker-entrypoint.…" 11 seconds ago Up 11 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp mynginx
  7. [root@localhost ~]#
  1. 登录到守护方式创建的容器
  1. [root@localhost ~]# docker exec -it mynginx /bin/bash
  2. root@a0a190090a23:/#
  1. 启动与停止容器
  1. [root@localhost ~]# docker start mynginx
  2. mynginx
  3. [root@localhost ~]# docker stop mynginx
  4. mynginx

删除容器

  1. [root@localhost ~]# docker rm 12c76efcc8f7 # 删除id为12c76efcc8f7的容器
  2. 12c76efcc8f7

3.文件拷贝

  1. 宿主机拷贝到容器
  1. docker cp 需要拷贝的文件或目录 容器名称:容器目录
  1. 例:将宿主机中的test.py文件拷贝到mynginx容器中的/root目录下
  1. [root@localhost ~]# vi test.py
  2. [root@localhost ~]# docker cp test.py mynginx:/root
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. a0a190090a23 nginx "/docker-entrypoint.…" 10 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp mynginx
  5. [root@localhost ~]# docker exec -it mynginx /bin/bash
  6. root@a0a190090a23:/# ls /root/
  7. test.py
  8. root@a0a190090a23:/#
  1. 容器拷贝到宿主机
  1. docker cp 容器名称:容器目录 需要拷贝的文件或目录
  1. 例:
  1. root@a0a190090a23:~# touch test.txt
  2. root@a0a190090a23:~# exit
  3. exit
  4. [root@localhost ~]# docker cp mynginx:/root/test.txt ./
  5. [root@localhost ~]# ls
  6. anaconda-ks.cfg index.txt test.py test.txt

7.目录挂载

1.指定目录挂载

  1. # 创建一个nginx容器,端口映射为81端口,宿主机的/root/mynginx_02映射到容器的/root/mynginx_02目录
  2. # 此时,修改宿主机的/root/mynginx_02同时会修改容器的/root/mynginx_02
  3. [root@localhost ~]# docker run -di --name nginx02 -p 81:80 -v /root/mynginx_02/:/root/mynginx_02 nginx
  4. 64e80e6ff89ecefea568b317690baa8c9072c270a480db3c749d0ebc07202616
  1. 查看某个容器的详细信息
  1. [root@localhost mynginx_02]# docker inspect nginx02 # 查看nginx02的详细信息

2.匿名挂载

匿名挂载就是在宿主机中生成的目录名称是随机命名的

  1. 匿名挂载只需要写容器的目录即可,容器外对应的目录会在/var/lib/docker/volume中生成。
  1. # 匿名挂载
  2. docker run -di -v /usr/local/data --name centos7-02 centos:7
  3. # 查看volume数据卷信息
  4. docker volume ls
  1. 例:
  1. # 匿名挂载目录
  2. [root@localhost ~]# docker run -di -v /usr/local/data --name centos7-02 centos:7
  3. 59778edd9a81bc5c5dcd24799429c05fec27e8d6042c88735f23d301f78a80c1
  4. # 查看生成的数据卷信息
  5. [root@localhost ~]# ls /var/lib/docker/volumes/
  6. backingFsBlockDev e83692d4dbc68d740a75f7761cec27738f120afae98be50948527665da95bf54 metadata.db
  7. # 进入容器的/usr/local/data目录随意创建一个文件
  8. [root@localhost ~]# docker exec -it centos7-02 /bin/bash
  9. [root@59778edd9a81 /]# cd /usr/local/data/
  10. [root@59778edd9a81 data]# ls
  11. [root@59778edd9a81 data]# touch test.txt
  12. # 在宿主机中查看容器对应的目录
  13. [root@localhost ~]# cd /var/lib/docker/volumes/
  14. backingFsBlockDev metadata.db
  15. e83692d4dbc68d740a75f7761cec27738f120afae98be50948527665da95bf54/
  16. [root@localhost ~]# cd /var/lib/docker/volumes/e83692d4dbc68d740a75f7761cec27738f120afae98be50948527665da95
  17. [root@localhost e83692d4dbc68d740a75f7761cec27738f120afae98be50948527665da95bf54]# ls
  18. _data
  19. [root@localhost e83692d4dbc68d740a75f7761cec27738f120afae98be50948527665da95bf54]# cd _data/
  20. # 发现宿主机此目录下同容器一样,有刚创建的test.txt文件
  21. [root@localhost _data]# ls
  22. test.txt

3.具名挂载

具名挂载就是在宿主机中生成的目录名称是我们指定的名称

  1. # 创建一个nginx容器,指定宿主机目录为docker_nginx_data,容器中对应的目录为/usr/local/data,
  2. [root@localhost volumes]# docker run -di -v docker_nginx_data:/usr/local/data --name nginx06 nginx
  3. e832041a8a3804f68204b8740c8addb8865df7ae8bec9b477ab851d0b1671c83
  4. # 查看宿主机生成的新目录,发现有了docker_nginx_data
  5. [root@localhost volumes]# ls /var/lib/docker/volumes/
  6. backingFsBlockDev docker_nginx_data e83692d4dbc68d740a75f7761cec27738f120afae98be50948527665da95bf54 metadata.db

4.数据卷只读

数据卷只读就是挂载到容器中目录,容器只有读取权限。

  1. [root@localhost volumes]# docker run -di --name nginx07 -P -v /nginx07:/nginx07:ro nginx
  2. 8ca4534cb21f57ff7d7a4fe2d6caa4caa25f604132982072c8664db1289c43d0
  3. # 发现在var/lib/docker/volumes/目录中不会有目录生成,新目录生成在宿主机的/nginx07
  4. [root@localhost volumes]# ls /var/lib/docker/volumes/
  5. backingFsBlockDev docker_nginx_data e83692d4dbc68d740a75f7761cec27738f120afae98be50948527665da95bf54 metadata.db
  6. # 此时想在容器中创建文件就会报错
  7. root@8ca4534cb21f:/nginx07# touch 123
  8. touch: cannot touch '123': Read-only file system

5.volume-form继承

一个容器继承领另一个容器挂载的地址

  1. # 创建一个新的容器nginx09,继承nginx07挂载的地址
  2. [root@localhost _data]# docker run -di --name nginx09 --volumes-from nginx07 nginx
  3. 0bc9b443996448b613df2841d229ec769418d1365737da16ed39ab1fc15df722
  4. # 此时进入容器中查看
  5. [root@localhost _data]# docker exec -it nginx09 /bin/bash
  6. # 发现有nginx07目录
  7. root@0bc9b4439964:/# ls
  8. bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt nginx07 opt proc root run sbin srv sys tmp usr var
  9. # 进入nginx07目录,发现有1.txt文件
  10. root@0bc9b4439964:/# cd nginx07/
  11. root@0bc9b4439964:/nginx07# ls
  12. 1.txt

6.查看目录卷挂载关系

  1. # 查看目录卷的创建时间、名称、位置
  2. [root@localhost _data]# docker volume inspect docker_nginx_data
  3. [
  4. {
  5. "CreatedAt": "2021-10-31T04:41:05-04:00",
  6. "Driver": "local",
  7. "Labels": null,
  8. "Mountpoint": "/var/lib/docker/volumes/docker_nginx_data/_data",
  9. "Name": "docker_nginx_data",
  10. "Options": null,
  11. "Scope": "local"
  12. }
  13. ]
  14. # 查看某个容器的ip
  15. [root@localhost _data]# docker inspect --format='{{.NetworkSettings.IPAddress}}' nginx07
  16. 172.17.0.2

8.docker镜像构建

  • docker commit:从容器创建一个新镜像
  • docker bulid:配合dockerfile创建一个镜像

1.通过docker commit来实现镜像的构建

我们通过基础镜像centos7,在容器中安装jdk和tomcat来创建一个新的镜像mycentos:Docker - 图4

  1. 创建容器
  1. [root@localhost _data]# docker pull cnetos:7 # 拉取镜像
  2. [root@localhost _data]# docker run -di --name centos7 centos:7 # 基于centos:7镜像创建一个容器centos
  3. d38288c7a5bd5ee4edc1586d20b7e3b1900084f8267d6be977187793d898f00a
  1. 拷贝资源

先在宿主机中准备好tomcat和jdk的安装包,然后将安装包拷贝到容器中

  1. # 将安装包拷贝到容器
  2. [root@localhost ~]# docker cp /root/apache-tomcat-8.5.72.tar.gz centos7:/root
  3. [root@localhost ~]# docker cp /root/jdk-8u311-linux-x64.tar.gz centos7:/root
  4. # 进入容器中查看拷贝的东西
  5. [root@localhost ~]# docker exec -it centos7 /bin/bash
  6. [root@d38288c7a5bd /]# cd /root/
  7. [root@d38288c7a5bd ~]# ls
  8. anaconda-ks.cfg apache-tomcat-8.5.72.tar.gz jdk-8u311-linux-x64.tar.gz
  9. # 解压文件
  10. [root@d38288c7a5bd ~]# tar -zxvf apache-tomcat-8.5.72.tar.gz -C /usr/local/tomcat/
  11. [root@d38288c7a5bd ~]# tar -zxvf jdk-8u311-linux-x64.tar.gz -C /usr/local/java/
  12. # 配置环境变量
  13. [root@d38288c7a5bd ~]# vi /etc/profile
  14. # 添加以下配置,然后保存退出
  15. export JAVA_HOME=/usr/local/java/jdk1.8.0_311
  16. export PATH=$PATH:$JAVA_HOME/bin
  17. # 使配置生效
  18. [root@d38288c7a5bd ~]# source /etc/profile
  19. # 检查java环境是否配好
  20. [root@d38288c7a5bd ~]# java -version
  21. java version "1.8.0_311"
  22. Java(TM) SE Runtime Environment (build 1.8.0_311-b11)
  23. Java HotSpot(TM) 64-Bit Server VM (build 25.311-b11, mixed mode)

Docker - 图5

  1. 构建镜像
  1. docker commit [options] CONTAINER [REPOSITORY[:TAG]]
  • -a : 提交镜像的作者
  • -m:提交镜像时的说明文字
  • -c:使用Dockerfile指令来创建镜像
  • -p:在创建镜像时将容器暂停
  1. # 将自己的centos7容器生成一个mycentos7:7的镜像,-a代表镜像作者,-m代表这次构建镜像提交的信息
  2. [root@localhost ~]# docker commit -a="myhelloworld" -m="java8 and tomcat9" centos7 mycentos:7
  3. sha256:4def58ba6be8479e46dc18af7189c0ae64db802fda4af16da3d33d37d3601d8b
  4. # 查看docker有哪些镜像,发现多了一个自己创建的mycentos
  5. [root@localhost ~]# docker images
  6. REPOSITORY TAG IMAGE ID CREATED SIZE
  7. mycentos 7 4def58ba6be8 11 seconds ago 741MB
  8. nginx latest 87a94228f133 2 weeks ago 133MB
  9. hello-world latest feb5d9fea6a5 5 weeks ago 13.3kB
  10. centos 7 eeb6ee3f44bd 6 weeks ago 204MB
  1. 用自己的镜像创建容器
  1. # 尝试用自己构建的镜像创建容器
  2. [root@localhost ~]# docker run -di --name centos7 -p 8080:8080 mycentos:7
  3. e4137800776b4b2e6dd37ee2a8203ae7a3e37d343cd140cd4de73190948d9337
  4. # 进入容器
  5. [root@localhost ~]# docker exec -it centos7 bash
  6. [root@e4137800776b /]#
  7. [root@e4137800776b /]# cd /usr/local/tomcat/apache-tomcat-8.5.72/
  8. # 尝试运行tomcat,发现报错
  9. [root@e4137800776b apache-tomcat-8.5.72]# ./bin/startup.sh
  10. Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
  11. At least one of these environment variable is needed to run this program
  12. # 使环境变量生效
  13. [root@e4137800776b apache-tomcat-8.5.72]# source /etc/profile
  14. # 再次尝试,发现成功
  15. [root@e4137800776b apache-tomcat-8.5.72]# ./bin/startup.sh
  16. Using CATALINA_BASE: /usr/local/tomcat/apache-tomcat-8.5.72
  17. Using CATALINA_HOME: /usr/local/tomcat/apache-tomcat-8.5.72
  18. Using CATALINA_TMPDIR: /usr/local/tomcat/apache-tomcat-8.5.72/temp
  19. Using JRE_HOME: /usr/local/java/jdk1.8.0_311
  20. Using CLASSPATH: /usr/local/tomcat/apache-tomcat-8.5.72/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-8.5.72/bin/tomcat-juli.jar
  21. Using CATALINA_OPTS:
  22. Tomcat started.

此时用一台电脑访问宿主机ip:8080即可看到如下界面:

Docker - 图6

2.Dockerfile作用

  1. [root@localhost ~]# mkdir -p /usr/local/dockerfile
  2. [root@localhost dockerfile]# vi Dockerfile
  3. ENV JAVA_HOME=/usr/loacl/java/jdk1.8.0_311
  4. ENV PATH=$PATH:$JAVA_HOME/bin
  5. # 启动容器时启动tomcat
  6. CMD ["/usr/local/tomcat/apache-tomcat-8.5.72/bin/catalina.sh","run"]
  7. [root@localhost ~]# vi /usr/local/dockerfile/Dockerfile
  8. # 指明构建的新镜像的基础镜像是来自centos:7的
  9. FROM centos:7
  10. # 通过镜像标签声明了作者信息
  11. LABEL maintainer="mrhelloworld.com"
  12. # 设置工作目录
  13. WORKDIR /usr/local
  14. # 新镜像构建成功以后创建指定目录
  15. RUN mkdir -p /usr/local/java && /usr/local/tomcat
  16. # 拷贝文件到镜像中并解压
  17. ADD jdk-8u311-linux-x64.tar.gz /usr/local/java
  18. ADD apache-tomcat-8.5.72.tar.gz /usr/local/tomcat
  19. # 暴露容器运行时的8080监听端口给外部
  20. EXPOSE 8080
  21. # 设置容器内JAVA_HOME的环境变量
  22. ENV JAVA_HOME=/usr/loacl/java/jdk1.8.0_311
  23. ENV PATH=$PATH:$JAVA_HOME/bin
  24. # 启动容器时启动tomcat
  25. CMD ["/usr/local/tomcat/apache-tomcat-8.5.72/bin/catalina.sh","run"]
  26. [root@localhost /]# cd root/
  27. [root@localhost ~]# ls
  28. anaconda-ks.cfg apache-tomcat-8.5.72.tar.gz index.txt jdk-8u311-linux-x64.tar.gz mynginx_02 test.py test.txt
  29. [root@localhost ~]# cp jdk-8u311-linux-x64.tar.gz /usr/local/dockerfile/
  30. [root@localhost ~]# cp apache-tomcat-8.5.72.tar.gz /usr/local/dockerfile/
  31. [root@localhost dockerfile]# ls
  32. apache-tomcat-8.5.72.tar.gz Dockerfile jdk-8u311-linux-x64.tar.gz

Docker - 图7

3.利用Dockerfile构建镜像

  1. # /usr/local/dockerfile代表Dockerfile所需的安装包存放的位置
  2. [root@localhost dockerfile]# docker build -f /usr/local/dockerfile/Dockerfile -t mycentos:7 /usr/local/dockerfile
  3. Sending build context to Docker daemon 157.4MB
  4. Step 1/11 : FROM centos:7
  5. ---> eeb6ee3f44bd
  6. Step 2/11 : LABEL maintainer="mrhelloworld.com"
  7. ---> Running in acc7170cea8a
  8. Removing intermediate container acc7170cea8a
  9. ---> f31f5a212077
  10. Step 3/11 : WORKDIR /usr/local
  11. ---> Running in e547a711ed7a
  12. Removing intermediate container e547a711ed7a
  13. ---> 5be9f42fcad7
  14. Step 4/11 : RUN mkdir -p /usr/local/java
  15. ---> Running in 1a3724eaec53
  16. Removing intermediate container 1a3724eaec53
  17. ---> 32f4418fdda7
  18. Step 5/11 : RUN mkdir -p /usr/local/tomcat
  19. ---> Running in c418f32a0523
  20. Removing intermediate container c418f32a0523
  21. ---> 8f58e412baca
  22. Step 6/11 : ADD jdk-8u311-linux-x64.tar.gz /usr/local/java
  23. ---> f4ecfe50eafa
  24. Step 7/11 : ADD apache-tomcat-8.5.72.tar.gz /usr/local/tomcat
  25. ---> 66b44bc9677a
  26. Step 8/11 : EXPOSE 8080
  27. ---> Running in 17c52ce48b43
  28. Removing intermediate container 17c52ce48b43
  29. ---> c54233396b41
  30. Step 9/11 : ENV JAVA_HOME=/usr/local/java/jdk1.8.0_311
  31. ---> Running in cfb5833a22c1
  32. Removing intermediate container cfb5833a22c1
  33. ---> 6252c1aba29d
  34. Step 10/11 : ENV PATH=$PATH:$JAVA_HOME/bin
  35. ---> Running in 74cb21f735fd
  36. Removing intermediate container 74cb21f735fd
  37. ---> 2d4b07c042a1
  38. Step 11/11 : CMD ["/usr/local/tomcat/apache-tomcat-8.5.72/bin/catalina.sh","run"]
  39. ---> Running in fcee15fd66ae
  40. Removing intermediate container fcee15fd66ae
  41. ---> bd1032e5d52e
  42. Successfully built bd1032e5d52e
  43. Successfully tagged mycentos:7
  44. [root@localhost dockerfile]#
  45. [root@localhost dockerfile]# docker images
  46. REPOSITORY TAG IMAGE ID CREATED SIZE
  47. mycentos 7 bd1032e5d52e 24 seconds ago 584MB
  48. hello-world latest feb5d9fea6a5 6 weeks ago 13.3kB
  49. centos 7 eeb6ee3f44bd 7 weeks ago 204MB
  50. # 用构建的镜像创建容器
  51. [root@localhost dockerfile]# docker run -di --name mycentos7 -p 8080 mycentos:7
  52. 835d9e6796d019c46bd6b97d1b396a9c47f76df6d09982148684a5d93d7f6b52
  53. # 查看容器是否已运行
  54. [root@localhost dockerfile]# docker ps
  55. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  56. 835d9e6796d0 mycentos:7 "/usr/local/tomcat/a…" 3 seconds ago Up 2 seconds 0.0.0.0:49156->8080/tcp, :::49156->8080/tcp mycentos7
  57. [root@localhost dockerfile]#
  58. # 接着直接在网页中访问ip:8080即可直接访问tomcat

Docker - 图8

9.Docker的备份恢复迁移

1.镜像备份

  1. 使用 **docker save** 将指定镜像保存成tar文件
  1. docker save [OPTIONS] IMAGE [IMAGE...]
  2. docker save -o /root/mycentos7.tar mycentos:7
  • -o :镜像保存后保存的目录

例:

  1. # 查看当前镜像有哪些
  2. [root@localhost ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. hello-world latest feb5d9fea6a5 6 weeks ago 13.3kB
  5. centos 7 eeb6ee3f44bd 8 weeks ago 204MB
  6. # 将centos:7镜像打包成tar文件
  7. [root@localhost ~]# docker save -o /root/mycentos.tar centos:7
  8. # 查看是否生成文件
  9. [root@localhost ~]# ls
  10. anaconda-ks.cfg apache-tomcat-8.5.72.tar.gz index.txt jdk-8u311-linux-x64.tar.gz mycentos.tar mynginx_02 test.py test.txt
  11. [root@localhost ~]#

2.镜像恢复

  1. 使用**docker load**将**docker save**保存的tar文件导入到当前镜像
  1. docker load [OPTIONS]
  2. docker load -i mycentos.tar
  • -i:指定导入的文件
  • -q:精简输出信息

例:

  1. # 删除原始镜像
  2. [root@localhost ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. hello-world latest feb5d9fea6a5 6 weeks ago 13.3kB
  5. centos 7 eeb6ee3f44bd 8 weeks ago 204MB
  6. [root@localhost ~]# docker rmi centos:7
  7. Untagged: centos:7
  8. Untagged: centos@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
  9. Deleted: sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9
  10. Deleted: sha256:174f5685490326fc0a1c0f5570b8663732189b327007e47ff13d2ca59673db02
  11. # 导入我们刚刚导出的镜像
  12. [root@localhost ~]# docker load -i mycentos.tar
  13. 174f56854903: Loading layer [==================================================>] 211.7MB/211.7MB
  14. Loaded image: centos:7
  15. # 查看是否导入
  16. [root@localhost ~]# docker images
  17. REPOSITORY TAG IMAGE ID CREATED SIZE
  18. hello-world latest feb5d9fea6a5 6 weeks ago 13.3kB
  19. centos 7 eeb6ee3f44bd 8 weeks ago 204MB
  20. [root@localhost ~]#

10.DockerHub的使用

1.注册账号

先去https://hub.docker.com/注册一个账号

Docker - 图9

2.登陆账号

然后登陆账号:

  1. [root@localhost ~]# docker login
  2. Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
  3. Username: 1787417712 # 输入自己的账号
  4. Password: # 输入自己的密码
  5. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  6. Configure a credential helper to remove this warning. See
  7. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  8. Login Succeeded # 登陆成功提示
  9. [root@localhost ~]#

3.推送镜像至仓库

为了方便测试,我们将hello-world推送到仓库

先给镜像设置标签:docker tag local-image:tagname new-repo:tagname

再将镜像推送到仓库:docker push new-repo:tagname

  1. # 查看有哪些镜像
  2. [root@localhost ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. hello-world latest feb5d9fea6a5 7 weeks ago 13.3kB
  5. centos 7 eeb6ee3f44bd 8 weeks ago 204MB
  6. # 给一个镜像打标签
  7. [root@localhost ~]# docker tag hello-world:latest 1787417712/test-helloworld:1.0.0
  8. # 再次查看有哪些镜像
  9. [root@localhost ~]# docker images
  10. REPOSITORY TAG IMAGE ID CREATED SIZE
  11. 1787417712/test-helloworld 1.0.0 feb5d9fea6a5 7 weeks ago 13.3kB
  12. hello-world latest feb5d9fea6a5 7 weeks ago 13.3kB
  13. centos 7 eeb6ee3f44bd 8 weeks ago 204MB
  14. # 上传镜像
  15. [root@localhost ~]# docker push 1787417712/test-helloworld:1.0.0
  16. The push refers to repository [docker.io/1787417712/test-helloworld]
  17. e07ee1baac5f: Pushed
  18. 1.0.0: digest: sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4 size: 525
  19. [root@localhost ~]#

接着可以到hub.docker.com登陆自己的账号查看是否上传成功:

Docker - 图10

4.尝试拉取自己上传的镜像

  1. # 查看当前镜像
  2. [root@localhost ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. 1787417712/test-helloworld 1.0.0 feb5d9fea6a5 7 weeks ago 13.3kB
  5. hello-world latest feb5d9fea6a5 7 weeks ago 13.3kB
  6. centos 7 eeb6ee3f44bd 8 weeks ago 204MB
  7. # 删除之前的镜像
  8. [root@localhost ~]# docker rmi 1787417712/test-helloworld:1.0.0
  9. Untagged: 1787417712/test-helloworld:1.0.0
  10. Untagged: 1787417712/test-helloworld@sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4
  11. [root@localhost ~]# docker images
  12. REPOSITORY TAG IMAGE ID CREATED SIZE
  13. hello-world latest feb5d9fea6a5 7 weeks ago 13.3kB
  14. centos 7 eeb6ee3f44bd 8 weeks ago 204MB
  15. # 拉取自己上传的镜像
  16. [root@localhost ~]# docker pull 1787417712/test-helloworld:1.0.0
  17. 1.0.0: Pulling from 1787417712/test-helloworld
  18. Digest: sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4
  19. Status: Downloaded newer image for 1787417712/test-helloworld:1.0.0
  20. docker.io/1787417712/test-helloworld:1.0.0
  21. # 查看是否拉取成功
  22. [root@localhost ~]# docker images
  23. REPOSITORY TAG IMAGE ID CREATED SIZE
  24. 1787417712/test-helloworld 1.0.0 feb5d9fea6a5 7 weeks ago 13.3kB
  25. hello-world latest feb5d9fea6a5 7 weeks ago 13.3kB
  26. centos 7 eeb6ee3f44bd 8 weeks ago 204MB
  27. [root@localhost ~]#

5.退出账号

  1. [root@localhost ~]# docker logout
  2. Removing login credentials for https://index.docker.io/v1/
  3. [root@localhost ~]#

11.Docker私有仓库搭建

1.拉取registry镜像

  1. [root@localhost ~]# docker pull registry
  2. Using default tag: latest
  3. latest: Pulling from library/registry
  4. 79e9f2f55bf5: Pull complete
  5. 0d96da54f60b: Pull complete
  6. 5b27040df4a2: Downloading [===============================================> ] 6.449MB/6.824MB
  7. e2ead8259a04: Downloading
  8. 3790aef225b9: Downloading
  9. latest: Pulling from library/registry
  10. 79e9f2f55bf5: Pull complete
  11. 0d96da54f60b: Pull complete
  12. 5b27040df4a2: Pull complete
  13. e2ead8259a04: Pull complete
  14. 3790aef225b9: Pull complete
  15. Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
  16. Status: Downloaded newer image for registry:latest
  17. docker.io/library/registry:latest
  18. [root@localhost ~]#

2.修改配置文件

Docker - 图11

  1. [root@localhost ~]# vim /etc/docker/daemon.json
  2. # 应用配置
  3. [root@localhost ~]# sudo systemctl daemon-reload
  4. # 重启docker
  5. [root@localhost ~]# systemctl restart docker

3.创建私有仓库容器

  1. [root@localhost ~]# docker run -di --name registry -p 5000:5000 -v /mydata/docker_registry:/var/lib/registry registry
  2. 8ce589a0bce5a7703d5c9fb018cdbdf8e147c05f878f057c4078731f4e8b5d84
  3. [root@localhost ~]#
  4. [root@localhost ~]# cd /mydata/docker_registry/
  5. [root@localhost docker_registry]#
  6. [root@localhost docker_registry]# ls
  7. [root@localhost docker_registry]# docker ps
  8. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  9. 8ce589a0bce5 registry "/entrypoint.sh /etc…" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp registry
  10. [root@localhost docker_registry]#

接着打开浏览器输入ip:5000/v2/_catalog看到:

Docker - 图12

即代表成功!

4.推送镜像到私有仓库

  1. [root@localhost docker_registry]# docker tag hello-world:latest 192.168.1.250:5000/test-helloworld:1.0.0
  2. # 192.168.1.250:5000是自己的ip+端口号
  3. [root@localhost docker_registry]# docker push 192.168.1.250:5000/test-helloworld:1.0.0
  4. The push refers to repository [192.168.1.250:5000/test-helloworld]
  5. e07ee1baac5f: Pushed
  6. 1.0.0: digest: sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4 size: 525
  7. [root@localhost docker_registry]#

刷新浏览器可以看到:

Docker - 图13

5.拉取私有仓库镜像

  1. [root@localhost docker_registry]# docker pull 192.168.1.250:5000/test-helloworld:1.0.0
  2. 1.0.0: Pulling from test-helloworld
  3. Digest: sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4
  4. Status: Downloaded newer image for 192.168.1.250:5000/test-helloworld:1.0.0
  5. 192.168.1.250:5000/test-helloworld:1.0.0
  6. [root@localhost docker_registry]# docker images
  7. REPOSITORY TAG IMAGE ID CREATED SIZE
  8. registry latest b8604a3fe854 3 hours ago 26.2MB
  9. 192.168.1.250:5000/test-helloworld 1.0.0 feb5d9fea6a5 7 weeks ago 13.3kB
  10. 1787417712/test-helloworld 1.0.0 feb5d9fea6a5 7 weeks ago 13.3kB
  11. hello-world latest feb5d9fea6a5 7 weeks ago 13.3kB
  12. centos 7 eeb6ee3f44bd 8 weeks ago 204MB
  13. [root@localhost docker_registry]#

12.Docker网络模式

Docker四种网络模式

Docker网络模式 配置 说明
host模式 –net=host 容器和宿主机共享Network namespace。
container模式 –net=container:NAME_or_ID 容器和另外一个容器共享Network namespace。 kubernetes中的pod就是多个容器共享一个Network namespace。
none模式 –net=none 容器有独立的Network namespace,但并没有对其进行任何网络设置,如分配veth pair 和网桥连接,配置IP等。
bridge模式 –net=bridge (默认为该模式)

1.host模式

创建容器时加上参数:—network host即可

  1. # 创建一个容器,指定网络模式为host
  2. [root@localhost /]# docker run -it --name bbox02 --network host busybox
  3. / #
  4. # 查看网卡ip
  5. / # ip a
  6. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
  7. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  8. inet 127.0.0.1/8 scope host lo
  9. valid_lft forever preferred_lft forever
  10. inet6 ::1/128 scope host
  11. valid_lft forever preferred_lft forever
  12. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
  13. link/ether 00:0c:29:b5:59:76 brd ff:ff:ff:ff:ff:ff
  14. inet 192.168.1.250/24 brd 192.168.1.255 scope global ens33
  15. valid_lft forever preferred_lft forever
  16. inet6 fe80::e136:cd54:f97c:f725/64 scope link
  17. valid_lft forever preferred_lft forever
  18. 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue
  19. link/ether 02:42:23:92:ba:90 brd ff:ff:ff:ff:ff:ff
  20. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  21. valid_lft forever preferred_lft forever
  22. inet6 fe80::42:23ff:fe92:ba90/64 scope link
  23. valid_lft forever preferred_lft forever
  24. / # exit
  25. [root@localhost /]# ip a
  26. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
  27. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  28. inet 127.0.0.1/8 scope host lo
  29. valid_lft forever preferred_lft forever
  30. inet6 ::1/128 scope host
  31. valid_lft forever preferred_lft forever
  32. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  33. link/ether 00:0c:29:b5:59:76 brd ff:ff:ff:ff:ff:ff
  34. inet 192.168.1.250/24 brd 192.168.1.255 scope global ens33
  35. valid_lft forever preferred_lft forever
  36. inet6 fe80::e136:cd54:f97c:f725/64 scope link
  37. valid_lft forever preferred_lft forever
  38. 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
  39. link/ether 02:42:23:92:ba:90 brd ff:ff:ff:ff:ff:ff
  40. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  41. valid_lft forever preferred_lft forever
  42. inet6 fe80::42:23ff:fe92:ba90/64 scope link
  43. valid_lft forever preferred_lft forever
  44. [root@localhost /]#

发现两者是相同的,即host模式就是容器使用的是宿主机的卡,相同的网络环境。

bridge模式

  1. # 容器默认为bridge模式,所以创建的时候可以不加参数即为bridge模式
  2. [root@localhost /]# docker run -it --name bbox02 busybox
  3. / # ip a
  4. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
  5. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  6. inet 127.0.0.1/8 scope host lo
  7. valid_lft forever preferred_lft forever
  8. 10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
  9. link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
  10. inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
  11. valid_lft forever preferred_lft forever
  12. / #
  13. # 查看宿主机网卡情况,发现多了个13: veth7e697be@if12,容器和宿主机通信就是靠的这个网卡
  14. [root@localhost ~]# ip a
  15. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
  16. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  17. inet 127.0.0.1/8 scope host lo
  18. valid_lft forever preferred_lft forever
  19. inet6 ::1/128 scope host
  20. valid_lft forever preferred_lft forever
  21. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  22. link/ether 00:0c:29:b5:59:76 brd ff:ff:ff:ff:ff:ff
  23. inet 192.168.1.250/24 brd 192.168.1.255 scope global ens33
  24. valid_lft forever preferred_lft forever
  25. inet6 fe80::e136:cd54:f97c:f725/64 scope link
  26. valid_lft forever preferred_lft forever
  27. 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
  28. link/ether 02:42:23:92:ba:90 brd ff:ff:ff:ff:ff:ff
  29. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  30. valid_lft forever preferred_lft forever
  31. inet6 fe80::42:23ff:fe92:ba90/64 scope link
  32. valid_lft forever preferred_lft forever
  33. 13: veth7e697be@if12: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP
  34. link/ether 96:86:47:33:2b:55 brd ff:ff:ff:ff:ff:ff link-netnsid 0
  35. inet6 fe80::9486:47ff:fe33:2b55/64 scope link
  36. valid_lft forever preferred_lft forever
  37. [root@localhost ~]#

3.none模式

指定容器为None模式即容器开始没有网络,需要自己配置。

创建容器的时候加上参数—network none即可创建网络模式为none的容器。

4.container模式

创建容器时加上参数:—network container:已运行的容器名/容器ID 即可

  1. # 创建一个网络模式为container模式的容器,用bbox1相同的网络模式
  2. [root@localhost /]# docker run -it --name bbox04 --network container:bbox1 busybox
  3. / # ip a
  4. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
  5. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  6. inet 127.0.0.1/8 scope host lo
  7. valid_lft forever preferred_lft forever
  8. 14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
  9. link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
  10. inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
  11. valid_lft forever preferred_lft forever
  12. / #
  13. # 查看bbox1的网卡信息,发现和bbox04的一样
  14. [root@localhost ~]# docker exec -it bbox1 sh
  15. / # ip a
  16. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
  17. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  18. inet 127.0.0.1/8 scope host lo
  19. valid_lft forever preferred_lft forever
  20. 14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
  21. link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
  22. inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
  23. valid_lft forever preferred_lft forever
  24. / #

12.自定义网络

1.创建网络

  1. # 创建一个新的网络
  2. [root@localhost /]# docker network create custom_network
  3. 358dcd3df93625bb7328383e89fc38cadb7046e5e31e4123bf72976d3ea874ca
  4. # 查看是否创建成功
  5. [root@localhost /]# docker network ls
  6. NETWORK ID NAME DRIVER SCOPE
  7. a4417a6df8fc bridge bridge local
  8. 358dcd3df936 custom_network bridge local
  9. 7e8bff04dc54 host host local
  10. 466f8d7fd78e none null local
  11. [root@localhost /]#
  12. # 分别创建bbox05和bbox06,网络模式选择刚创建的custom
  13. [root@localhost /]# docker run -it --name bbox05 --network custom_network busybox
  14. [root@localhost ~]# docker run -it --name bbox06 --network custom_network busybox
  15. # 进入容器bbox05内,发现可以直接用容器名字ping
  16. / # ping bbox06
  17. PING bbox06 (172.18.0.3): 56 data bytes
  18. 64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.054 ms
  19. 64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.189 ms
  20. 64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.127 ms
  21. 64 bytes from 172.18.0.3: seq=3 ttl=64 time=0.131 ms
  22. 64 bytes from 172.18.0.3: seq=4 ttl=64 time=0.102 ms
  23. ^C
  24. --- bbox06 ping statistics ---
  25. 5 packets transmitted, 5 packets received, 0% packet loss
  26. round-trip min/avg/max = 0.054/0.120/0.189 ms
  27. / #

13.搭建Redis集群

1.环境准备

两台虚拟机:192.168.1.250,192.168.1.251

docker环境:

Docker - 图14

docker镜像:redis

2.搭建

  • 下载redis镜像
    1. [root@localhost ~]# docker pull redis
  • 编写redis配置文件
    在两台虚拟机中分别执行:
    1. [root@localhost ~]# mkdir -p /usr/local/docker-redis/redis-cluster
    2. [root@localhost ~]# cd /usr/local/docker-redis/redis-cluster/
    3. [root@localhost redis-cluster]# vi redis-cluster.tmpl


在192.168.1.250中编辑文件写入:

  1. port ${PORT} # 端口
  2. requirepass 1234 # 密码
  3. masterauth 1234 # 集群节点密码
  4. protected-mode no # 安全模式- 关闭
  5. daemonize no # 是否后台
  6. appendonly yes # aof文件是否开启
  7. cluster-enabled yes # 集群环境是否开启
  8. cluster-config-file nodes.conf # 配置文件名称
  9. cluster-node-timeout 15000 # 超市时间
  10. cluster-announce-ip 192.168.1.250 # ip
  11. cluster-announce-port ${PORT} # 集群端口
  12. cluster-announce-bus-port 1${PORT} # 消息总线内部端口


在192.168.1.251中编辑文件写入:

  1. port ${PORT} # 端口
  2. requirepass 1234 # 密码
  3. masterauth 1234 # 集群节点密码
  4. protected-mode no # 安全模式- 关闭
  5. daemonize no # 是否后台
  6. appendonly yes # aof文件是否开启
  7. cluster-enabled yes # 集群环境是否开启
  8. cluster-config-file nodes.conf # 配置文件名称
  9. cluster-node-timeout 15000 # 超市时间
  10. cluster-announce-ip 192.168.1.251 # ip
  11. cluster-announce-port ${PORT} # 集群端口
  12. cluster-announce-bus-port 1${PORT} # 消息总线内部端口


在192.168.1.250执行:

  1. for port in `seq 6371 6373`; do \
  2. mkdir -p ${port}/conf \
  3. && PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf \
  4. && mkdir -p ${port}/data;\
  5. done


在192.168.1.251执行:

  1. for port in `seq 6374 6376`; do \
  2. mkdir -p ${port}/conf \
  3. && PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf \
  4. && mkdir -p ${port}/data;\
  5. done


在192.168.1.250中执行:

  1. [root@localhost redis-cluster]# tree /usr/local/docker-redis/redis-cluster/
  2. /usr/local/docker-redis/redis-cluster/
  3. ├── 6371
  4. ├── conf
  5. └── redis.conf
  6. └── data
  7. ├── 6372
  8. ├── conf
  9. └── redis.conf
  10. └── data
  11. ├── 6373
  12. ├── conf
  13. └── redis.conf
  14. └── data
  15. └── redis-cluster.tmpl
  16. 9 directories, 4 files
  17. [root@localhost redis-cluster]#


在192.168.1.251中执行:

  1. [root@localhost redis-cluster]# tree /usr/local/docker-redis/redis-cluster/
  2. /usr/local/docker-redis/redis-cluster/
  3. ├── 6374
  4. ├── conf
  5. └── redis.conf
  6. └── data
  7. ├── 6375
  8. ├── conf
  9. └── redis.conf
  10. └── data
  11. ├── 6376
  12. ├── conf
  13. └── redis.conf
  14. └── data
  15. └── redis-cluster.tmpl
  16. 9 directories, 4 files
  17. [root@localhost redis-cluster]#
  • 创建redis容器

在192.168.1.250中执行:

  1. for port in $(seq 6371 6373); do \
  2. docker run -id --restart always --name redis-${port} --net host \
  3. -v /usr/local/docker-redis/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
  4. -v /usr/local/docker-redis/redis-cluster/${port}/data:/data \
  5. redis redis-server /usr/local/etc/redis/redis.conf; \
  6. done

在192.168.1.251中执行:

  1. for port in $(seq 6374 6376); do \
  2. docker run -id --restart always --name redis-${port} --net host \
  3. -v /usr/local/docker-redis/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
  4. -v /usr/local/docker-redis/redis-cluster/${port}/data:/data \
  5. redis redis-server /usr/local/etc/redis/redis.conf; \
  6. done

就分别在两台虚拟机中各创建了3个redis容器

  • 创建redis cluster集群

在192.168.1.250中执行:

  1. [root@localhost redis-cluster]# docker exec -it redis-6371 bash
  2. root@localhost:/data# cd /usr/local/bin/
  3. root@localhost:/usr/local/bin# ls
  4. docker-entrypoint.sh gosu redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
  5. root@localhost:/usr/local/bin#
  6. root@localhost:/data# redis-cli -a 1234 --cluster create 192.168.1.250:6371 192.168.1.250:6372 192.168.1.250:6373 192.168.1.251:6374 192.168.1.251:6375 192.168.1.251:6376 --cluster-replicas 1
  7. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  8. >>> Performing hash slots allocation on 6 nodes...
  9. Master[0] -> Slots 0 - 5460
  10. Master[1] -> Slots 5461 - 10922
  11. Master[2] -> Slots 10923 - 16383
  12. Adding replica 192.168.1.251:6376 to 192.168.1.250:6371
  13. Adding replica 192.168.1.250:6373 to 192.168.1.251:6374
  14. Adding replica 192.168.1.251:6375 to 192.168.1.250:6372
  15. M: 456297129cb486b101c40e341365c10aa75ef9d6 192.168.1.250:6371
  16. slots:[0-5460] (5461 slots) master
  17. M: ce097bae6e524e927a3c618cff0842b8e2ee733e 192.168.1.250:6372
  18. slots:[10923-16383] (5461 slots) master
  19. S: 0dac43912efbda099854300fa6e1026247ff0808 192.168.1.250:6373
  20. replicates 1f11869fb991614ba37bc21944e9fe1a39a667ea
  21. M: 1f11869fb991614ba37bc21944e9fe1a39a667ea 192.168.1.251:6374
  22. slots:[5461-10922] (5462 slots) master
  23. S: 34365fc1c7931a5025364a7acd6f8c0a7b59e538 192.168.1.251:6375
  24. replicates ce097bae6e524e927a3c618cff0842b8e2ee733e
  25. S: 7b13fc4d721522edd661755e058a9dda7b6dc579 192.168.1.251:6376
  26. replicates 456297129cb486b101c40e341365c10aa75ef9d6
  27. Can I set the above configuration? (type 'yes' to accept): yes
  28. >>> Nodes configuration updated
  29. >>> Assign a different config epoch to each node
  30. >>> Sending CLUSTER MEET messages to join the cluster
  31. Waiting for the cluster to join
  32. .
  33. >>> Performing Cluster Check (using node 192.168.1.250:6371)
  34. M: 456297129cb486b101c40e341365c10aa75ef9d6 192.168.1.250:6371
  35. slots:[0-5460] (5461 slots) master
  36. 1 additional replica(s)
  37. M: ce097bae6e524e927a3c618cff0842b8e2ee733e 192.168.1.250:6372
  38. slots:[10923-16383] (5461 slots) master
  39. 1 additional replica(s)
  40. M: 1f11869fb991614ba37bc21944e9fe1a39a667ea 192.168.1.251:6374
  41. slots:[5461-10922] (5462 slots) master
  42. 1 additional replica(s)
  43. S: 0dac43912efbda099854300fa6e1026247ff0808 192.168.1.250:6373
  44. slots: (0 slots) slave
  45. replicates 1f11869fb991614ba37bc21944e9fe1a39a667ea
  46. S: 7b13fc4d721522edd661755e058a9dda7b6dc579 192.168.1.251:6376
  47. slots: (0 slots) slave
  48. replicates 456297129cb486b101c40e341365c10aa75ef9d6
  49. S: 34365fc1c7931a5025364a7acd6f8c0a7b59e538 192.168.1.251:6375
  50. slots: (0 slots) slave
  51. replicates ce097bae6e524e927a3c618cff0842b8e2ee733e
  52. [OK] All nodes agree about slots configuration.
  53. >>> Check for open slots...
  54. >>> Check slots coverage...
  55. [OK] All 16384 slots covered.
  56. root@localhost:/data#

14.DockerCompose安装

  1. # 安装
  2. [root@localhost redis-cluster]# sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  3. % Total % Received % Xferd Average Speed Time Time Time Current
  4. Dload Upload Total Spent Left Speed
  5. 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 100 633 100 633 0 0 880 0 --:--:-- --:--:-- --:--:-- 881
  6. 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 6 12.1M 6 804k 0 0 383k 0 0:00:32 0:00:02 0:00:30 100 12.1M 100 12.1M 0 0 204k 0 0:01:00 0:01:00 --:--:-- 162k
  7. [root@localhost redis-cluster]# cd /usr/local/bin/
  8. [root@localhost bin]# ls
  9. docker-compose
  10. # 赋予执行权限
  11. [root@localhost bin]# sudo chmod +x /usr/local/bin/docker-compose
  12. [root@localhost bin]# ll
  13. total 12440
  14. -rwxr-xr-x. 1 root root 12737304 Nov 14 02:27 docker-compose
  15. [root@localhost bin]#