什么是docker!
Docker的思想来自于集装箱,集装箱解决了什么问题?在一艘大船上,可以把货物规整的摆放起来。并且各种各样的货物被集装箱标准化了,集装箱和集装箱之间不会互相影响。那么我就不需要专门运送水果的船和专门运送化学品的船了。只要这些货物在集装箱里封装的好好的,那我就可以用一艘大船把他们都运走.
OCI!!
- 由Linux基金会主导于2015年6月创立
- 旨在围绕容器格式和运行时制定一个开放的工业化标准
- 包含两个规格
- 运行时规范(runtime-spec)
- 镜像规范(image-spec)
OCF!!
runC是一个CLI工具,用于根据OCI规范生成和运行容器
- 容器是作为runC的子进程启动的,并且不需要运行守护进程就可以嵌入到各种其他系统中
- runC是建立在libcontainer之上的,同样的容器技术支持了无数Docker引擎的安装 docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com
docker架构!!
- 一般client和server端都在同一台主机上
- client端使用各种docker命令来管理容器
- server端可以存在多个容器、镜像和本地存储
- registry中可以提供不同系统类型的镜像
工作流程
首先,客户端使用docker命令
- 服务端检测到命令后,先查看本地是否存在所操作的镜像
- 若没有所操作的镜像, 则从hub.docker上拉取镜像到本地- 此时本地已经有了所操作的镜像,则直接执行操作
docker的工作原理
docker是一个C/S架构,用来管理容器的工具,docker的守护进程运行在主机上,荣光socket的方式来通信。
服务端通过socket接收客户端的指令,收到指令后则会执行这个命令,docker通过修改namespaces(命名空间)的系统调用参数来建立容器的边界,以来划分容器。
docker通过CGroups(控制组)来管理(限制)一个容器的资源的占用情况,以免容器之间的资源抢占的情况。
docker镜像与镜像仓库
- tag是应用的版本号
- latest为最新版本
- stable为稳定版
- 镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。
docker对象
当你使用docker时,你能创建、使用和管理镜像、容器、网络、存储卷、插件和其它对象。
镜像
- 镜像是一个用来创建容器的只读模板 - 通常,一个镜像的制作基于另一个系统镜像,并进行一些定制的操作,如安装并启动某个服务 - 可以自己创建镜像,也可以使用别人创建的镜像拉取到本地使用(建议使用官方镜像)
容器
- 容器是一个运行中的镜像 - 可以在API接口中或者命令行上来创建、运行、停止、移动和删除你的容器 - 可以将容器连接到一个或多个网络中,为其附加存储卷甚至根据当前的状态创建一个新镜像
docker安装
[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# vim docker-ce.repo
[root@master yum.repos.d]# cat docker-ce.repo
[docker-ce]
name=docker-ce
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
enabled=1
[root@master yum.repos.d]# dnf clean all
26 文件已删除
[root@master yum.repos.d]# dnf -y install docker-ce
CentOS Stream 8 - AppStream 112 kB/s | 12 MB 01:48
CentOS Stream 8 - BaseOS 99 kB/s | 7.7 MB 01:19
CentOS Stream 8 - Extras 1.3 kB/s | 14 kB 00:10
docker-ce 2.1 kB/s | 14 kB 00:06
Salt repo for RHEL/CentOS 8 PY3 12 kB/s | 242 kB 00:20
docker加速
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,/etc/docker目录是在docker启动之后生成的,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
- docker cn
- 中国科技大学加速器
- 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
[root@master ~]# cd /etc/docker/
[root@master docker]# ls
key.json
[root@master docker]# vim deamon.json
[root@master docker]# cat deamon.json
{
"registry-mirrors": ["https://5ryw5lau.mirror.aliyuncs.com"]
}
[root@master docker]# systemctl daemon-reload
[root@master docker]# systemctl restart docker
docker常用操作
命令 | 功能 |
---|---|
docker search | 用来在docker hub上查找镜像 |
docker pull | 用来在docker上拉取指定镜像 |
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 | 用来查看指定对象的详细信息 |
docker search
[root@master docker]# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 3598 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 40
centos/httpd 34 [OK]
arm64v8/httpd The Apache HTTP Server Project 7
polinux/httpd-php Apache with PHP in Docker (Supervisor, CentO… 5 [OK]
solsson/httpd-openidc mod_auth_openidc on official httpd image, ve… 2 [OK]
hypoport/httpd-cgi httpd-cgi 2 [OK]
centos/httpd-24-centos8 1
clearlinux/httpd httpd HyperText Transfer Protocol (HTTP) ser… 1
inanimate/httpd-ssl A play container with httpd, ssl enabled, an… 1 [OK]
dockerpinata/httpd 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]
jonathanheilmann/httpd-alpine-rewrite httpd:alpine with enabled mod_rewrite 1 [OK]
publici/httpd httpd:latest 1 [OK]
lead4good/httpd-fpm httpd server which connects via fcgi proxy h… 1 [OK]
interlutions/httpd httpd docker image with debian-based config … 0 [OK]
appertly/httpd Customized Apache HTTPD that uses a PHP-FPM … 0 [OK]
amd64/httpd The Apache HTTP Server Project 0
manasip/httpd 0
manageiq/httpd_configmap_generator Httpd Configmap Generator 0 [OK]
trollin/httpd 0
itsziget/httpd24 Extended HTTPD Docker image based on the off… 0 [OK]
ysli/httpd Httpd for DeepWeb 0 [OK]
e2eteam/httpd 0
docker pull
[root@master docker]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
33847f680f63: Pull complete
d74938eee980: Pull complete
963cfdce5a0c: Pull complete
8d5a3cca778c: Pull complete
e06a573b193b: Pull complete
Digest: sha256:61e49dd08a51d6fc421ed257bd8eb461cf2d48269d9ab2b4ff5d4c69826c3c9c
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
docker images
[root@master docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest 73b8cfec1155 5 days ago 138MB
docker create
[root@master docker]# docker create httpd
9e84110fa7fdf26df44d3655469115b5cbf8b77f7247413a83472ede7f44be75
docker ps
[root@master docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@master docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e84110fa7fd httpd "httpd-foreground" 51 seconds ago Created gallant_swartz
docker start
[root@master docker]# docker start 9e84110fa7fd
9e84110fa7fd
[root@master docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e84110fa7fd httpd "httpd-foreground" About a minute ago Up 16 seconds 80/tcp gallant_swartz
docker run
[root@master docker]# docker run httpd
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
[Wed Jul 28 04:06:47.644621 2021] [mpm_event:notice] [pid 1:tid 140665671206016] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 04:06:47.644815 2021] [core:notice] [pid 1:tid 140665671206016] AH00094: Command line: 'httpd -D FOREGROUND'
[root@master ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85e481d357ba httpd "httpd-foreground" 26 seconds ago Up 25 seconds 80/tcp sad_jemison
9e84110fa7fd httpd "httpd-foreground" 3 minutes ago Up About a minute 80/tcp gallant_swartz
# ctrl+c终止,容器也会停止
[root@master ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e84110fa7fd httpd "httpd-foreground" 3 minutes ago Up About a minute 80/tcp gallant_swartz
docker attach
[root@master ~]# docker attach 9e84110fa7fd
^C[Wed Jul 28 04:08:46.947770 2021] [mpm_event:notice] [pid 1:tid 140190087959680] AH00491: caught SIGTERM, shutting down
[root@master ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker logs
[root@master docker]# docker start 9e84110fa7fd
9e84110fa7fd
[root@master docker]# docker logs 9e84110fa7fd
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
[Wed Jul 28 04:05:45.499920 2021] [mpm_event:notice] [pid 1:tid 140190087959680] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 04:05:45.500715 2021] [core:notice] [pid 1:tid 140190087959680] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Jul 28 04:08:46.947770 2021] [mpm_event:notice] [pid 1:tid 140190087959680] AH00491: caught SIGTERM, shutting down
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
[Wed Jul 28 04:10:02.414826 2021] [mpm_event:notice] [pid 1:tid 140048856097920] AH00489: Apache/2.4.48 (Unix) configured -- resuming normal operations
[Wed Jul 28 04:10:02.415084 2021] [core:notice] [pid 1:tid 140048856097920] AH00094: Command line: 'httpd -D FOREGROUND'
docker stop
[root@master docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e84110fa7fd httpd "httpd-foreground" 6 minutes ago Up About a minute 80/tcp gallant_swartz
[root@master docker]# docker stop 9e84110fa7fd
9e84110fa7fd
[root@master docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
docker rm
[root@master ~]# docker rm 9e84110fa7fd
9e84110fa7fd
[root@master ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
85e481d357ba httpd "httpd-foreground" 8 minutes ago Exited (0) 7 minutes ago sad_jemison