Famousmai's blog Famousmai's blog
首页
👍 网站介绍
💯 编程分享
✍️ 生活感悟
🎮 游戏人生
📈 网站动态
💌 收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Famous Mai

爱玩辅助的后端小哥
首页
👍 网站介绍
💯 编程分享
✍️ 生活感悟
🎮 游戏人生
📈 网站动态
💌 收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 算法

  • 学习笔记

    • Golang学习笔记
    • Docker学习笔记
      • Docker概述
        • Docker为什么出现
        • Docker的历史
        • 聊聊docker(网址)
        • Docker能干嘛
        • 之前的虚拟机技术
        • 容器化技术
        • DevOps(开发、运维)
      • Docker安装
        • docker 的基本组成
        • 安装Docker
        • 环境准备
        • 环境查看
        • 安装(centos)
        • 阿里云镜像加速
        • 回顾helloworld过程
        • 底层原理
      • Docker的常用命令
        • 帮助命令
        • 镜像命令
        • docker images 查看镜像
        • docker search 搜索镜像
        • docker pull 下载镜像
        • docker rmi 删除镜像
        • 容器命令
        • docker run 新建容器并启动
        • docker ps 列出运行容器
        • 退出容器
        • docker rm 删除容器
        • 启动和停止容器的操作
        • 常用其他命令
        • 后台启动容器
        • 查看日志
        • 查看容器中进程信息
        • 进入当前的
        • 操作命令
      • Docker镜像
      • 容器数据卷
      • Dockerfile
      • Docker网络原理
      • IDEA整合Docker
      • Docker Compose
      • Docker Swarm
      • CI\CD Jenkins
    • Consul学习笔记
    • Elk学习笔记
    • 数据结构与算法之美
    • Go实操项目学习笔记
    • Paypal对接学习笔记
  • 踩坑记录

  • 面试分享

  • 技术方案文章梳理

  • 设计模式

  • 编程分享
  • 学习笔记
famousmai
2023-02-17
目录

Docker学习笔记

# 学习笔记

说在前面

  • 以下笔记是跟着狂神自己手敲的,并非原创
    • 【狂神说Java】Docker最新超详细版教程通俗易懂 (opens new window)
    • 【狂神说Java】Docker进阶篇超详细版教程通俗易懂 (opens new window)

# Docker概述

# Docker为什么出现

一款产品:开发 上线 两套环境 ! 应用环境,应用配置

开发 --- 运维。问题:我在我的电脑上可以运行!版本更新,导致服务不可用!对于运维来说,考验就十分大

环境配置是十分麻烦的,每一个机器都要部署环境(集群Redis、ES、Hadoop...)! 费时费力。

发布一个项目(程序文件 + (Redis、MySql、ES、Hadoop)),项目能不能带上环境安装打包

之前在服务器配置一个应用的环境 Redis、Mysql、ES、Hadoop 配置超麻烦,不能够跨平台

Windows,最后发布到Linux!

传统:开发go程序,部署运维来做!

现在:开发打包部署上线,一套流程做完!

java --- apk -- 发布(应用商店) --- 张三使用apk ---- 安装即可用!

java --- jar(环境) --- 打包项目带上环境(镜像) ---- (Docker仓库:商店) ---- 下载我们发布的镜像 --- 直接运行即可!

docker给以上的问题,提出了解决方案!

Docker的思想就来自于集装箱!

JRE -- 多个应用(端口冲突) -- 原来都是交叉的

隔离:docker核心思想!打包装箱!每个箱子是互相隔离的

水果 生化武器

Docker 通过隔离机制,可以将服务器利用到极致!

本质:所有的技术都是因为出现了一些问题,我们需要去解决,才去学习!

# Docker的历史

2010年,几个搞IT的年轻人,就在美国成立了一家公司dotCloud

做一些pass的云计算服务!LXC有关的容器技术!

image-20230117182705585

他们将自己的技术 (容器化技术) 命名就是 Docker!

Docker刚刚诞生的时候,没有引起行业注意!dotCloud,就活不下去!

开源

开放源代码!

2013年,Docker开源!

Docker 越来越多的人发现了docker的优点!火了,Docker每个月都会更新一个版本!

2014年4月9日,Docker1.0发布

Docker为什么这么火?十分的轻巧!

在容器技术出来之前,我们都是使用虚拟机技术!

虚拟机:在window中装一个Vmware,通过这个软件我们可以虚拟出来一台或多台电脑!笨重!

虚拟机也是属于虚拟化技术,Docker 容器技术,也是一种 虚拟化技术!

vm: linux centos原生镜像(一个电脑!)隔离,需要开启多个虚拟机!几个G 几分钟
docker:隔离,镜像(最核心的环境 4m + jdk + mysql) 十分的小巧,运行镜像就可以了!小巧!几个M  KB 秒级启动!
1
2

到现在,所有开发人员都必须要会Docker!

# 聊聊docker(网址)

Docker 是基于Go语言开发的!开源项目!

官网:https://www.docker.com

文档地址:https://docs.docker.com

仓库地址:https://hub.docker.com

# Docker能干嘛

# 之前的虚拟机技术

![image-20230117123541741](/Users/mym/Library/Application Support/typora-user-images/image-20230117123541741.png)

虚拟机技术缺点:

  1. 资源占用十分多
  2. 冗余步骤多
  3. 启动很慢!

# 容器化技术

==容器化技术不是模拟的一个完整的操作系统==image-20230117124052734

比较Docker 和 虚拟机技术的不同

  • 传统虚拟机,虚拟出一条硬件,运行一个完整的操作系统,然后在这个系统上安装和运行软件
  • 容器内的应用直接运行在 宿主机的内容,容器是没有自己的内核的,也没有虚拟我们的意见,所以就轻便了
  • 每个容器间是互相隔离,每个容器内都有一个属于自己的文件系统,互不影响

# DevOps(开发、运维)

应用更快速的交付和部署

传统:一堆帮助文档,安装程序

Dokcer:打包镜像发布测试,一键运行

更便捷的升级和阔所容

使用了Docker之后,我们部署应用就和搭积木一样!

项目打包为一个镜像,扩展 服务器A!服务器B

更简单的系统运维

在容器化之后,我们的开发,测试环境都是高度一致的

更高效的计算资源利用

Docker是 内核级别的虚拟话,可以在一个物理机上可以运行很多的容器实例!服务器的性能可以被压榨到极致

# Docker安装

# docker 的基本组成

img

镜像(image)

就好比是一个模版,可以通过这么模板来创建容器服务,tomcat镜像 ===> run ==> tomcat01容器(提供服务器)

通过这个镜像可以创建多个容器(最终服务运行或者项目运行就是在容器中的)。

容器(container)

Docker利用容器技术,独立运行一个或者一个组应用,通过镜像来创建的。

启动,停止,删除,基本命令!

目前就可以把这个容器理解为一个简易的linux系统

仓库(repository)

仓库就是存放镜像的地方!

仓库分为公有仓库和私有仓库!

Docker Hub(默认是国外的)

阿里云... 都有容器服务(配置镜像加速!)

# 安装Docker

# 环境准备

  1. 需要会一点点的linux基础
  2. Centos 7
  3. 我们使用xshell连接远程服务器进行操作!

# 环境查看

# 系统内核是3.10以上的
[root@iZwz954642t91dbe6lc2l1Z ~]# uname -r
3.10.0-1127.19.1.el7.x86_64
1
2
3
# 系统版本
[root@iZwz954642t91dbe6lc2l1Z ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 安装(centos)

参考链接

https://developer.aliyun.com/mirror/docker-ce?spm=a2c6h.13651102.0.0.55e01b110vCziG

https://juejin.cn/post/7189159542918217789

帮助文档

# 1. 卸载旧版本
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
已加载插件:fastestmirror
参数 docker 没有匹配
参数 docker-client 没有匹配
参数 docker-client-latest 没有匹配
参数 docker-common 没有匹配
参数 docker-latest 没有匹配
参数 docker-latest-logrotate 没有匹配
参数 docker-logrotate 没有匹配
参数 docker-engine 没有匹配

# 2. 需要的安装包
yum install -y yum-utils

# 3. 设置镜像的仓库
yum-config-manager \
	--add-repo \
	https://download.docker.com/linux/centos/docker-ce.repo # 默认是国外的
	
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 阿里云加速,十分地快

# 更新yum软件包索引
yum makecache fast

# 4. 安装docker docker-ce社区版 ee是企业版
yum install docker-ce docker-ce-cli containerd.io

# 5. 启动docker
systemctl start docker
service docker start

# 6. docker version 检查是否成功
[root@iZwz954642t91dbe6lc2l1Z ~]# docker version
Client: Docker Engine - Community
 Version:           20.10.22
 API version:       1.41
 Go version:        go1.18.9
 Git commit:        3a2c30b
 Built:             Thu Dec 15 22:30:24 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.22
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.9
  Git commit:       42c8b31
  Built:            Thu Dec 15 22:28:33 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.15
  GitCommit:        5b842e528e99d4d4c1686467debf2bd4b88ecd86
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

# 7. hello world
[root@iZwz954642t91dbe6lc2l1Z ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:aa0cc8055b82dc2509bed2e19b275c8f463506616377219d9642221ab53cf9fe
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/
 
 # 8. 查看一下下载的这个 hello-world镜像
 [root@iZwz954642t91dbe6lc2l1Z ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   16 months ago   13.3kB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

了解:卸载docker

# 1. 卸载依赖
yum remove docker-ce docker-ce-cli containerd.io

# 2. 删除资源
rm -rf /var/lib/docker

# /var/lib/docker docker的默认工作路径!
1
2
3
4
5
6
7

# 阿里云镜像加速

  1. 登录阿里云

  2. 找到镜像加速地址

    ![image-20230117150149564](/Users/mym/Library/Application Support/typora-user-images/image-20230117150149564.png)

  3. 配置使用

    sudo mkdir -p /etc/docker
    sudo tee /etc/docker/daemon.json <<-'EOF'
    {
      "registry-mirrors": ["https://9lxn3itv.mirror.aliyuncs.com"]
    }
    EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker
    
    1
    2
    3
    4
    5
    6
    7
    8

# 回顾helloworld过程

![image-20230117150603100](/Users/mym/Library/Application Support/typora-user-images/image-20230117150603100.png)

![image-20230117150758667](/Users/mym/Library/Application Support/typora-user-images/image-20230117150758667.png)

# 底层原理

Docker是怎么工作的?

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

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

![image-20230117151816826](/Users/mym/Library/Application Support/typora-user-images/image-20230117151816826.png)

Docker为什么比VM快?

  1. Docker有着比虚拟机更少的抽象层
  2. Docker利用的宿主机的内核,VM需要是 Guest OS

img

img

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

img

之后学习完毕所有的命令, 再回头来看这段理论,就会很清晰!

# Docker的常用命令

# 帮助命令

docker version 		# 显示docker的版本信息
docker info    		# 显示更详细信息
docker 命令 --help # 万能命令
1
2
3

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

# 镜像命令

# docker images 查看镜像

[root@iZwz954642t91dbe6lc2l1Z ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    feb5d9fea6a5   16 months ago   13.3kB


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

# 可选项
	-a, --all    # 列出所有镜像
	-q, --quitet # 只显示镜像的id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# docker search 搜索镜像

[root@iZwz954642t91dbe6lc2l1Z jupiter-layout]# docker search mysql
NAME                            DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                           MySQL is a widely used, open-source relation…   13702     [OK]       
mariadb                         MariaDB Server is a high performing open sou…   5231      [OK]  

# 可选项,通过搜索来过滤
--fillter=STARS=3000 #大于3000
[root@iZwz954642t91dbe6lc2l1Z ~]# docker search mysql --filter=STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   13702     [OK]       
mariadb   MariaDB Server is a high performing open sou…   5231      [OK]   
1
2
3
4
5
6
7
8
9
10
11

# docker pull 下载镜像

# 下载镜像 docker pull 镜像名[:tag]
# 如果不写tag,默认就是latest
[root@iZwz954642t91dbe6lc2l1Z ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
72a69066d2fe: Already exists # 分层下载,docker image的核心 联合文件系统
93619dbc5b36: Already exists 
99da31dd6142: Already exists 
626033c43d70: Already exists 
37d5d7efb64e: Already exists 
ac563158d721: Already exists 
d2ba16033dad: Already exists 
688ba7d5c01a: Pull complete 
00e060b6d11d: Pull complete 
1c04857f594f: Pull complete 
4d7cfa90e6ea: Pull complete 
e0431212d27d: Pull complete 
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 # 签名
Status: Downloaded newer image for mysql:latest # 真实地址
docker.io/library/mysql:latest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# docker rmi 删除镜像

# 删除指定的容器
docker rmi -f 镜像id

# 删除多个容器
docker rmi -f 镜像id 镜像id 镜像id

# 删除全部容器
docker rmi -f $(docker images -aq)
1
2
3
4
5
6
7
8

# 容器命令

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

# docker run 新建容器并启动

docker run [可选参数] image

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

# 测试,启动并进入容器
[root@iZwz954642t91dbe6lc2l1Z ~]# docker run -it centos /bin/bash
[root@69f6b8df1e65 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# 从容器中退回主机
[root@69f6b8df1e65 /]#  exit
exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# docker ps 列出运行容器

# docker ps 命令

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

[root@iZwz954642t91dbe6lc2l1Z ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@iZwz954642t91dbe6lc2l1Z ~]# docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED         STATUS                      PORTS     NAMES
69f6b8df1e65   centos        "/bin/bash"   3 minutes ago   Exited (0) 44 seconds ago             relaxed_blackburn
93226653704e   hello-world   "/hello"      26 hours ago    Exited (0) 26 hours ago               musing_williams
1
2
3
4
5
6
7
8
9
10
11
12

# 退出容器

exit         # 直接容器停止并退出
Ctrl + P + Q # 容器不停止退出
1
2

# docker rm 删除容器

docker rm 容器id								 # 删除指定容器,不能删除长在运行的容器,如果要强制删除 rm -f
docker rm -f $(docker ps -aq)		# 删除所有的容器
docker ps -a -q|xargs	docker rm	# 删除所有的容器
1
2
3

# 启动和停止容器的操作

docker start 容器id		# 启动容器
docker restart 容器id # 重启容器
docker stop 容器id		# 停止当前正在进行的容器
docker kill 容器id    # 强制停止当前的容器
1
2
3
4

# 常用其他命令

# 后台启动容器

# 命令 docker run -d 镜像名
[root@iZwz954642t91dbe6lc2l1Z ~]# docker run -d centos
1
2

注意

docker ps,发现 centos停止了

常见的坑,docker容器使用后台运行,就必须要有一个前台进程,docker发现没有应用,就会自动停止

nginx,容器启动后,发现自己没有提供服务,就会立刻停止,就是没有程序了

# 查看日志

docker logs -f -t --tail 容器,没有日志

# 自己编写一段shell脚本
docker run -d centos /bin/sh -c "while true;do echo maimaimai;sleep 2;done"

# [root@iZwz954642t91dbe6lc2l1Z ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
415c046871f9   centos    "/bin/sh -c 'while t…"   7 seconds ago   Up 6 seconds             amazing_germain

# 显示日志
-tf
-- tail number # 要显示日志条数

[root@iZwz954642t91dbe6lc2l1Z ~]# docker logs -tf --tail 10 415c046871f9
2023-01-18T09:37:51.991608375Z maimaimai
2023-01-18T09:37:53.993733005Z maimaimai
2023-01-18T09:37:55.995781070Z maimaimai
2023-01-18T09:37:57.997893404Z maimaimai
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 查看容器中进程信息

# 命令docker top

[root@iZwz954642t91dbe6lc2l1Z ~]# docker top 415c046871f9
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                18316               18297               0                   17:35               ?                   00:00:00            
root                22394               18316               0                   17:39               ?                   00:00:00    
1
2
3
4
5
6

# 进入当前的

# 操作命令

# Docker镜像

# 容器数据卷

# Dockerfile

# Docker网络原理

# IDEA整合Docker

# Docker Compose

https://get.daocloud.io/

# Docker Swarm

# CI\CD Jenkins

编辑 (opens new window)
#Docker
上次更新: 2023/02/19, 00:03:59
Golang学习笔记
Consul学习笔记

← Golang学习笔记 Consul学习笔记→

最近更新
01
策略模式
03-13
02
单例模式
03-05
03
设计模式介绍
03-05
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Evan Xu | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式