Docker学习(二)Docker的基本命令

上篇咱们学习了使用Docker的好处以及如何安装、卸载Docker,接下来我们学习Docker的流程、原理以及基本命令。

1. 配置阿里云镜像加速

 1.1 打开阿里云 找到容器镜像服务

1.2  找到镜像加速地址

1.3 配置使用

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://d1ov1s4k.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

2. Docker 运行的流程

3. 底层原理

 3.1  Docker是怎么工作的

 Docker 是一个c-s (Client-Server)结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问。

 DockerServer 接收到DockerClient的指令,就会执行这个命令。

3.2 Docker为什么比虚拟机快

  • Docker有着比虚拟机更少的抽象层
  • Docker利用的是宿主机的内核,虚拟机则需要搭建一个Guest OS 

 所以,新建一个容器的时候,docker不需要像虚拟机一样重新加载一个操作系统内核。 虚拟机是搭建一个Guest OS , 而docker是利用宿主机的操作系统,省略了这个复杂的过程。

4. Docker的基本命令

4.1 帮助命令

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

 帮助文档地址:https://docs.docker.com/engine/reference

4.2 镜像的基本命令

  4.2.1 docker images 查看本机所有镜像

[root@iZuf6ecpsnuw5412vz7xhsZ ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              d1165f221234        2 months ago        13.3 kB


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

[root@iZuf6ecpsnuw5412vz7xhsZ ~]# docker images --help

  -a, --all             #显示所有镜像
  -q, --quiet           #只显示镜像ID

 4.2.2 docker search  搜索镜像

[root@iZuf6ecpsnuw5412vz7xhsZ ~]# docker search mysql
INDEX       NAME                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql                             MySQL is a widely used, open-source relati...   10846     [OK]       
docker.io   docker.io/mariadb                           MariaDB Server is a high performing open s...   4091      [OK]       
docker.io   docker.io/mysql/mysql-server                Optimized MySQL Server Docker images. Crea...   800                  [OK]

# 可通过收藏来过滤
--filter=STARS=3000  #搜索收藏数大于等于3000的
[root@iZuf6ecpsnuw5412vz7xhsZ ~]# docker search mysql --filter=STARS=3000
INDEX       NAME                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql     MySQL is a widely used, open-source relati...   10846     [OK]       
docker.io   docker.io/mariadb   MariaDB Server is a high performing open s...   4091      [OK]       
[root@iZuf6ecpsnuw5412vz7xhsZ ~]# docker search mysql --filter=STARS=5000
INDEX       NAME              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql   MySQL is a widely used, open-source relati...   10846     [OK]       

4.2.3 docker pull  下载镜像

docker pull 镜像名[:tag]
[root@iZuf6ecpsnuw5412vz7xhsZ ~]# docker pull mysql
Using default tag: latest  #如果不指定tag,则默认下载最新版本
Trying to pull repository docker.io/library/mysql ... 
latest: Pulling from docker.io/library/mysql 
f7ec5a41d630: Pull complete  #分层下载 docker images的核心 联合文件系统
9444bb562699: Pull complete 
6a4207b96940: Pull complete 
181cefd361ce: Pull complete 
8a2090759d8a: Pull complete 
15f235e0d7ee: Pull complete 
d870539cd9db: Pull complete 
493aaa84617a: Pull complete 
bfc0e534fc78: Pull complete 
fae20d253f9d: Pull complete 
9350664305b3: Pull complete 
e47da95a5aab: Pull complete 
Digest: sha256:04ee7141256e83797ea4a84a4d31b1f1bc10111c8d1bc1879d52729ccd19e20a #签名
Status: Downloaded newer image for docker.io/mysql:latest #镜像的真实地址

#docker pull mysql 就等价于 docker pull docker.io/mysql:latest

#指定版本下载
[root@iZuf6ecpsnuw5412vz7xhsZ ~]# docker pull mysql:5.7
Trying to pull repository docker.io/library/mysql ... 
5.7: Pulling from docker.io/library/mysql
f7ec5a41d630: Already exists 
9444bb562699: Already exists 
6a4207b96940: Already exists  # 这一块就体现出了docker images分层下载的好处,已经存在的就不会在重复下载,大大的节省了空间
181cefd361ce: Already exists 
8a2090759d8a: Already exists 
15f235e0d7ee: Already exists 
d870539cd9db: Already exists 
cb7af63cbefa: Pull complete 
151f1721bdbf: Pull complete 
fcd19c3dd488: Pull complete 
415af2aa5ddc: Pull complete 
Digest: sha256:a655529fdfcbaf0ef28984d68a3e21778e061c886ff458b677391924f62fb457
Status: Downloaded newer image for docker.io/mysql:5.7

4.2.4 docker rmi 删除镜像 

# 根据镜像ID删除单个镜像   
docker rmi -f 镜像ID 

# 删除多个镜像
docker rmi -f 镜像ID  镜像ID  镜像ID
#删除全部镜像
docker rmi -f $(docker images -aq)

 

4.3 容器的基本命令

说明:我们有了镜像才可以创建容器,linux下载一个centos镜像来测试学习。

4.3.1 新建容器并启动

docker run [可选参数] image

#常用参数说明
--name="Name"    # 容器名字 例如:tomcat01 tomcat02 用来区分容器
-d               # 后台方式运行
-it              # 使用交互方式运行,进入容器查看内容
-p(小写p)        # 指定容器的端口
-P(大写P)        # 随机指定端口        

 

[root@iZuf6ecpsnuw5412vz7xhsZ ~]# docker run -it centos /bin/bash  #启动容器
[root@f2c93e35e958 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

退出容器 

exit  #退出容器并停止
Ctrl + P + Q #退出容器但不停止   

 docker ps 列出容器

docker ps

# 可选参数
      # 列出当前正在运行的容器
-a    # 列出当前正在运行的容器+历史运行的容器
-n=?  # 显示最近创建的容器 
-q    # 只显示容器的编号

删除容器

docker rm 容器ID                 # 删除指定容器,但不能删除正在运行的容器
docker rm -f 容器ID              # 删除指定容器,可以强制删除正在运行的容器
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        # 强制停止当前的容器

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值