【Docker】Docker 快速入门(精讲)
Docker的官方文档:https://docs.docker.com/
文章目录
Docker的官方文档:https://docs.docker.com/engine/install/centos/
一、Docker 简介
1、docker的概念
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包(环境)到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。
Docker将应用程序与该程序的依赖,打包在一个文件里面。运行这个文件,就会生成一个虚拟容器。程序在这个虚拟容器里运行,就好像在真实的物理机上运行一样。有了Docker,就不用担心环境问题。
总体来说,Docker的接口相当简单,用户可以方便地创建和使用容器,把自己的应用放入容器。容器还可以进行版本管理、复制、分享、修改,就像管理普通的代码一样。
Docker是开发人员和系统管理员使用容器开发、部署和运行应用程序的平台。使用Linux容器来部署应用程序称为集装箱化。使用docker轻松部署应用程序。
2、镜像(images)、容器(container)和仓库(DockerHub)
Docker中的三大名词:镜像(images)、容器(container)和仓库(DockerHub)
镜像: 通过运行镜像启动容器,一个镜像是一个可执行的包,其中包括运行应用程序所需要的所有内容-代码,运行时,库、环境变量和配置文件。
容器: 容器时镜像运行时示例-当被执行时(即,镜像状态,或者用户进程)在内存中,可以使用命令查看正在运行容器的列表docker ps,就像在Linux中一样。
仓库: DockerHub就相当于Maven的Repository,在Respository中存放是一个一个的jar包,而在DockerHub存放的是一个一个的镜像。
上面对于images(镜像)、容器的说法可能过于抽象,我们可以这么理解,你可以将images当做是java中的类,将容器当做是java中的对象,java中的一个类可以new出多个对象。在docker中,我们可以通过images(镜像)创建出多个容器。
3、容器与虚拟机的区别
一个容器中运行原生Linux和共享主机与其它容器的内核,它运行一个独立的进程,不占用任何其它可执行文件的内存,使其轻量化。
相比之下,虚拟机(VM)运行一个完整的“客户”操作系统,通过虚拟机管理程序虚拟访问主机资源。一般来说,虚拟机提供的环境比大多数应用程序需要的资源多。
从上图我可以看到,docker中的容器是共享Host OS 的,而VM存在独立的OS。每一个容器与VM的功能是相似的,但是没有VM那么笨重
二、Docker 安装
1、基本步骤
第一步:准备一个Linux,内核版本如下
[root@localhost ~]# uname -r
3.10.0-514.el7.x86_64
第二步:如果系统是没有安装 gcc 的,需要先安装 gcc
yum -y install gcc
yum -y install gcc-c++
第三步:卸载旧的docker
[root@localhost ~]# yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
第四步:安装yum-utils
[root@localhost ~]# yum install -y yum-utils
第五步:安装docker仓库
注意:我们使用国内阿里云的镜像,国外(官网)的太慢了
[root@localhost ~]# yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
第六步:更新 yum 软件包索引
[root@localhost ~]# yum makecache fast
第七步:安装 docker-ce Docker社区版
[root@localhost ~]# yum install docker-ce docker-ce-cli containerd.io
第八步:启动 Docker
[root@localhost ~]# systemctl start docker
第九步:测试 Docker 的安装,查看安装的 Docker 版本
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 20.10.8
API version: 1.41
Go version: go1.16.6
Git commit: 3967b7d
Built: Fri Jul 30 19:55:49 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
第十步:测试hello-world
[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally // 没有找到hello-world
latest: Pulling from library/hello-world // 从仓库中取拉取hello-world
2db29710123e: Pull complete // 拉取完成
Digest: sha256:9ade9cc2e26189a19c2e8854b9c8f1e14829b51c55a630ee675a5a9540ef6ccf
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/
[root@localhost ~]# docker run hello-world //再次执行hello-world
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/
执行的基本步骤入下:
第十一步:查看下载的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 8 days ago 13.3kB
// hello-world 就是我们新下载的镜像
2、配置阿里云镜像加速
我们已经配置了阿里云的镜像,docker的速度比之前要快不少,但是我们还可以通过配置镜像加速器使得docker的速度更快,步骤如下:
第一步:登录阿里云
第二步:找到配置镜像加速指令
第三步:执行镜像加速指令
[root@localhost ~]# sudo mkdir -p /etc/docker // 指令1
[root@localhost ~]# sudo tee /etc/docker/daemon.json <<-'EOF' // 指令2
> {
> "registry-mirrors": ["https://cxcbcm3k.mirror.aliyuncs.com"]
> }
> EOF
[root@localhost ~]# sudo systemctl daemon-reload // 指令3
[root@localhost ~]# sudo systemctl restart docker // 指令4
三、Docker 卸载
第一步:卸载Docker引擎
[root@localhost ~]# yum remove docker-ce docker-ce-cli containerd.io
第二步:删除Docker的相关目录
[root@localhost ~]# rm -rf /var/lib/docker
[root@localhost ~]# rm -rf /var/lib/containerd
四、Docker 的基本命令
1、帮助命令
命令:docker 命令 --help
[root@localhost ~]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
[root@localhost ~]#
2、镜像命令
命令1:查看所有的镜像
docker images
docker images -a
展示所有镜像
docker images -q
只展示镜像的ID
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 10 days ago 13.3kB
[root@localhost ~]#
REPOSITORY:镜像的仓库源
TAG:镜像的标签
IMAGE ID:镜像的ID
CREATED:镜像的创建时间
SIZE:镜像的大小
命令2:搜索镜像
docker search 镜像
docker search 镜像 --filter=starts=3000
// 简单搜索(默认搜索25条信息)
[root@localhost ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11498 [OK]
mariadb MariaDB Server is a high performing open sou… 4363 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 849 [OK]
percona Percona Server is a fork of the MySQL relati… 557 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 335 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 91
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 88
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 51
prom/mysqld-exporter 42 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 31 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 31
mysql/mysql-router MySQL Router provides transparent routing be… 22
centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
arey/mysql-client Run a MySQL client from a docker container 18 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 16 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
jelastic/mysql An image of the MySQL database server mainta… 2
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
centos/mysql-80-centos7 MySQL 8.0 SQL database server 1
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]
//搜索stars数在3000以上的image
[root@localhost ~]# docker search mysql --filter=stars=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 11498 [OK]
mariadb MariaDB Server is a high performing open sou… 4363 [OK]
[root@localhost ~]#
命令3:下载镜像
docker pull 镜像
:默认下载最新版本(latest)
docker pull 镜像:版本
:下载指定的版本
// 下载 mysql 8.0.19
[root@localhost ~]# docker pull mysql:8.0.19
8.0.19: Pulling from library/mysql // 版本
54fec2fa59d0: Pull complete // 分层下载
bcc6c6145912: Pull complete
951c3d959c9d: Pull complete
05de4d0e206e: Pull complete
319f0394ef42: Pull complete
d9185034607b: Pull complete
013a9c64dadc: Pull complete
96d4c3d31f9f: Pull complete
785bc90808da: Pull complete
1339cf094729: Pull complete
beb8f531cc37: Pull complete
2b40c9f6a918: Pull complete
// 签名信息(防伪)
Digest: sha256:9643e9fbd6330d10686f8922292dcb20995e7b792c17d4e94ddf95255f1d5449
Status: Downloaded newer image for mysql:8.0.19
docker.io/library/mysql:8.0.19 // 真是地址
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 10 days ago 13.3kB
mysql 8.0.19 0c27e8e5fcfa 17 months ago 546MB
所以docker pull mysql:8.0.19
<==> docker pull docker.io/library/mysql:8.0.19
命令4:删除镜像
删除指定的镜像
docker rmi -f 镜像ID
:删除镜像
// 删除前的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 10 days ago 13.3kB
mysql 8.0.19 0c27e8e5fcfa 17 months ago 546MB
// 通过ID进行删除
[root@localhost ~]# docker rmi -f 0c27e8e5fcfa
Untagged: mysql:8.0.19
Untagged: mysql@sha256:9643e9fbd6330d10686f8922292dcb20995e7b792c17d4e94ddf95255f1d5449
Deleted: sha256:0c27e8e5fcfab7805cfed996b55e5e98f43fd7ee76e1516f20cba139c6a299c5
Deleted: sha256:6bfe76815d91ca6b8738bfe4ca74f9b75c2f61f6b4b9c75b85043c29166a232a
Deleted: sha256:36f11e66d5dc29b48e0037579f1a06df6114e2c47bad5ba81dda9719118cce43
Deleted: sha256:f4843d87b0b8dde5a18f1518c401e0a039b67f414680d38b64023ef6323e6894
Deleted: sha256:933b70328a21badbb5c3d1e701f0653d40b7a6b01a20681fe4ad3a5b551334e3
Deleted: sha256:6b301e0572b3db7503e82da6f8e2aec9b3ec722c3e53263f234ef9e2fce91373
Deleted: sha256:af3a3fda5da43f7f03f8b3ca0330d120f9a7e5c0d9c5fc7b738ac45468a7de38
Deleted: sha256:86dceae8843638ef500e2a796a326ecb2ba1fd31cff817c4537afb67f04d6ff2
Deleted: sha256:2021ba03e116f319f5e8a90e3498d338ed213f1e036f59ffacc98266e7d3da6b
Deleted: sha256:3a3e1773b14d8b704f018b086292f46309cc27535f7484e0dfbf5a4308c81202
Deleted: sha256:372a8a077b4e29fa382990aeee3e38181481043fe145cb979ccd52588b4f36be
Deleted: sha256:821bf1f5688724dd59945682c34c0d33694af9c554cc1c3c69ca3aa9ba5d79ea
Deleted: sha256:c2adabaecedbda0af72b153c6499a0555f3a769d52370469d8f6bd6328af9b13
// 发现确实已经删除了
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 10 days ago 13.3kB
[root@localhost ~]#
批量删除镜像
我们先下载两个mysql
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest 2fe463762680 5 days ago 514MB
hello-world latest feb5d9fea6a5 10 days ago 13.3kB
mysql 8.0.19 0c27e8e5fcfa 17 months ago 546MB
[root@localhost ~]#
批量删除操作
docker rmi -f $(docker images -aq) :批量删除所有的images,$(docker images -aq)
查询到的ID作为删除时的参数。
[root@localhost ~]# docker rmi -f $(docker images -aq)
Untagged: mysql:latest
Untagged: ...
Untagged: hello-world:latest
Untagged: ...
Untagged: mysql:8.0.19
Untagged: ...
[root@localhost ~]#
镜像确实是被批量删除了
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost ~]#
3、容器命令
值得注意的是,我们有了进行才可以创建容器。
下载一个centos镜像作为测试使用
[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 weeks ago 231MB
[root@localhost ~]#
命令1:新建容器并启动
docker run [可选参数] 镜像
可选参数:
--name="名称" // 设置容器名
-d // 后台方式运行
-it // 使用交互方式运行,可以进入容器查看内容
-p //(小p), 指定容器端口
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用)
-p 容器端口
-P //(大P), 随机指定容器端口
// 新建并启动容器
[root@localhost ~]# docker run -it centos /bin/bash
[root@449bc6926d17 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@449bc6926d17 /]#
// 退出并停止容器
[root@449bc6926d17 /]# exit
exit
[root@localhost ~]# docker ps //查看正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
// 退出不停止容器(Ctrl + P + Q)
[root@d2c5642d2ab5 /]# [root@localhost ~]#
[root@localhost ~]# docker ps //查看正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2c5642d2ab5 centos "/bin/bash" 13 seconds ago Up 12 seconds nifty_kalam
[root@localhost ~]# docker attach d2c5642d2ab5 //再次进入交互模式的容器
[root@d2c5642d2ab5 /]# exit // 退出并关闭容器
exit
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
命令2:列出运行中的容器
docker ps
:查看正在运行的容器
docker ps -a
:查看运行容器的历史记录
docker ps -a -n=2
:查看最近运行的两个容器
docker ps -aq
:查看所有容器的ID
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
449bc6926d17 centos "/bin/bash" 4 minutes ago Exited (0) 2 minutes ago mycentos
d3a811f2bbdc feb5d9fea6a5 "/hello" 2 days ago Exited (0) 2 days ago busy_brahmagupta
6cd43b7bbb71 feb5d9fea6a5 "/hello" 2 days ago Exited (0) 2 days ago gifted_spence
[root@localhost ~]#
命令3:启动并停止容器
docker start 容器ID
:启动容器
docker restart 容器ID
:重启容器
docker stop 容器ID
:停止容器
docker kill 容器ID
:杀死容器
命令4:删除容器
docker rm 容器ID
:删除指定容器(不能删除正在运行的容器)
[root@localhost ~]# docker ps -aq // 查看所有的容器ID
d2c5642d2ab5
449bc6926d17
d3a811f2bbdc
6cd43b7bbb71
[root@localhost ~]# docker rm d2c5642d2ab5 // 删除指定的容器
d2c5642d2ab5
[root@localhost ~]# docker ps -aq // 查看所有的容器ID
449bc6926d17
d3a811f2bbdc
6cd43b7bbb71
[root@localhost ~]#
docker rm -f 容器ID
:强制删除指定容器
docker rm -f $(docker ps -aq)
:删除所有的容器, -aq:列出所有容器的ID
[root@localhost ~]# docker rm -f $(docker ps -aq)
449bc6926d17
d3a811f2bbdc
6cd43b7bbb71
[root@localhost ~]# docker ps -aq
[root@localhost ~]#
4、其他一些命令
命令1:后台启动容器
docker run -d centos
[root@localhost ~]# docker run -d centos
836d8e9432b4db173f4b28a45bb080bab887fd26bb358ab6a48685fd0a519ff5
[root@localhost ~]#
问题:docker ps 后发现并没有centos的进程
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]#
原因:docker容器使用后台进程,在容器中并没有任何应用(进程),所以是UN呢个通过docker ps 查看到docker的进程的。但是你是可以通过 docker ps -a 进行查看的。
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
836d8e9432b4 centos "/bin/bash" 21 seconds ago Exited (0) 19 seconds ago happy_sinoussi
解决:自定义一小段shell脚本,让容器中有东西在运行
docker run -d centos /bin/sh -c "while true;do echo studioustiger;sleep 2;done"
"while true;do echo studioustiger;sleep 2;done"
:每隔2秒打印一个studioustiger
【第一步】
// 停止容器
[root@localhost ~]# docker stop 836d8e9432b4
836d8e9432b4
// 删除容器
[root@localhost ~]# docker rm 836d8e9432b4
836d8e9432b4
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
【第二步】
// 重新启动一个容器(centos),编写一段shell脚本,使得centos中有东西在运行
[root@localhost ~]# docker run -d centos /bin/sh -c "while true;do echo studioustiger;sleep 2;done"
a39654c2f226c5efbd4f7c5f852757744a760adbb28cadd12ff132f7dd5f0fad
// 查看docker的进程
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a39654c2f226 centos "/bin/sh -c 'while t…" 8 seconds ago Up 8 seconds intelligent_jones
命令2:查询日志
docker logs -tf --tail number 容器ID
-tf
: 显示日志
--tail number
:显示日志尾部的几条日志
[root@localhost ~]# docker logs -tf --tail 5 a39654c2f226
2021-10-04T12:37:58.493751915Z studioustiger
2021-10-04T12:38:00.496582737Z studioustiger
2021-10-04T12:38:02.499201695Z studioustiger
2021-10-04T12:38:04.502634831Z studioustiger
2021-10-04T12:38:06.508148873Z studioustiger
^C
[root@localhost ~]#
命令3:查看docker容器内部的进程信息
docker top 容器ID
[root@localhost ~]# docker top a39654c2f226
UID PID PPID C STIME TTY TIME CMD
root 2716 2697 0 20:37 ? 00:00:00 /bin/sh -c while true;do echo studioustiger;sleep 2;done
root 3280 2716 0 20:53 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 2
命令4:查看容器中的元数据
docker inspect 容器ID
[root@localhost ~]# docker inspect [root@localhost ~]# docker top a39654c2f226
[
{
"Id": "a39654c2f226c5efbd4f7c5f852757744a760adbb28cadd12ff132f7dd5f0fad",
"Created": "2021-10-04T12:37:14.086138274Z",
"Path": "/bin/sh",
"Args": [
"-c",
"while true;do echo studioustiger;sleep 2;done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2716,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-10-04T12:37:14.38241984Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/a39654c2f226c5efbd4f7c5f852757744a760adbb28cadd12ff132f7dd5f0fad/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/a39654c2f226c5efbd4f7c5f852757744a760adbb28cadd12ff132f7dd5f0fad/hostname",
"HostsPath": "/var/lib/docker/containers/a39654c2f226c5efbd4f7c5f852757744a760adbb28cadd12ff132f7dd5f0fad/hosts",
"LogPath": "/var/lib/docker/containers/a39654c2f226c5efbd4f7c5f852757744a760adbb28cadd12ff132f7dd5f0fad/a39654c2f226c5efbd4f7c5f852757744a760adbb28cadd12ff132f7dd5f0fad-json.log",
"Name": "/intelligent_jones",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/b04d7c33dc2115e56de4045d570bbef080457ab86ca91d4a45229a3e0b31ba66-init/diff:/var/lib/docker/overlay2/70ba3860a37a3bd40525416617ca826eaab0d821c0d42b7dc9144f19256f2bdf/diff",
"MergedDir": "/var/lib/docker/overlay2/b04d7c33dc2115e56de4045d570bbef080457ab86ca91d4a45229a3e0b31ba66/merged",
"UpperDir": "/var/lib/docker/overlay2/b04d7c33dc2115e56de4045d570bbef080457ab86ca91d4a45229a3e0b31ba66/diff",
"WorkDir": "/var/lib/docker/overlay2/b04d7c33dc2115e56de4045d570bbef080457ab86ca91d4a45229a3e0b31ba66/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "a39654c2f226",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"while true;do echo studioustiger;sleep 2;done"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "4b221ce0adb2b6db8ae9e2a7df68ac6523e746e57e25f341c715a85cbc2f5f30",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/4b221ce0adb2",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "d2d17ee5553dbdb983e608ed3a4184d3acb395d68d1b54cd10daefe0ddde599f",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "ddf97db91f0c79191dd951cde8a84a8aabaec40795c5cebb32dfc740aa7c5857",
"EndpointID": "d2d17ee5553dbdb983e608ed3a4184d3acb395d68d1b54cd10daefe0ddde599f",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
命令5:进入当前正在运行的容器
方式1:进入容器后,开启一个新的终端
docker exec -it 容器ID bashShell
// 查找正在执行的容器的ID
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a39654c2f226 centos "/bin/sh -c 'while t…" 45 minutes ago Up 45 minutes intelligent_jones
// 进入容器
[root@localhost ~]# docker exec -it a39654c2f226 /bin/bash
[root@a39654c2f226 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@a39654c2f226 /]
方式2:进入当前正在执行的终端
docker attach 容器ID
// 因为我们之前写了一个shell脚本,所以一直在打印studioustiger
[root@localhost ~]# docker attach a39654c2f226
studioustiger
studioustiger
studioustiger
studioustiger
studioustiger
studioustiger
命令6:拷贝容器内的文件到本地
第一步:进入容器,并新建一个文件
[root@localhost ~]# docker exec -it a39654c2f226 /bin/bash // 进入容器
[root@a39654c2f226 /]# cd /home/
[root@a39654c2f226 home]# ls
[root@a39654c2f226 home]# touch tiger.jar // 新建一个文件夹
[root@a39654c2f226 home]# ls
tiger.jar
第二步:退出容器,并拷贝容器内的文件到本地
[root@a39654c2f226 home]# exit // 退出
exit
// 将容器中的tiger.jar文件拷贝到本地的home目录
[root@localhost ~]# docker cp a39654c2f226:/home/tiger.jar /home
[root@localhost ~]# cd /home/
[root@localhost home]# ls // 拷贝成功
tiger.jar
这个的拷贝是手动操作的,后面我们会使用 -v 卷技术,实现自动备份(将容器的home和本地的home打通)
五、Docker 中安装软件
dockerhub官网:https://hub.docker.com/
1、安装 Centos
搜索Centos的版本
安装
// 安装
[root@localhost home]# docker pull centos:centos7
centos7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
Status: Downloaded newer image for centos:centos7
docker.io/library/centos:centos7
//查看镜像
[root@localhost home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos centos7 eeb6ee3f44bd 2 weeks ago 204MB
运行
-it:表示进入交互模式
–name:给容器气的名字
// 启动
[root@localhost home]# docker run -it --name centos-test centos:centos7
[root@e10cdc141fd0 /]# ls
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
// 退出容器
[root@e10cdc141fd0 /]# exit
exit
// 查看进程
[root@localhost home]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e10cdc141fd0 centos:centos7 "/bin/bash" About a minute ago Exited (127) 13 seconds ago centos-test
2、安装 Niginx
搜索Niginx的版本
安装
// 安装
[root@localhost home]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
07aded7c29c6: Pull complete
bbe0b7acc89c: Pull complete
44ac32b0bba8: Pull complete
91d6e3e593db: Pull complete
8700267f2376: Pull complete
4ce73aa6e9b0: Pull complete
Digest: sha256:765e51caa9e739220d59c7f7a75508e77361b441dccf128483b7f5cce8306652
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
// 查看
[root@localhost home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f8f4ffc8092c 6 days ago 133MB
centos centos7 eeb6ee3f44bd 2 weeks ago 204MB
启动
-p 8080:80:表示端口映射,容器的80端口映射到本机的8080端口
// 启动
[root@localhost home]# docker run --name nginx-test -p 8080:80 f8f4ffc8092c
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/10/04 14:21:13 [notice] 1#1: using the "epoll" event method
2021/10/04 14:21:13 [notice] 1#1: nginx/1.21.3
2021/10/04 14:21:13 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/10/04 14:21:13 [notice] 1#1: OS: Linux 3.10.0-514.el7.x86_64
2021/10/04 14:21:13 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/10/04 14:21:13 [notice] 1#1: start worker processes
2021/10/04 14:21:13 [notice] 1#1: start worker process 31
2021/10/04 14:21:13 [notice] 1#1: start worker process 32
2021/10/04 14:21:13 [notice] 1#1: start worker process 33
2021/10/04 14:21:13 [notice] 1#1: start worker process 34
2021/10/04 14:21:31 [notice] 1#1: signal 28 (SIGWINCH) received
// 查看进程
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95f4f0f5366b f8f4ffc8092c "/docker-entrypoint.…" About a minute ago Up About a minute 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx-test
e10cdc141fd0 centos:centos7 "/bin/bash" 10 minutes ago Exited (127) 9 minutes ago centos-test
访问
3、安装 Node.js
搜索Node.js版本
安装
// 安装
[root@localhost ~]# docker pull node
Using default tag: latest
latest: Pulling from library/node
5e7b6b7bd506: Pull complete
fd67d668d691: Pull complete
1ae016bc2687: Pull complete
0b0af05a4d86: Pull complete
ca4689f0588c: Pull complete
8c33de21d690: Pull complete
f113b2c481db: Pull complete
0f84649efc4d: Pull complete
5990cbd9430a: Pull complete
Digest: sha256:a1118930ecc77da1ce4b19ac8c17adf386b7bb36b348111437f1cfca5a5c9fd7
Status: Downloaded newer image for node:latest
docker.io/library/node:latest
// 查看镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
node latest 9c23a8242f8b 6 days ago 908MB
nginx latest f8f4ffc8092c 6 days ago 133MB
centos centos7 eeb6ee3f44bd 2 weeks ago 204MB
启动
-itd:it表示交互模式,d表示后台运行
// 启动
[root@localhost ~]# docker run -itd --name node-test node
1a64080b9fc492c6095a92d7ae87b8306a8de7939a3a1a820f251efcb7291e48
// 进入容器
[root@localhost ~]# docker exec -it node-test bin/bash
root@1a64080b9fc4:/# node -v // 测试(启迪成功)
v16.10.0
root@1a64080b9fc4:/#
4、安装 Mysql
搜索Mysql版本
安装
// 安装
[root@localhost ~]# docker pull mysql:8.0.19
8.0.19: Pulling from library/mysql
54fec2fa59d0: Pull complete
bcc6c6145912: Pull complete
951c3d959c9d: Pull complete
05de4d0e206e: Pull complete
319f0394ef42: Pull complete
d9185034607b: Pull complete
013a9c64dadc: Pull complete
96d4c3d31f9f: Pull complete
785bc90808da: Pull complete
1339cf094729: Pull complete
beb8f531cc37: Pull complete
2b40c9f6a918: Pull complete
Digest: sha256:9643e9fbd6330d10686f8922292dcb20995e7b792c17d4e94ddf95255f1d5449
Status: Downloaded newer image for mysql:8.0.19
docker.io/library/mysql:8.0.19
// 查看镜像(安装成功)
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php latest 27155161fe5e 6 days ago 480MB
node latest 9c23a8242f8b 6 days ago 908MB
nginx latest f8f4ffc8092c 6 days ago 133MB
centos centos7 eeb6ee3f44bd 2 weeks ago 204MB
mysql 8.0.19 0c27e8e5fcfa 17 months ago 546MB
启动
因为我的本机上有mysql,所以我将端口号改为3307
-e:设置环境变量
MYSQL_ROOT_PASSWORD=123456:表示登录名为root,命令为123456
// 启动
[root@localhost ~]# docker run -itd --name mysql-test -p 3307:3307 -e MYSQL_ROOT_PASSWORD=123456 mysql
650054eb987010dd00ef2fef3553b4383d1f52e5b0ec92682e53fcb710fedab2
// 查看进程
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
650054eb9870 mysql "docker-entrypoint.s…" 6 seconds ago Up 6 seconds 3306/tcp, 33060/tcp, 0.0.0.0:3307->3307/tcp, :::3307->3307/tcp mysql-test
// 进入mysql
[root@localhost ~]# docker exec -it 650054eb9870 /bin/bash
// 登录(启动成功)
root@650054eb9870:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.26 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>
5、安装 Tomcat
搜索Tomcat版本
安装
我这就安装Tomcat9.0,你可以自行安装
// 安装
[root@localhost ~]# docker pull tomcat:9.0
9.0: Pulling from library/tomcat
df5590a8898b: Pull complete
705bb4cb554e: Pull complete
519df5fceacd: Pull complete
ccc287cbeddc: Pull complete
39a2961e8351: Pull complete
0287b7aa7f62: Pull complete
165d4a436d89: Pull complete
2b9d00974b45: Pull complete
698b93af36c0: Pull complete
0cef25a39a38: Pull complete
Digest: sha256:04ae6b17ef8fbf0f262f1b559a485fee8477acbc295cec44bc408810300c0f4d
Status: Downloaded newer image for tomcat:9.0
docker.io/library/tomcat:9.0
// 查看镜像(安装成功)
[root@localhost ~]# docker images | grep tomcat
tomcat 9.0 86783bb5af6c 5 days ago 680MB
启动
-d:后台运行
-p 8081:8080:端口映射
–name tomcat-test:给容器设置一个名字
tomcat:9.0:启动指定版本的Tomcat
// 启动
[root@localhost ~]# docker run -d --name tomcat-test -p 8081:8080 tomcat:9.0
cbbdeca88131d4b40239401d90151c292c4d452b8bde05b39a5e4f280f610745
// 查看docker进程(启动成功)
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cbbdeca88131 tomcat:9.0 "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp tomcat-test
访问
发现问题,出现这种问题的原因是因为Tomacat的webapps下面没有东西,我们可以进入容器看一看
// 进入容器
[root@localhost ~]# docker exec -it tomcat-test /bin/bash
root@cbbdeca88131:/usr/local/tomcat# ls
BUILDING.txt NOTICE RUNNING.txt lib temp work
CONTRIBUTING.md README.md bin logs webapps
LICENSE RELEASE-NOTES conf native-jni-lib webapps.dist
// 发现什么都没有
root@cbbdeca88131:/usr/local/tomcat# cd webapps
root@cbbdeca88131:/usr/local/tomcat/webapps# ls
root@cbbdeca88131:/usr/local/tomcat/webapps#
解决方案:将webapps.dist下的东西复制到webapps
-r:表示递归执行
root@cbbdeca88131:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@cbbdeca88131:/usr/local/tomcat# cd webapps
root@cbbdeca88131:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
此时,我们再次访问http://192.168.174.130:8081/
6、安装 Redis
搜索Redis版本
选择你想要安装的版本,我下载的是redis-6.2.5
安装
// 1、安装
[root@localhost ~]# docker pull redis:6.2.5
6.2.5: Pulling from library/redis
07aded7c29c6: Already exists
1a5d64c027a4: Pull complete
189d72810950: Pull complete
b85aac102f7d: Pull complete
fe1bca69301e: Pull complete
fd69789c2b06: Pull complete
Digest: sha256:c98f0230b5f1831f4f5dd764c4ea8ef11d3e3a1a3593278eb952373d97c82b27
Status: Downloaded newer image for redis:6.2.5
docker.io/library/redis:6.2.5
// 2、查看(安装成功)
[root@localhost ~]# docker images | grep redis
redis 6.2.5 5d89766432d0 6 days ago 105MB
启动(创建容器)
// 1、启动
[root@localhost ~]# docker run -itd --name redis-test -p 6379:6379 redis:6.2.5
f375cc3de02a41a36f669c393ea07e7edd8b444875a2e2499dc4f9d834172cec
// 2、查看进程(启动成功)
[root@localhost ~]# docker ps | grep redis
f375cc3de02a redis:6.2.5 "docker-entrypoint.s…" 18 seconds ago Up 18 seconds 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis-test
测试
// 1、进入redis容器
[root@localhost ~]# docker exec -it redis-test /bin/bash
// 2、进入redis客户端
root@f375cc3de02a:/data# redis-cli
// 3.1、设置一个name
127.0.0.1:6379> set name studioustiger
OK
// 3.2、去除name
127.0.0.1:6379> get name
"studioustiger"
// 4.1、退出redis-cli
127.0.0.1:6379> exit
// 4.1、退出redis容器
root@f375cc3de02a:/data# exit
exit
[root@localhost ~]#
7、安装 MongoDB
搜索MongoDB版本
安装
docker pull mongo:latest
// 1、安装
[root@localhost ~]# docker pull mongo:latest
latest: Pulling from library/mongo
f3ef4ff62e0d: Pull complete
b1a49f973a13: Pull complete
a94d4dd6d900: Pull complete
f6e410f3a61f: Pull complete
84513ed7bcb6: Pull complete
6c69ef6aac84: Pull complete
3fc764861fa2: Pull complete
a312d8b37ffd: Pull complete
73f757690572: Pull complete
7e6327a25cb4: Pull complete
Digest: sha256:71348de35c2edef847906cbcf175d12e15244cdc37fc4ae5661fde361f4e4893
Status: Downloaded newer image for mongo:latest
// 2、查看镜像(安装成功)
docker.io/library/mongo:latest
[root@localhost ~]# docker images | grep mongo
mongo latest c1a14d3979c5 4 days ago 691MB
启动(运行容器)
docker run -itd --name mongo-test -p 27017:27017 mongo --auth
–auth:表示需要密码才能访问容器服务。
// 1、启动
[root@localhost ~]# docker run -itd --name mongo-test -p 27017:27017 mongo --auth
c6552e3f63b1a53d82cb0ca98d6f9e86f763acf2c874f36f47a274d7e7a72817
// 2、查看进程(启动成功)
[root@localhost ~]# docker ps | grep mongo
c6552e3f63b1 mongo "docker-entrypoint.s…" 16 seconds ago Up 16 seconds 0.0.0.0:27017->27017/tcp, :::27017->27017/tcp mongo-test
接着使用以下命令添加用户和设置密码,并且尝试连接。
// 1、进入 mongo-test容器
[root@localhost ~]# docker exec -it mongo-test mongo admin
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("0f2c3a82-ca1f-4c38-8fcd-76fa26ed9390") }
MongoDB server version: 5.0.3
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
// 2、创建一个名为 admin,密码为 123456 的用户
> db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
"readWriteAnyDatabase"
]
}
// 3、尝试使用上面创建的用户信息进行连接
> db.auth('admin', '123456')
1 // 连接成功
// 4.1、添加一个信息
> db.getCollection('user').insert({"userId" : "001","uclass" : "A","name" : "studioustiger","age" : 18});
WriteResult({ "nInserted" : 1 })
// 4.2、查询一个信息
> db.getCollection('user').find({});
{ "_id" : ObjectId("615befb22c1da5b2740e58f9"), "userId" : "001", "uclass" : "A", "name" : "studioustiger", "age" : 18 }
>
8、安装 ElasticSearch+Kibana
① 安装 ElasticSearch
搜索ElasticSearch版本
安装
docker pull elasticsearch:7.7.1
// 安装
[root@localhost ~]# docker pull elasticsearch:7.7.1
7.7.1: Pulling from library/elasticsearch
524b0c1e57f8: Pull complete
4f79045bc94a: Pull complete
4602c5830f92: Pull complete
10ef2eb1c9b1: Pull complete
47fca9194a1b: Pull complete
c282e1371ecc: Pull complete
302e1effd34b: Pull complete
50acbec75309: Pull complete
f89bc5c60b5f: Pull complete
Digest: sha256:dff614393a31b93e8bbe9f8d1a77be041da37eac2a7a9567166dd5a2abab7c67
Status: Downloaded newer image for elasticsearch:7.7.1
docker.io/library/elasticsearch:7.7.1
// 查看镜像,安装成功
[root@localhost ~]# docker images | grep elasticsearch
elasticsearch 7.7.1 830a894845e3 16 months ago 804MB
启动
docker run -d --name es-test -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.7.1
[root@localhost es]# docker run --name es-test -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.7.1
{"type": "server", "timestamp": "2021-10-05T02:22:32,101Z", "level": "INFO", "component": "o.e.e.NodeEnvironment", "cluster.name": "docker-cluster", "node.name": "062bb66730f8", "message": "using [1] data paths, mounts [[/ (rootfs)]], net usable_space [10.4gb], net total_space [16.9gb], types [rootfs]" }
{"type": "server", "timestamp": "2021-10-05T02:22:32,105Z", "level": "INFO", "component": "o.e.e.NodeEnvironment", "cluster.name": "docker-cluster", "node.name": "062bb66730f8", "message": "heap size [1gb], compressed ordinary object pointers [true]" }
....
访问
curl localhost:9200
,发现是拒绝访问
[root@localhost ~]# curl localhost:9200
curl: (56) Recv failure: Connection reset by peer
解决方案:修改配置,解决跨域访问问题
// 1、进入容器
[root@localhost ~]# docker exec -it es-test /bin/bash
// 2、编写elasticsearch.yml
[root@062bb66730f8 elasticsearch]# cd /usr/share/elasticsearch/config/
[root@062bb66730f8 config]# vi elasticsearch.yml
[root@062bb66730f8 config]# cat elasticsearch.yml
cluster.name: "docker-cluster"
network.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
// 3、退出
[root@062bb66730f8 config]# exit
exit
// 4、重启elasticsearch
[root@localhost ~]# docker restart es-test
es-test
// 5、访问(启动成功)
[root@localhost ~]# curl localhost:9200
{
"name" : "062bb66730f8",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "brGVeMeIRXyA-4h9VgnZNw",
"version" : {
"number" : "7.7.1",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ad56dce891c901a492bb1ee393f12dfff473a423",
"build_date" : "2020-05-28T16:30:01.040088Z",
"build_snapshot" : false,
"lucene_version" : "8.5.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
安装ik分词器
进入elasticsearch容器,并切换到plugins目录
// 进入elasticsearch
[root@localhost ~]# docker exec -it es-test /bin/bash
[root@062bb66730f8 elasticsearch]# ls
LICENSE.txt NOTICE.txt README.asciidoc bin config data jdk lib logs modules plugins
// 切换到plugins目录
[root@062bb66730f8 elasticsearch]# cd plugins/
下载安装与elasticsearch相应的版本的ik分析器
// 1、下载
[root@062bb66730f8 plugins]# elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.7.1/elasticsearch-analysis-ik-7.7.1.zip
-> Installing https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.7.1/elasticsearch-analysis-ik-7.7.1.zip
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.7.1/elasticsearch-analysis-ik-7.7.1.zip
[=================================================] 100%??
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: plugin requires additional permissions @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.
// 2、安装
Continue with installation? [y/N]y
-> Installed analysis-ik
// 3、安装成功
[root@062bb66730f8 plugins]# ls
analysis-ik
退出,并重启elasticsearch
// 1、退出
[root@062bb66730f8 plugins]# exit
exit
// 2、重启
[root@localhost ~]# docker restart es-test
es-test
测试(注意:这里你要在完成安装kibana之后才可以测试)
② 安装Kibana
安装与elasticsearch版本相同的kibana
docker pull kibana:7.7.1
// 1、安装
[root@localhost ~]# docker pull kibana:7.7.1
7.7.1: Pulling from library/kibana
524b0c1e57f8: Already exists
103dc10f20b6: Pull complete
e397e023efd5: Pull complete
f0ee6620405c: Pull complete
17e4e03944f0: Pull complete
eff8f4cc3749: Pull complete
fa92cc28ed7e: Pull complete
afda7e77e6ed: Pull complete
019e109bb7c5: Pull complete
e82949888e47: Pull complete
15f31b4d9a52: Pull complete
Digest: sha256:ea0eab16b0330e6b3d9083e3c8fd6e82964fc9659989a75ecda782fbd160fdaa
Status: Downloaded newer image for kibana:7.7.1
docker.io/library/kibana:7.7.1
// 2、查看镜像
[root@localhost ~]# docker images | grep kibana
kibana 7.7.1 6de54f813b39 16 months ago 1.2GB
启动kibana
docker run --name kibana-test --link=es-test:elasticsearch -p 5601:5601 -d kibana:7.7.1
// 1、启动
[root@localhost home]# docker run --name kibana-test --link=es-test:elasticsearch -p 5601:5601 -d kibana:7.7.1
0d0d60209857369825d3321d7fcdb83a6cf90b4b73df6ae7374717577c8d7457
// 2、查看进程(启动成功)
[root@localhost home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0d0d60209857 kibana:7.7.1 "/usr/local/bin/dumb…" 23 seconds ago Up 22 seconds 0.0.0.0:5601->5601/tcp, :::5601->5601/tcp kibana-test
062bb66730f8 elasticsearch:7.7.1 "/tini -- /usr/local…" 58 minutes ago Up 47 seconds 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp es-test
可能会遇到的问题:如果启动失败(如下),那么重启docker
// 1、启动失败
[root@localhost home]# docker run --name kibana-test --link=es-test:elasticsearch -p 5601:5601 -d kibana:7.7.1
aaf776720185827551c5c790409bde8b75e5dd2ff7980b9388538e9bac6101cc
docker: Error response from daemon: driver failed programming external connectivity on endpoint kibana-test (ac8712dd3f6c11b4f6b2068b6aa9c73680b23a9f20d3b94496b8250cdb51b2a6): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 5601 -j DNAT --to-destination 172.17.0.3:5601 ! -i docker0: iptables: No chain/target/match by that name.
(exit status 1)).
// 2、重启docker
[root@localhost home]# systemctl restart docker
// 3.1、启动elasticsearch
[root@localhost home]# docker start es-test
es-test
// 3.2、删除启动失败的容器kibana-test
[root@localhost home]# docker rm kibana-test
kibana-test
// 4、docker启动成功
[root@localhost home]# docker run --name kibana-test --link=es-test:elasticsearch -p 5601:5601 -d kibana:7.7.1
0d0d60209857369825d3321d7fcdb83a6cf90b4b73df6ae7374717577c8d7457
进入kibana,配置kibana.yum(连接elasticsearch和kibana汉化)
// 1、编写kibana.yum
[root@localhost home]# docker exec -it kibana-test /bin/bash
bash-4.2$ cd config/
bash-4.2$ vi kibana.yml
#
# ** THIS IS AN AUTO-GENERATED FILE **
#
# Default Kibana configuration for docker target
server.name: kibana
server.host: "0"
# elasticsearch.hosts: [ "http://elasticsearch:9200" ]
// 1.1、将上面的elasticsearch换成docker容器的IP(查看docker的IP,方法在下面),使得kibana可以访问,如下
elasticsearch.hosts: [ "http://172.17.0.1:9200" ]
monitoring.ui.container.elasticsearch.enabled: true
// 1.2 汉化kibana
i18n.locale: "zh-CN"
// 2、退出
bash-4.2$ exit
exit
// 3、重启
[root@localhost home]# docker restart kibana-test
kibana-test
查看docker容器的IP
ip add
[root@localhost es]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:01:c0:93 brd ff:ff:ff:ff:ff:ff
inet 192.168.174.130/24 brd 192.168.174.255 scope global dynamic ens33
valid_lft 1198sec preferred_lft 1198sec
inet6 fe80::f2a9:297:7911:3239/64 scope link
valid_lft forever preferred_lft forever
==========================这个就是docker容器的IP=================
3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 02:42:dd:e3:f3:ab brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:ddff:fee3:f3ab/64 scope link
valid_lft forever preferred_lft forever
================================================================
39: vetha464cbe@if38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP
link/ether 12:6b:af:ee:84:bf brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::106b:afff:feee:84bf/64 scope link
valid_lft forever preferred_lft forever
41: veth235a5a7@if40: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP
link/ether a6:59:26:f5:f4:83 brd ff:ff:ff:ff:ff:ff link-netnsid 1
inet6 fe80::a459:26ff:fef5:f483/64 scope link
valid_lft forever preferred_lft forever
外部访问
http://192.168.174.130:5601/
kibana连接elasticsearch成功,kibana汉化成功
六、镜像加载原理
1、镜像简介
镜像是一种轻量级、可执行的独立软件包,用来打包软件运行环境和基于运行环境的开发软件,它包含运行某个软件所需的所有内容,包括代码、运行时、库、环境变量和配置文件。
2、UnionFS(联合文件系统)
Union文件系统(UnionFS) 是一种分层、轻量级并且高性能的文件系统,他支持对文件系统的修改作为一次提交来层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem)。
Union文件系统是Docker镜像的基础。镜像可以通过分层来进行集成,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
特性:一次同时加载多个文件系统,但从外面看起来,只能看到一个文件系统你那个,联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层文件和目录。
3、Docker镜像加载原理
docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统UnionFS。例如,我们在进行下载镜像的时候,镜像是分层进行下载的。
bootfs(boot file system) 主要包含bootloader和kernel,bootloader主要是引导加载kernel,Linux刚启动时会加载bootfs文件系统,在Docker镜像的最底层是bootfs。这一层与我们典型的Linux/Unix系统是一样的, 包含boot加载器和内核。当boot加载完成之后整个内核就都在内存中了,此时内存的使用权已由bootfs转交给内核,此时系统也会卸载bootfs。
rootfs(root file system), 在bootfs之上。包含的就是典型Linux系统中的/dev, /proc, /bin, /etc等标准目录和文件。rootfs就是各种不同的操作系统发行版,比如Ubuntu,Centos等等。
平时我们安装进虚拟机的CentOS都是好几个G,为什么docker的centos这里才204MB?
对以一个精简的OS,rootfs可以很小,只需要包括最基本的命令、工具和程序库就行,因为底层直接用host和kernel,自己只需要提供rootfs就行。由此可见对于不同的Linux发行版,bootfs基本是一致的,rootfs会有差别,因此不同的发行版可以公用bootfs。这也是为什么docker的centos这里才204MB的原因。
七、commit 镜像(提交自己的镜像到本地仓库)
我们可以提交自己的镜像到本地仓库。当我们使用一个镜像创建一个容器后,该容器往往是不符合我的要求的,我们可以在这个容器中进行个性化配置,然后将该容器提交到本地仓库,生成自己的镜像。那么以后同过自己的镜像生成的容器,就已经完成了我们需要的配置了。
提交镜像的命令
docker commit -m="镜像的描述信息" -a="作者" 容器ID 自定义镜像名:版本(TAG)
举一个小例子,我们在前面安装的tomact中的webapps目录下面是没有东西的,但是我们通过tomcat生成的容器tomcat-test是经过了自己的配置,webapps是完善的。下面我们就将容器tomcat-test进行提交。
提交镜像
// 1、启动容器
[root@localhost ~]# docker start tomcat-test
tomcat-test
// 2、查看进程
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cbbdeca88131 tomcat:9.0 "catalina.sh run" 11 hours ago Up 5 seconds 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp tomcat-test
// 3、提交
[root@localhost ~]# docker commit -m="webapps目录下面完善" -a="studioustiger" cbbdeca88131 mytomcat:1.0
sha256:f680f1d8bb1bfaac2c67ef328221e83d2c75ee7aac6086ab103d835adfee81fa
// 4、查看镜像(提交成功)
[root@localhost ~]# docker images | grep mycentos
mytomcat 1.0 f680f1d8bb1b About a minute ago 685MB
[root@localhost ~]#
生成容器并测试
暴露的端口号为8080,如果我们访问192.168.174.31:8080
不是空,那么说明webapps目录是完善的。
[root@localhost ~]# docker run -d --name mytomcat-test -p 8080:8080 mytomcat:1.0
511e125c455ff37d454e5bfbd1b8ea724cdd3ab776b72514063033d547959663
八、容器数据卷的使用
1、介绍
我们会有这么一种需求,我们需要容器中的一些目录可以同步到本地,这个做的目的有两个:第一,对于一些容器我们我往往需要经常的修改配置文件,频繁的进入容器显得们麻烦。第二,对于容器(mysql)中的数据,我们希望可以持久化到本地,作为一种备份。
针对上面的两个需求,docker给我提供了一种数据卷的技术,我们可以通过数据卷将docker容器中的目录与本地的Linux的目录进行挂载(双向绑定)。除了上述的本机与容器之间的数据挂载,容器与容器之间也是可以进行挂载的。
2、本地挂载
宿主机与容器进行挂载的命令:-v linux目录路径:docker容器中的目录路径
举例:下面我们将mysql中的/etc/mysql/conf.d
和/var/lib/mysql
目录挂载到本地的/home/mysql/config
和/home/mysql/data
目录下。
命令:docker run --name mysql-test02 -itd -p 3310:3306 -v /home/mysql/config:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql
// 1、启动(挂载)
[root@localhost home]# docker run --name mysql-test02 -itd -p 3310:3306 -v /home/mysql/config:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql
41251406e9127657185b0e1afab99c5b9dea7814f94a0030d2c7b40eda2b9134
// 2、发现home目录下存在mysql目录
[root@localhost config]# cd /home/
[root@localhost home]# ls
mysql
[root@localhost home]# cd mysql/
// 3、发现存在config 和 data目录
[root@localhost mysql]# ls
config data
[root@localhost mysql]# cd data/
// 4、发现data目录下 的文件确实是/var/lib/mysql目录下的东西
[root@localhost data]# ls
41251406e912.err ca-key.pem #ib_16384_1.dblwr ibtmp1 private_key.pem undo_001
auto.cnf ca.pem ib_buffer_pool #innodb_temp public_key.pem undo_002
binlog.000001 client-cert.pem ibdata1 mysql server-cert.pem
binlog.000002 client-key.pem ib_logfile0 mysql.ibd server-key.pem
binlog.index #ib_16384_0.dblwr ib_logfile1 performance_schema sys
3、具名挂载和匿名挂载
我们上述讲的挂载方式为指定路径挂载(我们是指定了宿主机的挂载路径)。我们也可以通过具名和匿名两种方式进行挂载。具名和匿名挂载我们都不需要指定宿主机的挂载路径。
具名挂载:docker run -d -P --name nginx-01 -v /etc/nginx nginx
-P大写就是随机端口
匿名挂载(推荐):docker run -d -P --name nginx-02 -v nginx-volume:/etc/nginx nginx
nginx-volume为挂载名
我们可以通过 docker volume ls
查看挂载名列表
我们可以通过 docker volume inspect 挂载名
查看挂载情况
我们可以发现/var/lib/docker/volumes/nginx-volume/_data
中的内容确实是nginx中/etc/nginx nginx
的内容
拓展:
我们往往可以看到如下形式的挂载方式
docker run -d -P --name nginx-02 -v nginx-volume:/etc/nginx:ro nginx
docker run -d -P --name nginx-02 -v nginx-volume:/etc/nginx:rw nginx
ro: readonly ,容器中被挂载的目录在容器中只能进行读
rw:readwrite(默认) ,容器中被挂载的目录在容器中可读可写
4、容器数据卷
更新中…
九、DockerFile
1、dockerfile 介绍
Dockerfile 是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。比如说,我们可以构建一个自己的镜像(centos + mysql + redis + elasticsearch + jdk)。
dockerfile是面向开发的,也就是说我们以后需要将我们的项目打包成docker镜像,提交给甲方。
下面我们来看一下,官方是怎么写dickerfile的(centos7为例)
2、编写一个Dockerfile文件
基础知识
- 每一个指令都是大写字母
- 指令是是从往下进行执行的
- 每一个指令都会创建一个新的镜像层,并提交
dockerfile 命令介绍
FROM:基础镜像,一切从这里构建
MAINTAINER:镜像的作者(姓名+邮箱)
RUN:镜像构建是需要运行的指令
ADD:添加一些其他的资源(比如添加项目的一些其他依赖)
WORKDIR:镜像的工作目录,用于指定容器的一个目录, 容器启动时执行的命令会在该目录下执行
VOLUME:挂载目录
EXPOSE:暴露端口(和 -p 是一个道理)
CMD:指定容器启动后要运行的命令,如果手动添加其他命令,那么CMD指定的命令将会被替换
INTENYPOINT:指定容器启动后要运行的命令,如果手动添加其他命令,那么INTENYPOINT指定的命令后面将会被追加
ONBUILD:当构建一个继承该镜像的容器时,ONBUILD指令将会被触发
COPY:将本地文件拷贝到镜像中(与ADD类似)
ENV:设置环境变量
diy一个自己的Tomcat
我们统一使用Dockerfile作为文件名
编写Dockerfile文件
# 基于cnetos7的镜像进行构建的
FROM centos:centos7
# 设置作者和联系方式
MAINTAINER studioustiger<studioustiger@163.com>
# 将该目录下的usedoc.txt文件copy到centos的/usr/local/目录下
COPY usedoc.txt /usr/local/
# 复制当前目录下的apache-tomcat-8.5.70.tar.gz到centos的/usr/local/目录下,并解压
ADD apache-tomcat-8.5.70.tar.gz /usr/local/
# 复制当前目录下的jdk-8u301-linux-x64.tar.gz到centos的/usr/local/目录下,并解压
ADD jdk-8u301-linux-x64.tar.gz /usr/local/
# 执行yum -y install vim*命令 和 yum -y install net-tools命令
RUN yum -y install vim* && yum -y install net-tools
# 设置工作目录
ENV MYPATH /usr/local
WORKDIR $MYPATH
# 设置java的环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_301/
ENV CLASSPATH $JAVA_HOME/lib
# 设置Tomcat的环境变量
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.70
# 添加到总的环境变量中
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/bin
#公开端口
EXPOSE 8080
#设置启动命令
CMD ["/usr/local/apache-tomcat-8.5.70/bin/catalina.sh","run"]
3、docker build 构建一个镜像
docker build -f Dockerfile -t mytomcat:1.0 .
-f Dockerfile
可以省略,因为文件名是Dockerfile,所以可以省略。注意最后还有一个 .
4、docker run 运行一个镜像
docker run -d --name 容器名 -p 8080:8080 镜像ID
5、docker push 发布一个镜像
第一步:注册账户
https://hub.docker.com/
第二步:命令行登录docker
docker login -u 用户名
第三步:发布进行
doxker push repository:tag
十、docker 网络
1、介绍
目前来说,我们只是学习了单个容器的使用,但是容器和容器之间是可以进行通信的。如果你细心的话,我们前面在部署elasticsearch和kibana的时候就使用了容器之间的通信。
我们先来看一下IP,在;Linux中我们使用ip addr
172.168.0.1 这个IP大家是不是很熟悉,这就一个回环地址,我们家里的路由器的回环地址是192.168.0.1。
既然docker是有回环地址的,那么docker就像一个路由器一样,他可以管理很多的地址。
我们带着这个疑虑启动一个容器试一试,发现多了一个网卡,这是怎么回事,难道是我启动的容器的缘故。
于是我们可以查看一下容器中的IP,docker exec -it 容器名 id addr
会发现有一个对应的映射另一个映射。说明docker容器网络是通过veth-pair
技术实现的
注意:如果你的docker容器中没有 ip addr
命令,那么你可以执行 docker exec -it 容器名 yum -y install iproute
命令
网络模型图:
docker使用的是桥接技术,docker0是docker容器的网关
2、容器互联(–link)
在启动kibana的时候,我们用过docker run --name kibana-test --link=es-test:elasticsearch -p 5601:5601 -d kibana:7.7.1
命令,其中的--link=es-test:elasticsearch
就是实现容器的通信的技术(已过时)
通过 --link
我们可以实现容器之间的单行通信。下面我们做一个实验,证明--link
是容器单向通信技术
// 1.1、正常启动centos01
[root@localhost /]# docker run --name centos01 -itd centos:centos7
f7e4ad96897c465b8d57f1f30fc6bc3fead593b49f99fc1f760ef9ab84d442fe
// 1.2、通过--link centos01的方式启动centos02
[root@localhost /]# docker run --name centos02 --link centos01 -itd centos:centos7
0e60f83b0e17f3a7b629cc7c06362167e0fcc975783e7511f844e9adab1e3020
// 2.1、centos02 ping centos01 (可以ping通)
[root@localhost /]# docker exec -it centos02 ping centos01
PING centos01 (172.17.0.2) 56(84) bytes of data.
64 bytes from centos01 (172.17.0.2): icmp_seq=1 ttl=64 time=0.161 ms
64 bytes from centos01 (172.17.0.2): icmp_seq=2 ttl=64 time=0.125 ms
64 bytes from centos01 (172.17.0.2): icmp_seq=3 ttl=64 time=0.110 ms
64 bytes from centos01 (172.17.0.2): icmp_seq=4 ttl=64 time=0.100 ms
// 2.2、centos02 ping centos01 (不可以ping通)
[root@localhost /]# docker exec -it centos01 ping centos02
ping: centos02: Name or service not known
为什么 centoso2 --link centos01
之后,centos02就可以 ping 通centos01?
我们知道windows中有一个hosts文件可以实现本地映射,在Linux中也是存在hosts文件的。
centos02中的hosts文件:
centos01中的hosts文件:
3.1、容器互联(自定义局域网络☆☆☆☆)
我们为什么需要自定义网络?
将容器放在自定义的网络(数据局域网)中,容器之间是可以相互通信的,这样的话,我们就不需要使用 --link
来实现通信了。
如何自定义网络?
查看容器网络docker network ls
,容器启动时默认的网络是bridge(桥接)模式(即,容器启动时默认是存在--net bridge
)
我们可以通过 docker network create
自定义一个网络
参数:
自定义网络:
// 1、自定义网络
[root@localhost /]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 tigerNet
8fe9146fa41772f54fdb95b0d4d510b08df3596ef034404bd66a327c0b3ca817
// 2、查看容器网络
[root@localhost /]# docker network ls
NETWORK ID NAME DRIVER SCOPE
fd8851346218 bridge bridge local
ea9e3751f590 host host local
8e3e1706738d none null local
8fe9146fa417 tigerNet bridge local # 这个是我们自定义的网络
查看自定义网络:
接下来,我们启动两个容器,并将其放入我们自定义的网络中
// 1、启动centos01
--net tigerNet 将容器发布到我们自定义的网络中去
[root@localhost /]# docker run --name centos01 -itd --net tigerNet centos:centos7
// 1、启动centos02
0cfb02b5bfb96d705bd4216a3c50f4b71614631f418694301896e4d75a9d38a2
[root@localhost /]# docker run --name centos02 -itd --net tigerNet centos:centos7
31f982e351c9fb748ebbca247b0978d40586282f1cc95e95f2bb6b82390f9918
// 3、查看 tigerNet 网络中的变化
[root@localhost /]# docker network inspect tigerNet
[
{
...
// 我们可以发现多了两个容器
"Containers": {
"0cfb02b5bfb96d705bd4216a3c50f4b71614631f418694301896e4d75a9d38a2": {
"Name": "centos01",
"EndpointID": "ff76dfdb768869596e11948fad96afad9220d0b8d9c6bf1949222844b3c2aa11",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"31f982e351c9fb748ebbca247b0978d40586282f1cc95e95f2bb6b82390f9918": {
"Name": "centos02",
"EndpointID": "7621ff14626877ca68d9599f9489826f30992c2fa9cf9b3f320d7b560690d243",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
}
},
...
}
]
我们在没有使用--link
的情况下,我们ping一下两个容器
// 1、centos02 ping centos01 可ping通
[root@localhost /]# docker exec -it centos01 ping centos02
PING centos02 (192.168.0.3) 56(84) bytes of data.
64 bytes from centos02.tigerNet (192.168.0.3): icmp_seq=1 ttl=64 time=0.122 ms
64 bytes from centos02.tigerNet (192.168.0.3): icmp_seq=2 ttl=64 time=0.125 ms
64 bytes from centos02.tigerNet (192.168.0.3): icmp_seq=3 ttl=64 time=0.089 ms
// 2、centos01 ping centos02 可ping通
[root@localhost /]# docker exec -it centos02 ping centos01
PING centos01 (192.168.0.2) 56(84) bytes of data.
64 bytes from centos01.tigerNet (192.168.0.2): icmp_seq=1 ttl=64 time=0.119 ms
64 bytes from centos01.tigerNet (192.168.0.2): icmp_seq=2 ttl=64 time=0.114 ms
64 bytes from centos01.tigerNet (192.168.0.2): icmp_seq=3 ttl=64 time=0.114 ms
64 bytes from centos01.tigerNet (192.168.0.2): icmp_seq=4 ttl=64 time=0.070 ms
3.2、容器互联(局域网络之间的联通☆☆☆☆)
此时,我们有这么一个需求,我们希望一个局域网中的容器可以与另一个局域网中的容器进行通信,如下:
现在我们构建两个局域网
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 tigerNet
docker network create --driver bridge --subnet 192.167.0.0/16 --gateway 192.178.0.1 studiouNet
在 tigerNet 中启动 centos01 和 centos02(3.1中已实现)
在 studiousNet中启动 tomcat01 和 tomcat02
docker run --name tomcat01 -itd -P --net studiousNet tomcat:9.0
docker run --name tomcat02 -itd -P --net studiousNet tomcat:9.0
现在我们来打通局域网与容器之间的通信
docer给我提供了docker network connect
命令,可以实现打通局域网与容器之间的通信
将 tomcat01 添加到 tigerNet 中,我们可以发现原来 docker 是给 tomcat01 设置了双IP
docker network connect tigerNet tomcat01
[root@localhost /]# docker network connect tigerNet tomcat01
[root@localhost /]# docker network inspect tigerNet
[
{
...
"Containers": {
...
// 1 我们可以看到tomcat01已经被加到tigerNet中去了
"34fa6d5c70716766991491a0a928e548b6783e9508988981c197e7df2e91c61f": {
"Name": "tomcat01",
"EndpointID": "2b49fa6c8beec42699482518a464a9f0bda218eb8202e6f6000c3b650cc27d52",
"MacAddress": "02:42:c0:a8:00:04",
"IPv4Address": "192.168.0.4/16",
"IPv6Address": ""
}
},
...
}
]
我们ping一下试一下
// 1、 centos01 ping tomcat01 可ping通
[root@localhost /]# docker exec -it centos01 ping tomcat01
PING tomcat01 (192.168.0.4) 56(84) bytes of data.
64 bytes from tomcat01.tigerNet (192.168.0.4): icmp_seq=1 ttl=64 time=0.245 ms
64 bytes from tomcat01.tigerNet (192.168.0.4): icmp_seq=2 ttl=64 time=0.090 ms
64 bytes from tomcat01.tigerNet (192.168.0.4): icmp_seq=3 ttl=64 time=0.111 ms
// 2、 centos01 ping tomcat02 可ping通
[root@localhost /]# docker exec -it centos01 ping tomcat02
ping: tomcat02: Name or service not known
3.3、容器互联(Redis集群实战☆☆☆☆)
1、集群介绍
2、集群搭建
① 创建一个redis网络
docker network create redis --subnet 172.38.0.0/16
② 编写 redis.conf 文件
我们打算,使用数据卷挂载 redis 中的redis.conf,所以我们需要宿主机上提前配置到redis集群的配置文件
redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1X
cluster-announce-bus-port 16379
appendonly yes
③ 启动6个redis容器
docker run --name redis0X -p 637X:6379 -p 1637X:16379 \
-v /home/redis/node0X/data:/data \
-v /home/redis/node0X/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1X redis:6.2.5 redis-server /etc/redis/redis.conf
④ 随便进入一个容器,并创建一个redis集群
docker exec -it redis01 /bin/bash
redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1
⑤ 进入集群,并查看集群信息
redis -cli -c
cluster info
cluster nodes
⑥ 集群高可用测试
存入一个name值到redis集群中,我们发下是存到了172.38.0.12对应的容器中去了
将172.38.0.12
对应的容器停掉
docker stop redis02
从新取name值(集群搭建成功)
✈ ❀ 希望平凡の我,可以给你不凡の体验 ☂ ✿…
更多推荐
所有评论(0)