docker基础用法

#docker基础用法

##什么是docker
docker中的容器:
lxc --> libcontainer --> runC

##OCI&OCF
###OCI
开发容器倡议

由Linux基金会主导于2015年6月创立
旨在围绕容器格式和运行时制定一个开放的工业化标准
包含两种规格
运行时规范
镜像规范

###OCF
断续器
开放容器格式

runC 是一个 CLI 工具,用于根据 OCI 规范生成和运行容器

容器作为 runC 的子进程启动,可以嵌入到各种其他系统中,而无需运行守护程序
runC 建立在 libcontainer 之上,同样的容器技术为数百万个 Docker 引擎安装提供支持
docker提供了一个专门容纳容器镜像的点:https://hub.docker.com
##docker架构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BmPOYutX-1659660650709)(./3.jpg)]

##docker镜像与镜像仓库
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的
##docker对象
使用 Docker 时,您正在创建和使用映像、容器、网络、卷、插件和其他对象。

图像
映像是一个只读模板,其中包含有关创建 docker 容器的说明。
通常,一个映像基于另一个映像,并具有一些额外的自定义。
您可以创建自己的映像,也可以仅使用其他人创建并在注册表中发布的映像。
器皿
控制器是映像的可运行实例。
您可以使用 Docker API 或 CLI 创建、运行、停止、移动或删除容器。
可以将容器连接到一个或多个网络,将存储附加到该容器,甚至可以基于其当前状态创建新映像

##安装及使用docker
###docker安装
###docker加速
使用 Docker 时,您正在创建和使用映像、容器、网络、卷、插件和其他对象。

图像
映像是一个只读模板,其中包含有关创建 docker 容器的说明。
通常,一个映像基于另一个映像,并具有一些额外的自定义。
您可以创建自己的映像,也可以仅使用其他人创建并在注册表中发布的映像。
器皿
控制器是映像的可运行实例。
您可以使用 Docker API 或 CLI 创建、运行、停止、移动或删除容器。
可以将容器连接到一个或多个网络,将存储附加到该容器,甚至可以基于其当前状态创建新映像

[root@localhost ~]# sudo tee /etc/docker/daemon.json <<-'EOF'

{
 "registry-mirrors":[https://1swrs873.mirror.aliyuncs.com]
}

###docker常用操作
|docker search|在 Docker 中心搜索映像|
|-|
|docker pull|从注册表中拉取镜像或存储库|
|docker images|列出镜像|
|docker create|创建新的连接器|
|docker start|启动一个或多个已停止的容器|
|docker run|在新容器中运行命令|
|docker attach|连接到运行容器|
|docker ps|列出容器|
|docker logs|获取容器日志|
|docker restart|重新启动容器|
|docker stop|停止一个或多个正在运行的容器|
|docker kill|杀死一个或多个正在运行的命令|
|docker rm|删除容器|
|docker exec|在运行的容器中运行命令|
|dcoker info|显示系统范围的信息|
|docker inspect|返回有关docker对象的低级信息|

docker search 从docker查找镜像

[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                    
[root@localhost ~]# 

docker pull 从镜像仓库中拉取镜像

[root@localhost ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
1efc276f4ff9: Pull complete 
aed046121ed8: Pull complete 
4340e7be3d7f: Pull complete 
80e368ef21fc: Pull complete 
80cb79a80bbe: Pull complete 
Digest: sha256:343452ec820a5d59eb3ab9aaa6201d193f91c3354f8c4f29705796d9353d4cc6
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    f2a976f932ec   2 days ago   145MB

docker create 创建新的容器不启动

[root@localhost ~]# docker create --name tnt -p 80:80 httpd
c651f0cda79eb2653891e4c3288b80a6a6ec6e27b550be693c119609ded871dd

docker ps 列出容器

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS    PORTS     NAMES
c651f0cda79e   httpd     "httpd-foreground"   46 seconds ago   Created             tnt

docker start 开启容器 用名字或id

[root@localhost ~]# docker start tnt
tnt
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS          PORTS                               NAMES
c651f0cda79e   httpd     "httpd-foreground"   2 minutes ago   Up 45 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   tnt

docker stop 关闭容器 用名字或id

[root@localhost ~]# docker stop c6
c6
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker restart 重启容器

[root@localhost ~]# docker restart tnt
tnt
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS                               NAMES
c651f0cda79e   httpd     "httpd-foreground"   7 minutes ago   Up 5 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   tnt

docker kill 杀死运行中的容器

[root@localhost ~]# docker kill tnt
tnt
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS                       PORTS     NAMES
c651f0cda79e   httpd     "httpd-foreground"   8 minutes ago   Exited (137) 8 seconds ago             tnt

docker logs 查看日志

[root@localhost ~]# docker logs tnt
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 06:47:02.038224 2022] [mpm_event:notice] [pid 1:tid 140025331109184] AH00489: Apache/2.4.54 (Unix) configured -- resuming normal operations
[Thu Aug 04 06:47:02.043370 2022] [core:notice] [pid 1:tid 140025331109184] AH00094: Command line: 'httpd -D FOREGROUND'
[Thu Aug 04 06:48:09.733102 2022] [mpm_event:notice] [pid 1:tid 140025331109184] 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 06:52:43.950128 2022] [mpm_event:notice] [pid 1:tid 140491052571968] AH00489: Apache/2.4.54 (Unix) configured -- resuming normal operations
[Thu Aug 04 06:52:43.950391 2022] [core:notice] [pid 1:tid 140491052571968] AH00094: Command line: 'httpd -D FOREGROUND'

docker tun 创建一个新的容器并运行一个命令

[root@localhost ~]# docker run -it --name mjq busybox /bin/sh
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
50783e0dfb64: Pull complete 
Digest: sha256:ef320ff10026a50cf5f0213d35537ce0041ac1d96e9b7800bafd8bc9eff6c693
Status: Downloaded newer image for busybox:latest
/ # exit

docker attach 连接到正在运行的容器

[root@localhost ~]# docker start mjq
mjq
[root@localhost ~]# docker attach mjq
/ # exit

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

[root@localhost ~]# docker start mjq
mjq
[root@localhost ~]# docker exec -it mjq /bin/sh
/ # exit
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                        PORTS     NAMES
7aaf77dbf95a   busybox   "/bin/sh"            6 minutes ago    Up 49 seconds                           mjq
c651f0cda79e   httpd     "httpd-foreground"   18 minutes ago   Exited (137) 10 minutes ago             tnt

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: 1
  Paused: 0
  Stopped: 1
 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: AIN3:GR44:6POW:XTSY:HUNI:KL23:SUFT:PY7X:YW65:LUAJ:Z6DI:7JPR
 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
 Live Restore Enabled: false
 

docker inspect 返回有关docker对象的低级信息

[root@localhost ~]# docker inspect c6
[
    {
        "Id": "c651f0cda79eb2653891e4c3288b80a6a6ec6e27b550be693c119609ded871dd",
        "Created": "2022-08-04T06:45:19.628866579Z",
        "Path": "httpd-foreground",
        "Args": [],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 137,
            "Error": "",
            "StartedAt": "2022-08-04T06:52:43.910709342Z",
            "FinishedAt": "2022-08-04T06:53:56.868847549Z"
        },

docker rm删除一个或多个容器 运行时不可以删除

[root@localhost ~]# docker stop mjq
mjq
[root@localhost ~]# docker rm mjq
mjq
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

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

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

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

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

注意:当删除容器时,这个容器自有的 “ 可写 ” 层会一起被删除
##docker镜像层
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kvVgXJwW-1659660650711)(./1.jpg)]

位于下层的镜像称为父镜像(parrent image),最底层的称为基础镜像(base image);
最上层为“可读写”层,其下的均为“只读”层。
##docker存储驱动
docker提供了多种存储驱动来实现不同的方式存储镜像,下面是常用的几种存储驱动:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值