欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker介绍和基本管理

程序员文章站 2022-07-01 11:27:37
...

Docker是什么?

  • 是一种轻量级的“虚拟机”
  • 在Linux容器里运行应用的开源工具

Docker介绍和基本管理

Docker的优点

  • 服务彼此间相互独立 服务间的解耦
  • 解耦:便于管理,防止数据过多积累在一个文件中
  • 服务可以灵活迁移docker引擎docker-ce

Docker与虚拟机的区别

Docker介绍和基本管理Docker介绍和基本管理

Docker使用场景

  • 打包应用程序简化部署
  • 可脱离底层硬件任意迁移

Docker核心概念

  • 镜像
    一个面向Docker容器引擎的只读模板
  • 容器
    从镜像创建的运行实例
  • 仓库
    集中保存镜像的地方

CentOS安装Docker的两种方式

使用CURL获得Docker的安装脚本进行安

1、使用官方安装脚本自动安装

[aaa@qq.com ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

2、使用国内安装脚本自动安装

[aaa@qq.com ~]# curl -sSL https://get.daocloud.io/docker | sh

使用YUM合库来安装Docker

1.设置yum源

[aaa@qq.com yum.repos.d]# cp backup/* ./
[aaa@qq.com yum.repos.d]# ll
总用量 32
drwxr-xr-x. 2 root root  187 10月 29 01:30 backup
-rw-r--r--  1 root root 1664 11月 25 15:31 CentOS-Base.repo
-rw-r--r--  1 root root 1309 11月 25 15:31 CentOS-CR.repo
-rw-r--r--  1 root root  649 11月 25 15:31 CentOS-Debuginfo.repo
-rw-r--r--  1 root root  314 11月 25 15:31 CentOS-fasttrack.repo
-rw-r--r--  1 root root  630 11月 25 15:31 CentOS-Media.repo
-rw-r--r--  1 root root 1331 11月 25 15:31 CentOS-Sources.repo
-rw-r--r--  1 root root 3830 11月 25 15:31 CentOS-Vault.repo
-rw-r--r--  1 root root  228 10月 29 03:11 local.repo

2.下载yum源(使用其中一个就行)
2.1、阿里源

[aaa@qq.com ~]# yum-config-manager     --add-repo     http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2.2、网易源

[aaa@qq.com yum.repos.d]# wget -P /etc/yum.repos.d http://mirrors.163.com/.help/CentOS7-Base-163.repo

3.安装依赖包

[aaa@qq.com ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
#yum-utils提供了yum-config-manager
#device mapper存储驱动程序需要device-mapper-persistent-data和lvm2
#Device Mapper是Linux2.6内核中支持逻辑卷管理的通用设备映射机制,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构。

4、安装doucker

[aaa@qq.com ~]# yum -y install docker-ce

5、启动docker

[aaa@qq.com ~]# systemctl start docker.service 
[aaa@qq.com ~]# systemctl enable docker.service	#设置开启自启动

6、查看docker启动进程

[aaa@qq.com ~]# ps aux|grep docker

7、网络优化

[aaa@qq.com ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1		#增加
[aaa@qq.com ~]# sysctl -p
[aaa@qq.com ~]# systemctl restart network

8、docker安装完成后会自动增加一张网卡(为后面的创建的容器网关)

[aaa@qq.com ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:3d:03:fc:a8  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

9、查看docker的版本号

[aaa@qq.com ~]# docker version

Docker镜像操作

搜索镜像

命令格式:docker	search  [仓库名称]
[aaa@qq.com ~]# docker search nginx		#例如搜索nginx镜像

获取镜像

命令格式:docker pull   仓库名称
[aaa@qq.com ~]# docker pull nginx		#例如在nginx仓库下载一个nginx镜像

获取镜像阿里云加速地址

如果获取镜像网速很慢可以使用设置加速
如何查看自己的阿里云加速地址
Docker介绍和基本管理1、手动配置加速

[aaa@qq.com ~]# vim /etc/docker/daemon.json
#增加如下设置
{
  "registry-mirrors": ["https://xxxxxx.mirror.aliyuncs.com"]	
}

2、免交互配置加速

[aaa@qq.com ~]# tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://xxxxx.mirror.aliyuncs.com"]
}
EOF

3、设置systemctl 管理
daemon-reload: 重新加载某个服务的配置文件,如果新安装了一个服务,归属于 systemctl 管理,要是新服务的服务程序配置文件生效,需重新加载。

[aaa@qq.com ~]# systemctl daemon-reload
[aaa@qq.com ~]# systemctl restart docker		#重启docker

4、拉取镜像报错

[aaa@qq.com docker]# docker pull nginx
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/library/nginx/manifests/latest: Get https://auth.docker.io/token?scope=repository%3Alibrary%2Fnginx%3Apull&service=registry.docker.io: net/http: TLS handshake timeout
解决:检查/etc/docker/ 下自己创建的daemon.json 文件里面是否有错误

查看镜像

1、查看所有的镜像

[aaa@qq.com ~]# docker images

2、查看指定的镜像

命令格式:docker images [镜像仓库名称]
[aaa@qq.com ~]# docker images nginx		#例如:查看nginx镜像

3、获取容器/镜像的元数据

[aaa@qq.com ~]# docker inspect [服务器名称/ID号]

4、设置镜像的tag标签(新创建的标签和原始标签的ID样,相当于是同一个镜像)

命令格式:docker tag   服务器名称:当前标签名称  服务器名称:新标签名称

设置新的标签不会把原来的标签删除,例如如下事例

[aaa@qq.com docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        10 hours ago        133MB
tomcat              latest              e0bd8b34b4ea        6 days ago          649MB
centos              7                   8652b9f0cb4c        11 days ago         204MB
[aaa@qq.com docker]# docker tag nginx:latest nginx:web
[aaa@qq.com docker]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        10 hours ago        133MB
nginx               web                 bc9a0695f571        10 hours ago        133MB
tomcat              latest              e0bd8b34b4ea        6 days ago          649MB
centos              7                   8652b9f0cb4c        11 days ago         204MB

删除镜像

1、删除没有重复ID的标签镜像

[aaa@qq.com ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        10 hours ago        133MB
nginx               web                 bc9a0695f571        10 hours ago        133MB
tomcat              latest              e0bd8b34b4ea        6 days ago          649MB
centos              7                   8652b9f0cb4c        11 days ago         204MB
[aaa@qq.com ~]# docker rmi tomcat:latest 
Untagged: tomcat:latest
Untagged: aaa@qq.com:a319b10d8729817c7ce0bcc2343a6f97711c7870395019340d96b6aafd6ccbea
Deleted: sha256:e0bd8b34b4ea904874e55eae50e8987815030d140f9773a4b61759f4f85bf38d
Deleted: sha256:79908ce5c984f62c9ccd49cda41b58a6bd4607ebe108d779c821267dfedfdd7b
Deleted: sha256:64f8057f45ae4c7bb7acb9261e2bc8241c8e05a05f8e25d5ce97a20c60573ae1
Deleted: sha256:80931c0a01a10c5078784b7d54780e657d4ff85856aaf038478583a787edfca8
Deleted: sha256:b8a793369a7b884f5b86aa05fb7af0d0ce8a3a11c817dc8138bafb926377eaa6
Deleted: sha256:1f1ac4286f586a4f563dbbd7a84d7865a4cc5e35548a827e58ef4c6dda4ed426
Deleted: sha256:045834a7a6e9ee497085a50881edd9c473e9676adcbee388bb6f290ff52dff42
Deleted: sha256:be048ca7e7af3c225efaf44d9bf81c2d274d0860e563cd97e5f2630c958f24cd
Deleted: sha256:e93ebf51004cfe34370a8930dc7016c067a8c5cd4754b932dedca1739f0c6e29
Deleted: sha256:86569a54d5d4735f27748c0e5d9ead04f48d36bbaaeaa755a1e022a6eb32d650
Deleted: sha256:114ca5b7280f3b49e94a67659890aadde83d58a8bde0d9020b2bc8c902c3b9de
[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        10 hours ago        133MB
nginx               web                 bc9a0695f571        10 hours ago        133MB
centos              7                   8652b9f0cb4c        11 days ago         204MB

2、删除有重复ID的标签镜像(只会删除标签,不会真正删除镜像)

[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        10 hours ago        133MB
nginx               web                 bc9a0695f571        10 hours ago        133MB
centos              7                   8652b9f0cb4c        11 days ago         204MB
[aaa@qq.com ~]# docker rmi nginx:latest 
Untagged: nginx:latest
[aaa@qq.com ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               web                 bc9a0695f571        10 hours ago        133MB
centos              7                   8652b9f0cb4c        11 days ago         204MB

3、使用删除ID的方法删除有重复ID的标签镜像(会报报错)

[aaa@qq.com ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        11 hours ago        133MB
nginx               web                 bc9a0695f571        11 hours ago        133MB
centos              7                   8652b9f0cb4c        11 days ago         204MB
[aaa@qq.com ~]# docker rmi bc9a0695f571
Error response from daemon: conflict: unable to delete bc9a0695f571 (must be forced) - image is referenced in multiple repositories
解决办法:
[aaa@qq.com ~]# docker rmi nginx:web
[aaa@qq.com ~]# docker rmi nginx:latest

镜像另存

命令格式:docker save -o 存储路径   镜像名称
[aaa@qq.com ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        11 hours ago        133MB
centos              7                   8652b9f0cb4c        11 days ago         204MB
[aaa@qq.com ~]# docker save -o /opt/a1/web nginx:latest 
[aaa@qq.com ~]# cd /opt/a1/
[aaa@qq.com a1]# ll
总用量 133884
-rw------- 1 root root 137096704 11月 25 19:16 web

载入镜像

命令格式:docker load < 镜像名称 
[aaa@qq.com a1]# ll
总用量 133884
-rw------- 1 root root 137096704 11月 25 19:16 web
[aaa@qq.com a1]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              7                   8652b9f0cb4c        11 days ago         204MB
[aaa@qq.com a1]# docker load < web 
f5600c6330da: Loading layer [==================================================>]  72.52MB/72.52MB
7ccabd267c9f: Loading layer [==================================================>]  64.54MB/64.54MB
850c2400ea4d: Loading layer [==================================================>]  3.072kB/3.072kB
f790aed835ee: Loading layer [==================================================>]  4.096kB/4.096kB
7e914612e366: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: nginx:latest
[aaa@qq.com a1]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        11 hours ago        133MB
centos              7                   8652b9f0cb4c        11 days ago         204MB
导入的名称是web显示的是nginx:是应为我另存的时候设置名称为web但实际的镜像是nginx镜像

容器操作

创建容器

命令格式:docker	create  [选项]  镜像名称  /bin/bash			#/bin/bash:执行环境
-i:让容器的标准输入保持打开
-t:让Docker分配一个伪终端
[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        11 hours ago        133MB
centos              7                   8652b9f0cb4c        11 days ago         204MB
[aaa@qq.com ~]# docker create -it nginx:latest /bin/bash
cb943dc3d85b15171bbd2ab7446a2537894458d6cbd9dda9c1e8fdb60efef08a

查看容器的运行状态

命令格式:docker ps -a
-a:列出所有的容器,包括未运行的容器
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
cb943dc3d85b        nginx:latest        "/docker-entrypoint.…"   2 minutes ago       Created                                 priceless_ramanujan

Created:创建状态		up:启动状态		 Exited(0):正常退出	 Exited(0):异常退出

启动容器

命令格式:docker   start	容器ID号
[aaa@qq.com ~]# docker start cb943dc3d85b
cb943dc3d85b
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
cb943dc3d85b        nginx:latest        "/docker-entrypoint.…"   5 minutes ago       Up 20 seconds       80/tcp              priceless_ramanujan

终止运行

终止运行(运行状态终止运行属于异常终止)

命令格式: docker stop  容器ID号
[aaa@qq.com ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        11 hours ago        133MB
centos              7                   8652b9f0cb4c        11 days ago         204MB
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
cb943dc3d85b        nginx:latest        "/docker-entrypoint.…"   29 minutes ago      Up 20 minutes       80/tcp              priceless_ramanujan
[aaa@qq.com ~]# docker stop cb943dc3d85b
cb943dc3d85b
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
cb943dc3d85b        nginx:latest        "/docker-entrypoint.…"   30 minutes ago      Exited (137) 8 seconds ago                       priceless_ramanujan

创建容器并运行

命令格式1:docker run  -itd  镜像名称  /bin/bash	#使用镜像名称创建
[aaa@qq.com ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        12 hours ago        133MB
centos              7                   8652b9f0cb4c        11 days ago         204MB
[aaa@qq.com ~]# docker run -itd centos:7 /bin/bash		#使用镜像名称
e2d0b685a465ba57c3921e458992f9fbded0d0bd4c1aa977b2ef850ec495f61d
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
e2d0b685a465        centos:7            "/bin/bash"              8 seconds ago       Up 7 seconds                            wonderful_swanson
cb943dc3d85b        nginx:latest        "/docker-entrypoint.…"   40 minutes ago      Up 8 minutes        80/tcp              priceless_ramanujan
命令格式2:docker run  -itd  镜像来源  /bin/bash		#使用镜像ID号
[aaa@qq.com ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              bc9a0695f571        12 hours ago        133MB
centos              7                   8652b9f0cb4c        11 days ago         204MB
[aaa@qq.com ~]# docker run -itd 8652b9f0cb4c /bin/bash
3ef285301f19610007055f323b135907c246c63bae86c6e64f587652f3e456ad
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3ef285301f19        8652b9f0cb4c        "/bin/bash"              10 seconds ago      Up 9 seconds                            exciting_fermat
cb943dc3d85b        nginx:latest        "/docker-entrypoint.…"   46 minutes ago      Up 14 minutes       80/tcp              priceless_ramanujan

容器导出

容器导出(容器启动和不启动都可导入出)

命令格式:docker export 容器的ID号 >导入的目录
[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                           PORTS               NAMES
02331a1adce4        centos:7            "/bin/bash"              3 minutes ago       Up 3 minutes                                         reverent_mirzakhani
cb943dc3d85b        nginx:latest        "/docker-entrypoint.…"   2 hours ago         Exited (137) About an hour ago                       priceless_ramanujan
[aaa@qq.com ~]# docker export 02331a1adce4 >/opt/centos-7
[aaa@qq.com ~]# docker export cb943dc3d85b >/opt/nginx-latest
[aaa@qq.com ~]# cd /opt/
[aaa@qq.com opt]# ls -lh
总用量 331M
drwxr-xr-x  2 root root   17 11月 25 19:16 a1
-rw-r--r--  1 root root 202M 11月 25 21:23 centos-7
drwx--x--x  4 root root   28 11月 25 16:39 containerd
-rw-r--r--  1 root root 130M 11月 25 21:24 nginx-latest
drwxr-xr-x. 2 root root    6 3月  26 2015 rh

容器导入

容器导入(会生成镜像,而不会创建容器)

命令格式:cat 容器名称 |docker import - 容器名称 
[aaa@qq.com opt]# ll
总用量 338904
drwxr-xr-x  2 root root        17 11月 25 19:16 a1
-rw-r--r--  1 root root 211688960 11月 25 21:23 centos-7
drwx--x--x  4 root root        28 11月 25 16:39 containerd
-rw-r--r--  1 root root 135345152 11月 25 21:24 nginx-latest
drwxr-xr-x. 2 root root         6 3月  26 2015 rh
[aaa@qq.com opt]# cat centos-7 |docker import - centos-7 
sha256:cbcd6db9d7e57a938a7aef700e3cf1f216a43c4c28bb2c66142431f71b4e6f44

[aaa@qq.com opt]# cat centos-7 |docker import - nginx-latest 
sha256:2157d58326b4e5fba9111190c2410a6dbfc45671eceec1dfaa1171b9421fba9a
[aaa@qq.com opt]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx-latest        latest              2157d58326b4        13 seconds ago      204MB
centos-7            latest              cbcd6db9d7e5        23 seconds ago      204MB
[aaa@qq.com opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[aaa@qq.com opt]#

进入容器查看信息

[aaa@qq.com ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ef44314a9605        nginx               "/bin/bash"         5 minutes ago       Up 5 minutes                            pensive_chebyshev
f4f04619ac56        centos              "/bin/bash"         7 minutes ago       Up 7 minutes                            funny_cartwright
[aaa@qq.com ~]# docker exec -it f4f04619ac56 /bin/bash
[aaa@qq.com /]# ifconfig
bash: ifconfig: command not found			#显示没有iconfig命令
[aaa@qq.com /]# yum -y install net-tools	
[aaa@qq.com /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 1011  bytes 10990597 (10.4 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 975  bytes 56044 (54.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[aaa@qq.com /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[aaa@qq.com /]# exit
exit

[aaa@qq.com ~]#

不进入容器查看容器目录

[aaa@qq.com ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              4a2271c3cb03        12 minutes ago      204MB
nginx               latest              6ea2c01dac3e        13 minutes ago      131MB
[aaa@qq.com ~]# docker run nginx:latest /bin/bash -c ls /
bin
boot
dev
docker-entrypoint.d
docker-entrypoint.sh
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
相关标签: dcoker docker