帮助命令
docker version #显示Docker版本信息
docker info #显示Docker系统信息,包括镜像和容器数
docker --help #帮助
镜像命令
列出本地主机上的镜像
docker images
[root@localhost ~]# docker images
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 21 months ago 231MB
解释说明
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像创建时间
SIZE 镜像大小
同一个仓库源可以有多个TAG,代表这个仓库源有不同的版本,我们使用REPOSITORY:TAG定义不同的镜像,如果你不定义镜像标签版本,docker将默认latest镜像。
参数可选项
-a: 列出本地所有镜像
-q: 只显示镜像id
-digests: 显示镜像摘要信息
搜索镜像
docker search
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 14218 [OK]
mariadb MariaDB Server is a high performing open sou… 5430 [OK]
percona Percona Server is a fork of the MySQL relati… 614 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 817 [OK]
bitnami/mysql Bitnami MySQL Docker Image 89 [OK]
circleci/mysql MySQL is a widely used, open-source relation… 29
bitnami/mysqld-exporter 5
ubuntu/mysql MySQL open source fast, stable, multi-thread… 49
cimg/mysql 0
rapidfort/mysql RapidFort optimized, hardened image for MySQL 23
rapidfort/mysql8-ib RapidFort optimized, hardened image for MySQ… 9
google/mysql MySQL server for Google Compute Engine 23 [OK]
hashicorp/mysql-portworx-demo 0
rapidfort/mysql-official RapidFort optimized, hardened image for MySQ… 7
newrelic/mysql-plugin New Relic Plugin for monitoring MySQL databa… 1 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 86
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 38
bitnamicharts/mysql 0
mirantis/mysql 0
docksal/mysql MySQL service images for Docksal - https://d… 0
linuxserver/mysql-workbench 50
vitess/mysqlctld vitess/mysqlctld 1 [OK]
eclipse/mysql Mysql 5.7, curl, rsync 0 [OK]
drupalci/mysql-5.5 https://www.drupal.org/project/drupalci 3 [OK]
drupalci/mysql-5.7 https://www.drupal.org/project/drupalci 0
[root@localhost ~]#
docker search 某个镜像名称,对应的dockerhub仓库中的镜像
参数可选项
--filter=start=50 列出收藏数不小于指定值的镜像
下载镜像
docker pull
[root@localhost ~]# docker pull mysql
Using default 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
可指定版本下载
docker pull mysql:5.7
删除镜像
docker rmi
docker rmi -f 镜像id #删除单个
docker rmi -f 镜像名:tag 镜像名:tag #删除多个
docker rmi -f $(docker images -qa) #删除全部
容器命令
说明:有镜像才能创建容器,我们这里使用centos镜像来验证测试,就是虚拟一个centos系统
新建容器并启动
命令参数说明
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
常用参数说明:
--name="name" #给容器指定一个名字
-d #后台方式运行容器,并返回容器的id
-i #以交互模式运行容器,通过-t 一起使用
-t #给容器重新分配一个终端,通常和-i一起使用
-P #随机映射端口(大写P)
-p #指定端口映射(小写p),一般可以有四种写法
验证测试
使用centos进行交互模式启动容器,在容器内执行/bin/bash命令
[root@localhost ~]# docker run -it centos /bin/bash
[root@63476f164a97 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@63476f164a97 /]#
注意:已经切换到容器内部
退出容器
[root@63476f164a97 /]# exit
exit
[root@localhost ~]#
说明:
exit #容器停止并退出
ctrl+P+Q #容器不停止退出
列出所有运行的容器
docker ps [OPTIONS]
命令参数说明
-a #列出当前所有正在运行的容器+历史运行过容器
-l #显示最近创建的容器
-n=? #显示最近N个创建的容器
-q #静默模式,只显示容器编号
启动停止容器
docker start (容器id or 容器名) #启动容器
docker restart (容器id or 容器名) #重启容器
docker stop (容器id or 容器名) #停止容器
docker kill (容器id or 容器名) #强制停止容器
删除容器
docker rm 容器id #删除指定容器
docker rm -f $(docker ps -a -q) #删除所有容器
docker ps -a -g|xargs docker rm #删除所有容器
常用其他命令
后台启动命令
docker run -d 容器名
例如:
docker run -d centos #启动centos,使用后台方式启动
问题
使用docker ps 查看,发现容器已经退出了?
解释
Docker容器后台运行,就必须有一个前台进程,容器运行的命令如果不是哪些一直挂起的命令,就会自动退出。
例如,你运行nginx服务,但是docker 前后没有运行应用,这种情况下,容器启动后,会立即自杀,因此它觉得没有程序了,所以最好的情况是将你的应用使用前台进程方式运行启动。
查看日志
docker logs -f -t --tail 容器id
例如:
我们启动centos,并编写一段脚本来测试,最后查看日志。
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo itcloudblog;sleep 1;done"
f0c58a8294a92d2658e6a9ceb10a324ecf4d17dadec5796c7c4cab5f328e3fe5
[root@localhost ~]#
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f0c58a8294a9 centos "/bin/sh -c 'while t…" 24 seconds ago Up 23 seconds lucid_kepler
[root@localhost ~]#
-t 显示时间戳
-f 打印最新的日志
--tail 数字 显示多少条
[root@localhost ~]# docker logs -tf --tail 10 f0c58a8294a9
2023-06-14T16:07:06.234688994Z itcloudblog
2023-06-14T16:07:07.237773020Z itcloudblog
2023-06-14T16:07:08.241163685Z itcloudblog
2023-06-14T16:07:09.244140364Z itcloudblog
2023-06-14T16:07:10.248124919Z itcloudblog
2023-06-14T16:07:11.251014665Z itcloudblog
2023-06-14T16:07:12.254189058Z itcloudblog
2023-06-14T16:07:13.257743602Z itcloudblog
2023-06-14T16:07:14.261203543Z itcloudblog
2023-06-14T16:07:15.264729435Z itcloudblog
2023-06-14T16:07:16.267789568Z itcloudblog
2023-06-14T16:07:17.271155547Z itcloudblog
2023-06-14T16:07:18.274921302Z itcloudblog
2023-06-14T16:07:19.278338761Z itcloudblog
2023-06-14T16:07:20.281703617Z itcloudblog
2023-06-14T16:07:21.284973629Z itcloudblog
2023-06-14T16:07:22.287707980Z itcloudblog
查看容器运行的进程信息,支持ps命令参数
docker top 容器id
例如:
[root@localhost ~]# docker top f0c58a8294a9
UID PID PPID C STIME TTY TIME CMD
root 16470 16449 0 11:51 ? 00:00:00 /bin/sh -c while true;do echo itcloudblog;sleep 1;done
root 17597 16470 0 12:09 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
[root@localhost ~]#
查看容器/镜像元数据
docker inspect 容器id
例如:
[root@localhost ~]# docker inspect f0c58a8294a9
[
{
"Id": "f0c58a8294a92d2658e6a9ceb10a324ecf4d17dadec5796c7c4cab5f328e3fe5",
"Created": "2023-06-14T15:51:32.893619559Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo itcloudblog;sleep 1;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 16470,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-06-14T15:51:33.461621878Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/f0c58a8294a92d2658e6a9ceb10a324ecf4d17dadec5796c7c4cab5f328e3fe5/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/f0c58a8294a92d2658e6a9ceb10a324ecf4d17dadec5796c7c4cab5f328e3fe5/hostname",
"HostsPath": "/var/lib/docker/containers/f0c58a8294a92d2658e6a9ceb10a324ecf4d17dadec5796c7c4cab5f328e3fe5/hosts",
"LogPath": "/var/lib/docker/containers/f0c58a8294a92d2658e6a9ceb10a324ecf4d17dadec5796c7c4cab5f328e3fe5/f0c58a8294a92d2658e6a9ceb10a324ecf4d17dadec5796c7c4cab5f328e3fe5-json.log",
进入正在运行的容器
命令1
docker exec -it 容器id shell
例如
[root@localhost ~]# docker exec -it f0c58a8294a9 /bin/bash
[root@f0c58a8294a9 /]#
[root@f0c58a8294a9 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 15:51 ? 00:00:00 /bin/sh -c while true;do echo itcloudblog;sleep 1;done
root 1286 0 0 16:12 pts/0 00:00:00 /bin/bash
root 1350 1 0 16:13 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
root 1351 1286 0 16:13 pts/0 00:00:00 ps -ef
[root@f0c58a8294a9 /]#
从容器内拷贝文件到宿主机
docker cp 容器id:容器内路径 目标主机路径
例如
容器内执行创建一个文件测试
[root@localhost ~]# docker exec -it f0c58a8294a9 /bin/bash #进入容器
[root@f0c58a8294a9 /]#
[root@f0c58a8294a9 /]# cd /home/
[root@f0c58a8294a9 home]# touch itcloudblog #创建itcloudlog文件
[root@f0c58a8294a9 home]# ls
itcloudblog
[root@f0c58a8294a9 home]#
[root@f0c58a8294a9 home]# exit #退出容器
exit
[root@localhost ~]# docker cp f0c58a8294a9:/home/itcloudblog /home/ #在宿主机上执行cp将容器/home/itcloudblog 复制到宿主机/home
Successfully copied 1.54kB to /home/
[root@localhost ~]# ls /home/ #查看宿主机/home/目录下有itloudblog文件
itcloudblog
[root@localhost ~]#
常用命令
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 #从容器中拷贝指定文件或者目录到宿主机中
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 # 显示系统相关信息
1inspect 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 # 截取容器停止时的退出状态值