25.1 docker简介

  • 官网 www.docker.com

  • github  https://github.com/docker/docker.github.io

  • 开源的容器引擎,可以让开发者打包应用以及依赖的库,然后发布到任何流行的linux发行版上,移植很方便

  • 由 go 语言编写,基于 apache2.0 协议发布

  • 基于 linux kernel,要想在 win 下运行需要借助一个 vm(虚拟机)来实现

  • 自 2013 年开始,近些年发展迅猛

  • docker 从1.13x开始,版本分为社区版 ce 和企业版 ee,并且基于年月的时间线形式,当前最新稳定版为 17.09 参考http://blog.csdn.net/chenhaifeng2016/article/details/6806241

Docker和传统的虚拟化比较:

Docker的优势:

  • 启动非常快,秒级实现

  • 资源利用率高,一台高配置服务器可以跑上千个docker容器

  • 更快的交付和部署,一次创建和配置后,可以在任意地方运行

  • 内核级别的虚拟化,不需要额外的hypevisor支持,会有更高的性能和效率

  • 易迁移,平台依赖性不强

Docker核心概念:

  • 镜像,是一个只读的模板,类似于安装系统用到的那个iso文件,我们通过镜像来完成各种应用的部署。

  • 容器,镜像类似于操作系统,而容器类似于虚拟机本身。它可以被启动、开始、停止、删除等操作,每个容器都是相互隔离的。

  • 仓库,存放镜像的一个场所,仓库分为公开仓库和私有仓库。 最大的公开仓库是Docker hub(hub.docker.com),国内公开仓库(dockerpool.com)

25.2 安装docker

1、下载 yum 源,并安装

[root@arslinux-01 ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o  /etc/yum.repos.d/docker.repo
[root@arslinux-01 ~]# yum install -y docker-ce

速度比较慢,也可以直接下载rpm包

https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

上传到 linux 下并 yum 安装,可以自动解决依赖 yum install -y docker-ce-xxxx.rpm

2、启动 docker

[root@arslinux-01 ~]# systemctl start docker

25.3 镜像管理

  • 下载 centos 镜像

[root@arslinux-01 ~]# docker pull centos

速度比较慢,可以使用 docker 加速器

  • 配置 docker 加速器

[root@arslinux-01 ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://dhq9bx4f.mirror.aliyuncs.com"]
}

说明:这个url为加速器地址,需要去ali云申请

配置完加速器,重启 docker,再次 docker pull centos 会快很多

  • docker image              查看本地镜像

  • docker search xxx          搜索镜像,其中 xxx 为关键词

  • docker tag 镜像名 标签      给镜像打标签(就是更改镜像名称)

[root@arslinux-01 ~]# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   5493                [OK]
ansible/centos7-ansible            Ansible on Centos7                              122                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.6.1810 x86…   111                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   96                                      [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   59
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              57                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      44
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   39
kinogmt/centos-ssh                 CentOS with SSH                                 28                                      [OK]
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   10
guyton/centos6                     From official centos6 container with full up…   9                                       [OK]
drecom/centos-ruby                 centos ruby                                     6                                       [OK]
pivotaldata/centos                 Base centos, freshened up a little with a Do…   3
mamohr/centos-java                 Oracle Java 8 Docker image based on Centos 7    3                                       [OK]
darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   2
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   2
miko2u/centos6                     CentOS6 日本語環境                                   2                                       [OK]
ovirtguestagent/centos7-atomic     The oVirt Guest Agent for Centos 7 Atomic Ho…   2
indigo/centos-maven                Vanilla CentOS 7 with Oracle Java Developmen…   1                                       [OK]
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
mcnaughton/centos-base             centos base image                               1                                       [OK]
pivotaldata/centos7-dev            CentosOS 7 image for GPDB development           0
smartentry/centos                  centos with smartentry                          0                                       [OK]
pivotaldata/centos6.8-dev          CentosOS 6.8 image for GPDB development         0
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
centos              latest              9f38484d220f        4 months ago        202MB
  • 如果想更改上方 TAG 处信息,可以用 : 将 REPOSITORY : TAG 分开

[root@arslinux-01 ~]# docker tag centos test111:190808
[root@arslinux-01 ~]# docker tag centos test111:1234
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
centos              latest              9f38484d220f        4 months ago        202MB
test111             1234                9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

这里的 TAG 和命令中的 tag 不是一个概念

  • docker run -itd 镜像名          把镜像启动为容器

  • docker ps              查看运行的容器

  • docker ps -a          查看所有容器(包括未运行的)

[root@arslinux-01 ~]# docker run -itd centos
9d06f43b687172d3cd5ab13529645482f2a9d6fad286c0dd82a5783863db2617
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d06f43b6871        centos              "/bin/bash"         5 seconds ago       Up 3 seconds                            zealous_mestorf
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9d06f43b6871        centos              "/bin/bash"         3 minutes ago       Up 3 minutes                            zealous_mestorf

说明:-i 表示让容器的标准输入打开,-t 表示分配一个伪终端,-d 表示后台启动,要把 -i -t -d 放到镜像名字前面

  • docker rmi 镜像名          删除指定镜像

[root@arslinux-01 ~]# docker rmi test111
Error: No such image: test111
[root@arslinux-01 ~]# docker rmi test111:1234
Untagged: test111:1234
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
centos              latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

其中后面的参数可以是 tag,如果是 tag 时,实际上是删除该 tag。当后面的参数为镜像 ID 时,则会彻底删除整个镜像,所有标签也会一同删除

不加 TAG 删除镜像,实际 TAG 就是 latest ,docker rmi test111 就是 docker rmi test111:latest

25.4 通过容器创建镜像

1、启动容器

[root@arslinux-01 ~]# docker run -itd centos
77fbe7aeaee649478aae095af386e6b7796e96e865bb95f7d673875fdf6767ec
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
77fbe7aeaee6        centos              "/bin/bash"         15 seconds ago      Up 12 seconds                           blissful_yonath

2、进入容器    docker exec -it xxxxx  bash

[root@arslinux-01 ~]# docker exec -it 77fbe7 bash
[root@77fbe7aeaee6 /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
overlay          28G  6.1G   22G  22% /
tmpfs            64M     0   64M   0% /dev
tmpfs           487M     0  487M   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/sda3        28G  6.1G   22G  22% /etc/hosts
tmpfs           487M     0  487M   0% /proc/asound
tmpfs           487M     0  487M   0% /proc/acpi
tmpfs           487M     0  487M   0% /proc/scsi
tmpfs           487M     0  487M   0% /sys/firmware
[root@77fbe7aeaee6 /]# free
total        used        free      shared  buff/cache   available
Mem:         995896      654636       73140        2620      268120      133448
Swap:       1999868      169472     1830396

说明:其中xxxxx为容器id,这个id可以用docker ps查看,最后面的bash为进入容器后我们要执行的命令,这样就可以打开一个终端

进入到该容器中,我们做一些变更,比如安装一些东西,然后针对这个容器进行创建新的镜像

3、容器中的操作和机器中操作没有区别

[root@77fbe7aeaee6 /]# yum install -y net-tools
[root@77fbe7aeaee6 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)RX packets 3381  bytes 14925255 (14.2 MiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 2448  bytes 135606 (132.4 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0loop  txqueuelen 1000  (Local Loopback)RX packets 0  bytes 0 (0.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 0  bytes 0 (0.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0[root@77fbe7aeaee6 /]# exit
[root@arslinux-01 ~]# ifconfig |grep docker
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255inet6 fe80::42:c8ff:fe99:eec1  prefixlen 64  scopeid 0x20<link>ether 02:42:c8:99:ee:c1  txqueuelen 0  (Ethernet)RX packets 2448  bytes 101334 (98.9 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 3373  bytes 14924599 (14.2 MiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

容器中的网卡在宿主机上也出现了

每开启一个容器,都会有一个虚拟网卡让容器和虚拟机通信

[root@arslinux-01 ~]# ifconfig |grep veth
veth47c82bd: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet6 fe80::5014:d2ff:fe64:e486  prefixlen 64  scopeid 0x20<link>ether 52:14:d2:64:e4:86  txqueuelen 0  (Ethernet)RX packets 2448  bytes 135606 (132.4 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 3381  bytes 14925255 (14.2 MiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

4、将容器做成镜像docker commit -m "具体操作" -a "操作人" 容器id 新镜像名称

[root@arslinux-01 ~]# docker commit -m "install net-tools" -a "arslinux" 77fbe7aeaee6 centos_with_net
sha256:9c213599fd8f289da3333e0a0ddb55d8f208c020e3ace833461d2f0706cdd4d6
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos_with_net     latest              9c213599fd8f        4 seconds ago       314MB
centos              latest              8f92b9698cb1        3 minutes ago       314MB
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

说明:-m 加一些改动信息,-a 指定作者相关信息  77fbe7aeaee6 这一串为容器id,再后面为新镜像的名字

5、打开新镜像的容器并进入

[root@arslinux-01 ~]# docker run -itd centos_with_net
b1778f203633755d801b022e6db3c07d781f010acf4681cfe5f86b7639b18d6f
[root@arslinux-01 ~]# docker exec -it b1778f bash
[root@b1778f203633 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)RX packets 8  bytes 656 (656.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 0  bytes 0 (0.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0loop  txqueuelen 1000  (Local Loopback)RX packets 0  bytes 0 (0.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 0  bytes 0 (0.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0[root@b1778f203633 /]# ping www.baidu.com
PING www.a.shifen.com (180.97.33.107) 56(84) bytes of data.
64 bytes from 180.97.33.107 (180.97.33.107): icmp_seq=5 ttl=127 time=6.15 ms
64 bytes from 180.97.33.107 (180.97.33.107): icmp_seq=6 ttl=127 time=6.53 ms
^C
--- www.a.shifen.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 14037ms
rtt min/avg/max/mdev = 5.900/6.595/7.355/0.532 ms

25.5 通过模板创建镜像

1、下载模板 http://download.openvz.org/template/precreated/

本地下完后上传到虚拟机中

2、cat tar包 | docker import - 镜像名(自定义)      导入镜像

[root@arslinux-01 ~]# cat centos-6-x86-minimal.tar.gz |docker import - centos6
sha256:d0fc4ef11ea3c1cd7039fb595bdb51023d1f01af68d7c58c45b201000e12306e
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              d0fc4ef11ea3        9 seconds ago       512MB
centos_with_net     latest              9c213599fd8f        53 minutes ago      314MB
centos              latest              8f92b9698cb1        56 minutes ago      314MB
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

3、启动容器,进入

[root@arslinux-01 ~]# docker run -itd centos6
ced4739b62045d77aa6983589a1e43edeacad2b8ec979b77a47b901131cd637
[root@arslinux-01 ~]# docker exec -it ced4739b62 bash
[root@ced4739b6204 /]# cat /etc/issue
CentOS release 6.8 (Final)
Kernel \r on an \m
[root@ced4739b6204 /]# uname -a
Linux ced4739b6204 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

docker 用的是虚拟机 linux 的内核,哪怕是 centos6 的docker ,用的依然是虚拟机 centos7 的内核

4、docker save -o 保存的文件名 镜像名 导出镜像

[root@arslinux-01 ~]# docker save -o centos7_with_nettool.tar centos_with_net
[root@arslinux-01 ~]# du -sh centos7_with_nettool.tar
307Mcento7_with_nettool.tar

5、先删除镜像

[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ced4739b6204        centos6             "bash"              11 minutes ago      Up 11 minutes                           suspicious_perlman
b1778f203633        centos_with_net     "/bin/bash"         About an hour ago   Up About an hour                        dazzling_bell
77fbe7aeaee6        9f38484d220f        "/bin/bash"         2 hours ago         Up 2 hours                              blissful_yonath
[root@arslinux-01 ~]# docker rm -f b1778f203633
b1778f203633
[root@arslinux-01 ~]# docker rmi 9c213599fd8f
Untagged: centos_with_net:latest
Deleted: sha256:9c213599fd8f289da3333e0a0ddb55d8f208c020e3ace833461d2f0706cdd4d6
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              d0fc4ef11ea3        12 minutes ago      512MB
centos              latest              8f92b9698cb1        About an hour ago   314MB
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

6、再用 docker load 载入

  • docker load imput 镜像包

  • docker load < 镜像包

以上两种命令皆可

[root@arslinux-01 ~]# docker load < centos7_with_nettool.tar
Loaded image: centos_with_net:latest
[root@arslinux-01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos6             latest              d0fc4ef11ea3        16 minutes ago      512MB
centos_with_net     latest              9c213599fd8f        About an hour ago   314MB
centos              latest              8f92b9698cb1        About an hour ago   314MB
ubuntu              latest              3556258649b2        2 weeks ago         64.2MB
arslinux            latest              9f38484d220f        4 months ago        202MB
test111             190808              9f38484d220f        4 months ago        202MB

25.6 容器管理

  • docker create -it 镜像名 bash      创建容器

  • docker start 容器id      启动容器

[root@arslinux-01 ~]# docker create -it centos6 bash
03cea99df28677d8b4ade4e1d729d14b3c0d4c8d183cc9e76f239456ed878534
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
03cea99df286        centos6             "bash"              8 seconds ago       Created                                       hopeful_darwin
ced4739b6204        centos6             "bash"              37 minutes ago      Up 37 minutes                                 suspicious_perlman
77fbe7aeaee6        9f38484d220f        "/bin/bash"         2 hours ago         Up 2 hours                                    blissful_yonath
9d06f43b6871        9f38484d220f        "/bin/bash"         2 days ago          Exited (137) 2 days ago                       zealous_mestorf
[root@arslinux-01 ~]# docker start 03cea99df286
03cea99df286
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
03cea99df286        centos6             "bash"              29 seconds ago      Up 5 seconds                            hopeful_darwin
ced4739b6204        centos6             "bash"              37 minutes ago      Up 37 minutes                           suspicious_perlman
77fbe7aeaee6        9f38484d220f        "/bin/bash"         2 hours ago         Up 2 hours                              blissful_yonath

启动容器后,可以使用 docker ps  查看到,有 start 就有 stop,和 restart

docker create 再 docker start 相当于 docker run

  • docker run -it 镜像名 bash     启动并进入容器

[root@arslinux-01 ~]# docker run -it centos bash
  • docker run -d   可以让容器在后台运行

例如:docker run -d centos bash -c "while :; do echo "123"; sleep 2; done"

  • docker run --name 名称 -itd 镜像 bash      给容器自定义名字

[root@arslinux-01 ~]# docker run --name arsenal_pepe -itd centos6 bash
6b975789ec240520e4316699cfb83a9752a84da8890f62b18085ce759d252152
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6b975789ec24        centos6             "bash"              22 seconds ago      Up 21 seconds                           arsenal_pepe
03cea99df286        centos6             "bash"              27 minutes ago      Up 27 minutes                           hopeful_darwin
ced4739b6204        centos6             "bash"              About an hour ago   Up About an hour                        suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"         2 hours ago         Up 2 hours                              blissful_yonath
[root@arslinux-01 ~]# docker exec -it arsenal_pepe bash
[root@6b975789ec24 /]#
  • docker run --rm -it 容器名 bash -c "命令"      命令执行完容器就会退出

[root@arslinux-01 ~]# docker run --rm -it centos bash -c "sleep 15"

--rm 可以让容器退出后直接删除,在这里命令执行完容器就会退出

  • docker logs 容器id      查看容器运行历史信息

[root@arslinux-01 ~]# docker run -itd centos bash -c "echo 123"
066f53f67367fe0da907c26d7abd2d0c95acff40eaf2a0203fba556787f4ab83
[root@arslinux-01 ~]# docker logs 066f53f67367
123
  • docker attach 容器id      进入后台运行的容器

  • docker exec -it 容器id bash      临时打开一个虚拟终端,exit 后,容器依然运行着

attach命令不算好用,比如我们想要退出终端,就得exit了,这样容器也就退出了,用 exec 比较

  • docker rm 容器id      删除容器

[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                        PORTS               NAMES
066f53f67367        centos              "bash -c 'echo 123'"   5 minutes ago       Exited (0) 5 minutes ago                          sharp_liskov
6b975789ec24        centos6             "bash"                 18 minutes ago      Up 18 minutes                                     arsenal_pepe
944bc4839130        centos              "bash"                 30 minutes ago      Exited (127) 30 minutes ago                       nervous_sammet
37366b75bd90        centos              "/bin/bash"            30 minutes ago      Exited (127) 30 minutes ago                       inspiring_saha
a80eed529a34        centos              "bash"                 31 minutes ago      Exited (0) 30 minutes ago                         hungry_elgamal
32da41e087c2        centos              "bash"                 32 minutes ago      Exited (0) 32 minutes ago                         infallible_tereshkova
985de0f4f889        centos              "bash"                 33 minutes ago      Exited (0) 33 minutes ago                         nifty_nightingale
d55e81eae6aa        centos              "/bin/bash"            35 minutes ago      Exited (0) 33 minutes ago                         condescending_varahamihira
02bccad25efb        centos              "/bin/bash"            38 minutes ago      Exited (0) 37 minutes ago                         heuristic_wu
03cea99df286        centos6             "bash"                 46 minutes ago      Up 45 minutes                                     hopeful_darwin
ced4739b6204        centos6             "bash"                 About an hour ago   Up About an hour                                  suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"            3 hours ago         Up 3 hours                                        blissful_yonath
9d06f43b6871        centos              "/bin/bash"            2 days ago          Exited (137) 2 days ago                           zealous_mestorf
[root@arslinux-01 ~]# docker rm 066f53f67367
066f53f67367
[root@arslinux-01 ~]# docker rm 944bc4839130
944bc4839130
[root@arslinux-01 ~]# docker rm 37366b75bd90
37366b75bd90
[root@arslinux-01 ~]# docker rm a80eed529a34
a80eed529a34
[root@arslinux-01 ~]# docker rm 32da41e087c2
32da41e087c2
[root@arslinux-01 ~]# docker rm 985de0f4f889
985de0f4f889
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
6b975789ec24        centos6             "bash"              19 minutes ago      Up 19 minutes                                   arsenal_pepe
d55e81eae6aa        centos              "/bin/bash"         36 minutes ago      Exited (0) 34 minutes ago                       condescending_varahamihira
02bccad25efb        centos              "/bin/bash"         40 minutes ago      Exited (0) 38 minutes ago                       heuristic_wu
03cea99df286        centos6             "bash"              47 minutes ago      Up 46 minutes                                   hopeful_darwin
ced4739b6204        centos6             "bash"              About an hour ago   Up About an hour                                suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"         3 hours ago         Up 3 hours                                      blissful_yonath
9d06f43b6871        centos              "/bin/bash"         2 days ago          Exited (137) 2 days ago                         zealous_mestor

——对于启动状态的容器,删除需要加 -f 强行删除

[root@arslinux-01 ~]# docker rm 03cea99df286
Error response from daemon: You cannot remove a running container 03cea99df28677d8b4ade4e1d729d14b3c0d4c8d183cc9e76f239456ed878534. Stop the container before attempting removal or force remove
[root@arslinux-01 ~]# docker rm -f 03cea99df286
03cea99df286
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
6b975789ec24        centos6             "bash"              21 minutes ago      Up 21 minutes                                   arsenal_pepe
d55e81eae6aa        centos              "/bin/bash"         38 minutes ago      Exited (0) 36 minutes ago                       condescending_varahamihira
02bccad25efb        centos              "/bin/bash"         41 minutes ago      Exited (0) 40 minutes ago                       heuristic_wu
ced4739b6204        centos6             "bash"              About an hour ago   Up About an hour                                suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"         3 hours ago         Up 3 hours                                      blissful_yonath
9d06f43b6871        centos              "/bin/bash"         2 days ago          Exited (137) 2 days ago                         zealous_mestor
  • docker export 容器id > 文件.tar      导出容器

  • cat 文件.tar |docker import - 镜像名      生成镜像

[root@arslinux-01 ~]# docker export 9d06f43b6871 > centos.tar

[root@arslinux-01 ~]# cat centos.tar |docker import - centos111

sha256:48f71221434353e6f02227794e1a95fcc8ae5f0dc9608142febc957dd124dba0

25.7 仓库管理

  • docker start          启动未启动的容器(重启docker服务后,容器会退出)

[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
6b975789ec24        centos6             "bash"              27 hours ago        Exited (137) 22 hours ago                       arsenal_pepe
d55e81eae6aa        centos              "/bin/bash"         28 hours ago        Exited (0) 28 hours ago                         condescending_varahamihira
02bccad25efb        centos              "/bin/bash"         28 hours ago        Exited (0) 28 hours ago                         heuristic_wu
ced4739b6204        centos6             "bash"              29 hours ago        Exited (137) 22 hours ago                       suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"         30 hours ago        Exited (137) 22 hours ago                       blissful_yonath
9d06f43b6871        centos              "/bin/bash"         3 days ago          Exited (137) 3 days ago                         zealous_mestorf
[root@arslinux-01 ~]# docker start 6b975789ec24
6b975789ec24
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
6b975789ec24        centos6             "bash"              27 hours ago        Up 1 second                             arsenal_pepe

1、利用 registry 创建私有仓库

1)下载 registry 镜像

[root@arslinux-01 ~]# docker pull registry

2)启动 registry 容器

  • docker run -d -p 宿主机端口号:容器端口号 registry      启动 registry 容器

[root@arslinux-01 ~]# docker run -d -p 5000:5000 registry
3f51ff27501d3f81a29996cc04b8a16ae9b85a9c5b03f4876d13f16fc53e2315
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
3f51ff27501d        registry            "/entrypoint.sh /etc…"   9 seconds ago       Up 7 seconds        0.0.0.0:5000->5000/tcp   quizzical_chandrasekhar
6b975789ec24        centos6             "bash"                   28 hours ago        Up 14 minutes                                arsenal_pepe

说明:以registry镜像启动容器,-p会把容器的端口映射到宿主机上,:左边为宿主机监听端口,:右边为容器监听端口

3)查看 registry 仓库内容

  • curl 127.0.0.1:5000/v2/_catalog     查看 registry 仓库内容

[root@arslinux-01 ~]# curl 127.0.0.1:5000/v2/_catalog
{"repositories":[]}

2、上传镜像到私有仓库

1)标记镜像

  • docker tag 要标记的镜像 ip:端口/镜像      标记tag,必须带私有仓库 ip:端口

[root@arslinux-01 ~]# docker tag centos6 192.168.194.130:5000/centos6
[root@arslinux-01 ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
192.168.194.130:5000/centos6   latest              d0fc4ef11ea3        29 hours ago        512MB
centos6                        latest              d0fc4ef11ea3        29 hours ago        512MB
centos_with_net                latest              9c213599fd8f        30 hours ago        314MB
ubuntu                         latest              3556258649b2        2 weeks ago         64.2MB
arslinux                       latest              9f38484d220f        5 months ago        202MB
centos                         latest              9f38484d220f        5 months ago        202MB
test111                        190808              9f38484d220f        5 months ago        202MB
registry                       latest              f32a97de94e1        5 months ago        25.8MB

2)上传镜像

  • docker push ip:端口/镜像      上传镜像到 registry

[root@arslinux-01 ~]# docker push 192.168.194.130:5000/centos6
The push refers to repository [192.168.194.130:5000/centos6]
Get https://192.168.194.130:5000/v2/: http: server gave HTTP response to HTTPS client

3)指定私有仓库地址

以上报错需要修改配置文件 /etc/docker/daemon.json,在 { } 中加一行内容(如果原本有内容需用逗号 , 隔开)

[root@arslinux-01 ~]# vim /etc/docker/daemon.json
{ "insecure-registries":["192.168.194.130:5000"] }

4)重启 docker、镜像

[root@arslinux-01 ~]# systemctl restart docker
[root@arslinux-01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
3f51ff27501d        registry            "/entrypoint.sh /etc…"   31 minutes ago      Exited (2) 2 minutes ago                         quizzical_chandrasekhar
6b975789ec24        centos6             "bash"                   28 hours ago        Exited (137) 4 minutes ago                       arsenal_pepe
d55e81eae6aa        centos              "/bin/bash"              28 hours ago        Exited (0) 28 hours ago                          condescending_varahamihira
02bccad25efb        centos              "/bin/bash"              29 hours ago        Exited (0) 29 hours ago                          heuristic_wu
ced4739b6204        centos6             "bash"                   29 hours ago        Exited (137) 23 hours ago                        suspicious_perlman
77fbe7aeaee6        centos              "/bin/bash"              31 hours ago        Exited (137) 23 hours ago                        blissful_yonath
9d06f43b6871        centos              "/bin/bash"              4 days ago          Exited (137) 4 days ago                          zealous_mestorf
[root@arslinux-01 ~]# docker start 3f51ff27501d
3f51ff27501d
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
3f51ff27501d        registry            "/entrypoint.sh /etc…"   31 minutes ago      Up 3 seconds        0.0.0.0:5000->5000/tcp   quizzical_chandrasekhar

5)重新上传

[root@arslinux-01 ~]# docker push 192.168.194.130:5000/centos6
The push refers to repository [192.168.194.130:5000/centos6]
0a2f11f7b1ef: Pushed
latest: digest: sha256:5c0d4992624653abc8764aec2409903be43105246fffd961fa69c3adbc78f0be size: 529

6)查看仓库内容 curl

[root@arslinux-01 ~]# curl 192.168.194.130:5000/v2/_catalog
{"repositories":["centos6"]}

7)再用以上方法上传 ubuntu

[root@arslinux-01 ~]# docker tag ubuntu 192.168.194.130:5000/ubuntu
[root@arslinux-01 ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
192.168.194.130:5000/centos6   latest              d0fc4ef11ea3        29 hours ago        512MB
centos6                        latest              d0fc4ef11ea3        29 hours ago        512MB
centos_with_net                latest              9c213599fd8f        30 hours ago        314MB
192.168.194.130:5000/ubuntu    latest              3556258649b2        2 weeks ago         64.2MB
ubuntu                         latest              3556258649b2        2 weeks ago         64.2MB
centos                         latest              9f38484d220f        5 months ago        202MB
test111                        190808              9f38484d220f        5 months ago        202MB
arslinux                       latest              9f38484d220f        5 months ago        202MB
registry                       latest              f32a97de94e1        5 months ago        25.8MB
[root@arslinux-01 ~]# docker push 192.168.194.130:5000/ubuntu
The push refers to repository [192.168.194.130:5000/ubuntu]
b079b3fa8d1b: Pushed
a31dbd3063d7: Pushed
c56e09e1bd18: Pushed
543791078bdb: Pushed
latest: digest: sha256:d91842ef309155b85a9e5c59566719308fab816b40d376809c39cf1cf4de3c6a size: 1152
[root@arslinux-01 ~]# curl 192.168.194.130:5000/v2/_catalog
{"repositories":["centos6","ubuntu"]}

25.8 数据管理

  • 其他机器如何从私有仓库下载?

1、使用的机器安装 docker,启动docker

[root@arslinux-02 ~]# yum install -y docker-ce-19.03.1-3.el7.x86_64.rpm
[root@arslinux-02 ~]# systemctl start docker

2、编辑 /etc/docker/daemon.json,添加私有仓库地址,重启 docker

[root@arslinux-02 ~]# vim /etc/docker/daemon.json
{ "insecure-registries":["192.168.194.130:5000"] }
[root@arslinux-02 ~]# systemctl restart docker

3、从仓库下载镜像

[root@arslinux-02 ~]# docker pull 192.168.194.130:5000/ubuntu
Using default tag: latest
latest: Pulling from ubuntu
7413c47ba209: Pull complete
0fe7e7cbb2e8: Pull complete
1d425c982345: Pull complete
344da5c95cec: Pull complete
Digest: sha256:d91842ef309155b85a9e5c59566719308fab816b40d376809c39cf1cf4de3c6a
Status: Downloaded newer image for 192.168.194.130:5000/ubuntu:latest
192.168.194.130:5000/ubuntu:latest

同样也可以从官方下载镜像

——将宿主机目录挂载到容器中,减小数据丢失的风险

1、挂载本地的目录到容器中

  • docker run -itd -v 宿主机目录:容器目录 使用的镜像 bash         挂载

[root@arslinux-01 ~]# ls /data/
ftp  mysql  wwwroot
[root@arslinux-01 ~]# docker run -itd -v /data/:/data centos_with_net bash
b8bbf33335c6491bee278976f64383ca627b157d1f80f6dc132886fe4cc9513a
[root@arslinux-01 ~]# docker exec -it b8bbf33 bash
[root@b8bbf33335c6 /]# ls /data/
ftp  mysql  wwwroot

说明:-v 用来指定挂载目录;:前面的/data/为宿主机本地目录,:后面的/data/为容器里的目录,会在容器中自动创建,两个目录可以不一样

——容器中修改内容,看宿主机变化

[root@b8bbf33335c6 /]# mkdir /data/12334
[root@b8bbf33335c6 /]# exit
exit
[root@arslinux-01 ~]# ls /data/
12334  ftp  mysql  wwwroot

类似于映射

2、挂载数据卷

  • docker run -itd --volumes-from 容器名 镜像名 bash          挂载数据卷

[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
b8bbf33335c6        centos_with_net     "bash"                   11 minutes ago      Up 11 minutes                                keen_brattain
3f51ff27501d        registry            "/entrypoint.sh /etc…"   3 days ago          Up 33 minutes       0.0.0.0:5000->5000/tcp   quizzical_chandrasekhar
[root@arslinux-01 ~]# docker run -itd --volumes-from keen_brattain arslinux bash
6ba3b2b7b37ea12aeeca0e885ecc4796a3bfc50016f50cea73f4bbc455c742aa
[root@arslinux-01 ~]# docker exec -it 6ba3b2b7b bash
[root@6ba3b2b7b37e /]# ls /data/
12334  ftp  mysql  wwwroot

说明:其实我们挂载目录的时候,可以指定容器 name,如果不指定就随机定义了。比如上面我们没有指定,它就生成了一个名字为 keen_brattain,这个名字可以使用命令 docker ps 看最右侧一列

这样,我们使用 arslinux 镜像创建了新的容器,并且使用了 relaxed_franklin  容器的数据卷

3、定义数据卷容器

有时候,我们需要多个容器之间相互共享数据,类似于 linux 里面的 NFS,所以就可以搭建一个专门的数据卷容器,然后其他容器直接挂载该数据卷

首先,建立数据卷容器

[root@arslinux-01 ~]# docker run -itd -v /data/ --name testvol centos bash
8ac3ab5f3cabd5232ff1da88f7d188434c29e6597da2399ef14f585a570d22fc

然后,让其他容器挂载该数据卷

[root@arslinux-01 ~]# docker run -itd --volumes-from testvol arslinux bash
f222617a931c4857d14995593359c827a377bfc3200f89a01fbfdc4f01b01c14

25.9 数据卷备份恢复

——备份

1、宿主机创建备份目录

[root@arslinux-01 ~]# mkdir /data/backup

2、备份

[root@arslinux-01 ~]# docker run --volumes-from testvol -v /data/backup/:/backup centos tar cvf /backup/data.tar /data/
tar: Removing leading `/' from member names
/data/
[root@arslinux-01 ~]# ls /data/backup/
data.tar

说明:首先我们需要使用 testvol 数据卷新开一个容器,同时我们还需要把本地的 /data/backup/ 目录挂载到该容器的 /backup下,这样在容器中 /backup 目录里面新建的文件,我们就可以直接在 /data/backup/ 目录中看到了。 然后再把 /data/ 目录下面的文件打包到成 data.tar 文件放到 /backup 目录下面

——恢复

思路: 先新建一个数据卷容器,再建一个新的容器并挂载该数据卷容器,然后再把 tar 包解包

1、新建数据卷容器

[root@arslinux-01 ~]# docker run -itd -v /data/ --name testvol2 centos bash
db37af0c5168b405a17594857e80ad0ae8a1bdd780355fd5194269475b24eab0

2、挂载数据卷新建容器,并解包

[root@arslinux-01 ~]# docker run --volumes-from testvol2 -v /data/backup/:/backup centos tar xvf /backup/data.tar
data/

25.10 docker 网络模式

  • host 模式,使用 docker run 时使用 --net=host 指定

docker 使用的网络实际上和宿主机一样,在容器内看到的网卡ip是宿主机 ip

  • container 模式,使用 --net=container:container_id/container_name

多个容器使用共同的网络,看到的ip是一样的

  • none 模式,使用 --net=none 指定

这种模式下,不会配置任何网络

  • bridge 模式,使用 --net=bridge 指定默认模式,不用指定默认就是这种网络模式。

这种模式会为每个容器分配一个独立的 Network Namespace。类似于 vmware 的 nat 网络模式。同一个宿主机上的所有容器会在同一个网段下,相互之间是可以通信的

[root@arslinux-01 ~]# docker start keen_brattain
keen_brattain
[root@arslinux-01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
db37af0c5168        centos              "bash"              29 minutes ago      Up 29 minutes                           testvol2
ff72d738a130        arslinux            "bash"              31 minutes ago      Up 31 minutes                           musing_brown
504f568c9f78        centos              "bash"              31 minutes ago      Up 31 minutes                           testvol
3c8c66de157f        centos              "bash"              38 minutes ago      Up 38 minutes                           strange_merkle
b8bbf33335c6        centos_with_net     "bash"              3 days ago          Up 33 seconds                           keen_brattain
[root@arslinux-01 ~]# docker exec -it b8bbf33335c6 bash
[root@b8bbf33335c6 /]# yum install -y epel-release
[root@b8bbf33335c6 /]# yum install -y nginx
[root@b8bbf33335c6 /]# systemctl start nginx
Failed to get D-Bus connection: Operation not permitted
[root@b8bbf33335c6 /]# exit
exit
[root@arslinux-01 ~]# docker run -itd -p 8088:80 centos_with_nginx bash
8082d76f51020755cb179b8a9aa975a1eb648f36cd33aa837033a8a49bc4c677

25.11 opration not permitted

[root@arslinux-01 ~]# docker ps
CONTAINER ID     IMAGE             COMMAND       CREATED           STATUS          PORTS        NAMES
4b35349a250e     centos            "/bin/bash"   4 minutes ago     Up 4 minutes                 gracious_moore
db37af0c5168     centos            "bash"        44 minutes ago    Up 44 minutes                testvol2
ff72d738a130     arslinux          "bash"        47 minutes ago    Up 47 minutes                musing_brown
504f568c9f78     centos            "bash"        47 minutes ago    Up 47 minutes                testvol
3c8c66de157f     centos            "bash"        54 minutes ago    Up 54 minutes                strange_merkle
b8bbf33335c6     centos_with_net   "bash"        3 days ago        Up 16 minutes                keen_brattain
[root@arslinux-01 ~]# docker commit -m "install nginx" -a "arslinux" b8bbf33335c6 centos_with_nginx
sha256:b25e841545c54449ef945319e1e9b323a4f77b587591a1f30036a0ec6ed2b0bc
[root@arslinux-01 ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
centos_with_nginx              latest              b25e841545c5        8 seconds ago       523MB
centos111                      latest              48f712214343        6 days ago          202MB
centos6                        latest              d0fc4ef11ea3        7 days ago          512MB
192.168.194.130:5000/centos6   latest              d0fc4ef11ea3        7 days ago          512MB
centos_with_net                latest              9c213599fd8f        7 days ago          314MB
192.168.194.130:5000/ubuntu    latest              3556258649b2        3 weeks ago         64.2MB
ubuntu                         latest              3556258649b2        3 weeks ago         64.2MB
arslinux                       latest              9f38484d220f        5 months ago        202MB
centos                         latest              9f38484d220f        5 months ago        202MB
test111                        190808              9f38484d220f        5 months ago        202MB
registry                       latest              f32a97de94e1        5 months ago        25.8MB
[root@arslinux-01 ~]# docker run -itd --privileged -e "container=docker" -p 8088:80 centos_with_nginx /usr/sbin/init
f8125fbc05426351395f22f9b143509ff9df04c288c86b0b5086a582dcb7461d
[root@arslinux-01 ~]# docker ps
CONTAINER ID     IMAGE              COMMAND           CREATED            STATUS           PORTS                 NAMES
f8125fbc0542     centos_with_nginx  "/usr/sbin/init"  5 seconds ago      Up 3 seconds     0.0.0.0:8088->80/tcp  compassionate_brattain
4b35349a250e     centos             "/bin/bash"       16 minutes ago     Up 16 minutes                          gracious_moore
db37af0c5168     centos             "bash"            56 minutes ago     Up 56 minutes                          testvol2
ff72d738a130     arslinux           "bash"            58 minutes ago     Up 58 minutes                          musing_brown
504f568c9f78     centos             "bash"            59 minutes ago     Up 59 minutes                          testvol
3c8c66de157f     centos             "bash"            About an hour ago  Up About an hour                       strange_merkle
b8bbf33335c6     centos_with_net    "bash"            3 days ago         Up 27 minutes                          keen_brattain
[root@arslinux-01 ~]# docker exec -it f8125fbc0542 bash
[root@f8125fbc0542 /]# systemctl start nginx
[root@f8125fbc0542 /]# ps aux|grep nginx
root       3412  0.0  0.2 125108  2112 ?        Ss   14:17   0:00 nginx: master process /usr/sbin/nginx
nginx      3413  0.0  0.3 125496  3140 ?        S    14:17   0:00 nginx: worker process
root       3415  0.0  0.0   9088   668 pts/1    S+   14:17   0:00 grep --color=auto nginx
[root@f8125fbc0542 /]# netstat -lntp|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3412/nginx: master
tcp6       0      0 :::80                   :::*                    LISTEN      3412/nginx: master
[root@f8125fbc0542 /]# exit
[root@arslinux-01 ~]# curl localhost:8088
[root@arslinux-02 ~]# curl 192.168.194.130:8088

宿主机上访问 8088 就是容器中的 80 端口,其他主机想要访问容器,访问宿主机的 8088 端口即可

25.12 配置桥接网络

为了使本地网络中的机器和 Docker 容器更方便的通信,我们经常会有将 Docker 容器配置到和主机同一网段的需求。这个需求其实很容易实现,我们只要将 Docker 容器和宿主机的网卡桥接起来,再给 Docker 容器配上 IP 就可以了

1、以默认网卡参数为模板,编辑新的网卡参数 br0

[root@arslinux-01 ~]# cd /etc/sysconfig/network-scripts/
[root@arslinux-01 network-scripts]# cp ifcfg-ens33 ifcfg-br0
[root@arslinux-01 network-scripts]# vim !$
vim ifcfg-br0

将 第一行改为 TYPE=Bridge,NAME=br0,DEVICE=br0,其他参数不变

2、更改默认网卡参数 ens33,重启网络

[root@arslinux-01 network-scripts]# vim ifcfg-ens33

将 UUID、IPADDR、NETMASK、GATEWAY、DNS全部注释掉

并在最后增加一行 BRIDGE=br0

[root@arslinux-01 network-scripts]# systemctl restart network
[root@arslinux-01 network-scripts]# ifconfig
br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 192.168.194.130  netmask 255.255.255.0  broadcast 192.168.194.255inet6 fe80::7b5a:1723:e063:9f29  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:24:ea:f2  txqueuelen 1000  (Ethernet)RX packets 33  bytes 16939 (16.5 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 41  bytes 3861 (3.7 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255ether 02:42:2e:f0:d8:fb  txqueuelen 0  (Ethernet)RX packets 0  bytes 0 (0.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 0  bytes 0 (0.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500ether 00:0c:29:24:ea:f2  txqueuelen 1000  (Ethernet)RX packets 5569  bytes 971820 (949.0 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 4869  bytes 493960 (482.3 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 192.168.100.1  netmask 255.255.255.0  broadcast 192.168.100.255inet6 fe80::f41:9da7:d8e3:10ba  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:24:ea:fc  txqueuelen 1000  (Ethernet)RX packets 0  bytes 0 (0.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 59  bytes 4514 (4.4 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0inet6 ::1  prefixlen 128  scopeid 0x10<host>loop  txqueuelen 1000  (Local Loopback)RX packets 61507  bytes 19731949 (18.8 MiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 61507  bytes 19731949 (18.8 MiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

3、安装 pipwork

[root@arslinux-01 ~]# git clone https://github.com/jpetazzo/pipework
[root@arslinux-01 ~]# cp pipework/pipework /usr/local/bin/

4、开启一个容器

[root@arslinux-01 ~]# docker run -itd --net=none centos_with_nginx bash
43109bea3b870a590178677caf548dd04b0ab6d08201a50f9ff8c45c67b5e8a4
[root@arslinux-01 ~]# docker exec -it 43109b bash
[root@43109bea3b87 /]# ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0loop  txqueuelen 1000  (Local Loopback)RX packets 0  bytes 0 (0.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 0  bytes 0 (0.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0[root@43109bea3b87 /]# exit
exit

4、为容器添加新的虚拟网卡

[root@arslinux-01 ~]# pipework br0 43109b 192.168.194.135/24@192.168.194.2
[root@arslinux-01 ~]# docker exec -it 43109b bash
[root@43109bea3b87 /]# ifconfig
eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 192.168.194.135  netmask 255.255.255.0  broadcast 192.168.194.255ether 42:b1:e4:da:4e:c4  txqueuelen 1000  (Ethernet)RX packets 7  bytes 586 (586.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 1  bytes 42 (42.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0loop  txqueuelen 1000  (Local Loopback)RX packets 0  bytes 0 (0.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 0  bytes 0 (0.0 B)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

5、联网测试

[root@43109bea3b87 /]# ping www.qq.com
PING public.sparta.mig.tencent-cloud.net (61.151.166.139) 56(84) bytes of data.
64 bytes from 139.166.151.61.dial.xw.sh.dynamic.163data.com.cn (61.151.166.139): icmp_seq=1 ttl=128 time=12.0 ms
64 bytes from 139.166.151.61.dial.xw.sh.dynamic.163data.com.cn (61.151.166.139): icmp_seq=2 ttl=128 time=12.5 ms
64 bytes from 139.166.151.61.dial.xw.sh.dynamic.163data.com.cn (61.151.166.139): icmp_seq=3 ttl=128 time=12.0 ms
^C
--- public.sparta.mig.tencent-cloud.net ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 12.022/12.202/12.501/0.212 ms
[root@43109bea3b87 /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.194.2   0.0.0.0         UG    0      0        0 eth1
192.168.194.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1

6、其他主机上测试连接

[root@arslinux-02 ~]# ping 192.168.194.135
PING 192.168.194.135 (192.168.194.135) 56(84) bytes of data.
64 bytes from 192.168.194.135: icmp_seq=1 ttl=64 time=0.581 ms
64 bytes from 192.168.194.135: icmp_seq=2 ttl=64 time=0.318 ms
64 bytes from 192.168.194.135: icmp_seq=3 ttl=64 time=0.669 ms
^C
--- 192.168.194.135 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.318/0.522/0.669/0.151 ms

【0730】docker 入门(上)相关推荐

  1. docker启动odoo提示module没有安装_Windows Server 2019上的Docker 入门

    本文字数:1679字 阅读时间:6分钟 导读 Windows Server 2019 现在可以使用了!它是Windows Server的下一个长期支持版本,对运行Docker Windows容器进行了 ...

  2. Docker入门系列之三:如何将dockerfile制作好的镜像发布到Docker hub上

    这个系列的前两篇文章,我们已经把我们的应用成功地在Docker里通过nginx运行了起来,并且用dockerfile里制作好了一个镜像. Docker入门系列之一:在一个Docker容器里运行指定的w ...

  3. Docker 入门到精通 (图解+秒懂+史上最全)

    文章很长,建议收藏起来慢慢读!疯狂创客圈总目录 语雀版 | 总目录 码云版| 总目录 博客园版 为您奉上珍贵的学习资源 : 免费赠送 经典图书:<Java高并发核心编程(卷1)> 面试必备 ...

  4. Docker 入门教程 - 2021 最新版(上)

    容器化的概念很早就有了.2013 年 Docker 引擎的出现使应用程序容器化变得更加容易. 根据 Stack Overflow 开发者调查-2020,Docker是开发者 #1 最想要的平台.#2 ...

  5. Docker入门六部曲——Swarm

    原文链接:http://www.dubby.cn/detail.html?id=8738 准备工作 安装Docker(版本最低1.13). 安装好Docker Compose,上一篇文章介绍过的. 安 ...

  6. Docker入门六部曲——Stack

    原文链接:http://www.dubby.cn/detail.html?id=8739 准备知识 安装Docker(版本最低1.13). 阅读完Docker入门六部曲--Swarm,并且完成其中介绍 ...

  7. Docker入门六部曲——服务

    原文链接:http://www.dubby.cn/detail.html?id=8735 准备 已经安装好Docker 1.13或者以上的版本. 安装好Docker Compose.如果你是用的是Do ...

  8. 美亚排名超高的Docker入门书,不止简单易懂

    在美国亚马逊,有一本书的影响力超高的Docker入门书,在操作系统分类中排行第一,超越了众多实力派Docker书,众多五星好评.也许你有所耳闻,这本书就是<深入浅出Docker>. 这是一 ...

  9. 模型部署神器:高中生也能读懂的Docker入门教程

    转自:马哥Linux运维 Docker 是 Golang 编写的, 自 2013 年推出以来,受到越来越多的开发者的关注.如果你关注最新的技术发展,那么你一定听说过 Docker.不管是云服务还是微服 ...

  10. 高中生也能读懂的Docker入门教程

    Docker 是 Golang 编写的, 自 2013 年推出以来,受到越来越多的开发者的关注.如果你关注最新的技术发展,那么你一定听说过 Docker.不管是云服务还是微服务(Microservic ...

最新文章

  1. fastp: 极速全能的FASTQ文件自动质控+过滤+校正+预处理软件
  2. 讲真,上班路上 1 小时算很幸福了!
  3. System.DBNull
  4. 用python实现图书管理系统
  5. Bootstrap3 Font Awesome 字体图标带边框的图标
  6. MySQL数据库学习笔记(二)----MySQL数据类型
  7. Spring: ConfigurationClassUtils类
  8. 编写高质量代码 —— 异常退出条件的判断
  9. c++语言取整为什么要加0.5_蛋鸡饲料要加石粉,那你知道为什么加?要加什么石粉?我来告诉你...
  10. oracle分区索引优化,SQL优化思路结果集重用优化、分区索引优化测试
  11. 403. Frog Jump
  12. matlab最后ceq,求解 minimax 约束问题
  13. 运行Android Studio,一直提示:Error running app: Instant Run requires 'Tools | Android | Enable ADB integrat
  14. C++简介 C语言编程原理
  15. 软件项目管理 3.5.敏捷生存期模型
  16. 几何光学学习笔记(7)- 3.1 理想光学系统
  17. 伽罗华域(Galois Field,GF,有限域)
  18. 物联网+智能家具 潜力无限新生态
  19. c语言编程函数名:b开头
  20. safari阅读模式_如何在Safari中使用和调整阅读器模式

热门文章

  1. 木鱼cms系统审计小结
  2. 创建一个字体wx.Font
  3. mysql带中文日期转换_【MySQL】日期时间格式转换_MySQL
  4. DELL强化版《最终幻想11》认证电脑出炉
  5. android手机电池寿命,安卓手机如何查电池使用寿命
  6. 合力亿捷发布云客服3.0,开启全员客服新时代
  7. 京东淘宝,拼多多三大电商平台竞品分析
  8. 不懂就要问!流量卡拒收对个人会有影响吗?
  9. ROW_NUMBER、RANK、DENSE_RANK的用法(1)(转)
  10. AMiner背后的技术细节与挑战