Docker 命令
Docker 基础命令
-
启动 docker
systemctl start docker
-
关闭 docker
systemctl stop docker
-
重启 docker
systemctl restart docker
-
设置 docker 随服务启动而启动
systemctl enable docker
-
设置 docker 随服务关闭而关闭
systemctl disable docker
-
查看 docker 运行状态
systemctl status docker
-
查看 docker 版本号信息
docker version
-
查看 docker 详细信息
docker info
-
docker 帮助命令
docker --help
Docker 镜像命令
-
搜索 docker 镜像
docker search 镜像名 example: docker search centos
- NAME:镜像名称
- DESCRIPTION:描述
- STARS:星级
- OFFICIAL:是否官方创建
- AUTOMATED:是否主动创建
-
拉取 docker 镜像
- 不加 tag (版本号)即拉取 docker 仓库中该镜像的最新版本 latest,加 :tag 则拉取指定版本
- 官方 docker 镜像:link
docker pull 镜像名 docker pull 镜像名:tag example: docker pull centos docker pull centos:7.6.1810
-
查看 docker 镜像列表
docker images
- TAG:镜像的标签信息,标记同一个仓库中的不同镜像
- IMAGE ID:镜像的唯一 ID 号,唯一标识一个镜像
- CREATED:镜像创建时间
- VIRTUAL SIZE:镜像大小
-
获取 docker 镜像详细信息
docker inspect 镜像名/镜像ID example: docker inspect centos
-
为本地的镜像添加新的标签
docker tag 镜像名:标签 新镜像名:新标签 example: docker tag centos:7.6.18100 centos:7.6
-
运行 docker 镜像
在前台运行,使用 Ctrl + c 退出
docker run 镜像名 docker run 镜像名:Tag example: docker run centos docker run centos:7.6.1810
-
删除 docker 镜像
当前镜像没有被任何容器使用才可以删除
# 删除一个 docker rmi -f 镜像名/镜像ID example: docker rmi -f centos # 删除多个 其镜像名或镜像ID,用空格隔开即可 docker rmi -f 镜像名/镜像ID 镜像名/镜像ID 镜像名/镜像ID example: docker rmi -f centos mysql httpd # 删除全部镜像 -a 意思为显示全部, -q 意思为只显示ID docker rmi -f $(docker images -aq)
-
强制删除 docker 镜像
docker image rm 镜像名/镜像ID example: docker image rm centos
-
保存 docker 镜像
docker save 镜像名/镜像ID -o 镜像保存的位置及名称 example: docker save centos -o /root/centos7.6.tar
-
加载 docker 镜像
docker load -i 镜像保存的位置 docker load < 镜像保存的位置 example: docker load -i /root/centos7.6.tar docker load < /root/centos7.6.tar
-
上传 docker 镜像
docker push 仓库名称:标签 example: docker push centos:7.6
Docker 容器命令
-
查看正在运行容器列表
docker ps
-
查看所有容器
包含正在运行和已停止的
docker ps -a
-
创建一个容器
容器是关闭状态
docker create -it --name 别名 镜像名:Tag /bin/bash example: docker create -it --name centos centos:7.6 /bin/bash
- -i:让容器的输入保持打开
- -t:让 Docker 分配一个伪终端
- –name:指定容器名称
-
启动容器
docker start 容器名/容器ID example: docker start centos
-
停止容器
docker stop 容器名/容器ID example: docker stop centos
-
重启容器
docker restart 容器名/容器ID example: docker restart centos
-
删除容器
# 删除一个容器 docker rm -f 容器名/容器ID example: docker rm -f centos # 删除多个容器,空格隔开要删除的容器名或容器ID docker rm -f 容器名/容器ID 容器名/容器ID 容器名/容器ID example: docker rm -f centos mysql httpd # 删除全部容器 docker rm -f $(docker ps -aq)
-
运行一个容器
容器是运行状态
docker run -itd --name 别名 镜像名:Tag /bin/bash example: docker run -itd --name centos centos:7.6 /bin/bash
- -i:让容器的输入保持打开
- -t:让 Docker 分配一个伪终端
- -d:让容器在后台运行
- –name:指定容器名称
-
容器端口与服务器端口映射
-p 宿主机端口:容器端口 example: docker run -itd -p 80:443 --name httpd httpd:latest /bin/bash
- -p:指定映射端口
-
容器数据挂载
# 挂载单个文件 -v 宿主机文件存储位置:容器内文件存储位置 example: docker run -itd -v /data/www/test:/data/www/test --name httpd httpd:latest /bin/bash # 挂载多个文件 -v 宿主机文件存储位置:容器内文件存储位置 -v 宿主机文件存储位置:容器内文件存储位置 example: docker run -itd -v /data/www/test:/data/www/test -v /etc/httpd/httpd.conf:/etc/http/httpd.conf --name httpd httpd:latest /bin/bash
-
进入容器
docker exec -it 容器名/容器ID /bin/bash example: docker exec -it centos /bin/bash
-
退出容器
exit
-
容器导出
docker export 容器名/容器ID > 保存容器的位置及名称 example: docker export centos > /root/centos7.6.tar
-
容器导入
cat 容器名称 | docker import - 生成的镜像名:标签 example: cat /root/centos7.6.tar | docker import - centos:7.6
-
容器文件拷贝
# 从容器内拷出 docker cp 容器名/容器ID:容器内路径 容器外路径 example: docker cp centos:/root/test /root/ # 从外部拷贝文件到容器内 docker cp 容器外路径 容器名/容器ID:容器内路径 example: docker cp /root/test centos:/root/
-
查看容器日志
docker logs -f 容器名/容器ID example: docker logs -f centos
-
容器随 docker 启动而启动
docker run -itd --name 别名 --restart=always 镜像名:Tag /bin/bash example: docker run -itd --name centos --restart=always centos:7.6 /bin/bash 或者 docker update --restart=always 容器名/容器ID docker container update --restart=always 容器名/容器ID
- –restart=always:容器随 docker 启动而启动
-
更换容器名
docker rename 容器名/容器ID 新容器名 example: docker rename centos centos7
自己提交一个镜像
我们运行的容器可能在镜像的基础上做了一些修改,有时候我们希望保存起来,封装成一个更新的镜像,这时候我们就需要使用 commit 命令来构建一个新的镜像
docker commit -m="提交信息" -a="作者信息" 容器名/容器ID 提交后的镜像名:Tag
example:
docker commit -m="new" -a="qingshan" centos centos7:7.6
-
-m:说明信息
-
-a:作者信息
-
-p:生成过程中停止容器的运行
-
docker 登陆到镜像仓库
docker login -u 用户名 -p 密码 example: docker login -u root -p root
- -u:登陆的用户名
- -p:登陆的密码
-
docker 登出镜像仓库
docker logout