帮助命令
docker version
:查看Docker版本信息
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 19.03.6
API version: 1.40
Go version: go1.12.16
Git commit: 369ce74a3c
Built: Thu Feb 13 01:29:29 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.6
API version: 1.40 (minimum version 1.12)
Go version: go1.12.16
Git commit: 369ce74a3c
Built: Thu Feb 13 01:28:07 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683
docker info
:查看Docker的详细信息,包括容器的启动、暂停、停止个数,镜像个数等
docker --help
:Docker帮助命令,相当于Linux系统的 man 命令
熟读它,不求人,嘻嘻
[root@localhost ~]# docker --help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides DOCKER_HOST env var and default
context set with "docker context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Management Commands:
builder Manage builds
config Manage Docker configs
container Manage containers
context Manage contexts
engine Manage the docker engine
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
stack Manage Docker stacks
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Commands:
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
Run 'docker COMMAND --help' for more information on a command.
镜像命令
docker images [OPTIONS]
:列出本地主机的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat latest 4e7840b49fad 12 days ago 529MB
hello-world latest fce289e99eb9 14 months ago 1.84kB
REPOSITORY:镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小
同一仓库源可以有多个TAG,代表这个仓库源的不同个版本,例如:tomcat:7.2、tomcat:9.0
选项说明:
-a
:列出本地所有的镜像(含中间映像层)
-q
:只显示镜像ID
--digests
:显示镜像的摘要信息
--no-trunc
:显示完整的镜像信息
docker search [OPTIONS] 镜像名字
:在Docker Hub网站里搜索镜像
[root@localhost ~]# docker search tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementati… 2659 [OK]
tomee Apache TomEE is an all-Apache Java EE certif… 75 [OK]
dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 53 [OK]
bitnami/tomcat Bitnami Tomcat Docker Image 31 [OK]
......
选项说明:
--no-trunc
: 显示完整的镜像描述
-s
:列出收藏数不小于指定值的镜像
--automated
:只列出 automated build类型的镜像
docker pull xxx镜像名字[:TAG]
:下载镜像
[root@localhost ~]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
50e431f79093: Pull complete
dd8c6d374ea5: Pull complete
c85513200d84: Pull complete
55769680e827: Pull complete
e27ce2095ec2: Pull complete
5943eea6cb7c: Pull complete
3ed8ceae72a6: Pull complete
91d1e510d72b: Pull complete
98ce65c663bc: Pull complete
27d4ac9d012a: Pull complete
Digest: sha256:2c90303e910d7d5323935b6dc4f8ba59cc1ec99cf1b71fd6ca5158835cffdc9c
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
不加TAG版本号,默认下载最新版本
docker rmi xxx镜像名字或ID
:删除镜像(rmi = remove image)
删除单个镜像:docker rmi -f 镜像ID
删除多个镜像:docker rmi -f 镜像ID1:TAG 镜像ID2:TAG
删除全部镜像:docker rmi -f $(docker images -qa)
容器命令
下载一个CentOS镜像用于演示docker pull centos
新建并启动容器docker run [OPTIONS] IMAGE [COMMAND]
# 使用镜像centos:latest以交互模式启动一个容器,在容器内执行/bin/bash命令
[root@localhost ~]# docker run -it centos /bin/bash
[root@615f8da7ff70 /]#
选项说明:
--name="容器新名字"
: 为容器指定一个名称
-d
: 后台运行容器,并返回容器ID,也即启动守护式容器
-i
:以交互模式运行容器
-t
:为容器重新分配一个伪输入终端,通常-it
同时使用
-P
: 随机端口映射
-p
: 指定端口映射,有以下四种格式:
ip:hostPort:containerPort
ip::containerPort
hostPort:containerPort
containerPort
列出当前正在运行的容器 docker ps [OPTIONS]
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
615f8da7ff70 centos "/bin/bash" 2 minutes ago Exited (130) 6 seconds ago frosty_dubinsky
选项说明:
-a
:列出当前所有正在运行的容器+历史上运行过的
-l
:显示最近创建的容器
-n
:显示最近n个创建的容器
-q
:静默模式,只显示容器编号
--no-trunc
:不截断输出
不加选项即正在运行的容器
退出容器(2种方式)
exit
:容器停止并退出ctrl+p+q
:容器不停止,退出
启动容器 docker start 容器ID或容器名
重启容器 docker restart 容器ID或容器名
停止容器 docker stop 容器ID或容器名
强制停止容器 docker kill 容器ID或容器名
删除已停止的所有容器
docker rm -f $(docker ps -a -q)
, $
中的参数是容器ID
docker ps -a -q | xargs docker rm
以下命令重要
启动守护式容器 docker run -d 容器名
[root@localhost ~]# docker run -d centos
8f22d2af58ad76ce3e75a199f08acb64603673c6d40db41d9c641457483af6c0
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8f22d2af58ad centos "/bin/bash" 14 seconds ago Exited (0) 12 seconds ago compassionate_einstein
docker ps -a
进行查看, 会发现容器已经退出
Docker容器后台运行,就必须有一个前台进程
容器运行的命令如果不是那些一直挂起的命令(比如运行top
,tail
),就是会自动退出的
docker run -d centos /bin/sh -c "while true;do echo hello~ 123;sleep 2;done"
在后台启动centOS,每隔2秒打印一次,这样容器在后台一直有执行任务,不会被Docker机制杀掉
查看容器日志 docker logs -f --tail 行数 容器ID
[root@localhost ~]# docker logs -f -t --tail 10 4aa77bd01617
2020-03-11T06:02:11.294480761Z hello~ 123
2020-03-11T06:02:13.297408285Z hello~ 123
2020-03-11T06:02:15.301214919Z hello~ 123
2020-03-11T06:02:17.304140864Z hello~ 123
2020-03-11T06:02:19.307794909Z hello~ 123
2020-03-11T06:02:21.311661456Z hello~ 123
2020-03-11T06:02:23.314519185Z hello~ 123
2020-03-11T06:02:25.317276007Z hello~ 123
2020-03-11T06:02:27.320187635Z hello~ 123
2020-03-11T06:02:29.324677849Z hello~ 123
-t
:加入时间戳
-f
:跟随最新的日志打印
--tail
:数字显示最后多少条
查看容器内运行的进程 docker top 容器ID
[root@localhost ~]# docker top 4aa77bd01617
UID PID PPID C STIME TTY TIME CMD
root 3657 3641 0 14:00 ? 00:00:00 /bin/sh -c while true;do echo hello~ 123;sleep 2;done
root 3860 3657 0 14:04 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 2
查看容器内部细节 docker inspect 容器ID
[root@localhost ~]# docker inspect 4aa77bd01617
[
{
"Id": "4aa77bd01617d65f72a7f6669cd74cb68296f826cee405b4ae8d35d044f38a51",
"Created": "2020-03-11T06:00:12.212339436Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo hello~ 123;sleep 2;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3657,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-03-11T06:00:13.043281033Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
......
}
Docker镜像是一层套一层的,整个容器里面是以json串的形式展现的
进入正在运行的容器并以命令交互
[root@localhost ~]# docker exec 6fe33c410a8e ls -l /tmp
total 8
-rwx------. 1 root root 671 Jan 13 21:49 ks-script-_srt3u3c
-rwx------. 1 root root 1379 Jan 13 21:49 ks-script-gpqu_kuo
[root@localhost ~]# docker attach 6fe33c410a8e
[root@6fe33c410a8e /]#
区别:
attach
:直接进入容器启动命令的终端,不会启动新的进程
exec
:在容器中打开新的终端,并且可以启动新的进程(可以进入容器里面执行命令,也可以在外面执行命令返回到宿主机上
)
从容器复制文件到主机 docker cp 容器ID:容器内文件路径 目的主机路径
[root@localhost ~]# docker cp 6fe33c410a8e:/tmp .
[root@localhost ~]# ll
总用量 8
-rw-------. 1 root root 1333 2月 19 03:36 anaconda-ks.cfg
drwxrwxrwt. 7 root root 4096 1月 14 05:49 tmp
[root@localhost ~]# ls tmp
ks-script-gpqu_kuo ks-script-_srt3u3c