Docker的常用命令

182 阅读5分钟

帮助命令

docker version     显示docker的版本信息
docker info        显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help  帮助命令

镜像命令

docker images 查看所有本地镜像

REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   19 months ago   13.3kB

# 解释
REPOSITORY  镜像的仓库源
TAG         镜像的标签
IMAGE ID    镜像的ID
CREATED     镜像的创建时间
SIZE        镜像的大小

#可选项
-a 列出所有的镜像
-q 只显示镜像的ID

docker search 搜索镜像

NAME                            DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   14091     [OK] 


# 可选项

--filter=stars=3000  搜索出来的镜像就是stars大于3000

docker pull 下载镜像


[root@MiWiFi-R4A-srv ~]# docker pull mysql
Using default tag: latest  # 如果不写tag,默认就是latest
latest: Pulling from library/mysql
328ba678bf27: Pull complete   #分层下载,docker image的核心 联合文件系统
f3f5ff008d73: Pull complete
dd7054d6d0c7: Pull complete
70b5d4e8750e: Pull complete
cdc4a7b43bdd: Pull complete
a0608f8959e0: Pull complete
5823e721608f: Pull complete
a564ada930a9: Pull complete
539565d00e89: Pull complete
a11a06843fd5: Pull complete
92f6d4aa041d: Pull complete
Digest: sha256:a43f6e7e7f3a5e5b90f857fbed4e3103ece771b19f0f75880f767cf66bbb6577  #签名
Status: Downloaded newer image for mysql:latest  # 真实地址
docker.io/library/mysql:latest

docker rmi 删除镜像

docker rmi -f 镜像ID  #填入ID通过ID删除
docker rmi -f 镜像ID 镜像ID 镜像ID   #删除多个镜像

docker rmi -f $(docker images -aq)  #查出所有镜像ID然后循环删除

commit 提交镜像

如果想要保存当前容器状态,可以通过commit提交获得一个镜像

docker commit -a="tc" -m="test" 05d3d4aa0a4b nginxtest:1.0

#可选项
-a 作者
-m 说明

docker commit -a="作者" -m="说明" 目标镜像id 镜像名称:版本号 

容器命令

新建容器并启动

docker run [可选参数] image

# 参数说明
--name="Name" 容器名字,用来区分容器
-d            后台方式运行
-it           使用交互方式运行,进入容器查看内容
-P            指定容器的端口 -p  8080:8080 映射  
    -P 主机端口:容器端口
    -P ip:主机端口:容器端口
    -P 容器端口
-p            随机指定端口


# 测试,启动并进入容器
docker run -it centos /bin/bash

image.png

列出运行的容器

docker ps 
显示运行中的容器
# 可选项
-a #列出当前运行的容器+历史运行过的容器
-n=? #显示最近创建的容器
-q  # 只显示容器的编号

image.png

退出容器

exit  #停止并退出容器
Ctrl+p+q 快捷键  #退出容器不停止

删除容器

docker rm 容器id  #删除指定的容器,不能删除正在运行的容器,强制删除 rm -f
docker rm -f $(docker ps -aq)   #删除所有的容器
docker ps -a -q|xargs docker rm #删除所有的容器

启动和停止容器

docker start 容器id   #启动容器
docker restart 容器id #重启容器
docker stop 容器id    #停止容器
docker kill 容器id    #强制停止容器

常用其他命令

后台启动容器

docker run -d 镜像名
容器使用后台运行,必须要有一个前台进程,否则会立刻停止

查看日志

docker logs 

# 运行shell脚本
docker run -d centos /bin/sh -c "while true;do echo kuangshen;sleep 1'done"

# 可选项
-t 时间戳
-f 跟踪日志
--tail 行数

docker logs -tf --tail 10 f23ef3362974

查看进程中的命令 ps

docker top 容器id

docker top f23ef3362974

查看镜像的元数据

docker inspect 容器id

docker inspect f23ef3362974

进入当前正在运行的容器

docker exec it 容器id /bin/bash
docker exec it 容器id bash

# 方式二
docker attach 容器id

# docker exec   #进入容器后开启一个新的终端,可以在里面操作(常用)
# docker attach #进入容器正在执行的终端,不会启动新的进程

从容器内拷贝文件到主机上

docker cp 容器id:文件路径 目标路径
docker cp 1ecb3e95cf83:/test.java /

小结

attach    Attach to a running container                 # 当前 she11 下 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 tarbal1     # 从tar包中的内容创建一个新的文件系统映像[对应export]
info      Display system-wide information               # 显示系统相关信息
inspect   Return Tow-leve7 information on a container   # 查看容器详细信息
kill      Kil a running container                       # ki11 指定 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 a17 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           # 截取容器停止时的退出状态值
wait

学习视频