docker基础用法
什么是docker
容器(container)是指是与系统其他部分隔离开的一系列进程,从另一个镜像运行,并由该镜像提
供支持进程所需的全部文件。容器提供的镜像包含了应用的所有依赖项,因而在从开发到测试
再到生产的整个过程中,它都具有可移植性和一致性。
OCI&OCF
OCI
开放式集装箱倡议
由Linux基金会主导于2015年6月创立
旨在围绕容器格式和运行时制定一个开放的工业化标准
包含两种规格
运行时规范(Runtime spec)
图像规格(图像规格)
OCF公司
打开容器格式
runC是一个CLI工具,用于根据OCI规范生成和运行容器
容器作为runC的子进程启动,并且可以嵌入到各种其他系统中,而无需运行守护进程
runC是建立在libcontainer之上的,libcontainer是一种支持数百万Docker引擎安装的容器技术
docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com
Docker 架构
Docker 包括三个基本概念:
-
镜像(Image):Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。
-
容器(Container):镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
-
仓库(Repository):仓库可看成一个代码控制中心,用来保存镜像。
Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。
Docker 容器通过 Docker 镜像来创建。
容器与镜像的关系类似于面向对象编程中的对象与类。
Docker |
面向对象 |
---|---|
容器 |
对象 |
镜像 |
类 |
概念 |
说明 |
---|---|
Docker 镜像(Images) |
Docker 镜像是用于创建 Docker 容器的模板,比如 Ubuntu 系统。 |
Docker 容器(Container) |
容器是独立运行的一个或一组应用,是镜像运行时的实体。 |
Docker 客户端(Client) |
Docker 客户端通过命令行或者其他工具使用 Docker SDK (https://docs.docker.com/develop/sdk/) 与 Docker 的守护进程通信。 |
Docker 主机(Host) |
一个物理或者虚拟的机器用于执行 Docker 守护进程和容器。 |
Docker Registry |
Docker 仓库用来保存镜像,可以理解为代码控制中的代码仓库。Docker Hub(https://hub.docker.com) 提供了庞大的镜像集合供使用。一个 Docker Registry 中可以包含多个仓库(Repository);每个仓库可以包含多个标签(Tag);每个标签对应一个镜像。通常,一个仓库会包含同一个软件不同版本的镜像,而标签就常用于对应该软件的各个版本。我们可以通过 <仓库名>:<标签> 的格式来指定具体是这个软件哪个版本的镜像。如果不给出标签,将以 latest 作为默认标签。 |
Docker Machine |
Docker Machine是一个简化Docker安装的命令行工具,通过一个简单的命令行即可在相应的平台上安装Docker,比如VirtualBox、 Digital Ocean、Microsoft Azure。 |
docker对象
使用docker时,您正在创建和使用图像、容器、网络、卷、插件和其他对象。
-
-
图像是一个只读模板,其中包含创建docker容器的说明。
-
通常,一个图像基于另一个图像,并进行了一些额外的定制。
-
您可以创建自己的图像,也可以只使用其他人创建并在注册表中发布的图像。
-
-
容器
-
conntainer是映像的可运行实例。
-
您可以使用docker API或CLI创建、运行、停止、移动或删除容器。
-
安装及使用docker
docker安装
cd /etc/yum.repos.d/
curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
yum -y install docker-ce
systemctl start docker
docker加速
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
-
docker cn
-
中国科技大学加速器
-
阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
[root@RedHat yum.repos.d]# vim /etc/docker.daemon.json
{
"registry-mirrors": ["https://i4lro4s7.mirror.aliyuncs.com"]
}
docker常用操作
命令 |
功能 |
---|---|
docker search |
从Docker Hub查找镜像 |
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 系统信息,包括镜像和容器数 |
docker inspect |
获取容器/镜像的元数据 |
[root@RedHat yum.repos.d]# docker search centos //docker search NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 6423 [OK] ansible/centos7-ansible Ansible on Centos7 132 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC session… 125 [OK] jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 117 [OK] centos/systemd systemd enabled base container. 96 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 86 imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]
[root@RedHat ~]# docker pull centos //docker pull Using default tag: latest latest: Pulling from library/centos 7a0437f04f83: Pull complete Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1 Status: Downloaded newer image for centos:latest docker.io/library/centos:latest
[root@RedHat ~]# docker images //docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 300e315adb2f 2 months ago 209MB
[root@RedHat ~]# docker create centos:latest //docker create 72481c82a449d5c20de0d1ca2d37c2ff28ba42c4fcfddb249cbc88a53f98a386
[root@RedHat ~]# docker ps //docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@RedHat ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 72481c82a449 centos:latest "/bin/bash" 7 minutes ago Created elastic_galileo
[root@RedHat ~]# docker start 72481c82a449 //docker start 72481c82a449
[root@RedHat ~]# docker stop 72481c82a449 //docker stop 72481c82a449
[root@RedHat ~]# docker start 72481c82a449 //docker restart 72481c82a449 [root@RedHat ~]# docker restart 72481c82a449 72481c82a449 [root@RedHat ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 72481c82a449 centos:latest "/bin/bash" 10 minutes ago Exited (0) 8 seconds ago elastic_galileo
[root@RedHat ~]# docker kill 72481c82a449 //docker kill(docker rm 删除一个容器,不能删除正在运行的容器;-f可以删除正在运行的容器;rmi删除镜像) [root@RedHat ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8cd15984bceb centos:latest "/bin/bash" 29 seconds ago Created goofy_chatelet 72481c82a449 centos:latest "/bin/bash" 15 minutes ago Exited (0) 2 minutes ago elastic_galileo [root@RedHat ~]# docker rm -f 8cd15984bceb //docker rm 8cd15984bceb [root@RedHat ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 72481c82a449 centos:latest "/bin/bash" 16 minutes ago Exited (0) 3 minutes ago elastic_galileo
[root@RedHat ~]# docker run httpd:latest //docker run Unable to find image 'httpd:latest' locally latest: Pulling from library/httpd 45b42c59be33: Pull complete 83ac8490fcc3: Pull complete bdb2d204d86d: Pull complete 243acf75a504: Pull complete 8fc1ad93a9b1: Pull complete Digest: sha256:3c252c919ef2445a6a41dde913a56202754821af87c049c4312bf81bdbc6df4b Status: Downloaded newer image for httpd:latest 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 [Fri Feb 26 11:16:05.485323 2021] [mpm_event:notice] [pid 1:tid 140281929786496] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations ...... //去另一个终端查看 [root@RedHat ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ff1bf727c867 httpd:latest "httpd-foreground" 2 seconds ago Up 1 second 80/tcp interesting_banzai 72481c82a449 centos:latest "/bin/bash" 20 minutes ago Exited (0) 7 minutes ago elastic_galileo [root@RedHat ~]# docker run -d httpd:latest //-d 在后台运行 3adbadf32dd3c2421cae2f8d68bc41591d95bb383faeb4e650e9e96f7d740c2e [root@RedHat ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3adbadf32dd3 httpd:latest "httpd-foreground" 14 seconds ago Up 14 seconds 80/tcp zealous_roentgen ff1bf727c867 httpd:latest "httpd-foreground" About a minute ago Exited (0) 22 seconds ago interesting_banzai 72481c82a449 centos:latest "/bin/bash" 22 minutes ago Exited (0) 9 minutes ago elastic_galileo
[root@RedHat ~]# docker logs 3adbadf32dd3 //docker logs 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 [Fri Feb 26 11:17:40.822039 2021] [mpm_event:notice] [pid 1:tid 140524478424192] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations [Fri Feb 26 11:17:40.822229 2021] [core:notice] [pid 1:tid 140524478424192] AH00094: Command line: 'httpd -D FOREGROUND' ......
[root@RedHat ~]# docker inspect 3adbadf32dd3 //docker inspect [ { "Id": "3adbadf32dd3c2421cae2f8d68bc41591d95bb383faeb4e650e9e96f7d740c2e", "Created": "2021-02-26T11:17:40.463669882Z", "Path": "httpd-foreground", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 7048, "ExitCode": 0, "Error": "", "StartedAt": "2021-02-26T11:17:40.803393475Z", "FinishedAt": "0001-01-01T00:00:00Z"
[root@RedHat ~]# docker inspect 3adbadf32dd3 //docker inspect [ { "Id": "3adbadf32dd3c2421cae2f8d68bc41591d95bb383faeb4e650e9e96f7d740c2e", "Created": "2021-02-26T11:17:40.463669882Z", "Path": "httpd-foreground", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 7048, "ExitCode": 0, "Error": "", "StartedAt": "2021-02-26T11:17:40.803393475Z", "FinishedAt": "0001-01-01T00:00:00Z" .......
//docker attach 进入容器内部,但是不能操作,退出会终止容器,不推荐使用.好在attach是可以带上--sig-proxy=false来确保CTRL-D或CTRL-C不会关闭容器。 [root@RedHat ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4e63f0369e08 httpd "httpd-foreground" 2 minutes ago Up 2 minutes 80/tcp stoic_darwin 3adbadf32dd3 httpd:latest "httpd-foreground" 6 minutes ago Up 6 minutes 80/tcp zealous_roentgen ff1bf727c867 httpd:latest "httpd-foreground" 8 minutes ago Exited (0) 6 minutes ago interesting_banzai 72481c82a449 centos:latest "/bin/bash" 29 minutes ago Exited (0) 16 minutes ago elastic_galileo [root@RedHat ~]# docker attach --sig-proxy=false 4e63f0369e08 ^C [root@RedHat ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4e63f0369e08 httpd "httpd-foreground" 3 minutes ago Up 3 minutes 80/tcp stoic_darwin 3adbadf32dd3 httpd:latest "httpd-foreground" 7 minutes ago Up 7 minutes 80/tcp zealous_roentgen ff1bf727c867 httpd:latest "httpd-foreground" 8 minutes ago Exited (0) 7 minutes ago interesting_banzai 72481c82a449 centos:latest "/bin/bash" 29 minutes ago Exited (0) 16 minutes ago elastic_galileo
[root@RedHat ~]# docker exec -it 4e63f0369e08 /bin/bash //docker exec 一般我们采用这种方式进行交互式的进入容器 root@4e63f0369e08:/usr/local/apache2# ls bin build cgi-bin conf error htdocs icons include logs modules root@4e63f0369e08:/usr/local/apache2# cd conf/ root@4e63f0369e08:/usr/local/apache2/conf# ls extra httpd.conf magic mime.types original root@4e63f0369e08:/usr/local/apache2/conf# exit exit [root@RedHat ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4e63f0369e08 httpd "httpd-foreground" 5 minutes ago Up 5 minutes 80/tcp stoic_darwin 3adbadf32dd3 httpd:latest "httpd-foreground" 9 minutes ago Up 9 minutes 80/tcp zealous_roentgen [root@RedHat ~]#
docker events 命令
该命令实时输出 Docker 服务器端的事件,包括容器的创建、启动、关闭等。
语法
docker events [OPTIONS]
常用参数
-
-f:过滤出特定的信息
解释
通过指定容器的 ID 可以过滤其他信息,和容器相关的事件有:attach,commit,copy,create,destroy,detach,die,exec_create,exec_detach,exec_start,export,kill,oom,pause,rename,resize,restart,start,stop,top,unpause,update
docker events -f container=<name or id>
通过指定镜像 ID 可以过滤其他信息,和镜像相关的事件有:delete,import,load,pull,push,save,tag,untag
docker events -f image=<tag or id>
通过指定 volume ID 可以过滤其他信息,和 volume 相关的事件有:create,mount,unmount,destroy
docker events -f volume=<name or id>
通过指定网络 ID 可以过滤其他信息,和网络相关的事件有:create,connect,disconnect,destroy
docker events -f network=<name or id>
其他还有:
docker events -f daemon=<name or id> docker events -f label=<key> or label=<key>=<value> docker events -f event=<event action> docker events -f type=<container or image or volume or network or daemon>
帮助
$ docker events --help Usage: docker events [OPTIONS] Get real time events from the server Options: -f, --filter filter Filter output based on conditions provided --format string Format the output using the given Go template --since string Show all events created since timestamp --until string Stream events until this timestamp