docker容器技术基础入门

容器(Container)

  • 容器是一种基础工具;泛指任何可以用于容纳其他物品的工具,可以部分或完全封闭,被用于容纳、储存、运输物品;物体可以被放置在容器中,而容器则可以保护内容物;
  • 人类使用容器的历史至少有十万年,甚至可能有数百万年的历史;
  • 容器的类型
  • 瓶 - 指口部比腹部窄小、颈长的容器
  • 罐 - 指那些开口较大、一般为近圆筒形的器皿
  • 箱 - 通常是立方体或圆柱体。形状固定
  • 篮 - 以条状物编织而成
  • 桶 - 一种圆柱形的容器
  • 袋 - 柔性材料制成的容器,形状会受内容物而变化
  • 瓮 - 通常是指陶制,口小肚大的容器
  • 碗 - 用来盛载食物的容器
  • 柜 - 指一个由盒组成的家具
  • 鞘 - 用于装载刀刃的容器

传统虚拟化与容器的区别

虚拟化分为以下两类:

  • 主机级虚拟化
  • 全虚拟化
  • 半虚拟化
  • 容器级虚拟化

容器分离开的资源:

  • UTS(主机名与域名)
  • Mount(文件系统挂载树)
  • IPC
  • PID进程树
  • User
  • Network(tcp/ip协议栈)

Linux容器技术

Linux容器其实并不是什么新概念。最早的容器技术可以追遡到1982年Unix系列操作系统上的chroot工具(直到今天,主流的Unix、Linux操作系统仍然支持和带有该工具)。

Linux Namespaces

命名空间(Namespaces)是Linux内核针对实现容器虚拟化而引入的一个强大特性。

每个容器都可以拥有自己独立的命名空间,运行其中的应用都像是在独立的操作系统中运行一样。命名空间保证了容器间彼此互不影响。

namespaces 系统调用参数 隔离内容 内核版本
UTS CLONE_NEWUTS 主机名和域名 2.6.19
IPC CLONE_NEWIPC 信号量、消息队列和共享内存 2.6.19
PID CLONE_NEWPID 进程编号 2.6.24
Network CLONE_NEWNET 网络设备、网络栈、端口等 2.6.29
Mount CLONE_NEWNS 挂载点(文件系统) 2.4.19
User CLONE_NEWUSER 用户和用户组 3.8

CGroups

控制组(CGroups)是Linux内核的一个特性,用来对共享资源进行隔离、限制、审计等。只有能控制分配到容器的资源,Docker才能避免多个容器同时运行时的系统资源竞争。

控制组可以提供对容器的内存、CPU、磁盘IO等资源进行限制。

CGroups能够限制的资源有:

  • blkio:块设备IO
  • cpu:CPU
  • cpuacct:CPU资源使用报告
  • cpuset:多处理器平台上的CPU集合
  • devices:设备访问
  • freezer:挂起或恢复任务
  • memory:内存用量及报告
  • perf_event:对cgroup中的任务进行统一性能测试
  • net_cls:cgroup中的任务创建的数据报文的类别标识符

具体来看,控制组提供如下功能:

  • 资源限制(Resource Limitting)组可以设置为不超过设定的内存限制。比如:内存子系统可以为进行组设定一个内存使用上限,一旦进程组使用的内存达到限额再申请内存,就会发出Out of Memory警告
  • 优先级(Prioritization)通过优先级让一些组优先得到更多的CPU等资源
  • 资源审计(Accounting)用来统计系统实际上把多少资源用到合适的目的上,可以使用cpuacct子系统记录某个进程组使用的CPU时间
  • 隔离(Isolation)为组隔离命名空间,这样一个组不会看到另一个组的进程、网络连接和文件系统
  • 控制(Control)挂起、恢复和重启等操作

安装Docker后,用户可以在/sys/fs/cgroup/memory/docker/目录下看到对Docker组应用的各种限制项,包括

  1. [root@localhost ~]# cd /sys/fs/cgroup/memory/
  2. [root@localhost memory]# ls
  3. cgroup.clone_children memory.kmem.slabinfo memory.memsw.limit_in_bytes memory.swappiness
  4. cgroup.event_control memory.kmem.tcp.failcnt memory.memsw.max_usage_in_bytes memory.usage_in_bytes
  5. cgroup.procs memory.kmem.tcp.limit_in_bytes memory.memsw.usage_in_bytes memory.use_hierarchy
  6. cgroup.sane_behavior memory.kmem.tcp.max_usage_in_bytes memory.move_charge_at_immigrate notify_on_release
  7. memory.failcnt memory.kmem.tcp.usage_in_bytes memory.numa_stat release_agent
  8. memory.force_empty memory.kmem.usage_in_bytes memory.oom_control system.slice
  9. memory.kmem.failcnt memory.limit_in_bytes memory.pressure_level tasks
  10. memory.kmem.limit_in_bytes memory.max_usage_in_bytes memory.soft_limit_in_bytes user.slice
  11. memory.kmem.max_usage_in_bytes memory.memsw.failcnt memory.stat

用户可以通过修改这些文件值来控制组限制Docker应用资源。

LXC

通过传统方式使用容器功能的话需要我们自己写代码去进行系统调用来实现创建内核,实际上拥有此能力的人廖廖无几。而LXC(LinuX Container)把容器技术做得更加易用,把需要用到的容器功能做成一组工具,从而极大的简化用户使用容器技术的麻烦程度。

LXC是最早一批真正把完整的容器技术用一组简易使用的工具和模板来极大的简化了容器技术使用的一个方案。

LXC虽然极大的简化了容器技术的使用,但比起直接通过内核调用来使用容器技术,其复杂程度其实并没有多大降低,因为我们必须要学会LXC的一组命令工具,且由于内核的创建都是通过命令来实现的,通过批量命令实现数据迁移并不容易。其隔离性也没有虚拟机那么强大。

后来就出现了docker,所以从一定程度上来说,docker就是LXC的增强版。

docker基本概念

docker是容器技术的一个前端工具,容器是内核的一项技术,docker只是把这一项技术的使用得以简化,使之普及而已。

LXC进行大规模创建容器很难,想在另一台主机上复刻一个一模一样的容器也很难,而docker就是从这方面着手去找解决方案。所以docker早期的版本其核心就是一个LXC,docker对其进行了二次封装,功能的实现是通过LXC做容器管理引擎,但是在创建容器时,不再是像LXC一样用模板去现场安装,而是事先通过一种类似镜像技术,就像在KVM中一样,将一个操作系统打包成一个镜像,然后将这个镜像拷贝到目标主机上直接部署启动。

我们可以尝试着把一个操作系统用户空间需要用到的所有组件,事先准备、编排好,编排好以后整体打包成一个文件,这个文件我们称其为镜像文件(image)。

docker的镜像文件是放在一个集中统一的互联网仓库中的,把一些人们常用的镜像文件放在互联网仓库中,比如最小化的centos系统,有时我们需要在操作系统上安装一些应用,比如nginx,我们就可以在一个最小化的centos系统中安装一个nginx,然后将其打包成镜像,将其放在互联网仓库中,当人们想启动一个容器的时候,docker会到这个互联网仓库中去下载我们需要的镜像到本地,并基于镜像来启动容器。

自docker 0.9版本起,docker除了继续支持LXC外,还开始引入自家的libcontainer,试图打造更通用的底层容器虚拟化库。如今的docker基本上都已经是使用libcontainer而非LXC了。

从操作系统功能上看,docker底层依赖的核心技术主要包括Linux操作系统的命名空间、控制组、联合文件系统和Linux虚拟网络支持。

docker工作方式

为了使容器的使用更加易于管理,docker采取一个用户空间只跑一个业务进程的方式,在一个容器内只运行一个进程,比如我们要在一台主机上安装一个nginx和一个tomcat,那么nginx就运行在nginx的容器中,tomcat运行在tomcat的容器中,二者用容器间的通信逻辑来进行通信。

LXC是把一个容器当一个用户空间使用,当虚拟机一样使用,里面可以运行N个进程,这就使得我们在容器内去管理时极为不便,而docker用这种限制性的方式,在一个容器中只运行一个进程的方式,使得容器的管理更加方便。

使用docker的优劣:

  • 删除一个容器不会影响其他容器
  • 调试不便,占空间(每个容器中都必须自带调试工具,比如ps命令)
  • 分发容易,真正意义上一次编写到处运行,比java的跨平台更彻底
  • 部署容易,无论底层系统是什么,只要有docker,直接run就可以了
  • 分层构建,联合挂载

在容器中有数据称作有状态,没有数据称作无状态。在容器的使用中,我们应以有状态为耻,以无状态为荣。数据不应该放在容器中,而应放置于外部存储中,通过挂载到容器中从而进行数据的存储。

docker容器编排

当我们要去构建一个lnmp架构的时候,它们之间会有依赖关系,哪个应用应该在什么时候启动,在谁之前或之后启动,这种依赖关系我们应该要事先定义好,最好是按照一定的次序实现,而docker自身没有这个功能,所以我们需要一个在docker的基础上,能够把这种应用程序之间的依赖关系、从属关系、隶属关系等等反映在启动、关闭时的次序和管理逻辑中,这种功能被称为容器编排。

有了docker以后,运维的发布工作必须通过编排工具来实现容器的编排,如果没有编排工具,运维人员想去管理容器其实比直接管理程序要更加麻烦,增加了运维环境管理的复杂度。

常见的容器编排工具:

  • machine+swarm(把N个docker主机当一个主机来管理)+compose(单机编排)
  • mesos(实现统一资源调度和分配)+marathon
  • kubernetes --> k8s

docker基础用法

什么是docker

docker中的容器:

  • lxc --> libcontainer --> runC

OCI&OCF

OCI

Open Container-initiative(开放容器组织)

  • 由Linux基金会主导于2015年6月创立
  • 旨在围绕容器格式和运行时制定一个开放的工业化标准
  • contains two specifications(包含两种规格)
  • the Runtime Specification(运行时规格)(runtime-spec)
  • the Image Specification(图像规格)(image-spec)

OCF

Open Container Format(开放式容器格式)

runC is a CLI tool for spawning and running containers according to the OCI specification(runC是一个CLI工具,用于根据OCI规范生成和运行容器)

  • Containers are started as a child process of runC and can be embedded into various other systems without having to run a daemon(容器作为runC的子进程启动,可以嵌入到各种其他系统中,而不必运行守护进程)

  • runC is built on libcontainer, the same container technology powering millions of Docker Engine installations(runC是基于libcontainer构建的,同样的容器技术支持数百万的Docker引擎安装)

    docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com

docker架构

在客户端任意敲一条命令,都要到达服务端的守护进程,守护进程解析客户端的指令,然后在执行。如果本地没有此镜像,就会从官方镜像仓库(注册表)里拉取。

docker镜像与镜像仓库

为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。

镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。

docker对象

When you use docker, you are creating and using images, containers, networks, volumes, pluginns, and other objects.(当您使用docker容器时,您正在创建和使用映像、容器、网络、卷、插件和其他对象。)

  • IMAGES(图像)
  • An image is a read-only template with instructions for creating a docker - container.(图像是一个只读模板,包含创建docker容器的说明。)
  • Often, an image is based on another image, with some additional customization.(通常,一个图像基于另一个图像,并进行一些额外的定制。)
  • You might create your own images or you might only use those created by others and published in a registry.(您可以创建自己的图像,也可以只使用他人创建并在注册表中发布的图像。)
  • CONTAINERS(容器)
  • A container is a runnable instance of an image.(容器是图像的可运行实例。)
  • You can create, run, stop, move, or delete a container using the docker API or CLI.(您可以使用docker容器 API或CLI创建、运行、停止、移动或删除容器。)
  • You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.(您可以将容器连接到一个或多个网络,为其附加存储,甚至基于其当前状态创建新的映像。)

安装及使用docker

docker安装

  1. 删除本地仓库
  2. [root@localhost ~]# rm -rf /etc/yum.repos.d/*
  3. 安装阿里云网络仓库
  4. [root@localhost ~]# cd /etc/yum.repos.d/
  5. [root@localhost yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  6. % Total % Received % Xferd Average Speed Time Time Time Current
  7. Dload Upload Total Spent Left Speed
  8. 100 2495 100 2495 0 0 11138 0 --:--:-- --:--:-- --:--:-- 11138
  9. [root@localhost yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
  10. 安装epel源
  11. [root@localhost yum.repos.d]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
  12. [root@localhost yum.repos.d]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
  13. [root@localhost yum.repos.d]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
  14. 安装docker
  15. [root@localhost yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  16. [root@localhost yum.repos.d]# yum -y install docker-ce

安装Docker后,用户可以在/sys/fs/cgroup/memory/docker/目录下看到对Docker组应用的各种限制项,包括

  1. [root@localhost yum.repos.d]# cd /sys/fs/cgroup/memory/
  2. [root@localhost memory]# ls
  3. cgroup.clone_children memory.memsw.failcnt
  4. cgroup.event_control memory.memsw.limit_in_bytes
  5. cgroup.procs memory.memsw.max_usage_in_bytes
  6. cgroup.sane_behavior memory.memsw.usage_in_bytes
  7. init.scope memory.move_charge_at_immigrate
  8. memory.failcnt memory.numa_stat
  9. memory.force_empty memory.oom_control
  10. memory.kmem.failcnt memory.pressure_level
  11. memory.kmem.limit_in_bytes memory.soft_limit_in_bytes
  12. memory.kmem.max_usage_in_bytes memory.stat
  13. memory.kmem.slabinfo memory.swappiness
  14. memory.kmem.tcp.failcnt memory.usage_in_bytes
  15. memory.kmem.tcp.limit_in_bytes memory.use_hierarchy
  16. memory.kmem.tcp.max_usage_in_bytes notify_on_release
  17. memory.kmem.tcp.usage_in_bytes release_agent
  18. memory.kmem.usage_in_bytes system.slice
  19. memory.limit_in_bytes tasks
  20. memory.max_usage_in_bytes user.slice

docker加速

docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。

docker的加速有多种方式:

  • docker cn
  • 中国科技大学加速器
  • 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)

注册阿里云账号

  1. [root@localhost ~]# systemctl start docker
  2. [root@localhost ~]# systemctl status docker
  3. docker.service - Docker Application Container Engine
  4. Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset:>
  5. Active: active (running) since Wed 2022-08-03 14:53:13 CST; 10s ago
  6. Docs: https://docs.docker.com
  7. 配置docker-ce的配置文件
  8. [root@localhost ~]# vim /etc/docker/daemon.json
  9. {
  10. "registry-mirrors": ["https://4hygggbu.mirror.aliyuncs.com/"]
  11. }
  12. EOF
  13. [root@localhost ~]# docker version
  14. Client: Docker Engine - Community
  15. Version: 20.10.17
  16. API version: 1.41
  17. Go version: go1.17.11
  18. Git commit: 100c701
  19. Built: Mon Jun 6 23:03:11 2022
  20. OS/Arch: linux/amd64
  21. Context: default
  22. Experimental: true
  23. Server: Docker Engine - Community
  24. Engine:
  25. Version: 20.10.17
  26. API version: 1.41 (minimum version 1.12)
  27. Go version: go1.17.11
  28. Git commit: a89b842
  29. Built: Mon Jun 6 23:01:29 2022
  30. OS/Arch: linux/amd64
  31. Experimental: false
  32. containerd:
  33. Version: 1.6.6
  34. GitCommit: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
  35. runc:
  36. Version: 1.1.2
  37. GitCommit: v1.1.2-0-ga916309
  38. docker-init:
  39. Version: 0.19.0
  40. GitCommit: de40ad0
  41. [root@localhost ~]# docker info
  42. Client:
  43. Context: default
  44. Debug Mode: false
  45. Plugins:
  46. app: Docker App (Docker Inc., v0.9.1-beta3)
  47. buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
  48. scan: Docker Scan (Docker Inc., v0.17.0)
  49. Server:
  50. Containers: 0
  51. Running: 0
  52. Paused: 0
  53. Stopped: 0
  54. Images: 0
  55. Server Version: 20.10.17
  56. Storage Driver: overlay2
  57. Backing Filesystem: xfs
  58. Supports d_type: true
  59. Native Overlay Diff: true
  60. userxattr: false
  61. Logging Driver: json-file
  62. Cgroup Driver: cgroupfs
  63. Cgroup Version: 1
  64. Plugins:
  65. Volume: local
  66. Network: bridge host ipvlan macvlan null overlay
  67. Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
  68. Swarm: inactive
  69. Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
  70. Default Runtime: runc
  71. Init Binary: docker-init
  72. containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
  73. runc version: v1.1.2-0-ga916309
  74. init version: de40ad0
  75. Security Options:
  76. seccomp
  77. Profile: default
  78. Kernel Version: 4.18.0-257.el8.x86_64
  79. Operating System: CentOS Stream 8
  80. OSType: linux
  81. Architecture: x86_64
  82. CPUs: 4
  83. Total Memory: 1.748GiB
  84. Name: localhost.localdomain
  85. ID: FA5V:A2WK:BUPU:3MPL:S36P:CDJB:H3BQ:SGXY:4UIP:NDC2:ZCOX:O3VX
  86. Docker Root Dir: /var/lib/docker
  87. Debug Mode: false
  88. Registry: https://index.docker.io/v1/
  89. Labels:
  90. Experimental: false
  91. Insecure Registries:
  92. 127.0.0.0/8
  93. Registry Mirrors:
  94. https://4hygggbu.mirror.aliyuncs.com/
  95. Live Restore Enabled: false

docker常用操作

|命令| 功能|

docker search(在Docker Hub中搜索镜像)Search the Docker Hub for images

  1. [root@localhost ~]# docker search --help
  2. Usage: docker search [OPTIONS] TERM
  3. Search the Docker Hub for images
  4. Options:
  5. -f, --filter filter Filter output based on conditions provided
  6. --format string Pretty-print search using a Go template
  7. --limit int Max number of search results (default 25)
  8. --no-trunc Don't truncate output

docker pull (从注册中心拉取镜像或仓库)Pull an image or a repository from a registry

  1. [root@localhost ~]# docker pull --help
  2. Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
  3. Pull an image or a repository from a registry
  4. Options:
  5. -a, --all-tags Download all tagged images in the repository
  6. --disable-content-trust Skip image verification (default true)
  7. --platform string Set platform if server is multi-platform capable
  8. -q, --quiet Suppress verbose output

docker images(列出镜像列表) List images

  1. [root@localhost ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. centos latest 5d0da3dc9764 10 months ago 231MB

docker create(创建新的容器) Create a new conntainer

  1. [root@localhost ~]# docker create --help
  2. Usage: docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
  3. Create a new container
  4. Options:
  5. --add-host list Add a custom host-to-IP mapping (host:ip)
  6. -a, --attach list Attach to STDIN, STDOUT or STDERR
  7. --blkio-weight uint16 Block IO (relative weight), between 10
  8. and 1000, or 0 to disable (default 0)
  9. --blkio-weight-device list Block IO weight (relative device
  10. weight) (default [])
  11. --cap-add list Add Linux capabilities
  12. --cap-drop list Drop Linux capabilities
  13. --cgroup-parent string Optional parent cgroup for the container
  14. --cgroupns string Cgroup namespace to use (host|private)
  15. 'host': Run the container in the
  16. Docker host's cgroup namespace
  17. 'private': Run the container in its
  18. own private cgroup namespace
  19. '': Use the cgroup namespace as
  20. configured by the
  21. default-cgroupns-mode
  22. option on the daemon (default)
  23. --cidfile string Write the container ID to the file
  24. --cpu-period int Limit CPU CFS (Completely Fair
  25. Scheduler) period
  26. --cpu-quota int Limit CPU CFS (Completely Fair
  27. Scheduler) quota
  28. --cpu-rt-period int Limit CPU real-time period in microseconds
  29. --cpu-rt-runtime int Limit CPU real-time runtime in microseconds
  30. -c, --cpu-shares int CPU shares (relative weight)
  31. --cpus decimal Number of CPUs
  32. --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
  33. --cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
  34. --device list Add a host device to the container
  35. --device-cgroup-rule list Add a rule to the cgroup allowed
  36. devices list
  37. --device-read-bps list Limit read rate (bytes per second)
  38. from a device (default [])
  39. --device-read-iops list Limit read rate (IO per second) from a
  40. device (default [])
  41. --device-write-bps list Limit write rate (bytes per second) to
  42. a device (default [])
  43. --device-write-iops list Limit write rate (IO per second) to a
  44. device (default [])
  45. --disable-content-trust Skip image verification (default true)
  46. --dns list Set custom DNS servers
  47. --dns-option list Set DNS options
  48. --dns-search list Set custom DNS search domains
  49. --domainname string Container NIS domain name
  50. --entrypoint string Overwrite the default ENTRYPOINT of
  51. the image
  52. -e, --env list Set environment variables
  53. --env-file list Read in a file of environment variables
  54. --expose list Expose a port or a range of ports
  55. --gpus gpu-request GPU devices to add to the container
  56. ('all' to pass all GPUs)
  57. --group-add list Add additional groups to join
  58. --health-cmd string Command to run to check health
  59. --health-interval duration Time between running the check
  60. (ms|s|m|h) (default 0s)
  61. --health-retries int Consecutive failures needed to report
  62. unhealthy
  63. --health-start-period duration Start period for the container to
  64. initialize before starting
  65. health-retries countdown (ms|s|m|h)
  66. (default 0s)
  67. --health-timeout duration Maximum time to allow one check to run
  68. (ms|s|m|h) (default 0s)
  69. --help Print usage
  70. -h, --hostname string Container host name
  71. --init Run an init inside the container that
  72. forwards signals and reaps processes
  73. -i, --interactive Keep STDIN open even if not attached
  74. --ip string IPv4 address (e.g., 172.30.100.104)
  75. --ip6 string IPv6 address (e.g., 2001:db8::33)
  76. --ipc string IPC mode to use
  77. --isolation string Container isolation technology
  78. --kernel-memory bytes Kernel memory limit
  79. -l, --label list Set meta data on a container
  80. --label-file list Read in a line delimited file of labels
  81. --link list Add link to another container
  82. --link-local-ip list Container IPv4/IPv6 link-local addresses
  83. --log-driver string Logging driver for the container
  84. --log-opt list Log driver options
  85. --mac-address string Container MAC address (e.g.,
  86. 92:d0:c6:0a:29:33)
  87. -m, --memory bytes Memory limit
  88. --memory-reservation bytes Memory soft limit
  89. --memory-swap bytes Swap limit equal to memory plus swap:
  90. '-1' to enable unlimited swap
  91. --memory-swappiness int Tune container memory swappiness (0 to
  92. 100) (default -1)
  93. --mount mount Attach a filesystem mount to the container
  94. --name string Assign a name to the container
  95. --network network Connect a container to a network
  96. --network-alias list Add network-scoped alias for the container
  97. --no-healthcheck Disable any container-specified HEALTHCHECK
  98. --oom-kill-disable Disable OOM Killer
  99. --oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
  100. --pid string PID namespace to use
  101. --pids-limit int Tune container pids limit (set -1 for
  102. unlimited)
  103. --platform string Set platform if server is
  104. multi-platform capable
  105. --privileged Give extended privileges to this container
  106. -p, --publish list Publish a container's port(s) to the host
  107. -P, --publish-all Publish all exposed ports to random ports
  108. --pull string Pull image before creating
  109. ("always"|"missing"|"never") (default
  110. "missing")
  111. --read-only Mount the container's root filesystem
  112. as read only
  113. --restart string Restart policy to apply when a
  114. container exits (default "no")
  115. --rm Automatically remove the container
  116. when it exits
  117. --runtime string Runtime to use for this container
  118. --security-opt list Security Options
  119. --shm-size bytes Size of /dev/shm
  120. --stop-signal string Signal to stop a container (default
  121. "SIGTERM")
  122. --stop-timeout int Timeout (in seconds) to stop a container
  123. --storage-opt list Storage driver options for the container
  124. --sysctl map Sysctl options (default map[])
  125. --tmpfs list Mount a tmpfs directory
  126. -t, --tty Allocate a pseudo-TTY
  127. --ulimit ulimit Ulimit options (default [])
  128. -u, --user string Username or UID (format:
  129. <name|uid>[:<group|gid>])
  130. --userns string User namespace to use
  131. --uts string UTS namespace to use
  132. -v, --volume list Bind mount a volume
  133. --volume-driver string Optional volume driver for the container
  134. --volumes-from list Mount volumes from the specified
  135. container(s)
  136. -w, --workdir string Working directory inside the container

docker start(启动一个或多个已停止的容器) Start one or more stopped containers

  1. [root@localhost ~]# docker start --help
  2. Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
  3. Start one or more stopped containers
  4. Options:
  5. -a, --attach Attach STDOUT/STDERR and forward signals
  6. --detach-keys string Override the key sequence for detaching a container
  7. -i, --interactive Attach container's STDIN

docker run(在新容器中运行的命令) Run a command in a new container

  1. [root@localhost ~]# docker run --help
  2. Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  3. Run a command in a new container
  4. Options:
  5. --add-host list Add a custom host-to-IP mapping (host:ip)
  6. -a, --attach list Attach to STDIN, STDOUT or STDERR
  7. --blkio-weight uint16 Block IO (relative weight), between 10
  8. and 1000, or 0 to disable (default 0)
  9. --blkio-weight-device list Block IO weight (relative device
  10. weight) (default [])
  11. --cap-add list Add Linux capabilities
  12. --cap-drop list Drop Linux capabilities
  13. --cgroup-parent string Optional parent cgroup for the container
  14. --cgroupns string Cgroup namespace to use (host|private)
  15. 'host': Run the container in the
  16. Docker host's cgroup namespace
  17. 'private': Run the container in its
  18. own private cgroup namespace
  19. '': Use the cgroup namespace as
  20. configured by the
  21. default-cgroupns-mode
  22. option on the daemon (default)
  23. --cidfile string Write the container ID to the file
  24. --cpu-period int Limit CPU CFS (Completely Fair
  25. Scheduler) period
  26. --cpu-quota int Limit CPU CFS (Completely Fair
  27. Scheduler) quota
  28. --cpu-rt-period int Limit CPU real-time period in microseconds
  29. --cpu-rt-runtime int Limit CPU real-time runtime in microseconds
  30. -c, --cpu-shares int CPU shares (relative weight)
  31. --cpus decimal Number of CPUs
  32. --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
  33. --cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
  34. -d, --detach Run container in background and print
  35. container ID
  36. --detach-keys string Override the key sequence for
  37. detaching a container
  38. --device list Add a host device to the container
  39. --device-cgroup-rule list Add a rule to the cgroup allowed
  40. devices list
  41. --device-read-bps list Limit read rate (bytes per second)
  42. from a device (default [])
  43. --device-read-iops list Limit read rate (IO per second) from a
  44. device (default [])
  45. --device-write-bps list Limit write rate (bytes per second) to
  46. a device (default [])
  47. --device-write-iops list Limit write rate (IO per second) to a
  48. device (default [])
  49. --disable-content-trust Skip image verification (default true)
  50. --dns list Set custom DNS servers
  51. --dns-option list Set DNS options
  52. --dns-search list Set custom DNS search domains
  53. --domainname string Container NIS domain name
  54. --entrypoint string Overwrite the default ENTRYPOINT of
  55. the image
  56. -e, --env list Set environment variables
  57. --env-file list Read in a file of environment variables
  58. --expose list Expose a port or a range of ports
  59. --gpus gpu-request GPU devices to add to the container
  60. ('all' to pass all GPUs)
  61. --group-add list Add additional groups to join
  62. --health-cmd string Command to run to check health
  63. --health-interval duration Time between running the check
  64. (ms|s|m|h) (default 0s)
  65. --health-retries int Consecutive failures needed to report
  66. unhealthy
  67. --health-start-period duration Start period for the container to
  68. initialize before starting
  69. health-retries countdown (ms|s|m|h)
  70. (default 0s)
  71. --health-timeout duration Maximum time to allow one check to run
  72. (ms|s|m|h) (default 0s)
  73. --help Print usage
  74. -h, --hostname string Container host name
  75. --init Run an init inside the container that
  76. forwards signals and reaps processes
  77. -i, --interactive Keep STDIN open even if not attached
  78. --ip string IPv4 address (e.g., 172.30.100.104)
  79. --ip6 string IPv6 address (e.g., 2001:db8::33)
  80. --ipc string IPC mode to use
  81. --isolation string Container isolation technology
  82. --kernel-memory bytes Kernel memory limit
  83. -l, --label list Set meta data on a container
  84. --label-file list Read in a line delimited file of labels
  85. --link list Add link to another container
  86. --link-local-ip list Container IPv4/IPv6 link-local addresses
  87. --log-driver string Logging driver for the container
  88. --log-opt list Log driver options
  89. --mac-address string Container MAC address (e.g.,
  90. 92:d0:c6:0a:29:33)
  91. -m, --memory bytes Memory limit
  92. --memory-reservation bytes Memory soft limit
  93. --memory-swap bytes Swap limit equal to memory plus swap:
  94. '-1' to enable unlimited swap
  95. --memory-swappiness int Tune container memory swappiness (0 to
  96. 100) (default -1)
  97. --mount mount Attach a filesystem mount to the container
  98. --name string Assign a name to the container
  99. --network network Connect a container to a network
  100. --network-alias list Add network-scoped alias for the container
  101. --no-healthcheck Disable any container-specified HEALTHCHECK
  102. --oom-kill-disable Disable OOM Killer
  103. --oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
  104. --pid string PID namespace to use
  105. --pids-limit int Tune container pids limit (set -1 for
  106. unlimited)
  107. --platform string Set platform if server is
  108. multi-platform capable
  109. --privileged Give extended privileges to this container
  110. -p, --publish list Publish a container's port(s) to the host
  111. -P, --publish-all Publish all exposed ports to random ports
  112. --pull string Pull image before running
  113. ("always"|"missing"|"never") (default
  114. "missing")
  115. --read-only Mount the container's root filesystem
  116. as read only
  117. --restart string Restart policy to apply when a
  118. container exits (default "no")
  119. --rm Automatically remove the container
  120. when it exits
  121. --runtime string Runtime to use for this container
  122. --security-opt list Security Options
  123. --shm-size bytes Size of /dev/shm
  124. --sig-proxy Proxy received signals to the process
  125. (default true)
  126. --stop-signal string Signal to stop a container (default
  127. "SIGTERM")
  128. --stop-timeout int Timeout (in seconds) to stop a container
  129. --storage-opt list Storage driver options for the container
  130. --sysctl map Sysctl options (default map[])
  131. --tmpfs list Mount a tmpfs directory
  132. -t, --tty Allocate a pseudo-TTY
  133. --ulimit ulimit Ulimit options (default [])
  134. -u, --user string Username or UID (format:
  135. <name|uid>[:<group|gid>])
  136. --userns string User namespace to use
  137. --uts string UTS namespace to use
  138. -v, --volume list Bind mount a volume
  139. --volume-driver string Optional volume driver for the container
  140. --volumes-from list Mount volumes from the specified
  141. container(s)
  142. -w, --workdir string Working directory inside the container

docker attach(连接到正在运行的容器) Attach to a runninng container

  1. [root@localhost ~]# docker attach --help
  2. Usage: docker attach [OPTIONS] CONTAINER
  3. Attach local standard input, output, and error streams to a running container
  4. Options:
  5. --detach-keys string Override the key sequence for detaching a container
  6. --no-stdin Do not attach STDIN
  7. --sig-proxy Proxy all received signals to the process
  8. (default true)

docker ps(列出容器) List containers

  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

docker logs Fetch the logs of a container

docker restart Restart a container

  1. [root@localhost ~]# docker restart --help
  2. Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]
  3. Restart one or more containers
  4. Options:
  5. -t, --time int Seconds to wait for stop before killing the container
  6. (default 10)

docker stop(停止一个或多个正在运行的容器) Stop one or more running containers

  1. [root@localhost ~]# docker stop --help
  2. Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
  3. Stop one or more running containers
  4. Options:
  5. -t, --time int Seconds to wait for stop before killing it (default 10)

docker kill(终止一个或多个正在运行的容器) Kill one or more running containers

  1. [root@localhost ~]# docker kill --help
  2. Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]
  3. Kill one or more running containers
  4. Options:
  5. -s, --signal string Signal to send to the container (default "KILL")

docker rm(移走一个或多个容器) Remove onne or more containers

  1. [root@localhost ~]# docker rm --help
  2. Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
  3. Remove one or more containers
  4. Options:
  5. -f, --force Force the removal of a running container (uses SIGKILL)
  6. -l, --link Remove the specified link
  7. -v, --volumes Remove anonymous volumes associated with the container

docker exec (在运行的容器中运行命令)Run a command in a running container

  1. [root@localhost ~]# docker exec --help
  2. Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
  3. Run a command in a running container
  4. Options:
  5. -d, --detach Detached mode: run command in the background
  6. --detach-keys string Override the key sequence for detaching a container
  7. -e, --env list Set environment variables
  8. --env-file list Read in a file of environment variables
  9. -i, --interactive Keep STDIN open even if not attached
  10. --privileged Give extended privileges to the command
  11. -t, --tty Allocate a pseudo-TTY
  12. -u, --user string Username or UID (format: <name|uid>[:<group|gid>])
  13. -w, --workdir string Working directory inside the container

docker info (显示系统范围的信息)Display system-wide information

  1. [root@localhost ~]# docker info
  2. Client:
  3. Context: default
  4. Debug Mode: false
  5. Plugins:
  6. app: Docker App (Docker Inc., v0.9.1-beta3)
  7. buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
  8. scan: Docker Scan (Docker Inc., v0.17.0)
  9. Server:
  10. Containers: 1
  11. Running: 0
  12. Paused: 0
  13. Stopped: 1
  14. Images: 1
  15. Server Version: 20.10.17
  16. Storage Driver: overlay2
  17. Backing Filesystem: xfs
  18. Supports d_type: true
  19. Native Overlay Diff: true
  20. userxattr: false
  21. Logging Driver: json-file
  22. Cgroup Driver: cgroupfs
  23. Cgroup Version: 1
  24. Plugins:
  25. Volume: local
  26. Network: bridge host ipvlan macvlan null overlay
  27. Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
  28. Swarm: inactive
  29. Runtimes: runc io.containerd.runc.v2 io.containerd.runtime.v1.linux
  30. Default Runtime: runc
  31. Init Binary: docker-init
  32. containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
  33. runc version: v1.1.2-0-ga916309
  34. init version: de40ad0
  35. Security Options:
  36. seccomp
  37. Profile: default
  38. Kernel Version: 4.18.0-257.el8.x86_64
  39. Operating System: CentOS Stream 8
  40. OSType: linux
  41. Architecture: x86_64
  42. CPUs: 4
  43. Total Memory: 1.748GiB
  44. Name: localhost.localdomain
  45. ID: FA5V:A2WK:BUPU:3MPL:S36P:CDJB:H3BQ:SGXY:4UIP:NDC2:ZCOX:O3VX
  46. Docker Root Dir: /var/lib/docker
  47. Debug Mode: false
  48. Registry: https://index.docker.io/v1/
  49. Labels:
  50. Experimental: false
  51. Insecure Registries:
  52. 127.0.0.0/8
  53. Registry Mirrors:
  54. https://4hygggbu.mirror.aliyuncs.com/
  55. Live Restore Enabled: false

docker inspect (返回Docker容器对象的底层信息)Return low-level information on Docker objects

  1. [root@localhost ~]# docker inspect --help
  2. Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
  3. Return low-level information on Docker objects
  4. Options:
  5. -f, --format string Format the output using the given Go template
  6. -s, --size Display total file sizes if the type is container
  7. --type string Return JSON for specified type
  1. 查看docker镜像
  2. [root@localhost ~]# docker images
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. 从外网拉取centos镜像
  5. [root@localhost ~]# docker run -it centos:latest bash
  6. Unable to find image 'centos:latest' locally
  7. latest: Pulling from library/centos
  8. a1d0c7532777: Pull complete
  9. Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
  10. Status: Downloaded newer image for centos:latest
  11. 查看当前运行centos版本然后退出
  12. [root@65150665b70d /]# cat /etc/redhat-release
  13. CentOS Linux release 8.4.2105
  14. [root@65150665b70d /]# exit
  15. exit

docker event state(docker容器事件状态)

docker容器技术基础入门的更多相关文章

  1. 1.docker容器技术基础入门

    内容来自:https://www.cnblogs.com/marility/p/10215062.html https://blog.51cto.com/gouyc/2310785?source=dr ...

  2. 1、docker容器技术基础入门

    Docker和传统虚拟机的区别               参考文章: https://lwn.net/Articles/531114/    操作中的命名空间详解 https://blog.yadu ...

  3. docker容器技术基础之联合文件系统OverlayFS

    我们在上篇介绍了容器技术中资源隔离与限制docker容器技术基础之linux cgroup.namespace 这篇小作文我们要尝试学习容器的另外一个重要技术之联合文件系统之OverlayFS,在介绍 ...

  4. Docker 学习1 容器技术基础入门

    一.虚拟化 1.主机级别虚拟化(两种) a.类型一虚拟化:在硬件上直接安装hyper-ver,然后再安装虚拟机 1).操作系统就两棵树:进程树,文件系统树. 2).一个内核只能有一个root b.类型 ...

  5. Docker容器技术基础

    Docker基础 目录 Docker基础 容器(Container) 传统虚拟化与容器的区别 Linux容器技术 Linux Namespaces CGroups LXC docker基本概念 doc ...

  6. Docker容器技术-基础命令

    一.基础命令 1.运行一个镜像 [root@bogon ~]# docker run debian echo "Hello World" Unable to find image ...

  7. Docker容器技术-基础与架构

    一.什么是容器 容器是对应用程序及其依赖关系的封装. 1.容器的优点 容器与主机的操作系统共享资源,提高了效率,性能损耗低 容器具有可移植性 容器是轻量的,可同时运行数十个容器,模拟分布式系统 不必花 ...

  8. docker容器技术基础之linux cgroup、namespace

    一.开头 接触过docker的同学多多少少听过这样一句话"docker容器通过linux namespace.cgroup特性实现资源的隔离与限制".今天我们来尝试学习一下这两个东 ...

  9. 1.云原生之Docker容器技术基础知识介绍

    转载自:https://www.bilibili.com/read/cv15180540/?from=readlist

随机推荐

  1. Python实现将excel文件转化为html文件

    需要转化的excel文件(nsrxx.xlsx): 源代码: import pandas as pdimport codecspd.set_option('display.width', 1000)p ...

  2. 个人作业——体温上报app(二阶段)

    Code.java package com.example.helloworld; import android.graphics.Bitmap; import android.graphics.Ca ...

  3. ElasticSearch7.3学习(二十八)----聚合实战之电视案例

    一.电视案例 1.1 数据准备 创建索引及映射 建立价格.颜色.品牌.售卖日期 字段 PUT /tvs PUT /tvs/_mapping { "properties": { &q ...

  4. 第06组 Beta冲刺 (4/5)

    目录 1.1 基本情况 1.2 冲刺概况汇报 1.郝雷明 2. 方梓涵 3.曾丽莉 4.鲍凌函 5. 董翔云 6.黄少丹 7.杜筱 8.詹鑫冰 9.曹兰英 10.吴沅静 1.3 冲刺成果展示 1.1 ...

  5. 用STM32玩OLED(显示文字、图片、动图gif等)

    目录 用STM32玩OLED(显示文字.图片.动图gif等) 1. 显示字符串 2. 显示中文 3. 显示图片 4. 显示动图 5. 总结测试 用STM32玩OLED(显示文字.图片.动图gif等) ...

  6. 『忘了再学』Shell基础 — 26、cut列提取命令

    目录 1.cut命令说明 2.cut命令练习 (1)cut命令基本用法 (2)cut命令选取多列 (3)按字符来进行提取 (4)按指定分隔符进行截取数据 3.cut命令分隔符说明 1.cut命令说明 ...

  7. centos6搭建mysql

    目前CentOS6.5及一下版本基本上被官方给放弃更新了,但是考虑到忠实粉丝迟迟不肯放手,所以还留了入口但是非常有限 1.搭建mysql 可参照:https://blog.csdn.net/huang ...

  8. 验证cuda和cudnn是否安装成功(转载)

    本人cuda安装目录: 当然cuda安装目录也可默认:此处为方便安装不同cuda版本,所以单独建了文件夹. 转载自:https://zhuanlan.zhihu.com/p/139668028 安装完 ...

  9. Tarjan算法模板(USACO03FALL受欢迎的牛)

    好文章 #include<bits/stdc++.h> using namespace std; const int N = 10010, M = 50010; int n, m; int ...

  10. npm版本兼容导致的npm ERR! ERESOLVE unable to resolve dependency tree

    当团队项目中,团队成员的npm包管理工具版本不一致时执行npm install报错: npm -v查看版本信息:7.x与6.x之间的兼容问题 解决方案: 一:升级或降级npm版本,保持一致npm in ...