docker发展史、存储驱动及一些基本命令用法

docker基础

1. docker发展史

​ Docker最初是dotCloud公司创始人Solomon Hykes在法国期间发起的一个公司内部项目,它是基于dotCloud公司多年云服务技术的一次革新,并于2013 年3月以Apache 2.0授权协议开源,主要项目代码在GitHub上进行维护。Docker项目后来还加入了Linux基金会,并成立推动开放容器联盟(OCI)。

​ Docker自开源后受到广泛的关注和讨论,至今其GitHub项目已经超过4万6千个星标和一万多个fork。甚至由于Docker项目的火爆,在 2013年底,dotCloud公司决定改名为Docker。Docker最初是在Ubuntu 12.04上开发实现的;Red Hat则从RHEL 6.5开始对Docker进行支持;Google也在其PaaS产品中广泛应用Docker。

​ Docker使用Google公司推出的Go语言进行开发实现,基于Linux内核的cgroup,namespace,以及AUFS类的Union FS等技术,对进程进行封装隔离,属于操作系统层面的虚拟化技术。由于隔离的进程独立于宿主和其它的隔离的进程,因此也称其为容器。最初实现是基于LXC,从0.7版本以后开始去除LXC,转而使用自行开发的libcontainer,从1.11开始,则进一步演进为使用runC和containerd。

2. 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在正在运行的容器中运行命令‎
docker info显示系统范围的信息‎
docker inspect返回有关 Docker 对象的低级信息‎
  • 用法
//安装docker
[root@localhost ~]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1919  100  1919    0     0   2281      0 --:--:-- --:--:-- --:--:--  2279
[root@localhost ~]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
[root@localhost ~]# ls
anaconda-ks.cfg  docker-ce.repo
[root@localhost ~]# mv docker-ce.repo /etc/yum.repos.d/
[root@localhost ~]# dnf -y install docker-ce
Failed to set locale, defaulting to C.UTF-8
Docker CE Stable - x86_64                       7.4 kB/s |  26 kB     00:03    
appstream                                       2.4 MB/s | 4.4 kB     00:00    
BaseOS                                          2.8 MB/s | 3.9 kB     00:00    
Dependencies resolved.
·······
Complete!


//启动docker并设为开机自启
[root@localhost ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@localhost ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor pres>
   Active: active (running) since Fri 2022-08-05 15:26:34 CST; 6s ago
     Docs: https://docs.docker.com
 Main PID: 40172 (dockerd)
    Tasks: 7
   Memory: 31.7M
   CGroup: /system.slice/docker.service
           └─40172 /usr/bin/dockerd -H fd:// --cont
           ······
           

//配置docker镜像加速器
[root@localhost ~]# vim /etc/docker/daemon.json
[root@localhost ~]# cat /etc/docker/daemon.json
{
          "registry-mirrors": ["https://8r73devx.mirror.aliyuncs.com"]
}
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker info
······
 Registry Mirrors:
  https://8r73devx.mirror.aliyuncs.com/
 Live Restore Enabled: false


//查看docker版本信息
[root@localhost ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.17
 API version:       1.41
 Go version:        go1.17.11
 Git commit:        100c701
 Built:             Mon Jun  6 23:03:11 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.17
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.17.11
  Git commit:       a89b842
  Built:            Mon Jun  6 23:01:29 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.6
  GitCommit:        10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
 runc:
  Version:          1.1.2
  GitCommit:        v1.1.2-0-ga916309
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0


//拉取centos镜像
[root@localhost ~]# docker pull centos
Using default tag: latest
a1d0c7532777: Pull complete 
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest


//查看镜像
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
centos       latest    5d0da3dc9764   10 months ago   231MB


//搜索httpd
[root@localhost ~]# docker search httpd
NAME                                 DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
httpd                                The Apache HTTP Server Project                  4107      [OK]       
centos/httpd-24-centos7              Platform for running Apache httpd 2.4 or bui…   44                   
centos/httpd                                                                         35                   [OK]
clearlinux/httpd                     httpd HyperText Transfer Protocol (HTTP) ser…   2                    
hypoport/httpd-cgi                   httpd-cgi                                       2                    [OK]
solsson/httpd-openidc                mod_auth_openidc on official httpd image, ve…   2                    [OK]
dockerpinata/httpd                                                                   1                    
nnasaki/httpd-ssi                    SSI enabled Apache 2.4 on Alpine Linux          1                    
lead4good/httpd-fpm                  httpd server which connects via fcgi proxy h…   1                    [OK]
inanimate/httpd-ssl                  A play container with httpd, ssl enabled, an…   1                    [OK]
publici/httpd                        httpd:latest                                    1                    [OK]
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]
centos/httpd-24-centos8                                                              1                    
manasip/httpd                                                                        0                    
amd64/httpd                          The Apache HTTP Server Project                  0                    
patrickha/httpd-err                                                                  0                    
manageiq/httpd_configmap_generator   Httpd Configmap Generator                       0                    [OK]
httpdss/archerysec                   ArcherySec repository                           0                    [OK]
paketobuildpacks/httpd                                                               0                    
httpdocker/kubia                                                                     0                    
sandeep1988/httpd-new                httpd-new                                       0                    
e2eteam/httpd                                                                        0                    
19022021/httpd-connection_test       This httpd image will test the connectivity …   0                    
sherazahmedvaival/httpd-php-fpm74                                                    0                    
[root@localhost ~]# 


//使用busybox镜像创建一个容器
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
busybox      latest    beae173ccac6   7 months ago    1.24MB
centos       latest    5d0da3dc9764   10 months ago   231MB
[root@localhost ~]# docker create busybox
5536de865dd06bc3e31de7ce9be3695414f3e5fe812f8837281c1527fd71daf1

//显示所有容器
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS    PORTS     NAMES
5536de865dd0   busybox   "sh"      28 seconds ago   Created             confident_sammet
[root@localhost ~]# 


//启动创建的容器
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS    PORTS     NAMES
5536de865dd0   busybox   "sh"      28 seconds ago   Created             confident_sammet
[root@localhost ~]# docker start 5536de865dd0
5536de865dd0
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS                     PORTS     NAMES
5536de865dd0   busybox   "sh"      4 minutes ago   Exited (0) 7 seconds ago             confident_sammet


docker创建web并运行访问网页

[root@localhost ~]# docker run -d --name web -p 80:80 httpd
9d1d2ca1380d215aa8ac57984954f9836c51ca005353746fe3b9842b3ebdeb81
[root@localhost ~]# 

在这里插入图片描述

//获取web容器日志
[root@localhost ~]# docker logs web
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. 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.3. Set the 'ServerName' directive globally to suppress this message
[Fri Aug 05 08:13:59.798953 2022] [mpm_event:notice] [pid 1:tid 140464886181184] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
[Fri Aug 05 08:13:59.799074 2022] [core:notice] [pid 1:tid 140464886181184] AH00094: Command line: 'httpd -D FOREGROUND'
192.168.183.1 - - [05/Aug/2022:08:14:21 +0000] "GET / HTTP/1.1" 200 45
192.168.183.1 - - [05/Aug/2022:08:14:21 +0000] "GET /favicon.ico HTTP/1.1" 404 196
192.168.183.1 - - [05/Aug/2022:08:15:12 +0000] "-" 408 -
192.168.183.1 - - [05/Aug/2022:08:17:06 +0000] "-" 408 -
192.168.183.1 - - [05/Aug/2022:08:17:06 +0000] "-" 408 -


//docker重启、停止web容器
[root@localhost ~]# docker restart web
web
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS                               NAMES
9d1d2ca1380d   httpd     "httpd-foreground"   6 minutes ago    Up 4 seconds    0.0.0.0:80->80/tcp, :::80->80/tcp   web
039326b658b3   busybox   "sleep 6000"         15 minutes ago   Up 14 minutes                                       relaxed_mcclintock
[root@localhost ~]# docker stop web
web
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND        CREATED          STATUS          PORTS     NAMES
039326b658b3   busybox   "sleep 6000"   15 minutes ago   Up 14 minutes             relaxed_mcclintock
[root@localhost ~]# 


//杀死容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS                               NAMES
9d1d2ca1380d   httpd     "httpd-foreground"   7 minutes ago    Up 5 seconds    0.0.0.0:80->80/tcp, :::80->80/tcp   web
039326b658b3   busybox   "sleep 6000"         16 minutes ago   Up 15 minutes                                       relaxed_mcclintock
[root@localhost ~]# docker kill 039326b658b3
039326b658b3
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS          PORTS                               NAMES
9d1d2ca1380d   httpd     "httpd-foreground"   7 minutes ago   Up 21 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web


//强制删除容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS          PORTS                               NAMES
9d1d2ca1380d   httpd     "httpd-foreground"   7 minutes ago   Up 21 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   web
[root@localhost ~]# docker rm -f web
web
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker帮助文档

[root@localhost ~]# docker --help

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default
                           "/root/.docker")
  -c, --context string     Name of the context to use to connect to the
                           daemon (overrides DOCKER_HOST env var and
                           default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level
                           ("debug"|"info"|"warn"|"error"|"fatal")
                           (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default
                           "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default
                           "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default
                           "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  app*        Docker App (Docker Inc., v0.9.1-beta3)
  builder     Manage builds
  buildx*     Docker Buildx (Docker Inc., v0.8.2-docker)
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  image       Manage images
  manifest    Manage Docker image manifests and manifest lists
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  scan*       Docker Scan (Docker Inc., v0.17.0)
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
[root@localhost ~]# 

3. docker存储驱动

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

  • AUFS
  • OverlayFS
  • Devicemapper
  • Btrfs
  • ZFS

AUFS:
文件级的存储驱动。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。

Devicemapper:
Device mapper是块级存储,所有的操作都是直接对块进行操作,而不是文件。Device mapper驱动会先在块设备上创建一个资源池,然后在资源池上创建一个带有文件系统的基本设备,所有镜像都是这个基本设备的快照,而容器则是镜像的快照。所以在容器里看到文件系统是资源池上基本设备的文件系统的快照,并不有为容器分配空间。当要写入一个新文件时,在容器的镜像内为其分配新的块并写入数据,这个叫用时分配。当要修改已有文件时,再使用CoW为容器快照分配块空间,将要修改的数据复制到在容器快照中新的块里再进行修改。Device mapper 驱动默认会创建一个100G的文件包含镜像和容器。每一个容器被限制在10G大小的卷内,可以自己配置调整。

Btrfs:
Btrfs被称为下一代写时复制文件系统,并入Linux内核,也是文件级存储驱动,但可以像Device mapper一直接操作底层设备。Btrfs把文件系统的一部分配置为一个完整的子文件系统,称之为subvolume 。采用 subvolume,一个大的文件系统可以被划分为多个子文件系统,这些子文件系统共享底层的设备空间,在需要磁盘空间时便从底层设备中分配,类似应用程序调用 malloc()分配内存一样。为了灵活利用设备空间,Btrfs 将磁盘空间划分为多个chunk 。每个chunk可以使用不同的磁盘空间分配策略。比如某些chunk只存放metadata,某些chunk只存放数据。这种模型有很多优点,比如Btrfs支持动态添加设备。用户在系统中增加新的磁盘之后,可以使用Btrfs的命令将该设备添加到文件系统中。Btrfs把一个大的文件系统当成一个资源池,配置成多个完整的子文件系统,还可以往资源池里加新的子文件系统,而基础镜像则是子文件系统的快照,每个子镜像和容器都有自己的快照,这些快照则都是subvolume的快照。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值