1.帮助命令
docker version 显示版本信息
docker info 显示docker的系统信息,包括镜像和容器个数
docker 命令 --help 帮助命令
2.镜像命令
2.1 docker images 查看本地的主机上的所有镜像
[root@bogon ~]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Aliases:
docker image ls, docker image list, docker 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 Format output using a custom template:
'table': Print output in table format with column headers
(default)
'table TEMPLATE': Print output in table format using the given Go
template
'json': Print in JSON format
'TEMPLATE': Print output using the given Go template.
Refer to https://docs.docker.com/go/formatting/ for more information
about formatting output with templates
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
[root@bogon ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 22 months ago 141MB
redis latest 7614ae9453d1 22 months ago 113MB
rabbitmq 3-management 6c3c2a225947 22 months ago 253MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
kibana 7.12.1 cf1c9961eeb6 2 years ago 1.06GB
elasticsearch 7.12.1 41dc8ea0f139 2 years ago 851MB
mysql 5.7.25 98455b9624a9 4 years ago 372MB
REPOSITORY 镜像的仓库源TAG 镜像的标签
IMAGE ID 镜像的id
CREATED ID 镜像的创建时间
SIZE 镜像的大小
可选项
-a, --all 列出所有镜像
-q, --quit 只显示镜像的id
[root@bogon ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 22 months ago 141MB
redis latest 7614ae9453d1 22 months ago 113MB
[root@bogon ~]# docker images -q
605c77e624dd
7614ae9453d1
2.2 docker search 搜索镜像
[root@bogon ~]# docker search --help
Usage: docker search [OPTIONS] TERM
Search 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
--no-trunc Don't truncate output
[root@bogon ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL
AUTOMATED
mysql MySQL is a widely used, open-source relation… 14535 [OK]
mariadb MariaDB Server is a high performing open sou… 5552 [OK]
可选项
--filter =STARS==3000
[root@bogon ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14535 [OK]
mariadb MariaDB Server is a high performing open sou… 5552 [OK]
2.3 docker pull 下载镜像
[root@bogon ~]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Download an image from a registry
Aliases:
docker image pull, docker pullOptions:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform capable
-q, --quiet Suppress verbose output
[root@bogon ~]# docker pull mysql
Using default tag: latest 如果不写tag 默认latest
latest: Pulling from library/mysql 分层下载 联合文件系统
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest 真实地址
docker.io/library/mysql:latest
指定版本下载
[root@bogon ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Already exists
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
2.4 dokcer rmi 删除镜像
docker rmi -f 镜像id 删除指定镜像
docker rmi -f 镜像id 镜像id 镜像id 删除多个镜像
docker rmi -f $(docker images -aq) 删除全部镜像
[root@bogon ~]# docker rmi -f c20987f18b13
Untagged: mysql:5.7
Untagged: mysql@sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Deleted: sha256:c20987f18b130f9d144c9828df630417e2a9523148930dc3963e9d0dab302a76
Deleted: sha256:6567396b065ee734fb2dbb80c8923324a778426dfd01969f091f1ab2d52c7989
Deleted: sha256:0910f12649d514b471f1583a16f672ab67e3d29d9833a15dc2df50dd5536e40f
Deleted: sha256:6682af2fb40555c448b84711c7302d0f86fc716bbe9c7dc7dbd739ef9d757150
Deleted: sha256:5c062c3ac20f576d24454e74781511a5f96739f289edaadf2de934d06e910b92
3.容器命令
有了镜像才可以创建容器(下载centos)
[root@bogon ~]# docker pull centos
3.1 docker run 启动容器
docker run [可选参数] image
参数说明
--name="Name" 容器名字
-d 后台方式运行
-it 交互方式,进入容器查看内容
-P 指定容器端口 -p 8080:8080
-P ip:主机端口:容器端口
-P 主机端口:容器端口
-P 容器端口
容器端口
-p 随机指定端口
启动并进入容器
[root@bogon ~]# docker run -it centos /bin/bash
[root@d20638f688cc /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr[root@d20638f688cc /]# exit 从容器退出
exit
3.2 docker ps 查看运行的容器
-a 列出正在运行的容器,历史运行过的容器
-n=? 显示最近创建的容器
-q 只显示容器的编号
[root@bogon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@bogon ~]# docker ps -a -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d20638f688cc centos "/bin/bash" 6 minutes ago Exited (0) 4 minutes ago condescending_bohr
3.3 退出容器
exit 容器停止并退出
Ctrl+P+Q 容器不停止退出
3.4 docker rm 删除容器
docker rm 容器id (不能删除正在运行的容器)
docker rm -f $(docker ps -aq) 删除所有的容器(强制删除在运行的容器)
docker ps -a -q|xargs docker rm 删除所有的容器
3.5 启动和停止容器
docker start 容器id 启动容器
docker restart 容器id 重启容器
docker stop 容器id 停止在运行的容器
docker kill 容器id 强制停止
[root@bogon ~]# docker start fc84b8115818
fc84b8115818
[root@bogon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc84b8115818 centos "/bin/bash" 49 seconds ago Up 15 seconds nifty_lamarr
[root@bogon ~]# docker stop fc84b8115818
fc84b8115818
[root@bogon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4. 其他常用命令
后台启动容器
[root@bogon ~]# docker run -d centos
20ed80fc5ead0830d44ecac0a6e7beda42a8f0648443d42192bb8692a374de41ps时发现centos停止(docker容器运行时,必须要有前台进程,docker发现没有应用就会自动停止)
查看日志
docker logs -tf --tail number 容器
查看容器中的进程信息
docker top 容器id
查看容器信息
docker inspect 容器id
进入正在运行的容器
docker exec -it 容器id (进入容器后开启新的终端)
docker attach 容器id(进入容器正在执行的终端,不会启动新的进程)
从容器拷贝文件
docker cp 容器id:容器内路径 目的地主机路径