docker定义
docker中的容器演变
- lxc --> libcontainer --> runC
OCI&OCF
OCI
open container-initiative:旨在围绕容器格式和运行时制定一个开放的工业化标准
规范:
- the Runtime Specification(runtime-spec)
- the Image Specification(image-spec)
OCF
Open Container Format
根据OCI规范生成和运行容器的命令行工具
docker提供了一个专门容纳容器镜像的站点:[https://hub.docker.com]
docker架构
- 创建build:客户端请求,docker主机守护进程接受处理成镜像
- 拉取pull:客户端请求,docker主机守护进程接收,调用网络镜像仓库资源下载至本地镜像库
- 运行run:客户端请求,docker主机守护进程接收,调用本地镜像生成容器
docker镜像与镜像仓库
镜像仓库Registry,存放镜像的地方。镜像命名以应用名称决定,通过标签(tag)来细分不同版本
镜像-->程序(都是静态)
容器-->进程(都是动态)
docker对象
当你使用docker时,你会创建或使用镜像,容器,网络,卷组,插件和其他对象
- 镜像
- 一个带有创建docker容器介绍的只读模板镜像
- 通常,一个镜像在另一个镜像基础建立的,还带有额外的定制
- 你也可以创建一个自己的镜像或者你也可以用别人写好的并且在注册中心出版了的
- 容器
- 一个容器是可运行实例的镜像
- 您能使用dockerAPI或命令行来创建,运行,停止,移动,或删除一个容器
- 您能将容器连接至多个网络,将存储联系起来,甚至在当前状态下创建一个新镜像
docker安装
安装
[root@node0 project]# cd /etc/yum.repos.d/
[root@node0 yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
[root@node0 yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
[root@node0 yum.repos.d]# yum -y install docker-ce
启动守护进程
[root@node0 yum.repos.d]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@node0 yum.repos.d]# ls /etc/docker/
key.json
docker加速
通过修改/etc/docker/daemon.json中源网址来进行加速(文件默认不存在,创建)
- docker cn
- 中国科技大学加速器
- 阿里云加速器(注册阿里云账户加速https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxx.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
常用命令
命令 | 功能 |
---|---|
docker search | Search the Docker Hub for images |
docker pull | Pull an image or a repository from a registry |
docker images | List images |
docker create | Create a new conntainer |
docker start | Start one or more stopped containers |
docker run | Run a command in a new container |
docker attach | Attach to a runninng container |
docker ps | List containers |
docker logs | Fetch the logs of a container |
docker restart | Restart a container |
docker stop | Stop one or more running containers |
docker kill | Kill one or more running containers |
docker rm | Remove onne or more containers |
docker exec | Run a command in a running container |
docker info | Display system-wide information |
docker inspect | Return low-level information on Docker objects |
- search搜索
//最好选用官方
[root@node0 ~]# docker search httpd
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 3371 [OK]
centos/httpd-24-centos7 Platform for running Apache httpd 2.4 or bui… 36
centos/httpd 33 [OK]
- pull拉取(下载)
[root@node0 ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
a076a628af6f: Pull complete
e444656f7792: Pull complete
0ec35e191b09: Pull complete
4aad5d8db1a6: Pull complete
eb1da3ea630f: Pull complete
Digest: sha256:2fab99fb3b1c7ddfa99d7dc55de8dad0a62dbe3e7c605d78ecbdf2c6c49fd636
Status: Downloaded newer image for httpd:latest
docker.io/library/httpd:latest
- images(查看拉取的镜像)
[root@node0 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest 683a7aad17d3 6 weeks ago 138MB
- create(创建新容器)
[root@node0 ~]# docker create httpd
940236c68b6df3516f79f354caeb86d4e06190a951bb731d529baa6b103e520c
[root@node0 ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
940236c68b6d httpd "httpd-foreground" About a minute ago Created cranky_gauss
- start(运行容器)
[root@node0 ~]# docker start 940236c68b6d
940236c68b6d
[root@node0 ~]# docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
940236c68b6d httpd "httpd-foreground" 2 minutes ago Up 5 seconds 80/tcp cranky_gauss
- restart重启容器
[root@node0 ~]# docker restart 940236c68b6d
940236c68b6d
- ps 列出运行的容器
[root@node0 ~]# docker start 940236c68b6d
940236c68b6d
[root@node0 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
940236c68b6d httpd "httpd-foreground" 27 minutes ago Up 1 second 80/tcp cranky_gauss
- stop 停止运行一个或多个的容器
[root@node0 ~]# docker stop 940236c68b6d
940236c68b6d
[root@node0 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
940236c68b6d httpd "httpd-foreground" 26 minutes ago Exited (0) 7 seconds ago cranky_gauss
- kill 杀掉一个或多个容器
[root@node0 ~]# docker kill 940236c68b6d
940236c68b6d
[root@node0 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- rm删除容器
//运行中最好不要删除,rm -f来实现,一般停止容器后删除
[root@node0 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
940236c68b6d httpd "httpd-foreground" 37 minutes ago Exited (137) 3 minutes ago cranky_gauss
[root@node0 ~]# docker rm 940236c68b6d
940236c68b6d
[root@node0 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
//镜像删除 rmi(必须停止所有以该镜像为基础的容器才能删除)
[root@node0 ~]# docker rmi httpd:latest
Untagged: httpd:latest
Untagged: httpd@sha256:2fab99fb3b1c7ddfa99d7dc55de8dad0a62dbe3e7c605d78ecbdf2c6c49fd636
Deleted: sha256:683a7aad17d3baed344799b397c55a9ccbc3ddabf0d077862739585ee76d4cb3
Deleted: sha256:fad4575f3e896bec98eda427846cb6d3cf41db97b121198c2ce799ecbf65d9e5
Deleted: sha256:2559e696bacdd5c33db647e52432638751e7dfaf6d9f1c5e1b40fa751151160b
Deleted: sha256:677fc5a4299ebc25eace2392ffe0c97fd729e720e6db38960a04a0695d25347e
Deleted: sha256:e17bc1f3292461da7df9d2ba962a62e2eed6d79c7bcbf51c672a922a99c5ba59
Deleted: sha256:cb42413394c4059335228c137fe884ff3ab8946a014014309676c25e3ac86864
- logs查看容器日志
[root@node0 ~]# docker logs 1c7a4017f86a
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 04:07:33.245735 2021] [mpm_event:notice] [pid 1:tid 140197445903488] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Fri Feb 26 04:07:33.245950 2021] [core:notice] [pid 1:tid 140197445903488] AH00094: Command line: 'httpd -D FOREGROUND'
- inspcet查看容器对象低级信息
[root@node0 ~]# docker inspect 1c7a4017f86a
[
{
"Id": "1c7a4017f86a15e4971c894dd2ea7ccad1889e0328e197e1035ea84e0dbd8da8",
"Created": "2021-02-26T04:06:45.410712983Z",
"Path": "httpd-foreground",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4379,
- info查看系统信息
[root@node0 ~]# 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)
//验证容器是否成功时,以apache服务举例,可用info命令查看ip,主机访问ip
[root@node0 ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
- run在新容器运行命令
//-d 后台运行
[root@node0 ~]# docker run -d httpd:latest
0c8ca2b1c1a1eb0967542a3aa243bfbdc6c3409fcee964baa2d50fb7223df568
[root@node0 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c8ca2b1c1a1 httpd:latest "httpd-foreground" 38 seconds ago Up 37 seconds 80/tcp beautiful_blackburn
- exec进入容器内部执行命令
docker exec -it containerid COMMAND
[root@node0 ~]# docker exec -it 1c7a4017f86a /bin/bash
root@1c7a4017f86a:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
- attach进入容器(只能看日志,当前终端无法操作)
[root@node0 ~]# docker attach e72676fd0383
172.17.0.1 - - [26/Feb/2021:04:36:10 +0000] "GET / HTTP/1.1" 200 45