文章目录
docke命令
查看
查看docker版本
docker -v
docker version
[root@localhost yum.repos.d]# docker -v
Docker version 20.10.11, build dea9396
[root@localhost yum.repos.d]# docker version
Client: Docker Engine - Community #客户端
Version: 20.10.11 #引擎
API version: 1.41 #引擎版本
Go version: go1.16.9 #go语言的版本
Git commit: dea9396 #git工具
Built: Thu Nov 18 00:38:53 2021 #创建时间
OS/Arch: linux/amd64 #操作系统
Context: default #连接方式
Experimental: true
Server: Docker Engine - Community #服务端
Engine:
Version: 20.10.11
API version: 1.41 (minimum version 1.12)
Go version: go1.16.9
Git commit: 847da18
Built: Thu Nov 18 00:37:17 2021
OS/Arch: linux/amd64
Experimental: false
containerd: #容器的版本
Version: 1.4.12
GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
runc: #runcontainerd运行的容器版本
Version: 1.0.2
GitCommit: v1.0.2-0-g52b36a2
docker-init: #初始化
Version: 0.19.0
GitCommit: de40ad0
显示 docker 的系统级信息
比如内核,镜像数,容器数等
docker info
[root@localhost yum.repos.d]# 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.6.3-docker)
scan: Docker Scan (Docker Inc., v0.9.0)
Server: #服务端
Containers: 0 #容器
Running: 0 运行个数
Paused: 0 #基础容器
Stopped: 0 #停止个数
Images: 0 #镜像
Server Version: 20.10.11 #版本
Storage Driver: overlay2 #储存引擎
Backing Filesystem: xfs #文件系统
Supports d_type: true #支持的类型
Native Overlay Diff: true #外接的连接性文件
userxattr: false
Logging Driver: json-file #加载的驱动
Cgroup Driver: cgroupfs
Cgroup Version: 1 #版本
Plugins:
Volume: local #本地卷
Network: bridge host ipvlan macvlan null overlay #插件支持的网络类型
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive #awarm资源管理器
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc #运行时环境
Default Runtime: runc #默认的运行时环境,运行时容器
Init Binary: docker-init #镜像基础对应的数据
containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d #容器版本
runc version: v1.0.2-0-g52b36a2 #运行时容器版本
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-693.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 3.686GiB
Name: localhost.localdomain
ID: 22PV:ED4B:BJLH:FLL6:BXVU:WARJ:6MJQ:STH5:EPIE:H44Z:5KGS:L526
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries: #私有地址网段
127.0.0.0/8
Registry Mirrors:
https://eusc79iy.mirror.aliyuncs.com/ #镜像仓库地址
Live Restore Enabled: false
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
引申一些配置文件的内容
vim /etc/docker/daemon.json ##docker配置文件还可以添加以下的建立配置:
{
"graph": "/data/docker", ##数据目录
"storage-driver": "overlay2", ##存储引擎;版本迭代:LXC——>overlay——>overlay2(overlayfs:文件系统,解决docker镜像分层)
"insecure-registries": [" registry.access.redhat.com", "quary.io"] ##私有仓库位置
"registry-mirrors": ["https://q***"] ##镜像加速
"bip": "172.7.5.1/24", ##docker网络;控制网段的位置;需要创建新的网桥,系统默认的docker0是不变的
"exec-opts": ["native.cgroupdriver-systemd"], ##启动时候的额外参数(驱动)
"live-restore":true ##当docker容器引擎挂掉的时候,使用docker跑起来的容器还能运行(分离)
systemctl daemon- reload
systemctl restart docker
Docker镜像相关操作
- 运行镜像
docker run hello-world ##运行hello-world镜像
run代表以下:
①:pull dockerhub 仓库中项目/库/镜像
②:start hello-world image
命令详解:
The Docker client contacted the Docker daemon. dockerclient客户端连接到了服务端(服务端是以一个守护进程的形式跑在操作系统里面的)restfulapi典型的C/s架构
The Docker daemon pulled the “hello-world” image from the Docker Hub.(amd64)
由docker服务端的守护进程从docker hub上下载了镜像(服务端会先检查本地系统是否有此镜像)
The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
服务端创建了一个新的容器,然后从拉取的这个镜像启动了一个容器,容器执行了脚本/可执行程序让我们可以查看/使用
The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
docker服务端把这些信息流(传递)返回到客户端并展示出来,( 展示在终端上)
docker client 可以是多种形式,比如"docker"命令工具所在的终端
搜索镜像-search
docker search nginx ##搜索镜像nginx
docker search centos: 7 ##搜索镜像centos:7
下载镜像-pull
client端连接服务端,从docker hub上下载镜像
格式:docker pull 镜像名称
docker pull nginx ##下载nginx最新的镜像
[root@localhost yum.repos.d]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
eff15d958d66: Pull complete
1e5351450a59: Pull complete
2df63e6ce2be: Pull complete
9171c7ae368c: Pull complete
020f975acd28: Pull complete
266f639b35ad: Pull complete
Digest: sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
查看镜像
docker images ##查看镜像列表
docker images -q ##查询镜像过滤ID
q:代表过滤;只过滤容器ID
查看当前docker下的镜像详细信息
格式:docker inspect 镜像ID
docker inspect ea335eea17ab
添加镜像标签tag
docker tag hello-world:latest hello-world:lamp
发现没hello-world镜像,需要先下载后可以添加
[root@localhost yum.repos.d]# docker tag hello-world:latest hello-world:lamp
Error response from daemon: No such image: hello-world:latest #报错没有此镜像
[root@localhost yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 7 days ago 141MB
[root@localhost yum.repos.d]# docker run hello-world #下载并执行hello-world镜像
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[root@localhost yum.repos.d]# docker tag hello-world:latest hello-world:lamp #添加标签lamp
删除镜像
- 要先删除容器才能删除镜像
docker rm id
docker rmi 镜像名称/镜像标签
rmi=rm image
[root@localhost ~]# docker images #查看所有镜像,有两个hello-world
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 7 days ago 141MB
hello-world lamp feb5d9fea6a5 2 months ago 13.3kB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
[root@localhost ~]# docker ps -a #查看所有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1518b3ffb111 hello-world "/hello" 14 hours ago Exited (0) 14 hours ago frosty_mestorf
dcc6ac7f4eba hello-world "/hello" 17 hours ago Exited (0) 17 hours ago elastic_mendeleev
[root@localhost ~]# docker rmi hello-world:lamp #删除lamp类型的标签
Untagged: hello-world:lamp
[root@localhost ~]# docker images #查看镜像,发现只剩最新的hello-world了
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 7 days ago 141MB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
[root@localhost ~]# docker ps -a #查看所有容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1518b3ffb111 hello-world "/hello" 14 hours ago Exited (0) 14 hours ago frosty_mestorf
dcc6ac7f4eba hello-world "/hello" 17 hours ago Exited (0) 17 hours ago elastic_mendeleev
[root@localhost ~]# docker rmi hello-world:lamp #删除lamp标签
Untagged: hello-world:lamp
[root@localhost ~]# docker images #查看所有镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ea335eea17ab 7 days ago 141MB
hello-world latest feb5d9fea6a5 2 months ago 13.3kB
[root@localhost ~]# docker rm 1518b3ffb111 #根据id删除容器
1518b3ffb111
[root@localhost ~]# docker ps -a #再次查看所有容器就显示已被删除
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dcc6ac7f4eba hello-world "/hello" 17 hours ago Exited (0) 17 hours ago elastic_mendeleev
镜像导出
docker save -o 文件名 镜像名
#将指定镜像保存成tar归档文件
-o :输出到的文件
示例:
docker save -o hello-world hello-world
镜像导入
docker load < hello-world
如何使用
scp hello-world root@192.168.3.13+:/opt ##传到其他安卓docker的内容
docker load < hello-world ##再进行镜像导入;
使用场景,有的生产环境,企业不直接使用docker 私有仓库,而是存放在一个ftp服务器中,按需上传下载
容器相关操作
查询容器
docker ps -a ##显示所有的容器,包括未运行的
a:all;全部
docker ps -aq ##查询容器的id
q:代表过滤;只过滤容器ID
创建容器
docker create -it centos:7 /bin/bash //创建一个新的容器但不启动它
#调用 /bin/bash 交互
#选项:
-i : 让容器的标准输入保持打开
-t : 分配一个伪终端
-d : 后台守护进程的方式运行
#去查询容器会发现状态为Created
[root@c7-5 opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
134a784c54a3 centos:7 "/bin/bash" 6 seconds ago Created hopeful_lichterman
删除容器
docker rm -f `docker ps -aq` ##强制批量删除容器;不建议使用,若需要删除删选出id进行删除
启动运行容器
- 使容器开启并持续性运行
①:创建容器
docker create -it nginx:latest /bin/bash
-i:让容器的标准输入保持打开
-t:分配一个伪终端
-d:后台守护进程的方式运行
②:启动容器
docker start 容器id
[root@localhost ~]# docker create -it nginx:latest /bin/bash #创建最新的nginx容器
bc8f77aabb8e7499c5ea710b9bd5aecf43973c99604357562bf671c125dd980c
[root@localhost ~]# docker ps -a #查看下
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc8f77aabb8e nginx:latest "/docker-entrypoint.…" 14 seconds ago Created relaxed_kalam
dcc6ac7f4eba hello-world "/hello" 23 hours ago Exited (0) 23 hours ago elastic_mendeleev
[root@localhost ~]# docker start bc8f77aabb8e #开启容器
bc8f77aabb8e
[root@localhost ~]# docker ps -a #再查看发现已运行
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bc8f77aabb8e nginx:latest "/docker-entrypoint.…" 5 minutes ago Up 17 seconds 80/tcp relaxed_kalam
dcc6ac7f4eba hello-world "/hello" 23 hours ago Exited (0) 4 minutes ago elastic_mendeleev
启动一次性运行容器,持续后台运行
持续性运行浪费资源,那么一次性执行如下操作
docker run centos:7 /usr/bin/bash -c ls /
#-c:传递命令参数
#接着去查询状态会发现是退出状态
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6a036e466e1a centos:7 "/usr/bin/bash" 3 seconds ago Exited (0) 1 second ago objective_leakey
#退出状态是因为没有设置守护进程,如果想要他持续运行,需要加上 -d,并给他一个持续性任务
docker run -d centos:7 /usr/bin/bash -c "while true;do echo hello; done" ##后台运行centos:7的容器
while true;do echo hello; done:代表给予一个死循环
停止容器
docker stop 容器ID ##停止容器
docker stop bc8f77aabb8e ##停止容器
状态码137:表示主动退出停止容器
进入/退出容器
1. 使用run
docker run -it nginx:latest /bin/bash
#若想退出则直接exit,但是容器会自动关闭
比如进入容器没有systemctl
命令解决:添加–privileged=true;指定此容器是否为特权容器,使用此参数,则不能用attach
示例:
docker run -itd --name test3 --privileged=true centos /sbin/init
/sbin/init内核启动时主动呼叫的第一个进程
docker ps -a ##查询容器是否开启
docker exec -it 容器ID /bin/bash #进入容器
docker exec -it 6be59840d78b /bin/bash
验证:
yum -y install httpd ##随便安装一个服务
systemctl status httpd ##使用systemctl命令查询服务
使用exec(容器必须为开启状态)
docker exec -it 容器ID /bin/bash
#退出也是exit,但是不会停止
exec利和shell是两种运行模式
ps
①docker run -it会创建前台进程,但是会在输入exit后终止进程。
②docker attach会通过连接 stdin ,连接到容器内输入输出流,会在输入 exit 后终止容器进程.
③docker exec -it会连接到容器,可以像SSH一样进入容器内部,进行操作,可以通过 exit 退出容器,不影响容器运行。
容器导出
docker export 容器ID > 文件名
例:
[root@c7-5 opt]# docker export 134a784c54a3 > centos_7
[root@c7-5 opt]# ls
centos_7
容器导入(生成镜像)
docker import 导出的文件名(容器) 指定镜像名称
#例:
docker import centos_7 centos:7_v1
删除容器
docker rm 容器ID
例:docker rm a710bbff377d
强制删除容器(正在运行的):
docker rm -f a710bbff377d
批量删除容器
docker rm `docker ps -aq`
#删除所有容器
查询所有当前容器资源消耗信息
docker stats