文章目录
dorcker简介
docker是容器技术的一个前端工具,容器是内核的一项技术,docker只是把这一项技术的使用得以简化,使之普及而已。(dorcker是管理容器的工具)
docker中的容器:
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 之上,libcontainer 是支持数百万个 Docker 引擎安装的相同容器技术)
docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com
docker架构
客户端和主机在同一台主机上客户机执行命令,向主机发送请求寻找服务,查找本地有无此服务,有的话就启动,没有去镜像中下载启动。
docker-ee 付费
docker-ce 开源
docker镜像与镜像仓库
为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。
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 conntainer 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安装
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@localhost yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
[root@localhost yum.repos.d]# yum install -y docker-ce --allowerasing
docker加速
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
- docker cn
- 中国科技大学加速器
- 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
[root@localhost ~]# systemctl start docker
[root@localhost ~]# cat > /etc/docker/daemon.json <<EOF
> {
> "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
> }
> EOF
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# ls /etc/docker/
daemon.json key.json
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 20.10.14
API version: 1.41
Go version: go1.16.15
Git commit: a224086
Built: Thu Mar 24 01:47:44 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.14
API version: 1.41 (minimum version 1.12)
Go version: go1.16.15
Git commit: 87a90dc
Built: Thu Mar 24 01:46:10 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.5.11
GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8
runc:
Version: 1.0.3
GitCommit: v1.0.3-0-gf46b6ba
docker-init:
Version: 0.19.0
GitCommit: de40ad0
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 version | 显示 Docker 版本信息 |
//在 Docker 中心搜索httpd映像
[root@localhost ~]# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 3976 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 44
centos/httpd 35 [OK]
hypoport/httpd-cgi httpd-cgi
......
//从镜像仓库中拉取httpd
[root@localhost ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
a2abf6c4d29d: Pull complete
dcc4698797c8: Pull complete
41c22baa66ec: Pull complete
67283bbdd4a0: Pull complete
d982c879c57e: Pull complete
Digest: sha256:0954cc1af252d824860b2c5dc0a10720af2b7a3d3435581ca788dff8480c7b32
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
//列出图像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest dabbfbe0c57b 4 months ago 144MB
//创建一个新的容器但不启动它
[root@localhost ~]# docker create --name web -p 090:090 httpd
1d34c75c6d86c8041df97466146311bac4d61e722a831b71cea04e3dc3941852
//列出所以的容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d34c75c6d86 httpd "httpd-foreground" 16 seconds ago Created web
//启动容器
[root@localhost ~]# docker start web
web
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d34c75c6d86 httpd "httpd-foreground" 34 seconds ago Up 7 seconds 80/tcp, 0.0.0.0:90->90/tcp, :::90->90/tcp web
//关闭容器
[root@localhost ~]# docker stop 1d34c75c6d86
1d34c75c6d86
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//重启容器
[root@localhost ~]# docker restart web
web
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d34c75c6d86 httpd "httpd-foreground" About a minute ago Up 2 seconds 80/tcp, 0.0.0.0:90->90/tcp, :::90->90/tcp web
//杀死运行中的容器
[root@localhost ~]# docker kill web
web
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//查看日志
[root@localhost ~]# docker logs web
[Sun Apr 24 12:47:44.936486 2022] [mpm_event:notice] [pid 1:tid 140003590405440] AH00489: Apache/2.4.52 (Unix) configured -- resuming normal operations
......
//移除容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d34c75c6d86 httpd "httpd-foreground" 5 minutes ago Exited (137) 3 minutes ago web
[root@localhost ~]# docker stop web
web
[root@localhost ~]# docker rm web
web
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//创建一个新的容器并运行一个命令
[root@localhost ~]# docker run -it --name zj busybox /bin/sh
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
5cc84ad355aa: Pull complete
Digest: sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
Status: Downloaded newer image for busybox:latest
/ # exit
[root@localhost ~]#
//连接到正在运行中的容器
[root@localhost ~]# docker start zj
zj
[root@localhost ~]# docker attach zj
/ # exit
[root@localhost ~]#
//在运行的容器中执行命令
[root@localhost ~]# docker start zj
zj
[root@localhost ~]# docker exec -it zj /bin/sh
/ # exit
//显示系统范围的信息
[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.1-docker)
scan: Docker Scan (Docker Inc., v0.17.0)
......