OCI&OCF
Open Container-initiative
-
由Linux基金会主导于2015年6月创立
-
旨在围绕容器格式和运行时制定一个开放的工业化标准
-
包含两种规格
-
运行时规范(Runtime spec)
-
图像规格(image-spec)
OCF
打开容器格式
runC是一个CLI工具,用于根据OCI规范生成和运行容器
-
容器作为runC的子进程启动,并且可以嵌入到各种其他系统中,而无需运行守护进程
-
runC是建立在libcontainer之上的,libcontainer是一种支持数百万Docker引擎安装的容器技术
docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com
-
docker架构
Docker
在运行时分为 Docker 引擎(服务端守护进程)
和 客户端工具
,我们日常使用各种 docker 命令
,其实就是在使用 客户端工具
与 Docker 引擎
进行交互
docker对象
使用docker时,您正在创建和使用图像、容器、网络、卷、插件和其他对象。
-
-
图像是一个只读模板,其中包含创建docker容器的说明。
-
通常,一个图像基于另一个图像,并进行了一些额外的定制。
-
您可以创建自己的图像,也可以只使用其他人创建并在注册表中发布的图像。
-
-
容器
-
conntainer是映像的可运行实例。
-
您可以使用docker API或CLI创建、运行、停止、移动或删除容器。
-
docker安装使用
[root@localhost ~]# cd /etc/yum.repos.d/ [root@localhost yum.repos.d]# curl -o docker-ce.repo 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 clean all [root@localhost ~]# yum -y install docker-ce [root@localhost ~]# systemctl start docker
docker加速
docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。
docker的加速有多种方式:
- docker cn
- 中国科技大学加速器
- 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器
这里使用阿里云加速器,登录阿里云,在控制台中找到容器与镜像服务
[root@localhost ~]# vim /etc/docker/daemon.json { "registry-mirrors": ["https://xxxx.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 | 获取容器/镜像的元数据 |
docker search
[root@localhost ~]# docker search centos 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] .........................................
docker pull
[root@localhost ~]# docker pull centos 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
docker images
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 300e315adb2f 2 months ago 209MB
docker create
[root@localhost ~]# docker create centos:latest 31faa228005d988af1fa19fd7423331d4b08e1ed5dc78e373df83a9e1c4d21b6
docker start
[root@localhost ~]# docker start 072f8e88ba93 072f8e88ba93 [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 072f8e88ba93 centos "/bin/bash" 12 minutes ago Up 5 seconds zen_moser
docker run
[root@localhost ~]# docker run -it -d --name testcentos --hostname test centos /bin/bash bdfcf7ee6f3444d419dfef5b2a80e7990cfdfee07483926cb41d05255ab99ce2
docker ps
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS bdfcf7ee6f34 centos "/bin/bash" 7 seconds ago Up 6 seconds
docker logs
[root@localhost ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx a076a628af6f: Pull complete 0732ab25fa22: Pull complete d7f36f6fe38f: Pull complete f72584a26f32: Pull complete 7125e4df9063: Pull complete Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@localhost ~]# docker create nginx:latest 7a9ac43ccda35320eb45be6ed087cd9bf4e04a95ee56ec20b5e407bd8c36214a [root@localhost ~]# docker start 7a9ac43ccda3 7a9ac43ccda3
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7a9ac43ccda3 nginx:latest "/docker-entrypoint.…" About a minute ago Up 6 seconds 80/tcp hungry_mccarthy
[root@localhost ~]# docker logs 7a9ac43ccda3 /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Configuration complete; ready for start up
docker attach
如果container当前在运行bash,CTRL-C自然是当前行的输入,没有退出;如果container当前正在前台运行进程,如输出nginx的access.log日志,CTRL-C不仅会导致退出容器,而且还stop了。这不是我们想要的,attach的意思按理应该是脱离容器终端,但容器依然运行。好在attach是可以带上--sig-proxy=false来确保CTRL-D或CTRL-C不会关闭容器。
[root@localhost ~]# docker attach --sig-proxy=false hungry_mccarthy ^C
docker exec
[root@localhost ~]# docker exec -it hungry_mccarthy /bin/bash root@7a9ac43ccda3:/# exit exit [root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7a9ac43ccda3 nginx:latest "/docker-entrypoint.…" 24 minutes ago Up 3 minutes 80/tcp hungry_mccarthy
docker inspect
[root@localhost ~]# docker inspect hungry_mccarthy [ { "Id": "7a9ac43ccda35320eb45be6ed087cd9bf4e04a95ee56ec20b5e407bd8c36214a", "Created": "2021-02-25T15:08:48.899142756Z", "Path": "/docker-entrypoint.sh", "Args": [ "nginx", "-g", "daemon off;" ..................................... ................................
docker info
[root@localhost ~]# docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Build with BuildKit (Docker Inc., v0.5.1-docker) Server: Containers: 4 Running: 1 Paused: 0 Stopped: 3 Images: 2 ...................... ....................