docker常用操作

docker常用操作

docker search 在Docker Hub中搜索镜像

[root@localhost ~]# docker search httpd
NAME                                    DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
httpd                                   The Apache HTTP Server Project                  4106      [OK]       
centos/httpd-24-centos7                 Platform for running Apache httpd 2.4 or bui…   44                   
centos/httpd                                                                            35                   [OK]
solsson/httpd-openidc                   mod_auth_openidc on official httpd image, ve…   2                    [OK]
clearlinux/httpd                        httpd HyperText Transfer Protocol (HTTP) ser…   2                    
hypoport/httpd-cgi                      httpd-cgi                                       2                    [OK]
nnasaki/httpd-ssi                       SSI enabled Apache 2.4 on Alpine Linux          1                    
dockerpinata/httpd                                                                      1                    
jonathanheilmann/httpd-alpine-rewrite   httpd:alpine with enabled mod_rewrite           1                    [OK]
inanimate/httpd-ssl                     A play container with httpd, ssl enabled, an…   1                    [OK]
centos/httpd-24-centos8                                                                 1                    
dariko/httpd-rproxy-ldap                Apache httpd reverse proxy with LDAP authent…   1                    [OK]
manageiq/httpd                          Container with httpd, built on CentOS for Ma…   1                    [OK]
publici/httpd                           httpd:latest                                    1                    [OK]
httpdocker/kubia                                                                        0                    
patrickha/httpd-err                                                                     0                    
e2eteam/httpd                                                                           0                    
amd64/httpd                             The Apache HTTP Server Project                  0                    
manageiq/httpd_configmap_generator      Httpd Configmap Generator                       0                    [OK]
manasip/httpd                                                                           0                    
ppc64le/httpd                           The Apache HTTP Server Project                  0                    
httpdss/archerysec                      ArcherySec repository                           0                    [OK]
paketobuildpacks/httpd                                                                  0                    
19022021/httpd-connection_test          This httpd image will test the connectivity …   0                    
sandeep1988/httpd-new                   httpd-new                                       0                   

docker pull 从注册表中提取映像或存储库

[root@localhost ~]# docker pull httpd:latest
latest: Pulling from library/httpd
a2abf6c4d29d: Pull complete 
dcc4698797c8: Pull complete 
41c22baa66ec: Pull complete 
67283bbdd4a0: Pull complete 
d982c879c57e: Pull complete 
Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest

docker images 镜像列表

[root@localhost ~]# docker images
REPOSITORY              TAG       IMAGE ID       CREATED        SIZE
httpd                   latest    dabbfbe0c57b   7 months ago   144MB
nickistre/centos-lamp   latest    0b8d572d1c7d   4 years ago    547MB

docker create 创建一个新的容器

[root@localhost ~]#  docker create --name web httpd
170ac333949ea993be387f7e70692b63604ae1c834299a3fd82261455cd918e8

docker start 启动一个或多个停止的容器

[root@localhost ~]# docker start web
web

docker run 在新容器中运行命令

[root@localhost ~]# docker run  -d  --name yy -p 80:80 httpd
5cf74dbad9751cbfef94138bd66b84dc3c0f94bcec98206172018661f6ba4fc6
docker: Error response from daemon: driver failed programming external connectivity on endpoint yy (98b5beb756b10672ff844592bff8e08d87d1850250e583d50ea17214959712a1):  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 80 -j DNAT --to-destination 172.17.0.3:80 ! -i docker0: iptables: No chain/target/match by that name.
 (exit status 1)).

docker attach 连接到运行的容器

^C[root@localhost ~]# docker run -it --name www httpd /bin/bash
root@a3828c50d208:/usr/local/apache2# exit
exit
[root@localhost ~]# docker start www
www
[root@localhost ~]# docker pas
docker: 'pas' is not a docker command.
See 'docker --help'
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS          PORTS     NAMES
a3828c50d208   httpd     "/bin/bash"   About a minute ago   Up 21 seconds   80/tcp    www
[root@localhost ~]# 
[root@localhost ~]# docker attach www
root@a3828c50d208:/usr/local/apache2# 

docker ps 列出本地容器

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS    PORTS     NAMES
170ac333949e   httpd     "httpd-foreground"   53 seconds ago   Created             web
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker logs 获取容器的日志

[root@localhost ~]# docker logs web
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Thu Aug 04 07:42:54.231384 2022] [mpm_event:notice] [pid 1:tid 140477859712320] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Thu Aug 04 07:42:54.447953 2022] [core:notice] [pid 1:tid 140477859712320] AH00094: Command line: 'httpd -D FOREGROUND'
[Thu Aug 04 07:44:22.402818 2022] [mpm_event:notice] [pid 1:tid 140477859712320] AH00492: caught SIGWINCH, shutting down gracefully
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Thu Aug 04 07:47:09.720992 2022] [mpm_event:notice] [pid 1:tid 140496939195712] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Thu Aug 04 07:47:09.721172 2022] [core:notice] [pid 1:tid 140496939195712] AH00094: Command line: 'httpd -D FOREGROUND'

docker restart 重启一个容器

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS     NAMES
170ac333949e   httpd     "httpd-foreground"   23 minutes ago   Up 15 seconds   80/tcp    web

docker stop 停止一个或多个运行中的容器

[root@localhost ~]# docker stop web
web
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                      PORTS     NAMES
170ac333949e   httpd     "httpd-foreground"   17 minutes ago   Exited (0) 29 seconds ago             web

docker kill 杀死一个或多个运行中的容器

[root@localhost ~]# docker kill yy
Error response from daemon: Cannot kill container: yy: Container 5cf74dbad9751cbfef94138bd66b84dc3c0f94bcec98206172018661f6ba4fc6 is not running

docker rm 移除onne或多个容器

[root@localhost ~]# docker rm zz
zz
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED             STATUS          PORTS     NAMES
5cf74dbad975   httpd     "httpd-foreground"   5 minutes ago       Created                   yy
170ac333949e   httpd     "httpd-foreground"   About an hour ago   Up 56 minutes   80/tcp    web
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# 

docker exec 在运行的容器中运行命令

[root@localhost ~]# docker exec -it web /bin/bash
root@170ac333949e:/usr/local/apache2# 

docker info 显示整个系统的信息

[root@localhost ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
  scan: Docker Scan (Docker Inc., v0.17.0)

Server:
 Containers: 2
  Running: 0
  Paused: 0
  Stopped: 2
 Images: 2
 Server Version: 20.10.17
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
 runc version: v1.1.2-0-ga916309
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.18.0-257.el8.x86_64
 Operating System: CentOS Stream 8
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 1.748GiB
 Name: localhost.localdomain
 ID: NEEH:UDES:DTWT:ZWQV:232L:WCQP:4DVP:MLNT:V5FJ:EKG4:64CF:HNCW
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://ziubzfxa.mirror.ali

docker inspect 返回Docker对象的低级信息

[root@localhost ~]#  docker inspect web
 "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
[root@localhost ~]# curl http://172.17.0.2
<html><body><h1>It works!</h1></body></html>

镜像的概念

镜像可以理解为应用程序的集装箱,而docker用来装卸集装箱。

docker镜像含有启动容器所需要的文件系统及其内容,因此,其用于创建并启动容器。

docker镜像采用分层构建机制,最底层为bootfs,其上为rootfs

  • bootfs:用于系统引导的文件系统,包括bootloader和kernel,容器启动完成后会被卸载以节约内存资源
  • rootfs:位于bootfs之上,表现为docker容器的根文件系统
    • 传统模式中,系统启动之时,内核挂载rootfs会首先将其挂载为“只读”模式,完整性自检完成后将其重新挂载为读写模式
    • docker中,rootfs由内核挂载为“只读”模式,而后通过“联合挂载”技术额外挂载一个“可写”层

注意:当删除容器时,这个容器自有的“可写”层会一起被删除

img

docker镜像层

img

位于下层的镜像称为父镜像(parrent image),最底层的称为基础镜像(base image);
最上层为“可读写”层,其下的均为“只读”层。

docker存储驱动

docker提供了多种存储驱动来实现不同的方式存储镜像,下面是常用的几种存储驱动:

  • AUFS
  • OverlayFS
  • Devicemapper
  • Btrfs
  • VFS

AUFS

AUFS(AnotherUnionFS)是一种Union FS,是文件级的存储驱动。AUFS是一个能透明覆盖一个或多个现有文件系统的层状文件系统,把多层合并成文件系统的单层表示。简单来说就是支持将不同目录挂载到同一个虚拟文件系统下的文件系统。这种文件系统可以一层一层地叠加修改文件。无论底下有多少层都是只读的,只有最上层的文件系统是可写的。当需要修改一个文件时,AUFS创建该文件的一个副本,使用CoW将文件从只读层复制到可写层进行修改,结果也保存在可写层。在Docker中,底下的只读层就是image,可写层就是Container。

AUFS文件系统据说有3W行代码,而ext4文件系统却只有4000-5000行左右代码,这些代码是要被整合进内核的,后来AUFS申请要被合并进内核代码的时候,linuz觉得它这代码太过臃肿,于是拒绝了。因此AUFS这个文件系统一直以来就不是linux内核中自有的文件系统,想用AUFS这个文件系统的话,必须自己向内核打补丁并去编译使用它,但redhat系列的操作系统一向以稳定著称,不会干这种出格的事,所以在redhat系列操作系统中使用AUFS并无可能。而ubuntu上的docker默认使用的就是AUFS。

OverlayFS

Overlay是Linux内核3.18后支持的,也是一种Union FS,和AUFS的多层不同的是Overlay只有两层:一个upper文件系统和一个lower文件系统,分别代表Docker的镜像层和容器层。当需要修改一个文件时,使用CoW将文件从只读的lower复制到可写的upper进行修改,结果也保存在upper层。在Docker中,底下的只读层就是image,可写层就是Container。目前最新的OverlayFS为Overlay2。

AUFS和Overlay都是联合文件系统,但AUFS有多层,而Overlay只有两层,所以在做写时复制操作时,如果文件比较大且存在比较低的层,则AUSF会慢一些。而且Overlay并入了linux kernel mainline,AUFS没有。目前AUFS已基本被淘汰。

DeviceMapper

Device mapper是Linux内核2.6.9后支持的,提供的一种从逻辑设备到物理设备的映射框架机制,在该机制下,用户可以很方便的根据自己的需要制定实现存储资源的管理策略。AUFS和OverlayFS都是文件级存储,而Device mapper是块级存储,所有的操作都是直接对块进行操作,而不是文件。Device mapper驱动会先在块设备上创建一个资源池,然后在资源池上创建一个带有文件系统的基本设备,所有镜像都是这个基本设备的快照,而容器则是镜像的快照。所以在容器里看到文件系统是资源池上基本设备的文件系统的快照,并没有为容器分配空间。当要写入一个新文件时,在容器的镜像内为其分配新的块并写入数据,这个叫用时分配。当要修改已有文件时,再使用CoW为容器快照分配块空间,将要修改的数据复制到在容器快照中新的块里再进行修改。

OverlayFS是文件级存储,Device mapper是块级存储,当文件特别大而修改的内容很小,Overlay不管修改的内容大小都会复制整个文件,对大文件进行修改显然要比小文件要消耗更多的时间,而块级无论是大文件还是小文件都只复制需要修改的块,并不是整个文件,在这种场景下,显然device mapper要快一些。因为块级的是直接访问逻辑盘,适合IO密集的场景。而对于程序内部复杂,大并发但少IO的场景,Overlay的性能相对要强一些。

docker registry

启动容器时,docker daemon会试图从本地获取相关的镜像,本地镜像不存在时,其将从Registry中下载该镜像并保存到本地。

Registry用于保存docker镜像,包括镜像的层次结构和元数据。用户可以自建Registry,亦可使用官方的Docker Hub。

docker registry的分类:

  • Sponsor Registry:第三方的Registry,供客户和Docker社区使用
  • Mirror Registry:第三方的Registry,只让客户使用
  • Vendor Registry:由发布docker镜像的供应商提供的registry
  • Private Registry:通过设有防火墙和额外的安全层的私有实体提供的registry

docker registry的组成:

  • Repository
    • 由某特定的docker镜像的所有迭代版本组成的镜像仓库
    • 一个Registry中可以存在多个Repository
      • Repository可分为“顶层仓库”和“用户仓库”
      • 用户仓库名称格式为“用户名/仓库名”
    • 每个仓库可包含多个Tag(标签),每个标签对应一个镜像
  • Index
    • 维护用户帐户、镜像的检验以及公共命名空间的信息
    • 相当于为Registry提供了一个完成用户认证等功能的检索接口

Docker Registry中的镜像通常由开发人员制作,而后推送至“公共”或“私有”Registry上保存,供其他人员使用,例如“部署”到生产环境。

组成:

  • Repository
    • 由某特定的docker镜像的所有迭代版本组成的镜像仓库
    • 一个Registry中可以存在多个Repository
      • Repository可分为“顶层仓库”和“用户仓库”
      • 用户仓库名称格式为“用户名/仓库名”
    • 每个仓库可包含多个Tag(标签),每个标签对应一个镜像
  • Index
    • 维护用户帐户、镜像的检验以及公共命名空间的信息
    • 相当于为Registry提供了一个完成用户认证等功能的检索接口

Docker Registry中的镜像通常由开发人员制作,而后推送至“公共”或“私有”Registry上保存,供其他人员使用,例如“部署”到生产环境。

img

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值