105.【Docker】(二)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 105.【Docker】

4. 阿里云镜像加速

  1. 找到位置

    2.找到对应服务

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

成功展示:

5.回顾Hello-world流程

Run的运行流程图

6.底层原理

Docker是怎么工作的

docker是Client-Server结构的系统,Docker的守护进程运行在主机上,通过Socket从客户端访问,DockerServer接收到Docker-Client的指令,就会执行这个命令。

Docker为什么比虚拟机快(VM)?

  1. Docker有着比虚拟机更少的抽象层

  2. Docker用的是宿主机的内核,Vm需要Guest OS

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

(三)、Docker命令

1.帮助命令

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

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

2.镜像命令

(1).查看镜像
docker images [-aq]

相当于一个表格

[root@Jsxs ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   18 months ago   13.3kB
#解释
REPOSITORY 镜像的仓库源
TAG        镜像的标签
IMAGE ID   镜像的id
CREATED    镜像的时间
SIZE       镜像的大小
------------------------------------------------
#可选项
  -a, --all             #列出所有的镜像             
  -q, --quiet           #只显示镜像id
[root@Jsxs ~]# docker images -a
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   18 months ago   13.3kB
[root@Jsxs ~]# docker images -q
feb5d9fea6a5
[root@Jsxs ~]# docker images -aq  #所有id
feb5d9fea6a5
(2).搜索镜像
[root@Jsxs ~]# docker search 镜像名 [--filter=条件]

实际上对应Docker商店

可选参数:

[root@Jsxs ~]# docker search --help
Usage:  docker search [OPTIONS] TERM
Search Docker Hub for images
Options:
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print search using a Go template
      --limit int       Max number of search results
      --no-trunc        Don't truncate output
# 可选参数,通过收藏进行过滤
-f, --filter=STARS=3000 搜索出来的镜像就是STARS大于3000的
--------------------------------------------------------------
[root@Jsxs ~]# docker search mysql  --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   13958     [OK]
mariadb   MariaDB Server is a high performing open sou…   5319      [OK]
(3).下载镜像

如果不写版本号,默认就是最新

docker pull 镜像名[:版本号]
# 下载镜像,docker pull 镜像名[:tag]
[root@Jsxs ~]# docker pull mysql
Using default tag: latest           # 如果不写tag,默认就是最新
latest: Pulling from library/mysql
bf5952930446: Pull complete         # 分层下载,docker images 的核心,联合文件系统
8254623a9871: Pull complete 
938e3e06dac4: Pull complete 
ea28ebf28884: Pull complete 
f3cef38785c2: Pull complete 
894f9792565a: Pull complete 
1d8a57523420: Pull complete 
6c676912929f: Pull complete 
ff39fdb566b4: Pull complete 
fff872988aba: Pull complete 
4d34e365ae68: Pull complete 
7886ee20621e: Pull complete 
Digest: sha256:c358e72e100ab493a0304bda35e6f239db2ec8c9bb836d8a427ac34307d074ed     # 签名-防伪标志
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest      # 真实地址
# 等价于,下面两个命令是等价的。
  docker pull mysql
  docker pull docker.io/library/mysql:latest
 ---------------------------------------------------------------------
# 指定版本下载
docker pull mysql:5.7
5.7: Pulling from library/mysql
bf5952930446: Already exists   # 已存在,表示共用,可以极大的节省内存
8254623a9871: Already exists 
938e3e06dac4: Already exists 
ea28ebf28884: Already exists 
f3cef38785c2: Already exists 
894f9792565a: Already exists 
1d8a57523420: Already exists 
5f09bf1d31c1: Pull complete  # 没有的才需要下载更新
1b6ff254abe7: Pull complete 
74310a0bf42d: Pull complete 
d398726627fd: Pull complete 
Digest: sha256:da58f943b94721d46e87d5de208dc07302a8b13e638cd1d24285d222376d6d84
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
# 查看本地镜像
[root@Jsxs ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.7                 718a6da099d8        6 days ago          448MB
mysql               latest              0d64f46acfd1        6 days ago          544MB
hello-world         latest              bf756fb1ae65        7 months ago        13.3kB
(4).删除镜像

-f 是全部删除,可以通过imageID来删除,也可以通过name来删除。

[root@abc ~]# docker rmi -f 镜像ID           #删除指定的镜像
 [root@abc ~]# docker rmi -f 镜像ID 镜像ID 镜像ID   #删除多个镜像
 [root@abc ~]# docker rmi -f $(docker images -aq)   #删除所有镜像

3.容器命令

前提: 我们有了镜像才可以创建容器。下载一个Centos镜像来测试学习

(1).下载Centos镜像
docker pull centos   # 拉去镜像

(2).新增容器并启动容器
docker run [可选参数] 容器名字
#参数说明
--name="Name" 容器名字 tomcat01 tomcat02用来区分容器
-d            后台运行方式
-it           使用交互方式,进入容器
-p            指定容器的端口 -p 8080:8080
     -p ip:主机端口:容器端口
     -p 主机端口映射到容器端口(常用)
     -p 容器端口
     容器端口
-P            随机指定端口
------------------------------------------------------
[root@Jsxs ~]# docker run -it centos /bin/bash    # 启动并进入容器
[root@fa630255c36e /]# ls   #查看容器内的centos,基础版本,很多命令都是不完善的,
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@fa630255c36e /]# exit  # 从容器中退回到主机,且不再运行。只是不运行了,并不是删除,所以数据还在。
exit 

退出容器

exit    #直接退出容器并退出
ctrl+P+Q   #容器不停止退出
-----------------------------------------------------------
[root@Jsxs /]# docker run -it centos /bin/bash  #新建并启动
[root@6bb50fc7777f /]# [root@Jsxs /]# docker ps  #列出正在运行的容器
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS              PORTS     NAMES
6bb50fc7777f   centos    "/bin/bash"   About a minute ago   Up About a minute             amazing_lumiere
[root@Jsxs /]# docker rm 6bb50fc7777f  # 普通移除,移除不了正在运行的
Error response from daemon: You cannot remove a running container 6bb50fc7777f5ac25d954f1d12cdcf240e0ddd8ed5527477f6c1b28e92730ecf. Stop the container before attempting removal or force remove
[root@Jsxs /]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
6bb50fc7777f   centos    "/bin/bash"   2 minutes ago   Up 2 minutes             amazing_lumiere
[root@Jsxs /]# docker rm -f 6bb50fc7777f  #强制删除正在运行的
6bb50fc7777f
[root@Jsxs /]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
(3).列出容器
[root@Jsxs /]# docker ps     #列出所有正在运行的容器
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@Jsxs /]# docker ps -a   #列出之前运行的容器
CONTAINER ID   IMAGE         COMMAND       CREATED         STATUS                     PORTS     NAMES
fa630255c36e   centos        "/bin/bash"   5 minutes ago   Exited (0) 2 minutes ago             brave_bouman
e8d8887713bc   hello-world   "/hello"      2 hours ago     Exited (0) 2 hours ago               jolly_nobel
15ddd66064d0   hello-world   "/hello"      2 weeks ago     Exited (0) 2 weeks ago               jolly_spence
e62816aec698   hello-world   "/hello"      2 weeks ago     Exited (0) 2 weeks ago               vigilant_meitner
[root@Jsxs /]# docker ps -a -n=2  # 列出最近2个运行过的容器
CONTAINER ID   IMAGE         COMMAND       CREATED         STATUS                     PORTS     NAMES
fa630255c36e   centos        "/bin/bash"   7 minutes ago   Exited (0) 4 minutes ago             brave_bouman
e8d8887713bc   hello-world   "/hello"      2 hours ago     Exited (0) 2 hours ago               jolly_nobel
[root@Jsxs /]# docker ps -aq  # 列出最近运行的全部容器ID
fa630255c36e
e8d8887713bc
15ddd66064d0
e62816aec698
(4).删除容器
docker rm 容器id          #删除指定的容器,不能删除正在运行的容 器,如果要强制删除 rm -rf
[root@Jsxs /]# docker ps -a    #先列出所有的容器id
CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                      PORTS     NAMES
fa630255c36e   centos        "/bin/bash"   22 minutes ago   Exited (0) 20 minutes ago             brave_bouman
e8d8887713bc   hello-world   "/hello"      3 hours ago      Exited (0) 3 hours ago                jolly_nobel
15ddd66064d0   hello-world   "/hello"      2 weeks ago      Exited (0) 2 weeks ago                jolly_spence
e62816aec698   hello-world   "/hello"      2 weeks ago      Exited (0) 2 weeks ago                vigilant_meitner
[root@Jsxs /]# docker rm fa630255c36e   #通过容器id进行删除
fa630255c36e
--------------------------
 docker rm -f $(docker ps -aq)     #删除所有的容器(后面 ${}是参数传递),历史记录也删除了
[root@Jsxs /]# docker rm -f $(docker ps -aq)
e8d8887713bc
15ddd66064d0
e62816aec698
[root@Jsxs /]# docker ps -aq
-----------------------------
 docker ps -a -q|xargs docker rm  #删除所有的容器
(5).启动和停止容器的操作
docker start 容器id #docker run 是创建容器并启动,加入关闭后再就要用这个docker start
 docker restart 容器id  #重启容器
 docker stop 容器id #停止当前正在运行的容器,会给10s时间去处理后世
 docker kill 容器id #强制停止当前容器,直接噶皮

4.其他命令语言

(1).后台启动容器
# 命令 docker run -d 镜像名
-----------------------------------------------------------
[root@Jsxs ~]# docker run -d centos  #启动容器
1f521ebe85e64e8c4387a0b933d21c38437cbe62088b7f8b3731b44af12ce235
[root@Jsxs ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES 问题 docker ps, 发现centos停止了
# 问题 docker ps, 发现centos停止了
# 常见的坑, docker 容器使用后台运行, 就必须要有一个前台进程,docker发现没有应用,就会自动停止
# nginx, 容器启动后,发现自己没有提供服务,就会立即停止,就是没有程序了
(2). 查看容器的日志
docker logs -tf --tail number 容器id
        -tf                 # 显示日志
        --tail number       # 显示日志条数
------------------------------------------------------------------------
[root@25cdb0f99d5b /]# [root@Jsxs ~]# docker ps  #查看容器
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
25cdb0f99d5b   centos    "/bin/bash"   21 seconds ago   Up 19 seconds             thirsty_cray
[root@Jsxs ~]# docker logs -tf --tail 10 25cdb0f99d5b  #实时打印日志,因为没有日志所以一直等待
^C
[root@Jsxs ~]# docker run -d centos /bin/sh -c "while true;do echo xiaofan;sleep 1;done" #编写一个脚本
be003d68204c8b3093277632abaa73e74a2e3e9230ae0c64223b1642d1d5eeab
[root@Jsxs ~]# docker ps  #查看正在运行的脚本
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
be003d68204c   centos    "/bin/sh -c 'while t…"   18 seconds ago   Up 17 seconds             clever_cannon
25cdb0f99d5b   centos    "/bin/bash"              5 minutes ago    Up 5 minutes              thirsty_cray
[root@Jsxs ~]# docker logs -tf --tail 10 be003d68204c  #查看日志
2023-03-21T11:02:50.282553993Z xiaofan  #以下是打印的日志
2023-03-21T11:02:51.284511479Z xiaofan
2023-03-21T11:02:52.286475299Z xiaofan
2023-03-21T11:02:53.288525375Z xiaofan
2023-03-21T11:02:54.290252993Z xiaofan
2023-03-21T11:02:55.292331800Z xiaofan
2023-03-21T11:02:56.294111690Z xiaofan
2023-03-21T11:02:57.296295842Z xiaofan
2023-03-21T11:02:58.298222418Z xiaofan
2023-03-21T11:02:59.300133879Z xiaofan
2023-03-21T11:03:00.302025977Z xiaofan
2023-03-21T11:03:01.303913764Z xiaofan
2023-03-21T11:03:02.305736445Z xiaofan
2023-03-21T11:03:03.307603920Z xiaofan
2023-03-21T11:03:04.309328998Z xiaofan
2023-03-21T11:03:05.311165252Z xiaofan
2023-03-21T11:03:06.312965880Z xiaofan
2023-03-21T11:03:07.314699791Z xiaofan
2023-03-21T11:03:08.316592003Z xiaofan
^C
相关文章
|
1月前
|
应用服务中间件 nginx Docker
Docker的世界
Docker的世界
15 0
|
4月前
|
关系型数据库 MySQL 虚拟化
docker介绍
docker介绍
56 0
|
9月前
|
JavaScript 应用服务中间件 API
Docker
Docker 是一种开源的容器化平台,可以让开发者在容器中快速构建、打包、发布和运行应用程序,从而实现应用程序的快速交付和部署。
332 1
|
9月前
|
Docker 容器
Docker 初识
Docker 初识
44 0
|
10月前
|
存储 Kubernetes 负载均衡
Docker
是一个开源的容器化平台,它可以让开发者和系统管理员轻松地创建、部署和运行应用程序。Docker使用操作系统级别的虚拟化技术,可以将应用程序及其依赖项打包到一个称为Docker容器的单个可移植包中,从而实现应用程序的隔离、移植性和可重复性。
1837 2
|
10月前
|
存储 安全 测试技术
Docker初步了解
Docker初步了解
53 0
|
Linux Docker Python
JupyterNotebook-Docker版
制作jupyter notebook的docker镜像,兼容numpy, pandas, matplotlib, pyspark, opencv-python, requests, flask等组件。
362 0
|
存储 持续交付 虚拟化
|
关系型数据库 MySQL 应用服务中间件
docker
-
214 0