docker version :查看Docker 的版本信息
docker info:查看Docker 的具体详细信息
[root@bigdata01 ~]# docker version
Client: Docker Engine - Community
Version: 19.03.12
API version: 1.40
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:46:54 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.12
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:45:28 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
镜像命令
docker images
docker images:列出本地主机上的镜像
[root@bigdata01 ~]# docker images --help
Usage: docker images [OPTIONS][REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs
#使用镜像centos:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。
docker run -it centos /bin/bash
1
2
创建并运行一个容器,并且进入该容器里面
[root@bigdata01 ~]# docker run -it centos[root@1e4dff5a3236 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@1e4dff5a3236 /]#
1
2
3
4
docker ps
docker ps [OPTIONS]:列出当前所有正在运行的容器
OPTIONS说明(常用):
-a :列出当前所有正在运行的容器+历史上运行过的
-l :显示最近创建的容器。
-n:显示最近n个创建的容器。
-q :静默模式,只显示容器编号。
--no-trunc :不截断输出。
1
2
3
4
5
6
7
退出容器:
exit 容器停止退出
ctrl+P+Q 容器不停止退出
启动容器:docker start 容器ID或者容器名
重启容器:docker restart 容器ID或者容器名
停止容器:docker stop 容器ID或者容器名
强制停止容器:docker kill 容器ID或者容器名
删除已停止的容器:docker rm 容器ID
一次性删除多个容器:
docker rm -f $(docker ps -a -q)
启动守护式容器
启动守护式容器
:
docker run -d
容器名
使用镜像centos:latest以后台模式启动一个容器
docker run -d centos
1
问题
:然后docker ps -a 进行查看, 会发现容器已经退出,很重要的要说明的一点: Docker容器后台运行,就必须有一个前台进程.容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail),就是会自动退出的。
这个是docker的机制问题,比如你的web容器,我们以nginx为例,正常情况下,我们配置启动服务只需要启动响应的service即可。例如
service nginx start
但是,这样做,nginx为后台进程模式运行,就导致docker前台没有运行的应用,这样的容器后台启动后,会立即自杀因为他觉得他没事可做了.所以,
最佳的解决方案是,将你要运行的程序以前台进程的形式运行
查看容器日志
docker logs -f -t --tail
容器ID:查看容器日志
Optons:
-t 是加入时间戳
-f 跟随最新的日志打印
–tail 数字 显示最后多少条
docker top
容器ID :查看容器内运行的进程
[root@bigdata01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e4dff5a3236 centos "/bin/bash" 7 minutes ago Up 7 minutes silly_euclid
[root@bigdata01 ~]# docker top 1e4dff5a3236
UID PID PPID C STIME TTY TIME CMD
root 5614 5598 0 15:34 pts/0 00:00:00 /bin/bash
[root@bigdata01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1e4dff5a3236 centos "/bin/bash" 12 minutes ago Up 12 minutes silly_euclid
[root@bigdata01 ~]# docker exec -it 1e4dff5a3236 /bin/bash[root@1e4dff5a3236 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@1e4dff5a3236 /]# exitexit[root@bigdata01 ~]# docker attach 1e4dff5a3236[root@1e4dff5a3236 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
1
2
3
4
5
6
7
8
9
10
11
上述两个区别:
attach 直接进入容器启动命令的终端,不会启动新的进程
exec 是在容器中打开新的终端,并且可以启动新的进程
从容器内拷贝文件到主机上:docker cp 容器ID:容器内路径 目的主机路径
[root@bigdata01 ~]# docker exec -it 85b4cb80e68e /bin/bash[root@85b4cb80e68e /]# cd /tmp/[root@85b4cb80e68e tmp]# ls -al
total 8
drwxrwxrwt 7 root root 145 Jun 11 02:35 .
drwxr-xr-x 1 root root 18 Aug 9 07:48 ..
drwxrwxrwt 2 root root 6 Jun 11 02:35 .ICE-unix
drwxrwxrwt 2 root root 6 Jun 11 02:35 .Test-unix
drwxrwxrwt 2 root root 6 Jun 11 02:35 .X11-unix
drwxrwxrwt 2 root root 6 Jun 11 02:35 .XIM-unix
drwxrwxrwt 2 root root 6 Jun 11 02:35 .font-unix
-rwx------ 1 root root 1379 Jun 11 02:35 ks-script-585nin8f
-rwx------ 1 root root 671 Jun 11 02:35 ks-script-z6zw_bhq
[root@85b4cb80e68e tmp]# exitexit[root@bigdata01 ~]# docker cp 85b4cb80e68e:/tmp/ks-script-585nin8f /root[root@bigdata01 ~]# cd /root/[root@bigdata01 ~]# ls
app change.sh Could data ks-script-585nin8f lib logs script shell software sourcecode tmp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Docker镜像
UnionFS
UnionFS(联合文件系统):Union文件系统(UnionFS)是一种分层、轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem)。Union 文件系统是 Docker 镜像的基础。镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
# 查看docker进程[root@bigdata01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af1a3dbdb0bd tomcat "catalina.sh run" 36 minutes ago Up 36 minutes 0.0.0.0:8088->8080/tcp cranky_meninsky
1
2
3
4
# 进入docker交互式命令行[
root@bigdata01 ~]# docker exec -it af1a3dbdb0bd /bin/bash
root@af1a3dbdb0bd:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
root@af1a3dbdb0bd:/usr/local/tomcat# cd webapps
1
2
3
4
5
6
7
# 移动docs文件夹
root@af1a3dbdb0bd:/usr/local/tomcat/webapps# mv docs/ docs.bak[root@bigdata01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af1a3dbdb0bd tomcat "catalina.sh run" 41 minutes ago Up 41 minutes 0.0.0.0:8088->8080/tcp cranky_meninsky
1
2
3
4
5
# 提交操作[root@bigdata01 ~]# docker commit -a "nice" -m "mytomcat without docs" af1a3dbdb0bd nice/mytomcat
sha256:f183c09f98b7edcaa041394ae9348a4c9d6dcfcf3221c35769213324e1e7cbbf
1
2
3
# 查看镜像列表[root@bigdata01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nice/mytomcat latest f183c09f98b7 4 seconds ago 652MB
tomcat latest 2ae23eb477aa 3 days ago 647MB
nginx latest 08393e824c32 4 days ago 132MB
centos latest 831691599b88 7 weeks ago 215MB
hello-world latest bf756fb1ae65 7 months ago 13.3kB
[root@bigdata01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
af1a3dbdb0bd tomcat "catalina.sh run" 42 minutes ago Up 42 minutes 0.0.0.0:8088->8080/tcp cranky_meninsky
# 查看镜像[root@bigdata01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nice/mytomcat latest f183c09f98b7 58 seconds ago 652MB
tomcat latest 2ae23eb477aa 3 days ago 647MB
nginx latest 08393e824c32 4 days ago 132MB
centos latest 831691599b88 7 weeks ago 215MB
hello-world latest bf756fb1ae65 7 months ago 13.3kB
1
2
3
4
5
6
7
8
# 运行新的镜像[root@bigdata01 ~]# docker run -it -p 8080:8080 f183c09f98b7
1
2
Docker常用命令汇总
Command
释义
含义
attach
Attach to a running container
当前 shell 下 attach 连接指定运行镜像
build
Build an image from a Dockerfile
通过 Dockerfile 定制镜像
commit
Create a new image from a container changes
提交当前容器为新的镜像
cp
Copy files/folders from the containers filesystem to the host path
从容器中拷贝指定文件或者目录到宿主机中
create
Create a new container
创建一个新的容器,同 run,但不启动容器
diff
Inspect changes on a container’s filesystem
查看 docker 容器变化
events
Get real time events from the server
从 docker 服务获取容器实时事件
exec
Run a command in an existing container
在已存在的容器上运行命令
export
Stream the contents of a container as a tar archive
导出容器的内容流作为一个 tar 归档文件[对应 import ]
history
Show the history of an image
展示一个镜像形成历史
images
List images
列出系统当前镜像
import
Create a new filesystem image from the contents of a tarball
从tar包中的内容创建一个新的文件系统映像[对应export]
info
Display system-wide information
显示系统相关信息
inspect
Return low-level information on a container
查看容器详细信息
kill
Kill a running container
kill 指定 docker 容器
load
Load an image from a tar archive
从一个 tar 包中加载一个镜像[对应 save]
login
Register or Login to the docker registry server
注册或者登陆一个 docker 源服务器
logout
Log out from a Docker registry server
从当前 Docker registry 退出
logs
Fetch the logs of a container
输出当前容器日志信息
port
Lookup the public-facing port which is NAT-ed to PRIVATE_PORT
查看映射端口对应的容器内部源端口
pause
Pause all processes within a container
暂停容器
ps
List containers
列出容器列表
pull
Pull an image or a repository from the docker registry server
从docker镜像源服务器拉取指定镜像或者库镜像
push
Push an image or a repository to the docker registry server
推送指定镜像或者库镜像至docker源服务器
restart
Restart a running container
重启运行的容器
rm
Remove one or more containers
移除一个或者多个容器
rmi
Remove one or more images
移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run
Run a command in a new container
创建一个新的容器并运行一个命令
save
Save an image to a tar archive
保存一个镜像为一个 tar 包[对应 load]
search
Search for an image on the Docker Hub
在 docker hub 中搜索镜像
start
Start a stopped containers
启动容器
stop
Stop a running containers
停止容器
tag
Tag an image into a repository
给源中镜像打标签
top
Lookup the running processes of a container
查看容器中运行的进程信息
unpause
Unpause a paused container
取消暂停容器
version
Show the docker version information
查看 docker 版本号
wait
Block until a container stops, then print its exit code