Docker与微服务实战——基础篇

Docker与微服务实战——基础篇

  • 第一章 Docker 简介
    • 1.1 docker 理念
    • 1.2 容器与虚拟机比较
  • 第二章 Docker 安装
    • 2.1 前提说明
    • 2.2 Docker的基本组成
      • 2.2.1 镜像(image)
      • 2.2.2 容器(container)
      • 2.2.3 仓库(repository)
      • 2.2.4 小结
    • 2.3 Docker 工作原理
    • 2.4 Docker运行的基本流程
    • 2.5 CentOS 7 安装 Docker
    • 2.6 阿里云镜像加速
  • 第三章 Docker 常用命令
    • 3.1 帮助启动类命令
    • 3.2 镜像命令
      • 3.2.1 docker images——列出本地主机上的镜像
      • 3.2.2 docker search——搜索镜像
      • 3.2.3 docker pull——下载镜像
      • 3.2.4 docker system df ——查看镜像/容器/数据卷所占的空间
      • 3.2.5 docker rmi —— 删除镜像
    • 3.3 容器命令
      • 3.3.1 新建+启动容器
      • 3.3.2 列出当前所有正在运行的容器
      • 3.3.3 退出容器
      • 3.3.4 启动已停止运行的容器
      • 3.3.5 停止容器
      • 3.3.6 强制停止容器
      • 3.3.7 删除已停止的容器
      • 3.3.8 启动守护式容器(后台服务器)***
      • 3.3.9 查看容器日志
      • 3.3.10 查看容器内运行的进程
      • 3.3.11 进入正在运行的容器并以命令行交互
      • 3.3.12 从容器内拷贝文件到主机上
      • 3.3.13 导入和导出容器
  • 第四章 Docker 镜像
    • 4.1 镜像是什么
    • 4.2 分层的镜像
    • 4.3 UnionFS(联合文件系统)
    • 4.4 docker 镜像加载原理
    • 4.5 为什么 docker 镜像要采用这种分层结构
    • 4.6 Docker镜像commit操作命令
  • 第五章 本地镜像发布到阿里云
    • 5.1 将本地镜像推送到阿里云
    • 5.2 将阿里云上的镜像下载到本地
  • 第六章 本地镜像发布到私有库
  • 第七章 Docker容器数据卷
    • 7.1 容器数据卷的含义
    • 7.2 容器数据卷的功能
    • 7.3 数据卷的案例
      • 7.3.1 宿主和容器之间映射添加容器卷
      • 7.3.2 读写规则映射添加说明
      • 7.3.3 卷的继承和共享
  • 第八章 Docker常规安装简介
    • 8.1 安装tomcat
    • 8.2 安装mysql
    • 8.3 安装redis

第一章 Docker 简介

1.1 docker 理念

Docker是内核级虚拟化

Docker是基于Go语言实现的云开源项目。

Docker是一个C/S模式的架构,后端是一个松耦合架构,众多模块各司其职。

Docker解决了运行环境和配置问题软件容器,方便做持续集成并有助于整体发布的容器虚拟化技术。

1.2 容器与虚拟机比较

虚拟机(VM)Docker容器
操作系统宿主机OS上运行虚拟机OS与宿主机共享OS
存储大小镜像庞大(vmdk、vid等)镜像小,便于存储与传输
运行性能操作系统额外的CPU、内存消耗几乎无额外性能损失
移植性笨重,与虚拟化技术耦合度高轻便、灵活,适应于Linux
硬件亲和性面向硬件运维者面向软件开发者
部署速度较慢,10s以上快速,秒级

第二章 Docker 安装

2.1 前提说明

Docker 必须部署在Linux内核的系统上。

前提条件
目前,CentOS仅发行版本中的内核支持Docker。Docker运行在CentOS 7(64-bit),要求系统为64位、Linux系统内核版本为3.8以上,这里选用CentOS7.x

查看自己的内核
uname命令用于打印当前系统相关信息(内核版本号、硬件架构、主机名称和操作系统类型等)
在这里插入图片描述

2.2 Docker的基本组成

2.2.1 镜像(image)

Docker 镜像(image)就是一个只读的模板。镜像可以用来创建Docker容器,一个镜像可以创建很多容器

它也相当于是一个root文件系统。比如官方镜像 centos:7 就包括了完整的一套 centos:7最小系统的root文件系统。

相当于容器的“源代码”,docker镜像文件类似于Java的类模板,而docker容器实例类似于Java中new出来的实例对象

容器与镜像的关系类似于面向对象编程中的对象与类。

Docker面向对象
容器对象
镜像

2.2.2 容器(container)

1)从面向对象角度
Docker 利用容器(Container)独立运行的一个或一组应用,应用程序或服务运行在容器里面,容器就类似于一个虚拟化的环境,容器是用镜像创建的运行实例。就像是Java中的类和实例对象一样,镜像是静态的定义,容器是镜像运行时的实体。容器为镜像提供了一个标准的和隔离的运行环境,它可以被启动、开始、停止、删除。每个容器都是相互隔离的、保证安全的平台。

2)从镜像容器角度
可以把容器看做是一个简易版的Linux环境(包括root用户权限、进程空间、用户空间和网络空间等)和运行在其中的应用程序。

2.2.3 仓库(repository)

仓库(Repository)是集中存放镜像文件的场所。

类似于
Maven仓库,存放各种jar包的地方;
github仓库,存放各种git项目的地方;
Docker公司提供的官方registry被称为Docker Hub,存放各种镜像模板的地方。

仓库分为公开仓库(Public)和私有仓库(Private)两种形式。最大的公开仓库是Docker Hub(https://hub.docker.com/),存放了数量庞大的镜像供用户下载。国内的公开仓库包括阿里云、网易云等。

2.2.4 小结

Docker 本身是一个容器运行载体或称之为管理引擎。我们把应用程序和配置依赖打包好形成一个可交付的运行环境,这个打包好的运行环境就是image镜像文件。只有通过这个镜像文件才能生成Docker容器实例(类似java中new出来一个对象)

image文件可以看作是容器的模板。Docker 根据image文件生成容器的实例。同一个image 文件,可以生成多个同时运行的容器实例。

镜像文件: image文件生成的容器实例,本身也是一个文件,称为镜像文件。
容器实例: 一个容器运行一种服务,当我们需要的时候,就可以通过docker客户端创建一个对应的运行实例,也就是我们的容器
仓库: 就是放一堆镜像的地方,我们可以把镜像发布到仓库中,需要的时候再从仓库中拉下来就可以了。

2.3 Docker 工作原理

Docker是一个Client-Server结构的系统,Docker守护进程运行在主机上,然后通过Socket连接从客户端访问,守护进程从客户端接受命令并管理运行在主机上的容器。容器,是一个运行时环境。
在这里插入图片描述

2.4 Docker运行的基本流程

  1. 用户是使用 Docker Client(命令行窗口)与Docker Daemon(后台守护进程)建立通信,并发送请求给后者。
  2. Docker Daemon 作为 Docker 架构中的主体部分,首先提供 Docker Server 的功能使其可以接受 Docker Client 的请求。
  3. Docer Engine(引擎)执行 Docker 内部的一系列工作,每一项工作都是以一个Job的形式的存在。
  4. Job 的运行过程中,当需要容器镜像时,则从Docker Registry 中下载镜像,并通过镜像管理驱动 Graph driver 将下载镜像以Graph的形式存储。
  5. 当需要为Docker创建网络环境时,通过网络管理驱动 Network driver创建并配置 Docker 来完成。
  6. 当需要限制 Docker 容器运行资源或执行用户指令等操作时,则通过 Exec driver(执行驱动)来完成。
  7. Libcontainer是一项独立的容器管理包,Network driver以及Exec driver都是通过Libcontainer来实现具体对容器进行的操作。

2.5 CentOS 7 安装 Docker

安装地址:https://docs.docker.com/engine/install/centos

1)确定你是CentOS7及以上版本
在这里插入图片描述

2)卸载旧版本
在这里插入图片描述

3)yum 安装 gcc相关
首先要保证CentOS7能上外网,再执行以下操作:

[root@localhost ~]# yum -y install gcc
[root@localhost ~]# yum -y install gcc-c++

4)安装需要的软件包

[root@localhost ~]# yum -y install yum-utils

5)设置 stable 镜像仓库

[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

6)更新 yum 软件包索引

[root@localhost ~]# yum makecache fast

7)安装DOCKER CE

[root@localhost ~]# yum -y install docker-ce docker-ce-cli containerd.io

8)启动 docker

[root@localhost ~]# systemctl start docker
[root@localhost ~]# ps -ef | grep docker
root      83592      1  0 11:06 ?        00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
root      84314   2819  0 11:07 pts/0    00:00:00 grep --color=auto docker

9)测试

[root@localhost ~]# docker version 
Client: Docker Engine - Community
 Version:           24.0.7
 API version:       1.43
 Go version:        go1.20.10
 Git commit:        afdd53b
 Built:             Thu Oct 26 09:11:35 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.7
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.10
  Git commit:       311b9ff
  Built:            Thu Oct 26 09:10:36 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.24
  GitCommit:        61f9fd88f79f081d64d6fa3bb1a0dc71ec870523
 runc:
  Version:          1.1.9
  GitCommit:        v1.1.9-0-gccaecfc
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

[root@localhost ~]# docker run hello-world 
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete 
Digest: sha256:88ec0acaa3ec199d3b7eaf73588f4518c25f9d34f58ce9a0df68429c5af48e8d
Status: Downloaded newer image for hello-world:latest

Hello from Docker!     // 如果能看见这句话就代表安装成功

10)卸载

[root@localhost ~]# systemcl stop docker
[root@localhost ~]# yum remove docker-ce docker-ce-cli containerd.io
[root@localhost ~]# rm -rf /var/lib/docker
[root@localhost ~]# rm -rf /var/lib/containerd

2.6 阿里云镜像加速

https://promotion.aliyun.com/ntms/act/kubernetes.html

  1. 注册一个属于自己的阿里云账户(可复用淘宝账号)
  2. 登陆阿里云开发者平台
  3. 点击控制台
    在这里插入图片描述
    在这里插入图片描述
  4. 选择容器镜像服务
    在这里插入图片描述
  5. 获取加速器地址
    在这里插入图片描述
  6. 粘贴脚本直接执行
    在这里插入图片描述
[root@localhost ~]# mkdir -p /etc/docker
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://axlh76es.mirror.aliyuncs.com"]
> }
> EOF
[root@localhost ~]# systemctl daemon-reload 
[root@localhost ~]# systemctl restart docker
  1. 测试运行 hello-world
[root@localhost ~]# docker run hello-world

Hello from Docker!    // 能打印出这句话证明运行成功

第三章 Docker 常用命令

3.1 帮助启动类命令

1)启动docker:

[root@localhost ~]# systemctl start docker

2)停止docker:

[root@localhost ~]# systemctl stop docker

3)重启docker:

[root@localhost ~]# systemctl restart docker

4)查看docker状态:

[root@localhost ~]# systemctl status docker

5)开机启动:

[root@localhost ~]# systemctl enable docker

6)查看docker概要信息:

[root@localhost ~]# docker info 

7)查看docker总体帮助文档:

[root@localhost ~]# docker --help 

8)查看docker命令帮助文档:

[root@localhost ~]# docker cp --help

3.2 镜像命令

3.2.1 docker images——列出本地主机上的镜像

1)命令

docker images [参数]

2)显示说明

REPOSITORY:表示镜像的仓库源
TAG:镜像的标签版本号
IMAGE ID:镜像ID
CREATED:镜像创建时间
SIZE:镜像大小

3)参数说明

参数功能
-a列出本地所有的镜像(含历史映像层)
-q只显示镜像ID

4)案例实操

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    9c7a54a9a43c   5 months ago   13.3kB

3.2.2 docker search——搜索镜像

一般在dockerhub网站中搜索
1)命令

docker search [options] 镜像名字

2)显示说明

NAME:镜像名称
DESCRIPTION:镜像说明
STARS:点赞数量
OFFICIAL:是否是官方的
AUTOMATED:是否是自动构建的

3)参数说明

选项功能
- -limit只列出N个镜像,默认25个

4)案例实操

[root@localhost ~]# docker search redis
NAME                                DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED
redis                               Redis is an open source key-value store that…   12438     [OK]     

// 只显示用的最多的默认前五条命令
[root@localhost ~]# docker search --limit 5 redis

3.2.3 docker pull——下载镜像

1)命令

1. docker pull 镜像名字[:TAG]
2. docker pull 镜像名字

没有TAG就是最新版
等价于
docker pull 镜像名字:latest

2)案例实操

[root@localhost ~]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
7b1a6ab2e44d: Pull complete 
Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

3.2.4 docker system df ——查看镜像/容器/数据卷所占的空间

1)案例实操

[root@localhost ~]# docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          2         1         72.79MB   72.78MB (99%)
Containers      2         0         0B        0B
Local Volumes   0         0         0B        0B
Build Cache     0         0         0B        0B

3.2.5 docker rmi —— 删除镜像

1)命令

docker rmi [options] <某个XXX镜像名字ID>

docker rmi -f 镜像ID                    删除单个
docker rmi -f 镜像名1:TAG 镜像名2:TAG    删除多个
docker rmi -f $(docker images -qa)      删除全部

2)案例实操

删除单个
[root@localhost ~]# docker rmi -f 9c7a54a9a43c
删除多个
[root@localhost ~]# docker rmi -f ubuntu hello-world

3.3 容器命令

3.3.1 新建+启动容器

1)命令

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

2)参数说明

参数功能
–name=“容器新名字”为容器指定一个名称
-d后台运行容器并返回容器ID,也即启动守护式容器(后台运行)
-i以交互模式运行容器,通常与-t同时使用
-t为容器重新分配一个伪输入终端,通常与 -i 同时使用
也即启动交互式容器(前台有伪终端,等待交互)
-P随机端口映射,大写P
-p指定端口映射,小写p

参数说明:
-i:交互式操作。
-t:终端。

3)启动交互式容器(前台命令行)
在这里插入图片描述
4)指定容器名字
在这里插入图片描述

3.3.2 列出当前所有正在运行的容器

1)命令

docker ps [OPTIONS]

2)参数说明

参数功能
-a列出当前所有在正在运行的容器+历史上运行过的
-l显示最近创建的容器
-n显示最近n个创建的容器
-q静默模式,只显示容器编号

3)案例实操

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
9931fe1e7230   ubuntu    "/bin/bash"   5 minutes ago   Up 5 minutes             sleepy_feistel

[root@localhost ~]# docker ps -a

3.3.3 退出容器

exit             run进去容器,exit退出,容器停止
ctrl+p+q         run进去容器,ctrl+p+q退出,容器不停止

3.3.4 启动已停止运行的容器

1)命令

docker start 容器ID或者容器名

2)案例实操

[root@localhost ~]# docker ps -n 2
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS                         PORTS     NAMES
4e0b5de73965   ubuntu    "/bin/bash"   2 hours ago   Up 2 hours                               dazzling_wilson
722e5fcce992   ubuntu    "bash"        2 hours ago   Exited (0) About an hour ago             myu1
[root@localhost ~]# docker start 722e5fcce992
722e5fcce992
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED       STATUS         PORTS     NAMES
4e0b5de73965   ubuntu    "/bin/bash"   2 hours ago   Up 2 hours               dazzling_wilson
722e5fcce992   ubuntu    "bash"        2 hours ago   Up 4 seconds             myu1

3.3.5 停止容器

1)命令

docker stop 容器ID 或者容器名

3.3.6 强制停止容器

1)命令

docker kill 容器ID或者容器名

3.3.7 删除已停止的容器

1)命令

docker rm 容器ID

一次性删除多个容器实例
docker rm -f $(docker ps -a -q)
docker ps -a -q | xargs docker rm

3.3.8 启动守护式容器(后台服务器)***

在大部分的场景下,我们希望 docker 的服务是在后台运行的,我们可以通过-d 指定容器的后台运行模式。
1)命令

docker run -d 容器名

docker 容器后台运行,就必须有一个前台进程。容器运行的命令如果不是那些一直挂起的命令,就是会自动退出

2)redis 前后台启动演示
(1)前台交互式启动

[root@localhost ~]# docker run -it redis:6.0.8

(2)后台守护式启动

[root@localhost ~]# docker run -d redis:6.0.8

3.3.9 查看容器日志

1)命令

docker logs 容器ID

3.3.10 查看容器内运行的进程

1)命令

docker inspect 容器ID

3.3.11 进入正在运行的容器并以命令行交互

1)命令

docker exec -it 容器ID bin/bash
docker attach 容器ID(重新进入)

2)两个命令的区别
attach 直接进入容器启动命令的终端,不会启动新的进程
用exit退出,会导致容器的停止。

exec 是在容器中打开新的终端,并且可以启动新的进程
用exit退出,不会导致容器的停止。

推荐使用 docker exec命令,因为退出容器终端,不会导致容器的停止。

3.3.12 从容器内拷贝文件到主机上

1)命令

docker cp 容器ID:容器内路径 目的主机路径

3.3.13 导入和导出容器

1)命令

docker export 容器ID > 文件名.tar
cat 文件名.tar | docker import - 镜像用户/镜像名:镜像版本号

export 导出容器的内容流作为一个tar归档文件[对应 import 命令]

import 从tar 包中的内容创建一个新的文件系统再导入镜像[对应 export]

2)案例实操

[root@localhost ~]# docker cp 0176a29ea86d > a.tar
[root@localhost ~]# ls
a.tar
[root@localhost ~]# docker rm -f 0176a29ea86d
0176a29ea86d
[root@localhost ~]# cat a.tar | docker import - ubuntu/ubuntu:6.3
sha256:fc49059e80ad15fca9852d26ce0ef4ee8d628a0e855802fde4c5a970910784c1
[root@localhost ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED              SIZE
ubuntu/ubuntu   6.3       fc49059e80ad   About a minute ago   0B

第四章 Docker 镜像

4.1 镜像是什么

镜像是一种轻量级、可执行的独立软件包,它包含运行某个软件所需的所有内容,我们把应用程序和配置依赖打包好形成一个可交付的运行环境(包括代码、运行时需要的库、环境变量和配置文件等),这个打包好的运行环境就是 image 镜像文件。

只有通过这个镜像文件才能生成 docker容器实例

4.2 分层的镜像

在这里插入图片描述

4.3 UnionFS(联合文件系统)

UnionFS(联合文件系统):Union文件系统(UnionFS)是一种分层、轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem)。Union文件系统是 Docker 镜像的基础。镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。

特性:一次同时加载多个文件系统,但从外面看起来,只能看到一个文件系统,联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录

4.4 docker 镜像加载原理

docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统UnionFS。
boofts(boot system)主要包含bootloader和kernel,bootloader主要是引导加载kernel,linux刚启动会加载bootfs文件系统,在docker镜像的最底层是引导文件系统bootfs。这一层与我们典型的Linux/Unix系统是一样的,包含boot加载器和内核。当boot加载完成之后整个内核就都在内存中了,此时内存的使用权已由bootfs转交给内核,此时系统也会卸载bootfs。

rootfs(root system),在bootfs之上。包含的就是典型Linux系统中的/dev,/proc,/bin,/etx 等标准目录和文件。rootfs就是各种不同的操作系统发行版,比如Ubuntu,CentOS等等。
在这里插入图片描述

4.5 为什么 docker 镜像要采用这种分层结构

镜像分层最大的一个好处就是共享资源,方便复制迁移,就是为了复用。

比如说有多个镜像都从相同的 base 镜像构建而来,那么 Docker Host 只需在磁盘上保存一份 base 镜像;

同时内存中也只需加载一份 base 镜像,就可以为所有容器服务了。而且镜像的每一层都可以被共享。
Docker 镜像分层都是只读的,容器层是可写的
当容器启动时,一个新的可写层被加载到镜像的顶部。
这一层通常被称作“容器层”,“容器层”之下的都叫“镜像层”。

4.6 Docker镜像commit操作命令

docker commit 提交容器副本使之成为一个新的镜像

1)命令说明

docker commit -m="提交的描述信息"-a"作者"容器ID要创建的目标镜像名:[标签名]

2)案例演示ubuntu安装vim

(1)原始的默认ubuntu镜像是不带着vim命令的
[root@localhost ~]# docker run -it ubuntu
root@e6602cb72a30:/# vim a.txt
bash: vim: command not found

(2)外网连通的情况下,安装vim
# 先更新我们的包管理工具
root@e6602cb72a30:/# apt-get update   
# 然后安装我们需要的vim
root@e6602cb72a30:/# apt-get -y install vim

(3)安装完成后,commit我们自己的新镜像
[root@localhost ~]# docker commit -m="vim cmd add ok" -a="abc" e6602cb72a30 root/ubuntu:1.3
sha256:8b0e2010528a276b0fa0309b07d7bad3425b9e8c1fc5dc9e570c92791208dc80
[root@localhost ~]# docker images
REPOSITORY      TAG       IMAGE ID       CREATED              SIZE
root/ubuntu     1.3       8b0e2010528a   About a minute ago   162MB

(4)启动我们的新镜像并和原来的对比
[root@localhost ~]# docker run -it ubuntu /bin/bash
root@10209ad51630:/# vim a.txt
bash: vim: command not found
root@10209ad51630:/# exit
exit
[root@localhost ~]# docker run -it 8b0e2010528a /bin/bash
root@c98544b2ae0c:/# vim a.txt
root@c98544b2ae0c:/# cat a.txt
hello docker

docker中的镜像分层,支持通过扩展现有镜像,创建新的镜像。类似java继承于一个base基础类,自己再按需扩展。
新镜像是从 base 镜像一层一层叠加生成的。每安装一个软件,就在现有镜像的基础上增加一层。
在这里插入图片描述

第五章 本地镜像发布到阿里云

本地镜像发布到阿里云流程,如下图:
在这里插入图片描述

5.1 将本地镜像推送到阿里云

1)本地镜像素材原型
在这里插入图片描述
2)阿里云开发者平台
https://promotion.aliyun.com/ntms/act/kubernetes.html

3)创建仓库镜像
(1)选择控制台,进入容器镜像服务
(2)选择个人实例
(3)命名空间
在这里插入图片描述
(4)仓库名称
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
(5)进入管理界面获得脚本
在这里插入图片描述
在这里插入图片描述
4)将镜像推送到阿里云
将镜像推送到阿里云registry
(1)管理脚本界面
在这里插入图片描述
(2)脚本实例

[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED          SIZE
root/ubuntu   1.3       8b0e2010528a   44 minutes ago   162MB
tomcat        latest    fb5657adc892   22 months ago    680MB
ubuntu        latest    ba6acccedd29   2 years ago      72.8MB
hello-world   latest    feb5d9fea6a5   2 years ago      13.3kB
redis         6.0.8     16ecd2772934   3 years ago      104MB

[root@localhost ~]# docker login --username=aliyun5432219780 registry.cn-hangzhou.aliyuncs.com
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

[root@localhost ~]# docker tag 8b0e2010528a registry.cn-hangzhou.aliyuncs.com/aafd/myubuntu:1.3

[root@localhost ~]# docker push registry.cn-hangzhou.aliyuncs.com/aafd/myubuntu:1.3
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/aafd/myubuntu]
bfa7aa9aef0f: Pushed 
9f54eef41275: Pushed 
1.3: digest: sha256:e12d91342719d1593beeb7d76db23bd44cf17911db2ebb9b0e0503695fd0fae8 size: 741

[root@localhost ~]# docker images
REPOSITORY                                        TAG       IMAGE ID       CREATED          SIZE
registry.cn-hangzhou.aliyuncs.com/aafd/myubuntu   1.3       8b0e2010528a   54 minutes ago   162MB
root/ubuntu                                       1.3       8b0e2010528a   54 minutes ago   162MB
tomcat                                            latest    fb5657adc892   22 months ago    680MB
ubuntu                                            latest    ba6acccedd29   2 years ago      72.8MB
hello-world                                       latest    feb5d9fea6a5   2 years ago      13.3kB
redis                                             6.0.8     16ecd2772934   3 years ago      104MB

5.2 将阿里云上的镜像下载到本地

(1)登录阿里云docker registry
[root@localhost ~]# docker login --username=aliyun5432219780 registry.cn-hangzhou.aliyuncs.com
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

(2)从registry中拉取镜像
[root@localhost ~]# docker images
REPOSITORY                                        TAG       IMAGE ID       CREATED          SIZE
registry.cn-hangzhou.aliyuncs.com/aafd/myubuntu   1.3       8b0e2010528a   54 minutes ago   162MB
root/ubuntu                                       1.3       8b0e2010528a   54 minutes ago   162MB
tomcat                                            latest    fb5657adc892   22 months ago    680MB
ubuntu                                            latest    ba6acccedd29   2 years ago      72.8MB
hello-world                                       latest    feb5d9fea6a5   2 years ago      13.3kB
redis                                             6.0.8     16ecd2772934   3 years ago      104MB

[root@localhost ~]# docker rmi -f 8b0e2010528a
Untagged: root/ubuntu:1.3
Untagged: registry.cn-hangzhou.aliyuncs.com/aafd/myubuntu:1.3
Untagged: registry.cn-hangzhou.aliyuncs.com/aafd/myubuntu@sha256:e12d91342719d1593beeb7d76db23bd44cf17911db2ebb9b0e0503695fd0fae8
Deleted: sha256:8b0e2010528a276b0fa0309b07d7bad3425b9e8c1fc5dc9e570c92791208dc80
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
tomcat        latest    fb5657adc892   22 months ago   680MB
ubuntu        latest    ba6acccedd29   2 years ago     72.8MB
hello-world   latest    feb5d9fea6a5   2 years ago     13.3kB
redis         6.0.8     16ecd2772934   3 years ago     104MB

[root@localhost ~]# docker pull registry.cn-hangzhou.aliyuncs.com/aafd/myubuntu:1.3
1.3: Pulling from aafd/myubuntu
7b1a6ab2e44d: Already exists 
09ba0e50a073: Already exists 
Digest: sha256:e12d91342719d1593beeb7d76db23bd44cf17911db2ebb9b0e0503695fd0fae8
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/aafd/myubuntu:1.3
registry.cn-hangzhou.aliyuncs.com/aafd/myubuntu:1.3
[root@localhost ~]# docker images
REPOSITORY                                        TAG       IMAGE ID       CREATED          SIZE
registry.cn-hangzhou.aliyuncs.com/aafd/myubuntu   1.3       8b0e2010528a   57 minutes ago   162MB
tomcat                                            latest    fb5657adc892   22 months ago    680MB
ubuntu                                            latest    ba6acccedd29   2 years ago      72.8MB
hello-world                                       latest    feb5d9fea6a5   2 years ago      13.3kB
redis                                             6.0.8     16ecd2772934   3 years ago      104MB

[root@localhost ~]# docker run -it 8b0e2010528a /bin/bash
root@0d8aeb3b883a:/# vim a.txt
root@0d8aeb3b883a:/# exit
exit

第六章 本地镜像发布到私有库

docker registry是官方提供的工具,可以用于构建私有镜像仓库。
1)下载镜像docker registry

[root@localhost ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
79e9f2f55bf5: Pull complete 
0d96da54f60b: Pull complete 
5b27040df4a2: Pull complete 
e2ead8259a04: Pull complete 
3790aef225b9: Pull complete 
Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
registry      latest    b8604a3fe854   23 months ago   26.2MB

2)运行私有库registry,相当于本地有个私有docker hub

[root@localhost ~]# docker run -d -p 5000:5000 registry
fd0fa916bf1ff9677fd60f3e85e44cb319bdb43aa1e03d0936361c74edc2cdb4
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                   CREATED         STATUS         PORTS                                       NAMES
fd0fa916bf1f   registry   "/entrypoint.sh /etc…"   8 seconds ago   Up 7 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   cranky_archimedes

3)案例演示创建一个新镜像,ubuntu安装ifconfig命令

(1)原始的ubuntu镜像是不带着ifconfig命令的
[root@localhost ~]# docker run -it ubuntu /bin/bash
root@6f49d88a0556:/# ifconfig
bash: ifconfig: command not found

(2)外网连通的情况下,安装ifconfig命令并测试通过
root@6f49d88a0556:/# apt-get update
root@6f49d88a0556:/# apt-get install net-tools
root@6f49d88a0556:/# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 1995  bytes 21100770 (21.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1393  bytes 78212 (78.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

(3)安装完成后,commit我们自己的新镜像
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                   CREATED         STATUS         PORTS                                       NAMES
6f49d88a0556   ubuntu     "/bin/bash"               6 minutes ago   Up 6 minutes                                               pensive_turing
fd0fa916bf1f   registry   "/entrypoint.sh /etc…"   8 minutes ago   Up 8 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   cranky_archimedes
[root@localhost ~]# docker commit -m="ifconfig cmd add" -a="ahx" 6f49d88a0556 myubuntu:1.2
sha256:b35d6629da55eb92acc004e17bba9b8563108c55dbd1737941214e7deb28cef0
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
myubuntu      1.2       b35d6629da55   5 seconds ago   108MB
registry      latest    b8604a3fe854   24 months ago   26.2MB
ubuntu        latest    ba6acccedd29   2 years ago     72.8MB
hello-world   latest    feb5d9fea6a5   2 years ago     13.3kB
redis         6.0.8     16ecd2772934   3 years ago     104MB

(4)启动我们的新镜像并和原来的对比
[root@localhost ~]# docker stop 6f49d88a0556
6f49d88a0556
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED              SIZE
myubuntu      1.2       b35d6629da55   About a minute ago   108MB
registry      latest    b8604a3fe854   24 months ago        26.2MB
ubuntu        latest    ba6acccedd29   2 years ago          72.8MB
hello-world   latest    feb5d9fea6a5   2 years ago          13.3kB
redis         6.0.8     16ecd2772934   3 years ago          104MB
[root@localhost ~]# docker run -it b35d6629da55 /bin/bash
root@331eb9b604a9:/# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 6  bytes 508 (508.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@331eb9b604a9:/# exit
exit

4)curl验证私服库上有什么镜像

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                   CREATED        STATUS        PORTS                                       NAMES
fd0fa916bf1f   registry   "/entrypoint.sh /etc…"   25 hours ago   Up 25 hours   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   cranky_archimedes
[root@localhost ~]# curl -XGET http://192.168.47.199:5000/v2/_catalog
{"repositories":[]}

5)将新镜像myubuntu:1.2修改符合私服规范的Tag

[root@localhost ~]# docker tag myubuntu:1.2 192.168.47.199:5000/myubuntu:1.2
[root@localhost ~]# docker images
REPOSITORY                     TAG       IMAGE ID       CREATED         SIZE
192.168.47.199:5000/myubuntu   1.2       b35d6629da55   25 hours ago    108MB
myubuntu                       1.2       b35d6629da55   25 hours ago    108MB

6)修改配置文件使之支持http
2个配置中间有个逗号‘,’别漏了,这个配置是json格式的。

[root@localhost ~]# vim /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://axlh76es.mirror.aliyuncs.com"],
  "insecure-registries":["192.168.47.199:5000"]
}
修改完后如果不生效,建议重启docker服务。
[root@localhost ~]# systemctl restart docker

重启成功之后,再运行一下私有库
[root@localhost ~]# docker run -d -p 5000:5000 registry
a82ffb8bee06ad2b63fb615e1a55c2bb8e1776c44573a64be3ff63f6b1472938

7)push推送到私服库

[root@localhost ~]# docker push 192.168.47.199:5000/myubuntu:1.2
The push refers to repository [192.168.47.199:5000/myubuntu]
c97b0ab9299d: Pushed 
9f54eef41275: Pushed 
1.2: digest: sha256:7a01caa8f3614e982bd2571866ec100eee2b265c9ed579fb66042dfbc0076d11 size: 741

8)curl验证私服库上有什么镜像

[root@localhost ~]# curl -XGET http://192.168.47.199:5000/v2/_catalog
{"repositories":["myubuntu"]}

9)pull到本地并运行

[root@localhost ~]# docker rmi -f 192.168.47.199:5000/myubuntu:1.2
[root@localhost ~]# docker rmi -f myubuntu:1.2
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
registry      latest    b8604a3fe854   24 months ago   26.2MB
ubuntu        latest    ba6acccedd29   2 years ago     72.8MB
hello-world   latest    feb5d9fea6a5   2 years ago     13.3kB
redis         6.0.8     16ecd2772934   3 years ago     104MB
[root@localhost ~]# curl -XGET http://192.168.47.199:5000/v2/_catalog
{"repositories":["myubuntu"]}
[root@localhost ~]# docker pull 192.168.47.199:5000/myubuntu:1.2
1.2: Pulling from myubuntu
7b1a6ab2e44d: Already exists 
e0855fb5fc2d: Already exists 
Digest: sha256:7a01caa8f3614e982bd2571866ec100eee2b265c9ed579fb66042dfbc0076d11
Status: Downloaded newer image for 192.168.47.199:5000/myubuntu:1.2
192.168.47.199:5000/myubuntu:1.2
[root@localhost ~]# docker images
REPOSITORY                     TAG       IMAGE ID       CREATED         SIZE
192.168.47.199:5000/myubuntu   1.2       b35d6629da55   26 hours ago    108MB
[root@localhost ~]#  docker run -it 192.168.47.199:5000/myubuntu:1.2 /bin/bash
root@90b582e9b77a:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.3  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:03  txqueuelen 0  (Ethernet)
        RX packets 7  bytes 578 (578.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

第七章 Docker容器数据卷

7.1 容器数据卷的含义

卷就是目录或文件,存在于一个或多个容器中,有docker挂载到容器,但不属于联合文件系统,因此能够绕过Union File System提供一些用于持续存储或共享数据的特性。
卷的设计目的就是数据的持久化,完全独立于容器的生存周期,因此Docker不会在容器删除时删除其挂载的数据卷。

将docker容器内的数据保存进宿主机的磁盘中,以达到数据持久化和敏感重要的数据备份。

1)命令

--privileged=true

Docker挂载主机目录访问如果出现cannot open directory .:Permission denied
解决方法:在挂载目录后多加一个–privileged=true参数即可。

2)参数说明

参数功能
-v添加自定义的容器卷

3)运行一个带有容器卷存储功能的容器实例

docker run -it --privileged=true -v /宿主机绝对路径目录:/容器内目录  镜像名

7.2 容器数据卷的功能

将运用与运行的环境打包镜像,run后形成容器实例运行,但是我们对数据的要求希望是持久化的

特点:

  1. 数据卷可在容器之间共享或重用数据
  2. 卷中的更改可以直接实时生效
  3. 数据卷中的更改不会包含在镜像的更新中
  4. 数据卷的生命周期一直持续到没有容器使用它为止

7.3 数据卷的案例

7.3.1 宿主和容器之间映射添加容器卷

1)命令

docker run -it --privileged=true -v /宿主机绝对路径目录:/容器内目录  镜像名
# 将docker容器内的数据保存进宿主机的磁盘中
[root@localhost ~]# docker run -it --privileged=true -v /tmp/host_data:/tmp/docker_data --name=u1 ubuntu      //这里的host_data和docker_data目录是自动创建的
root@037178ca7174:/# cd /tmp/docker_data/
root@037178ca7174:/tmp/docker_data# ls
root@037178ca7174:/tmp/docker_data# touch dockerin.txt
root@037178ca7174:/tmp/docker_data# ls
dockerin.txt

# 在主机上去查看,并创建一个新的文件
[root@localhost ~]# cd /tmp/host_data/
[root@localhost host_data]# ls
dockerin.txt
[root@localhost host_data]# touch hostin.txt

# docker上查看是否有新建的文件
root@037178ca7174:/tmp/docker_data# ls
dockerin.txt  hostin.txt

2)查看数据卷是否挂载成功

[root@localhost ~]# docker inspect 037178ca7174
......
        "Mounts": [
            {
                "Type": "bind",
                "Source": "/tmp/host_data",
                "Destination": "/tmp/docker_data",
                "Mode": "",
                "RW": true,
                "Propagation": "rprivate"
            }
        ],
.......

3)容器和宿主机之间数据共享

  1. docker修改,主机同步获得
  2. 主机修改,docker同步获得
  3. docker容器stop,主机修改,docker容器重启看数据是否同步。
[root@localhost host_data]# touch c.txt

[root@localhost ~]# docker stop 037178ca7174
037178ca7174
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker start 037178ca7174
037178ca7174
[root@localhost ~]# docker exec -it 037178ca7174 /bin/bash
root@037178ca7174:/# cd /tmp/docker_data/
root@037178ca7174:/tmp/docker_data# ls
c.txt  dockerin.txt  hostin.txt

7.3.2 读写规则映射添加说明

1)读写(默认)

docker run -it --privileged=true -v /宿主机绝对路径目录:/容器内目录:rw  镜像名
默认同上案例,默认就是rw

2)只读

容器实例内部被限制,只能读取不能写
docker run -it --privileged=true -v /宿主机绝对路径目录:/容器内目录:ro  镜像名
[root@localhost ~]# docker run -it --privileged=true -v /mydocker/u:/tmp/u:ro --name u2 ubuntu
root@b4ecac69f23a:/# cd /tmp/u/
root@b4ecac69f23a:/tmp/u# ls
root@b4ecac69f23a:/tmp/u# pwd
/tmp/u

# 在主机的/mydocker/u/目录下创建a.txt并写入内容
[root@localhost ~]# cd /mydocker/u/
[root@localhost u]# touch a.txt
[root@localhost u]# vim a.txt 

# 在u2容器实例内部去查看
root@b4ecac69f23a:/tmp/u# ls
a.txt
root@b4ecac69f23a:/tmp/u# cat a.txt 
dabs
cdb

# 容器自己只能读取不能写
root@b4ecac69f23a:/tmp/u# touch b.txt
touch: cannot touch 'b.txt': Read-only file system
# 如果宿主机写入内容,可以同步给容器内,容器可以读取到。

7.3.3 卷的继承和共享

1)容器1完成和宿主机的映射

# 容器实例
[root@localhost ~]# docker run -it --privileged=true -v /mydocker/u:/tmp/u --name u1 ubuntu /bin/bash
root@cd788caa15dc:/# cd /tmp/u
root@cd788caa15dc:/tmp/u# ll
total 4
drwxr-xr-x. 2 root root 19 Nov  4 08:36 ./
drwxrwxrwt. 1 root root 15 Nov  4 08:44 ../
-rw-r--r--. 1 root root  9 Nov  4 08:36 a.txt
root@cd788caa15dc:/tmp/u# touch u1data.txt
root@cd788caa15dc:/tmp/u# ls
u1data.txt

# 主机
[root@localhost u]# touch host.txt

# 容器实例
root@cd788caa15dc:/tmp/u# ls
host.txt  u1data.txt

2)容器2继承容器1的卷规则

[root@localhost ~]# docker run -it --privileged=true --volumes-from u1 --name u3 ubuntu
root@2ba4fdfc72c4:/# cd /tmp/u/
root@2ba4fdfc72c4:/tmp/u# ll
total 0
drwxr-xr-x. 2 root root 40 Nov  4 08:45 ./
drwxrwxrwt. 1 root root 15 Nov  4 08:49 ../
-rw-r--r--. 1 root root  0 Nov  4 08:45 host.txt
-rw-r--r--. 1 root root  0 Nov  4 08:44 u1data.txt
root@2ba4fdfc72c4:/tmp/u# touch u2data.txt
root@2ba4fdfc72c4:/tmp/u# ls
host.txt  u1data.txt  u2data.txt
root@2ba4fdfc72c4:/tmp/u# ll
total 0
drwxr-xr-x. 2 root root 58 Nov  4 08:50 ./
drwxrwxrwt. 1 root root 15 Nov  4 08:49 ../
-rw-r--r--. 1 root root  0 Nov  4 08:45 host.txt
-rw-r--r--. 1 root root  0 Nov  4 08:44 u1data.txt
-rw-r--r--. 1 root root  0 Nov  4 08:50 u2data.txt

第八章 Docker常规安装简介

总体步骤:搜索镜像→拉取镜像→查看镜像→启动镜像(服务端口映射)→停止容器→移除容器

8.1 安装tomcat

1)docker hub上面查找tomcat镜像
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2)从docker hub上拉取tomcat镜像到本地
在这里插入图片描述
3)docker images查看是否有拉取到的tomcat
在这里插入图片描述
4)使用tomcat镜像创建容器实例(也叫运行镜像)

[root@localhost ~]# docker run -d -p 8080:8080 --name ti tomcat
712f1fd1edc9bbddad1db1e23f9eb7f3b9b6c6ee4a6e86da41527a58482a08dc

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS                                       NAMES
712f1fd1edc9   tomcat    "catalina.sh run"   33 seconds ago   Up 32 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   ti

5)访问猫首页

[root@localhost ~]# docker exec -it 712f1fd1edc9 /bin/bash
root@712f1fd1edc9:/usr/local/tomcat# ls -l
total 132
-rw-r--r--. 1 root root 18994 Dec  2  2021 BUILDING.txt
-rw-r--r--. 1 root root  6210 Dec  2  2021 CONTRIBUTING.md
-rw-r--r--. 1 root root 60269 Dec  2  2021 LICENSE
-rw-r--r--. 1 root root  2333 Dec  2  2021 NOTICE
-rw-r--r--. 1 root root  3378 Dec  2  2021 README.md
-rw-r--r--. 1 root root  6905 Dec  2  2021 RELEASE-NOTES
-rw-r--r--. 1 root root 16517 Dec  2  2021 RUNNING.txt
drwxr-xr-x. 2 root root  4096 Dec 22  2021 bin
drwxr-xr-x. 1 root root    22 Nov  4 09:29 conf
drwxr-xr-x. 2 root root  4096 Dec 22  2021 lib
drwxrwxrwx. 1 root root    80 Nov  4 09:29 logs
drwxr-xr-x. 2 root root   159 Dec 22  2021 native-jni-lib
drwxrwxrwx. 2 root root    30 Dec 22  2021 temp
drwxr-xr-x. 2 root root     6 Dec 22  2021 webapps
drwxr-xr-x. 7 root root    81 Dec  2  2021 webapps.dist
drwxrwxrwx. 2 root root     6 Dec  2  2021 work
root@712f1fd1edc9:/usr/local/tomcat# rm -r webapps
root@712f1fd1edc9:/usr/local/tomcat# ls -l
total 132
-rw-r--r--. 1 root root 18994 Dec  2  2021 BUILDING.txt
-rw-r--r--. 1 root root  6210 Dec  2  2021 CONTRIBUTING.md
-rw-r--r--. 1 root root 60269 Dec  2  2021 LICENSE
-rw-r--r--. 1 root root  2333 Dec  2  2021 NOTICE
-rw-r--r--. 1 root root  3378 Dec  2  2021 README.md
-rw-r--r--. 1 root root  6905 Dec  2  2021 RELEASE-NOTES
-rw-r--r--. 1 root root 16517 Dec  2  2021 RUNNING.txt
drwxr-xr-x. 2 root root  4096 Dec 22  2021 bin
drwxr-xr-x. 1 root root    22 Nov  4 09:29 conf
drwxr-xr-x. 2 root root  4096 Dec 22  2021 lib
drwxrwxrwx. 1 root root    80 Nov  4 09:29 logs
drwxr-xr-x. 2 root root   159 Dec 22  2021 native-jni-lib
drwxrwxrwx. 2 root root    30 Dec 22  2021 temp
drwxr-xr-x. 7 root root    81 Dec  2  2021 webapps.dist
drwxrwxrwx. 2 root root     6 Dec  2  2021 work
root@712f1fd1edc9:/usr/local/tomcat# mv webapps.dist webapps
root@712f1fd1edc9:/usr/local/tomcat# ls -l
total 132
-rw-r--r--. 1 root root 18994 Dec  2  2021 BUILDING.txt
-rw-r--r--. 1 root root  6210 Dec  2  2021 CONTRIBUTING.md
-rw-r--r--. 1 root root 60269 Dec  2  2021 LICENSE
-rw-r--r--. 1 root root  2333 Dec  2  2021 NOTICE
-rw-r--r--. 1 root root  3378 Dec  2  2021 README.md
-rw-r--r--. 1 root root  6905 Dec  2  2021 RELEASE-NOTES
-rw-r--r--. 1 root root 16517 Dec  2  2021 RUNNING.txt
drwxr-xr-x. 2 root root  4096 Dec 22  2021 bin
drwxr-xr-x. 1 root root    22 Nov  4 09:29 conf
drwxr-xr-x. 2 root root  4096 Dec 22  2021 lib
drwxrwxrwx. 1 root root    80 Nov  4 09:29 logs
drwxr-xr-x. 2 root root   159 Dec 22  2021 native-jni-lib
drwxrwxrwx. 2 root root    30 Dec 22  2021 temp
drwxr-xr-x. 7 root root    81 Dec  2  2021 webapps
drwxrwxrwx. 1 root root    22 Nov  4 09:35 work

在这里插入图片描述
6)免修改版说明

[root@localhost ~]# docker run -d -p 8080:8080 --name mytomcat8 billygoo/tomcat8-jdk8
Unable to find image 'billygoo/tomcat8-jdk8:latest' locally
latest: Pulling from billygoo/tomcat8-jdk8
55cbf04beb70: Pull complete 
1607093a898c: Pull complete 
9a8ea045c926: Pull complete 
1290813abd9d: Pull complete 
8a6b982ad6d7: Pull complete 
abb029e68402: Pull complete 
8cd067dc06dc: Pull complete 
1b9ce2097b98: Pull complete 
d6db5874b692: Pull complete 
25b4aa3d52c5: Pull complete 
d26b86f009c9: Pull complete 
e54998e5e699: Pull complete 
4a1e415a3c2e: Pull complete 
Digest: sha256:4e21f52d29e3a0baafc18979da2f9725449b54652db69d4cdaef9ba807097e11
Status: Downloaded newer image for billygoo/tomcat8-jdk8:latest
9e448debe4178455e46efe60300c866476907b31a91804e9c28d005952c453f4

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                   COMMAND             CREATED          STATUS          PORTS                                       NAMES
9e448debe417   billygoo/tomcat8-jdk8   "catalina.sh run"   32 seconds ago   Up 30 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   mytomcat8

在这里插入图片描述

8.2 安装mysql

1)docker hub上面查找mysql镜像
在这里插入图片描述
在这里插入图片描述
2)从docker hub上(阿里云加速器)拉取mysql镜像到本地标签为5.7
在这里插入图片描述
3)使用mysql5.7镜像创建容器(也叫运行镜像)
(1)简单版

[root@localhost ~]# docker images mysql:5.7 
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
mysql        5.7       c20987f18b13   22 months ago   448MB

# 确保linux服务器上没有mysql端口被启用,以免被占用
[root@localhost ~]# ps -ef |grep mysql
root      71326   3018  0 14:54 pts/0    00:00:00 grep --color=auto mysql

# 使用mysql镜像
[root@localhost ~]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 
46b5cc68b471e280d3ea10d52034b4f19e282ee0237b6ef69991c571ce1bc2b8
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                   CREATED         STATUS        PORTS                                                  NAMES
46b5cc68b471   mysql:5.7   "docker-entrypoint.s…"   2 seconds ago   Up 1 second   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   modest_hamilton
[root@localhost ~]# docker exec -it 46b5cc68b471 /bin/bash
root@46b5cc68b471:/# mysql -uroot -p  
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

# 建库建表插数据
mysql> create database db01;
Query OK, 1 row affected (0.00 sec)

mysql> use db01;
Database changed
mysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(1,'xx');
Query OK, 1 row affected (0.01 sec)

mysql> select * from t1;
+------+------+
| id   | name |
+------+------+
|    1 | xx   |
+------+------+
1 row in set (0.00 sec)

(2)实战版

# 新建mysql容器实例
[root@localhost ~]# docker run -d -p 3306:3306 --privileged=true -v /mydocker/mysql/log:/var/log/mysql -v /mydocker/mysql/data:/var/lib/mysql -v /mydocker/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7 
2f65e6007e58d5fc61a89a7581c5c3b5a662c360eb406c3b4de4b1c0a1768d0f
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                   CREATED         STATUS         PORTS                                                  NAMES
2f65e6007e58   mysql:5.7   "docker-entrypoint.s…"   6 seconds ago   Up 5 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

# 新建my.cnf
[root@localhost ~]# cd /mydocker/mysql/conf/
[root@localhost conf]# ls
[root@localhost conf]# vim my.cnf
[root@localhost conf]# cat my.cnf 
[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8

# 重新启动mysql容器实例再重新进入并查看字符编码
[root@localhost ~]# docker restart mysql
mysql
[root@localhost ~]# docker exec -it mysql /bin/bash
root@2f65e6007e58:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW VARIABLES LIKE 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

# 再新建库新建表再插入中文测试
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database db01;
Query OK, 1 row affected (0.00 sec)

mysql> use db01;
Database changed
mysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 values(1,'sson');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+------+------+
| id   | name |
+------+------+
|    1 | sson |
+------+------+
1 row in set (0.00 sec)

3)结论:
docker安装完mysql并run出容器后,建议请先修改完字符集编码后再新建mysql库-表-插数据。

8.3 安装redis

(1)从docker hub上(阿里云加速器)拉取redis镜像到本地标签为6.0.8
[root@localhost ~]# docker pull redis:6.0.8
6.0.8: Pulling from library/redis
Digest: sha256:21db12e5ab3cc343e9376d655e8eabbdbe5516801373e95a8a9e66010c5b8819
Status: Image is up to date for redis:6.0.8
docker.io/library/redis:6.0.8
[root@localhost ~]# docker images redis:6.0.8 
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
redis        6.0.8     16ecd2772934   3 years ago   104MB

(2)在centos宿主机下新建目录/app/redis
[root@localhost ~]# mkdir -p /app/redis

(3)先在宿主机上下载一个redis,再将redis.conf文件模板拷贝进/app/redis目录下
[root@localhost myrides]# wget https://download.redis.io/releases/redis-6.0.8.tar.gz
[root@localhost myrides]# tar -zxvf redis-6.0.8.tar.gz
[root@localhost myrides]# cd redis-6.0.8/
[root@localhost redis-6.0.8]# make
[root@localhost ~]# cp /myrides/redis-6.0.8/redis.conf  /app/redis/
[root@localhost ~]# cd /app/redis/
[root@localhost redis]# ll
总用量 84
drwxr-xr-x. 2 polkitd root     6 115 16:08 data
-rw-r--r--. 1 root    root 84642 115 16:15 redis.conf

(4)/app/redis目录下修改redis.conf文件
[root@localhost redis]# vim redis.conf 
// 允许redis外地连接,必须注释掉bind 127.0.0.1
# bind 127.0.0.1   
// 将daemonize yes注释起来或者daemonize no设置,因为该配置和docker run中-d参数冲突,会导致容器一直启动失败
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize no

(5)使用redis6.0.8镜像创建容器(也叫运行镜像)
[root@localhost redis]# docker run -d -p 6379:6379 --name myr2 --privileged=true -v /app/redis/redis.conf:/etc/redis/redis.conf -v /app/redis/data:/data -d redis:6.0.8 redis-server /etc/redis/redis.conf
1299c3a046e1ecb6837655ee856e77f6af2757e775acd6a5e2eddc03ade7ee5a
[root@localhost redis]# docker ps
CONTAINER ID   IMAGE         COMMAND                   CREATED         STATUS         PORTS                                       NAMES
1299c3a046e1   redis:6.0.8   "docker-entrypoint.s…"   4 seconds ago   Up 3 seconds   0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   myr2

(6)测试redis-cli连接上来
[root@localhost redis]# docker exec -it myr2 /bin/bash
root@1299c3a046e1:/data# redis-cli 
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"

(7)请证明docker启动使用了我们自己指定的配置文件
// 修改配置文件前
[root@localhost redis]# docker exec -it myr2 /bin/bash
root@1299c3a046e1:/data# redis-cli 
127.0.0.1:6379> set k1 v1
OK
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> select 15
OK
127.0.0.1:6379[15]> select 18
(error) ERR DB index is out of range
127.0.0.1:6379[15]> exit

(8)测试redis-cli连接上来第2次
// 修改配置文件后
[root@localhost redis]# vim redis.conf 
......
# dbid is a number between 0 and 'databases'-1
databases 10
......

// 重启服务
[root@localhost redis]# docker restart myr2
myr2

[root@localhost redis]# docker exec -it myr2 /bin/bash
root@1299c3a046e1:/data# redis-cli 
127.0.0.1:6379> get k1
"v1"
127.0.0.1:6379> select 3
OK
127.0.0.1:6379[3]> select 15
(error) ERR DB index is out of range

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/119873.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

php实现普通和定时跳转的几种方式

一、普通跳转 1、使用header函数&#xff1a;通过设置HTTP头部信息实现页面跳转。可以使用Location头部指定跳转的URL。例如&#xff1a; header("Location: http://www.example.com"); exit(); 2、使用JavaScript&#xff1a;可以使用JavaScript的window.location…

HCIA_数据链路层

如果数据进行封装时&#xff0c;基于E2或者802.3标准&#xff0c;此时我们称之为是一个以太网帧 1、EthernetII 采用EthernetII协议会在数据基础之上多出18Byte&#xff0c;EthernetII的数据长度是46-1500B FCS&#xff08;Frame check Sequence&#xff09;帧校验序列&#…

Linux安装nodejs问题

安装nodejs后&#xff0c;使用node -v报下图 参考下面两个可解决&#xff1a;【Linux-编译器gcc/glibc升级】CentOS7.9使用NodeJS18时报错/lib64/libm.so.6: version GLIBC_2.27‘ not found-CSDN博客 报错信息ImportError: /lib64/libstdc.so.6: version CXXABI_1.3.9‘ not f…

技术分享 | app自动化测试(Android)--元素定位方式与隐式等待

元素定位是 UI 自动化测试中最关键的一步&#xff0c;假如没有定位到元素&#xff0c;也就无法完成对页面的操作。那么在页面中如何定位到想要的元素&#xff0c;本小节讨论 Appium 元素定位方式。 Appium的元素定位方式 定位页面的元素有很多方式&#xff0c;比如可以通过 I…

C++day4

1.思维导图 2.设计一个Per类&#xff0c;类中包含私有成员:姓名、年龄、指针成员身高、体重&#xff0c;再设计一个Stu类&#xff0c;类中包含私有成员:成绩、Per类对象p1&#xff0c;设计这两个类的构造函数、析构函数和拷贝构造函数、拷贝赋值函数。 #include <iostream&…

mybatis在springboot当中的使用

1.当使用Mybatis实现数据访问时&#xff0c;主要&#xff1a; - 编写数据访问的抽象方法 - 配置抽象方法对应的SQL语句 关于抽象方法&#xff1a; - 必须定义在某个接口中&#xff0c;这样的接口通常使用Mapper作为名称的后缀&#xff0c;例如AdminMapper - Mybatis框架底…

2023年中国金融控股公司研究报告

第一章 行业概况 1.1 定义 金融控股公司这一术语最初源自美国&#xff0c;特别是在美国的《金融服务法案》关于银行控股公司组织结构的条文中&#xff0c;首次出现了“金融控股公司”&#xff08;Financial Holding Company&#xff09;这一法律术语&#xff0c;尽管法案中并…

使用Ruby编写通用爬虫程序

目录 一、引言 二、环境准备 三、爬虫程序设计 1. 抓取网页内容 2. 解析HTML内容 3. 提取特定信息 4. 数据存储 四、优化和扩展 五、结语 一、引言 网络爬虫是一种自动抓取互联网信息的程序。它们按照一定的规则和算法&#xff0c;遍历网页并提取所需的信息。使用Rub…

《安富莱嵌入式周报》第326期:航空航天级CANopen协议栈,开源USB PD电源和功耗分析,开源EtherCAT伺服驱动板,时序绘制软件,现代机器人设计

周报汇总地址&#xff1a;嵌入式周报 - uCOS & uCGUI & emWin & embOS & TouchGFX & ThreadX - 硬汉嵌入式论坛 - Powered by Discuz! 更新一期视频教程&#xff1a; BSP视频教程第28期&#xff1a;CANopen协议栈专题&#xff0c;CANopen主从机组网实战&a…

考研408-计算机网络 第一章-计算机网络体系结构学习笔记及习题

第一章 计算机网络体系结构 一 计算机网络概述 1.1 概念及功能 1.1.1 计算机网络的概念 计算机网络就是互连的、自治的计算机系统的集合 互连&#xff1a;通过通信链路互联互通 自治&#xff1a;各个节点之间无主从关系&#xff0c;高度自治的 1.1.2 计算机网络的功能 功…

【STM32】Systick定时器

一、STM32的5种定时器简介 1.独立看门狗&#xff08;IWDG&#xff09; VS 窗口看门狗&#xff08;WWDG&#xff09; 1.独立看门狗&#xff08;IWDG&#xff09; 独立看门狗&#xff1a;当没有到设定时间之前&#xff0c;给它喂了狗&#xff0c;就会回到初始值。 2.窗口看门狗…

【Linux】:初识git || centos下安装git || 创建本地仓库 || 配置本地仓库 || 认识工作区/暂存区(索引)以及版本库

&#x1f4ee;1.初识git Git 原理与使用 课程⽬标 • 技术⽬标:掌握Git企业级应⽤&#xff0c;深刻理解Git操作过程与操作原理&#xff0c;理解⼯作区&#xff0c;暂存区&#xff0c;版本库的含义 • 技术⽬标:掌握Git版本管理&#xff0c;⾃由进⾏版本回退、撤销、修改等Git操…

.NET Framework中自带的泛型委托Func

Func<>是.NET Framework中自带的泛型委托&#xff0c;可以接收一个或多个输入参数&#xff0c;并且有返回值&#xff0c;和Action类似&#xff0c;.NET基类库也提供了多达16个输入参数的Func委托&#xff0c;输出参数只有1个。 1、Func泛型委托 .NET Framework为我们提…

声音训练数据集哪里找?中文、英文

一般找数据集的都是需要训练底膜的&#xff0c;大家git上找的开源项目大多是预训练模型。预训练就是别人已经训练好的底膜&#xff0c;你在他的基础上进行调整。而我们训练如果他这个模型不理想是需要训练底膜的。 找的方式是从git开源上找 中文 推荐MockingBird&#xff0c;…

单链表的实现

单链表的实现 单链表的链表的概念及结构概念结构链表结构的分类链表常用的结构 无头单向不循环链表头文件 SList.h结构体 struct SListNode 源文件 SList.c创建结点 SLNode* SLBuyNode(SLDataType x)初始化链表 void SLInit(SLNode** pphead)链表尾部插入 void SLPushBack(SLNo…

【Qt之绘制兔纸】

效果 代码 class drawRabbit: public QWidget { public:drawRabbit(QWidget *parent nullptr) : QWidget(parent) {}private:void paintEvent(QPaintEvent *event) {QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing, true);// 绘制兔子的耳朵painter.s…

案例研究|腾讯音乐娱乐集团与JumpServer共探安全运维审计解决方案

近年来&#xff0c;得益于人民消费水平的提升以及版权意识的加强&#xff0c;用户付费意愿和在线用户数量持续增长&#xff0c;中国在线音乐市场呈现出稳定增长的发展态势。随着腾讯音乐于2018年12月上市&#xff0c;进一步推动了中国在线音乐市场的发展。 腾讯音乐娱乐集团&a…

用Rust和Scraper库编写图像爬虫的建议

本文提供一些有关如何使用Rust和Scraper库编写图像爬虫的一般建议&#xff1a; 1、首先&#xff0c;你需要安装Rust和Scraper库。你可以通过Rustup或Cargo来安装Rust&#xff0c;然后使用Cargo来安装Scraper库。 2、然后&#xff0c;你可以使用Scraper库的Crawler类来创建一个…

Config

因为我们微服务的模块太多了&#xff0c;这样每一个都有一个application.yml文件&#xff0c;假如说此时数据库的配置变了&#xff0c;这样一个个修改yml文件太麻烦了&#xff0c;所以我们想要一套集中式的&#xff0c;动态的配置管理设施是必不可少的。此时SpringCloud Config…

爬取Elastic Stack采集的Nginx内容

以下是一个简单的Go语言爬虫程序&#xff0c;用于爬取Elastic Stack采集的Nginx内容。请注意&#xff0c;这只是一个基本的示例&#xff0c;实际使用时可能需要根据具体情况进行修改和扩展。 package mainimport ("fmt""net/http""io/ioutil" )…
最新文章