Docker常用命令
一、帮助命令
1、docker版本信息
[root@xtank /]# docker version
2、系统信息
[root@xtank /]# docker info
3、帮助信息
[root@xtank /]# docker --help
官方地址:https://docs.docker.com/engine/reference/builder/
二、镜像管理命令
1、docker images
docker images查看当前机器的所有镜像
[root@xtank ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 4 months ago 13.3kB
[root@xtank ~]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all #列出所有的镜像
-q, --quiet #只显示镜像ID
[root@xtank ~]# docker images -aq
bf756fb1ae65
2、docker search
docker search 搜索镜像
[root@xtank ~]# docker search tomcat
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementati… 2735 [OK]
tomee Apache TomEE is an all-Apache Java EE certif… 79 [OK]
[root@xtank ~]# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print search using a Go template
--limit int Max number of search results (default 25)
--no-trunc Don't truncate output
#过滤
[root@xtank ~]# docker search tomcat --filter=STARS=300
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
tomcat Apache Tomcat is an open source implementati… 2735 [OK]
3、docker pull
docker pull 拉取镜像
#默认拉取最新的镜像
[root@xtank ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
afb6ec6fdc1c: Pull complete
0bdc5971ba40: Pull complete
97ae94a2c729: Pull complete
f777521d340e: Pull complete
1393ff7fc871: Pull complete
a499b89994d9: Pull complete
7ebe8eefbafe: Pull complete
597069368ef1: Pull complete
ce39a5501878: Pull complete
7d545bca14bf: Pull complete
211e5bb2ae7b: Pull complete
5914e537c077: Pull complete
Digest: sha256:a31a277d8d39450220c722c1302a345c84206e7fd4cdb619e7face046e89031d
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
docker pull + tag 下载指定版本的镜像,可以在dockerhub上搜索对应的版本:https://hub.docker.com/
[root@xtank ~]# docker pull mysql:5.7.30
5.7.30: Pulling from library/mysql
afb6ec6fdc1c: Already exists #之前下载过mysql:latest最新版,所以这里显示已经存在,可以很好的理解docker联合文件系统
0bdc5971ba40: Already exists
97ae94a2c729: Already exists
f777521d340e: Already exists
1393ff7fc871: Already exists
a499b89994d9: Already exists
7ebe8eefbafe: Already exists
4eec965ae405: Pull complete
a531a782d709: Pull complete
270aeddb45e3: Pull complete
b25569b61008: Pull complete
Digest: sha256:d16d9ef7a4ecb29efcd1ba46d5a82bda3c28bd18c0f1e3b86ba54816211e1ac4
Status: Downloaded newer image for mysql:5.7.30
docker.io/library/mysql:5.7.30
4、docker rmi
docker rmi 删除镜像
[root@xtank ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7.30 a4fdfd462add 2 days ago 448MB
mysql latest 30f937e841c8 2 days ago 541MB
hello-world latest bf756fb1ae65 4 months ago 13.3kB
[root@xtank ~]# docker rmi 30f937e841c8
Untagged: mysql:latest
Untagged: mysql@sha256:a31a277d8d39450220c722c1302a345c84206e7fd4cdb619e7face046e89031d
Deleted: sha256:30f937e841c82981a9a6363f7f6f35ed6b9d5e3f16df50a72207e4a2a389983f
Deleted: sha256:8a5e032615340d8936e0e3707a39ce3da51dc952368176818f879e2f868b535b
Deleted: sha256:c74673a735ca31b9b5162808ab451a8b20876a15e16a7899f2101f3c9b82df60
Deleted: sha256:430365c8e22a9207dca4638c523dc82163bca3ab8a335a71147af41d1551561f
Deleted: sha256:1ede41b1dbec1a5e4385200b62283ffb25c425275530ea9e9cc36b921186cd08
Deleted: sha256:2f6badb9fd9965261d3463591f8af4afddf5f141456de83dc994690ae64b34eb
#-f强制删除镜像文件
[root@xtank ~]# docker rmi -f 30f937e841c8
Untagged: mysql:latest
Untagged: mysql@sha256:a31a277d8d39450220c722c1302a345c84206e7fd4cdb619e7face046e89031d
Deleted: sha256:30f937e841c82981a9a6363f7f6f35ed6b9d5e3f16df50a72207e4a2a389983f
Deleted: sha256:8a5e032615340d8936e0e3707a39ce3da51dc952368176818f879e2f868b535b
Deleted: sha256:c74673a735ca31b9b5162808ab451a8b20876a15e16a7899f2101f3c9b82df60
Deleted: sha256:430365c8e22a9207dca4638c523dc82163bca3ab8a335a71147af41d1551561f
Deleted: sha256:1ede41b1dbec1a5e4385200b62283ffb25c425275530ea9e9cc36b921186cd08
Deleted: sha256:2f6badb9fd9965261d3463591f8af4afddf5f141456de83dc994690ae64b34eb
#显示所有镜像的ID
[root@xtank ~]# docker images -aq
a4fdfd462add
bf756fb1ae65
#删除所有的镜像文件
[root@xtank ~]# docker rmi -f $(docker images -aq)
Untagged: mysql:5.7.30
Untagged: mysql@sha256:d16d9ef7a4ecb29efcd1ba46d5a82bda3c28bd18c0f1e3b86ba54816211e1ac4
Deleted: sha256:a4fdfd462add8e63749aa08ff0044b13d342a042965f1ec6744586cda10dfce9
Deleted: sha256:637f0ff7e591e53fe997c634cf10e63ab810dd1d6cb587ce46a57f753c36bdbf
Deleted: sha256:65ba4d5ac7eb5218cfb4be1e7807584425c19f47606bc1e6d53e050d480d9581
Deleted: sha256:7d0236d50948d993a686c69889c6f016a8da89b8557c5e0eaf6af145ea5877cb
Deleted: sha256:a6219b1270405f43892a7a12895ae1e0ccff307d162cf0025df3ed87f511754b
Deleted: sha256:37803884320881cd931c77dea2ee4d8a7231dfed5a02dc595e6046ffacfa6e1b
Deleted: sha256:cefc9066dc1aa84f6cddead1bb5a8c590e8368d56fb65694e8783d70791bec20
Deleted: sha256:3bfbd2dd4507386ce56fd731b3c97d10bc058e6aa478f901466da69108db50e1
Deleted: sha256:9652363dd4c1146b3f9a519800a9f379adf0b6c4f9aece1ffe965dce5f52a8ca
Deleted: sha256:0ed190016efa0f19bcc5f1d66ffffc7b09716f3c57bcc5de74a4ce217af92278
Deleted: sha256:8399fb13d72603fdc8781075672ee25fedf8384f6721639a70dd3533250ed9e4
Deleted: sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07
Untagged: hello-world:latest
Untagged: hello-world@sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
5、docker tag
docker tag 给镜像文件打标签
[root@xtank ~]# docker tag --help
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
[root@xtank ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 a4fdfd462add 2 days ago 448MB
mysql latest 30f937e841c8 2 days ago 541MB
#将mysql:5.7打标签为xyzsql:1.0
[root@xtank ~]# docker tag mysql:5.7 xyzsql:1.0
[root@xtank ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 a4fdfd462add 2 days ago 448MB
xyzsql 1.0 a4fdfd462add 2 days ago 448MB
mysql latest 30f937e841c8 2 days ago 541MB
6、docker push
docker push 推送镜像到仓库
[root@xtank ~]# docker push --help
Usage: docker push [OPTIONS] NAME[:TAG]
Push an image or a repository to a registry
Options:
--disable-content-trust Skip image signing (default true)
#推送镜像到仓库
[root@xtank ~]# docker push xyzsql:1.0
The push refers to repository [docker.io/library/xyzsql]
9c2b1ab52340: Preparing
272768102e5b: Preparing
1ea529090bed: Preparing
bfd244a57b8b: Preparing
e204fb6af7ef: Preparing
三、容器管理命令
1、docker run
docker run 运行容器
[root@xtank ~]# docker run --help
可选项参数说明:
--name= 指定容器的名称(取别名)
-d 后台运行
-it 交互方式运行
-p 指定端口映射规则,比如: -p 8080:8080
-v 指定需要挂载的数据卷
--network 指定容器运行的网络模式
-env 指定需要传递给容器的环境变量
#运行容器,并进入容器
[root@xtank ~]# docker run --name mycentos -it centos /bin/bash
#在容器中执行ls命令
[root@c833c4916cbf /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#退出容器
[root@c833c4916cbf /]# exit
2、docker ps
docker ps 查看当前所有正在运行的容器
[root@xtank ~]# docker ps --help
可选参数:
-a, --all 查看所有容器
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int 显示最近创建的容器,用数字进行限定
-l, --latest 显示最近创建的容器
--no-trunc Don't truncate output
-q, --quiet Only display numeric IDs
-s, --size 显示文件大小
#查看当前所有正在运行的容器
[root@xtank ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#查看所有容器
[root@xtank ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c833c4916cbf centos "/bin/bash" 6 minutes ago Exited (0) 4 minutes ago mycentos
af808a7b3a44 bf756fb1ae65 "/hello" 44 hours ago Exited (0) 44 hours ago beautiful_williams
52ee3d7cf4d6 bf756fb1ae65 "/hello" 44 hours ago Exited (0) 44 hours ago sharp_stonebraker
#查看所有容器及大小
[root@xtank ~]# docker ps -s -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
c833c4916cbf centos "/bin/bash" 6 minutes ago Exited (0) 4 minutes ago mycentos 8B (virtual 237MB)
af808a7b3a44 bf756fb1ae65 "/hello" 44 hours ago Exited (0) 44 hours ago beautiful_williams 0B (virtual 13.3kB)
52ee3d7cf4d6 bf756fb1ae65 "/hello" 44 hours ago Exited (0) 44 hours ago sharp_stonebraker 0B (virtual 13.3kB)
#显示最近创建的2个容器
[root@xtank ~]# docker ps -a -n=2
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c833c4916cbf centos "/bin/bash" 10 minutes ago Exited (0) 8 minutes ago mycentos
af808a7b3a44 bf756fb1ae65 "/hello" 44 hours ago Exited (0) 44 hours ago beautiful_williams
#显示最近创建的容器
[root@xtank ~]# docker ps -a -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c833c4916cbf centos "/bin/bash" 10 minutes ago Exited (0) 8 minutes ago mycentos
3、退出容器
[root@xtank ~]# docker run --name mycentos1 -it centos /bin/bash
#退出容器,容器停止运行
[root@579ec7ece384 /]# exit
exit
[root@xtank ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
[root@xtank ~]# docker run --name mycentos2 -it centos /bin/bash
#ctrl+P+Q快捷键退出容器,容器在后台继续运行
[root@b46ac3b5200c /]# [root@xtank ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b46ac3b5200c centos "/bin/bash" 7 seconds ago Up 6 seconds mycentos2
4、docker rm
docker rm 删除容器
[root@xtank ~]# docker rm --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force 强制删除运行中的容器
-l, --link Remove the specified link
-v, --volumes Remove anonymous volumes associated with the container
[root@xtank ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b46ac3b5200c centos "/bin/bash" 3 minutes ago Up 3 minutes mycentos2
#删除指定的容器
[root@xtank ~]# docker rm -f b46ac3b5200c
b46ac3b5200c
[root@xtank ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@xtank ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
579ec7ece384 centos "/bin/bash" 6 minutes ago Exited (0) 6 minutes ago mycentos1
c833c4916cbf centos "/bin/bash" 20 minutes ago Exited (0) 18 minutes ago mycentos
af808a7b3a44 bf756fb1ae65 "/hello" 44 hours ago Exited (0) 44 hours ago beautiful_williams
52ee3d7cf4d6 bf756fb1ae65 "/hello" 44 hours ago Exited (0) 44 hours ago sharp_stonebraker
[root@xtank ~]# docker ps -aq
579ec7ece384
c833c4916cbf
af808a7b3a44
52ee3d7cf4d6
#删除全部的容器
[root@xtank ~]# docker rm $(docker ps -aq)
579ec7ece384
c833c4916cbf
af808a7b3a44
52ee3d7cf4d6
[root@xtank ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@xtank ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9d345b6006d4 centos "/bin/bash" 9 seconds ago Up 8 seconds mycentos
#删除全部的容器
[root@xtank ~]# docker ps -aq | xargs docker rm -f
9d345b6006d4
09e85e8f4d6d
441474f4952d
5、启动和停止容器
#停止容器
[root@xtank ~]# docker stop 容器ID
#启动容器
[root@xtank ~]# docker start 容器ID
#重启容器
[root@xtank ~]# docker restart 容器ID
#强制停止容器
[root@xtank ~]# docker kill 容器ID
四、查询信息命令
1、查看日志
[root@xtank ~]# docker logs -f 容器ID
可选参数:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
--tail string Number of lines to show from the end of the logs (default "all")
-t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g. 2013-01-02T13:23:37) or relative (e.g. 42m for 42 minutes)
#后台运行并打印信息
[root@xtank ~]# docker run -d centos /bin/bash -c "while true;do echo hello world;sleep 1;done"
#显示所有的日志信息
[root@xtank ~]# docker logs -f 64b6439bac50
hello world
hello world
hello world
hello world
hello world
hello world
hello world
#以时间戳格式显示所有的日志信息
[root@xtank ~]# docker logs -tf 64b6439bac50
2020-05-23T10:32:56.939044714Z hello world
2020-05-23T10:32:57.940658715Z hello world
2020-05-23T10:32:58.942631800Z hello world
2020-05-23T10:32:59.944626263Z hello world
#显示10行后的所有的日志信息
[root@xtank ~]# docker logs -tf --tail 10 64b6439bac50
2020-05-23T10:33:56.060878998Z hello world
2020-05-23T10:33:57.062994734Z hello world
2020-05-23T10:33:58.064970314Z hello world
2020-05-23T10:33:59.066960831Z hello world
2020-05-23T10:34:00.068940678Z hello world
2020-05-23T10:34:01.071012514Z hello world
2020-05-23T10:34:02.072962320Z hello world
2020-05-23T10:34:03.075194415Z hello world
2020-05-23T10:34:04.077229299Z hello world
2020-05-23T10:34:05.079309871Z hello world
2020-05-23T10:34:06.081370880Z hello world
2020-05-23T10:34:07.083417124Z hello world
2、查看docker服务信息
[root@xtank ~]# docker info
Client:
Debug Mode: false
Server:
Containers: 2
Running: 0
Paused: 0
Stopped: 2
Images: 3
Server Version: 19.03.9
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
3、查看容器的元数据
[root@xtank ~]# docker inspect 容器ID
[
{
"Id": "433e6fdb74f4618e16b3835d8adedcf7d9de9b940a35be2c0417726c944f89c2",
"Created": "2020-05-23T10:11:06.754688085Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 137,
"Error": "",
"StartedAt": "2020-05-23T10:13:45.22698357Z",
"FinishedAt": "2020-05-23T10:13:58.280686931Z"
},
4、查看容器中的进程信息
[root@xtank ~]# docker top 64b6439bac50
UID PID PPID C STIME TTY TIME CMD
root 18287 18271 0 18:32 ? 00:00:00 /bin/bash -c while true;do echo hello world;sleep 1;done
root 18742 18287 0 18:39 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
五、容器交互命令
1、进入容器进行命令
docker exec -it 容器ID /bin/bash,exec的意思就是在容器中运行一个命令,/bin/bash意味着进入容器后会与容器shell进行交互。
#注意:exec是进入容器后新开一个终端
[root@xtank ~]# docker exec -it 64b6439bac50 /bin/bash
[root@64b6439bac50 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
docker attach 容器ID,进入容器正在运行的终端
[root@xtank ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64b6439bac50 centos "/bin/bash -c 'while…" 3 hours ago Up 3 hours hungry_poincare
#进入容器中当前正在运行的终端
[root@xtank ~]# docker attach 64b6439bac50
hello world
hello world
hello world
2、将容器文件拷贝到宿主机
docker cp {容器ID}:{容器文件路径} {宿主机文件路径},将容器文件拷贝到宿主机
[root@xtank ~]# docker cp {容器ID}:{容器文件路径} {宿主机文件路径}
[root@xtank ~]# docker run -it centos
[root@479c6273d7ee /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@479c6273d7ee /]# cd /home/
[root@479c6273d7ee home]# ls
[root@479c6273d7ee home]# touch test.txt
[root@479c6273d7ee home]# echo hello >> test.txt
[root@479c6273d7ee home]# cat test.txt
hello
[root@xtank ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
479c6273d7ee centos "/bin/bash" About a minute ago Exited (0) 5 seconds ago compassionate_moore
#将容器文件拷贝到宿主机
[root@xtank ~]# docker cp 479c6273d7ee:/home/test.txt /home/
[root@xtank ~]# cd /home
[root@xtank home]# ls
test.txt
[root@xtank home]# cat test.txt
hello
六、容器数据卷
1、数据卷挂载
- 直接使用-v命令挂载(常用)
#命令:docker run -it -v 主机目录:容器目录
[root@xtank home]# docker run -it --name=mycentos -v /home/test:/home centos:latest /bin/bash
#查询容器的元数据
[root@xtank test]# docker inspect mycentos
2、具名和匿名挂载
- 查询挂载卷信息
#查询挂载卷信息
[root@xtank test]# docker volume --help
Usage: docker volume COMMAND
Manage volumes
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove all unused local volumes
rm Remove one or more volumes
- 具名挂载
#运行容器,-v 后面只写容器内挂载的路径,宿主机路径不写绝对路径
[root@xtank test]# docker run --name some-nginx -d -p 8080:80 -v test-nginx:/etc/nginx nginx:latest
#查看挂载卷信息
[root@xtank test]# docker volume ls
DRIVER VOLUME NAME
local test-nginx
#查看容器元信息
[root@xtank test]# docker volume ls
DRIVER VOLUME NAME
local test-nginx
- 匿名挂载
#-v 后面只写容器内挂载的路径,宿主机路径不写,然后宿主机挂载的路径会随机在/var/lob/docker/volumes下生成
[root@xtank test]# docker run --name some-nginx1 -d -p 80:80 -v /etc/nginx nginx:latest
#查看挂载卷信息
[root@xtank test]# docker volume ls
DRIVER VOLUME NAME
local 56a3a2999ae2f4d7c541a546c886003ea4e5f79d2dd4833a85de2e2584ac5c6c
#查看容器元信息
[root@xtank test]# docker inspect some-nginx1
3、删除数据卷
#查询数据卷
[root@xtank test]# docker volume ls
DRIVER VOLUME NAME
local 56a3a2999ae2f4d7c541a546c886003ea4e5f79d2dd4833a85de2e2584ac5c6c
local test-nginx
#删除数据卷,在删除之前请停止容器,并删除容器
[root@xtank test]# docker volume rm test-nginx
test-nginx