Docker之镜像、容器的管理命令详解(案例多多!!!)

一、docker的镜像操作

1、搜索镜像

格式:docker search 服务名

[root@localhost ~]# docker search nginx
name表示镜像名字
DESCRIPTION 表示该镜像的功能
STARS表示下载热度

在这里插入图片描述

2、下载镜像

格式:docker pull 镜像名
[root@localhost ~]# docker pull nginx

在这里插入图片描述

3、查看已下载的镜像

[root@localhost ~]# docker images
REPOSITORY:表示镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像ID号
SIZE 镜像大小

在这里插入图片描述

4、获取镜像的详细信息

格式:docker inspect 镜像ID号
[root@localhost ~]# docker inspect 8326be82abe6

在这里插入图片描述

5、添加镜像标签

[root@localhost ~]# docker tag nginx:latest nginx:web    #类似于别名

在这里插入图片描述

6、删除镜像

格式:docker rmi 仓库名:TAG标签
[root@localhost ~]# docker rmi nginx:latest

在这里插入图片描述

7、导出镜像

格式:docker save -o 文件名 镜像名
[root@localhost abc]# docker save -o nginx nginx:latest 

在这里插入图片描述

8、载入镜像

[root@localhost abc]# docker load < nginx

在这里插入图片描述

9、上传镜像到公有仓库(必须要有docker官方账号,且创建公有仓库)

//登录
docker login
Username: docker
password:
Emall: xxx@xxx. com    #输入账号、密码
//上传
docker push 镜像名:标签名(一般公司使用私有仓库)

二、容器的操作

1、创建容器(创建前必须有该镜像)

格式:docker create -it 镜像名 运行程序
[root@localhost abc]# docker create -it nginx:latest /bin/bash
-i  指容器的标准输入保持打开
-t  指Docker分配一个伪终端

2、查看容器运行状态

[root@localhost abc]# docker ps -a   #-a 指列出最近一次启动的容器

在这里插入图片描述

3、开启容器,关闭容器

格式:docker start 容器ID号
[root@localhost abc]# docker start af255575064f

格式:docker stop 容器ID号
[root@localhost abc]# docker stop af255575064f

在这里插入图片描述

4、docker run命令

命令格式:docker run 镜像名 /bin/bash -c ls /
[root@localhost abc]# docker run mysql /bin/bash -c ls /
注释:
 /bin/bash指定运行环境,-c指定执行命令

docker run命令会执行从创建容器,开启容器,执行命令,关闭容器等一系列过程。如果没有创建容器和镜像,就会从下载镜像开始执行,一直到关闭容器。执行完成后直接关闭,状态为exited

5、进入容器与退出容器

格式:docker exec -it 容器ID /bin/bash
[root@localhost abc]# docker exec -it af255575064f /bin/bash

在这里插入图片描述

6、导出、导入容器

导出容器格式为:docker export 容器ID > 定义容器名
[root@localhost abc]# docker export b1e1844148e8 > mysql

注意:导入容器只会生成镜像

格式:cat 容器名 | docker import - 镜像:标签
[root@localhost abc]# cat mysql | docker import - mysql:web
sha256:4d5ed5638a38db0c37393b9c99a147c5fe7bf9d1e51272dcd877021b26b6939c

在这里插入图片描述

7、删除容器

容器必须是exited 关闭状态,才能删除

格式:docker rm 容器ID号
[root@localhost abc]# docker rm dc81b1a7e500

在这里插入图片描述

8、批量删除容器

第一种:
[root@localhost ~]# dcoker rm `docker ps -aq` 

第二种:
[root@localhost ~]# docker ps -a | awk '{print "docker rm"$1}' | bash    不能删除up状态的容器

在这里插入图片描述

原创文章 112 获赞 44 访问量 9998

猜你喜欢

转载自blog.csdn.net/qq_28361541/article/details/105397006
今日推荐