docker常用命令--镜像、容器、拷贝、挂载
帮助启动命令
启动docker:
systemctl start docker
停止docker:systemctl stop docker
重启docker:systemctl restart docker
查看docker状态:systemctl status docker
开机启动:systemctl enable docker
查看docker概要信息:docker info
查看docker总体帮助文档:docker --help
查看docker命令帮助文档:docker 具体命令 --help
镜像命令
查看docker镜像:
docker images
- 列出本地主机上的镜像【表头选项说明】:
| REPOSITORY:表示镜像的仓库源
| TAG:镜像的标签版本号
| IMAGE ID:镜像ID
| CREATED:镜像创建时间
| SIZE:镜像大小
- options说明:
|-a:列出本地所有的镜像(包含中间映像层)
|-q:只显示镜像ID
|--digests:显示镜像的摘要信息
|--no-trunc:显示完整的镜像信息
搜索相关镜像:
docker search 镜像名字
- 网站:https://hub.docker.com
- docker search [OPTIONS]镜像名字
- 列出配置的镜像加速器地址中查找到的镜像【表头选项说明】:
| NAME:镜像名称
| DESCRIPTION:镜像说明
| STARS:点赞数量
| OFFICIAL:是否是官方的
| AUTOMATED:是否是自动构建的
- options说明:
|--no-trunc:显示完整的镜像描述
|-s:列出收藏数不小于指定值的镜像
|--automated:只列出automated build类型的镜像
| --limit:只列出N个镜像。默认25个
eg:docker search --limit 5 redis
拉取镜像:
docker pull 镜像名字[:tag]
- 下载镜像
- docker pull 镜像名字
- 没有TAG就是最新版---等价于---docker pull 镜像名字:latest
查看镜像/容器/数据卷所占用的空间:
docker system df 查看镜像/容器/数据卷所占用的空间
删除镜像:
docker rmi 镜像id
- options说明:
| -f:强制删除
- 删除镜像 :docker rmi 镜像id
- 删除单个镜像 :docker rmi -f 镜像ID
- 删除多个镜像 :docker rmi -f 镜像名1:TAG 镜像名2:TAG
- 删除全部镜像 :docker rmi -f $(docker images -qa)
面试题:谈谈docker虚悬镜像是什么?
是什么
仓库名、标签都是的镜像,俗称虚悬镜像dangling image
docker commit
docker push
容器命令
有镜像才能创建容器,这是根本前提!
新建并启动容器:
docker run [OPTIONS] IMAGE [COMMAND][ARG...]
- OPTIONS说明(常用):有一些是一个减号,一些是两个减号
|--name=“容器新名字” :为容器指定一个新名称
|-d : 后台运行容器,并返回容器id,也即启动守护式容器(后台运行)
|-i :以交互模式运行容器,通常与-t同时使用(i:interactive)
|-t :为容器重新分配一个伪输入终端,通常与-i同时使用(t:tty)
也即启动交互式容器(前台有伪终端,等待交互)。
|-P :随机端口映射
|-p :指定端口映射,有以下四种格式
ip:hostPort:containerProt
ip::containerProt
hostProt:containerProt
containerProt
查看正在运行的容器:
docker ps
OPTIONS说明:
-a :显示所有的容器,包括未运行的。
-f :根据条件过滤显示的内容。
--format :指定返回值的模板文件。
-l :显示最近创建的容器。
-n :列出最近创建的n个容器。
--no-trunc :不截断输出。
-q :静默模式,只显示容器编号。
-s :显示总的文件大小。
进入容器
docker run -it 镜像名称或id
docker attach 容器名称或id
docker exec -it 容器名称或id bash
docker run -it会创建前台进程,但是会在输入exit后终止进程。
docker attach 会通过连接stdin,连接到容器内输入输出流,会在输入exit后终止进程.
docker exec -it 会连接到容器,可以像SSH一样进入容器内部,进行操作,可以通过exit退出容器,不影响容器运行。
退出容器
exit
启动容器
docker start 容器ID(或着容器名)
重启容器
docker restart 容器ID(或着容器名)
停止容器
docker stop 容器ID(或着容器名)
强制停止容器
docker kill 容器ID(或着容器名)
删除已停止的容器
docker rm 容器ID(或着容器名)
一次性删除多个容器:
docker rm -f $(docker ps -a -q)
docker ps -a -q | xargs docker rm
拷贝
从容器内拷贝文件到主机上
docker cp 容器ID:容器内路径 目的主机路径
从宿主机拷贝到容器内
docker cp 宿主机文件路径 容器ID:容器目的路径
导入和导出容器
- export :导出容器的内容流作为一个tar归档文件【对应import命令】
- import :从tar包中的内容创建一个新的文件系统再导入为镜像【对应export】
案例:
导出:docker export 容器ID > 文件名.tar
导入:cat 文件名.tar | docker import - 镜像用户/镜像名:镜像版本号
---------------导出实操---------------------------------------------------------------------------------------
PS D:\opt\work> docker images ubuntu
REPOSITORY TAG IMAGE ID CREATED SIZE
PS D:\opt\work> docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
ea362f368469: Pull complete
Digest: sha256:b5a61709a9a44284d88fb12e5c48db0409cfad5b69d4ff8224077c57302df9cf
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
PS D:\opt\work> docker images ubuntu
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest d13c942271d6 12 days ago 72.8MB
PS D:\opt\work> docker run -it ubuntu /bin/bash
root@a0bb3808ea1c:/#
PS D:\opt\work> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0bb3808ea1c ubuntu "/bin/bash" 22 seconds ago Up 21 seconds sad_chaplygin
PS D:\opt\work> docker export a0bb3808ea1c > a.tar.gz
PS D:\opt\work> ls .\a.tar.gz
目录: D:\opt\work
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2022/1/19 17:10 129572462 a.tar.gz
PS D:\opt\work>
---------------导入实操---------------------------------------------------------------------------------------
PS D:\opt\work> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0bb3808ea1c ubuntu "/bin/bash" 6 minutes ago Up 6 minutes sad_chaplygin
PS D:\opt\work> docker rm -f a0bb3808ea1c
a0bb3808ea1c
PS D:\opt\work> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
PS D:\opt\work> cat .\a.tar.gz | docker import - yyds/ubuntu:2.0
Error response from daemon: Error processing tar file(exit status 1): archive/tar: invalid tar header
PS D:\opt\work> cat .\a.tar | docker import - yyds/ubuntu:2.0
Error response from daemon: Error processing tar file(exit status 1): archive/tar: invalid tar header
PS D:\opt\work>
----------------------由于我是在Windows操作,一直没有导入成功,可在linux试下-------------------------
PS D:\opt\work> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
PS D:\opt\work> docker images ubuntu
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest d13c942271d6 12 days ago 72.8MB
PS D:\opt\work> docker run -it -d ubuntu /bin/bash
31b8ddd49884ecec25251bbc9d4351442db46d6ed446c1c9cdc9e9f9f861c1c4
PS D:\opt\work> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
31b8ddd49884 ubuntu "/bin/bash" 3 seconds ago Up 2 seconds upbeat_feistel
PS D:\opt\work> docker export 31b8ddd49884 -o x.tar
PS D:\opt\work> ls .\x.tar
目录: D:\opt\work
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2022/1/19 17:29 75158528 x.tar
PS D:\opt\work> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
31b8ddd49884 ubuntu "/bin/bash" 46 seconds ago Up 46 seconds upbeat_feistel
PS D:\opt\work> docker stop 31b8ddd49884
31b8ddd49884
PS D:\opt\work> docker rm 31b8ddd49884
31b8ddd49884
PS D:\opt\work> cat .\x.tar | docker import yyds/ubuntu:2.0
open yyds/ubuntu:2.0: The system cannot find the path specified.
PS D:\opt\work> docker images yyds/ubuntu
REPOSITORY TAG IMAGE ID CREATED SIZE
PS D:\opt\work> cat .\x.tar | docker import ubuntu:2.0
open ubuntu:2.0: The system cannot find the file specified.
PS D:\opt\work>
数据卷(挂载/映射)
挂载命令:
docker run -it -v /宿主机绝对路径:/容器内目录 镜像名
-v:新建挂载目录
eg:
docker run -it -v /myDataVolume:/dataVolumeContainer 镜像名
注意:
容器停止后,在宿主机修改挂载目录的内容,当容器重新启动后,数据会同步。
--------------------------------------------------------------------------------------------------
docker run -it -v /宿主机绝对路径:/容器内目录:ro 镜像名
ro:目录权限设置:只读
eg:
docker run -it -v /myDataVolume:/dataVolumeContainer:ro 镜像名
注意:
只允许宿主机单向进行读写操作,容器内部只可以读取,不可以写。
查看挂载是否成功:
方式一:
在容器内和宿主机外部挂载的目录分别新建文件,或者修改文件,看是否可以同步内容。
方式二:
查看容器元数据。
docker inspect 容器ID
Docker Commands Diagram
Commands:
attach Attach local standard input, output, and error streams to a running container #当前shell下attach连接指定运行镜像
build Build an image from a Dockerfile #通过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 #创建一个新的容器,同run,但不启动
diff Inspect changes to files or directories on a container’s filesystem #查看docker容器变化
events Get real time events from the server #从docker服务获取容器实时事件
exec Run a command in a running container #在已存在的容器上运行命令
export Export a container’s filesystem as a tar archive #导出容器的内容流作为一个tar归档文件【对应import】
history Show the history of an image #展示一个镜像形成历史
images List images #列出系统当前镜像
import Import the contents from a tarball to create a filesystem image #从tar包中的内容创建一个新的文件系统映像【对应export】
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 #查看正在运行的容器log
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 #推送指定镜像或者库镜像至docker源服务器
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) #保存一个镜像为一个tar包【对应load】
search Search the Docker Hub for images #在docker hub中搜索镜像
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 #查看docker版本号
wait Block until one or more containers stop, then print their exit codes #截取容器停止时的退出状态值