垂死病中惊坐起,原来今天星期一。


文章目录

  • Docker概述
  • 安装docker
  • 如何配置镜像加速
  • 工作原理
  • 镜像常用命令
  • 容器常用命令
  • 常用的其他命令
  • 部署Nginx
    • 端口暴露原理分析
  • 部署tomcat
  • 部署Es+Kibana
  • 可视化管理面板
  • 继续浅入了解docker
    • docker镜像
    • 联合文件系统
    • docker镜像加载原理
    • 分层
    • 镜像与容器关联
  • docker commit 自己的容器
  • ================
  • | docker入门分界线 |
  • ================
  • 动态添加端口
  • 容器数据卷
  • 部署mysql-同步数据
  • 具名和匿名挂载
  • Dockerfile初识
  • --volume-from 实现数据同步
    • 多个mysql数据同步
  • 浅入DockerFile
    • dockerfile指令
    • 构建自己的dockerfile初体验
    • CMD和ENTRYPOINT区别
  • 构建自己的tomcat镜像
  • 发布镜像到DockerHub(基本不成功)
  • 发布镜像到腾讯云dockerhub
  • docker操作全流程
  • ================
  • | docker入坟分界线 |
  • ================
  • Docker网络
  • 容器互联 --link
  • 自定义网络
  • 网络连通
  • 部署redis集群
  • 部署springboot

前置学习 linux实践
https://blog.csdn.net/qq_19841133/article/details/108574319

Docker概述

docker为什么会出现?

开发要配置环境,项目开发上线环境部署也要配置环境,十分的麻烦。

在项目发布时,使用docker就可以将项目带上环境一块打包。

Docker的思想来自于集装箱,项目打包放入集装箱中,每个箱子间相互隔离。

docker通过隔离机制避免端口号之间冲突。

docker是基于go语言开发的开源项目。


虚拟机和docker都是虚拟化技术

虚拟机模拟一个完整的操作系统,和虚拟机不同,docker镜像只存放核心的环境,运行于操作系统之上。


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

文档位置:https://docs.docker.com/

docker架构图

分为三部分,客户端,docker服务器,远程仓库。

客户端用command操作docker,docker daemon是守护进程,镜像去运行容器。

镜像 image:镜像就好比一个模板,可以通过这个模板创建容器,将服务启动起来。通过这个镜像可以启动多个容器。

容器 container:服务运行在容器中,容器之间相互隔离,独立运行一组应用。

仓库 repository:存放镜像,和github概念差不多,docker hub

安装docker

环境准备

Centos7

系统内核3.10

[root @ VM-8-3-centos ~] # uname -r
3.10.0-1160.11.1.el7.x86_64
[root @ VM-8-3-centos ~] # sudo cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

开始学习安装,一起始于文档https://docs.docker.com/

选系统版本

先卸载旧的docker

sudo yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-engine

跟着文档做就好,docker文档十分详细

# provides the yum-config-manager utility
sudo yum install -y yum-utils
# add mirror
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 更新索引
sudo yum makecache fast
# install docker
sudo yum install docker-ce docker-ce-cli containerd.io
# 启动docker
sudo systemctl start docker
# 设置开机自启动
systemctl enable docker.service
# hello world
sudo docker run hello-world

正确安装显示

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
Status: Downloaded newer image for hello-world:latestHello 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 theexecutable that produces the output you are currently reading.4. The Docker daemon streamed that output to the Docker client, which sent itto your terminal.To try something more ambitious, you can run an Ubuntu container with:$ docker run -it ubuntu bashShare 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/

helloworld例子工作流程:docker在本机寻找镜像,有的话使用镜像运行,没有的话下载镜像,DockerHub是否可以找到这个镜像,找到了下载,找不到报错。

查看镜像

[root @ VM-8-3-centos ~] # docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    bf756fb1ae65   12 months ago   13.3kB

卸载docker操作:

Uninstall Docker Engine
Uninstall the Docker Engine, CLI, and Containerd packages:
卸载依赖
sudo yum remove docker-ce docker-ce-cli containerd.io
Images, containers, volumes, or customized configuration files on your host are not automatically removed. To delete all images, containers, and volumes:
删除运行环境
sudo rm -rf /var/lib/docker
You must delete any edited configuration files manually.

如何配置镜像加速

默认情况下,Docker 下载镜像是从官网下载,下载速度 特别特别的慢
使用国内加速器可以提升获取 Docker 官方镜像的速度

直接复制即可到 Linux 下回车即可

配置多个地址,避免某个站点不行时自动切换到后面的站点

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{"registry-mirrors": ["http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn","https://reg-mirror.qiniu.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

工作原理

docker是一个Client/Server结构,docker的守护进程运行在主机上,通过Socket从客户端访问服务器,Docker Server接收到Docker Client指令并执行。这里的守护进程就充当着服务器的角色,守护进程启动容器。

docker比虚拟机有更少的抽象层。VM需要OS,而Docker直接利用内核由Docker Engine统一管理。

镜像常用命令

显示信息
docker version
docker info

帮助命令
docker --help

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

查看主机上的镜像
docker images

[root @ VM-8-3-centos ~] # docker images --helpUsage:  docker images [OPTIONS] [REPOSITORY[:TAG]]List imagesOptions:-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  只显示id

dockerhub地址:https://hub.docker.com/

比如我想装mysql:https://hub.docker.com/_/mysql

可以用搜索命令

docker search mysql

拉取命令

docker pull mysqldocker pull docker.io/library/mysql:latest等价

[root @ VM-8-3-centos ~] # docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql   # 分层下载
a076a628af6f: Pull complete
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1a8c919e89bf: Pull complete
9f3cf4bd1a07: Pull complete
80539cea118d: Pull complete
201b3cad54ce: Pull complete
944ba37e1c06: Pull complete
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址

docker pull name[:版本]

在docker hub查看镜像支持的版本

[root @ VM-8-3-centos ~] # docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists  # 联合文件系统,重复的就不下载了
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
1831ac1245f4: Pull complete
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

删除镜像

docker rmi name|id

删除所有镜像
docker rmi -f ${docker images -aq}

容器常用命令

我有了镜像才能创建容器,下载一个centos镜像

[root @ VM-8-3-centos ~] # docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

通过docker run命令

启动容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

常用Option

--name centos1  容器名字
--d  后台方式运行
-it  使用交互方式运行
-p  指定端口-p 宿主机端口:容器端口(常用)-p 容器端口-p ip:主机端口:容器端口
-P  随机指定端口

启动并进入容器
docker run -it centos /bin/bash

[root @ VM-8-3-centos ~] # docker run  -it centos /bin/bash
[root@3a8a6bcb1068 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@3a8a6bcb1068 /]# uname -r
3.10.0-1160.11.1.el7.x86_64
[root@3a8a6bcb1068 /]# exit
exit

查看运行中的容器

docker ps [-a] [-n=?] [q]

[root @ VM-8-3-centos ~] # docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED       STATUS                   PORTS     NAMES
3a8a6bcb1068   centos        "/bin/bash"   3 hours ago   Exited (0) 3 hours ago             pedantic_archimedes
68cc5263bb91   hello-world   "/hello"      4 hours ago   Exited (0) 4 hours ago             silly_kalam

退出容器

exit 退出并停止
ctrl+p+q不停止退出

[root @ VM-8-3-centos ~] # docker run -it centos /bin/bash
[root@b74fbc687731 /]# [root @ VM-8-3-centos ~] #
[root @ VM-8-3-centos ~] #
[root @ VM-8-3-centos ~] #
[root @ VM-8-3-centos ~] # docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
b74fbc687731   centos    "/bin/bash"   18 seconds ago   Up 17 seconds             sad_galileo

删除容器

docker rm id

删除所有容器,运行中的不能删除,加上-f
docker rm -f $(docker ps -aq)

[root @ VM-8-3-centos ~] # docker ps -a
CONTAINER ID   IMAGE         COMMAND       CREATED              STATUS                   PORTS     NAMES
b74fbc687731   centos        "/bin/bash"   About a minute ago   Up About a minute                  sad_galileo
3a8a6bcb1068   centos        "/bin/bash"   3 hours ago          Exited (0) 3 hours ago             pedantic_archimedes
68cc5263bb91   hello-world   "/hello"      4 hours ago          Exited (0) 4 hours ago             silly_kalam
[root @ VM-8-3-centos ~] # docker rm -f $(docker ps -aq)
b74fbc687731
3a8a6bcb1068
68cc5263bb91

删除所有容器,管道用法
docker ps -a -q | xargs docker rm

启动容器
docker start id

重启容器
docker restart id

停止容器
docker stop|kill id

常用的其他命令

后台启动容器
docker run -d id|name

docker run -d centos 发现启动后centos停止了

[root @ VM-8-3-centos ~] # docker run -d centos
5fba04196af1ad5306a2613ffe4f47d1e9de6c204ee4f4fac36718057da71f85
[root @ VM-8-3-centos ~] #
[root @ VM-8-3-centos ~] #
[root @ VM-8-3-centos ~] # docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root @ VM-8-3-centos ~] #

docker容器使用后台运行,必须要有一个前台进程 如-it。

docker发现后台没有对外提供的服务,就会自动停止。常见的nginx容器启动后立刻停止。

查看日志
docker logs [option] container

Usage:  docker logs [OPTIONS] CONTAINERFetch the logs of a containerOptions:--details        Show extra details provided to logs-f, --follow         Follow log output--since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42minutes)-n, --tail string    Number of lines to show from the end of the logs (default "all")-t, --timestamps     Show timestamps--until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for42 minutes)

docker -tf --tail 10 id

-tf 显示日志和时间戳
–tail num 显示最新的num行日志

[root @ VM-8-3-centos ~] # docker run -d centos /bin/sh -c "while true;do echo 123;sleep 1;done"
441a96813ecf2a22bcdf557295e05a7b546ac86228dcae496de35179bcdc4d73
[root @ VM-8-3-centos ~] #
[root @ VM-8-3-centos ~] # docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
441a96813ecf   centos    "/bin/sh -c 'while t…"   2 seconds ago   Up 2 seconds             dazzling_cerf
[root @ VM-8-3-centos ~] #
[root @ VM-8-3-centos ~] # docker logs -f -t --tail 10 441a96813ecf
2021-01-26T05:06:35.863389675Z 123
2021-01-26T05:06:36.865628642Z 123
2021-01-26T05:06:37.867550699Z 123
2021-01-26T05:06:38.869527853Z 123
2021-01-26T05:06:39.871557044Z 123

查看容器中的进程

docker top id

[root @ VM-8-3-centos ~] # docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
441a96813ecf   centos    "/bin/sh -c 'while t…"   2 minutes ago   Up 2 minutes             dazzling_cerf
[root @ VM-8-3-centos ~] #
[root @ VM-8-3-centos ~] #
[root @ VM-8-3-centos ~] # docker top 441a96813ecf
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                543                 520                 0                   13:05               ?                   00:00:00            /bin/sh -c while true;do echo 123;sleep 1;done
root                1648                543                 0                   13:10               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
[root @ VM-8-3-centos ~] #

查看镜像的元数据

docker inspect [option] name|id

[root @ VM-8-3-centos ~] # docker inspect 441a96813ecf
[{"Id": "441a96813ecf2a22bcdf557295e05a7b546ac86228dcae496de35179bcdc4d73","Created": "2021-01-26T05:05:36.377119331Z","Path": "/bin/sh",// 传递的参数"Args": ["-c","while true;do echo 123;sleep 1;done"],"State": {"Status": "running","Running": true,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 543,"ExitCode": 0,"Error": "","StartedAt": "2021-01-26T05:05:36.737112206Z","FinishedAt": "0001-01-01T00:00:00Z"},"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55","ResolvConfPath": "/var/lib/docker/containers/441a96813ecf2a22bcdf557295e05a7b546ac86228dcae496de35179bcdc4d73/resolv.conf","HostnamePath": "/var/lib/docker/containers/441a96813ecf2a22bcdf557295e05a7b546ac86228dcae496de35179bcdc4d73/hostname","HostsPath": "/var/lib/docker/containers/441a96813ecf2a22bcdf557295e05a7b546ac86228dcae496de35179bcdc4d73/hosts","LogPath": "/var/lib/docker/containers/441a96813ecf2a22bcdf557295e05a7b546ac86228dcae496de35179bcdc4d73/441a96813ecf2a22bcdf557295e05a7b546ac86228dcae496de35179bcdc4d73-json.log",..."Networks": {"bridge": {"IPAMConfig": null,"Links": null,"Aliases": null,"NetworkID": "1abd08b32583ccc8fdd1c0e8b091170919727da2abbce89f2120a75be9030e7d","EndpointID": "3ef6fa2e0b4777a09cfe58a675a4e182d16980cc532ff66be6abe8ffa1770a0e","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}}}}
]
[root @ VM-8-3-centos ~] #

进入当前正在运行的容器

方式1:
docker exec -it id /bin/bash

新建一个bash进入

[root @ VM-8-3-centos ~] # docker exec -it 441a96813ecf /bin/bash
[root@441a96813ecf /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@441a96813ecf /]#
[root@441a96813ecf /]# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 05:05 ?        00:00:00 /bin/sh -c while true;do echo 123;sleep 1;done
root       827     0  0 05:19 pts/0    00:00:00 /bin/bash
root       869     1  0 05:19 ?        00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep
root       870   827  0 05:19 pts/0    00:00:00 ps -ef
[root@441a96813ecf /]#

方式2:
docker attach id
进入正在运行的命令窗口

[root @ VM-8-3-centos ~] #  docker attach 441a96813ecf
123
123

从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的主机路径

docker cp fedc8c32b382:/tmp/1.txt ~/


部署Nginx

docker hub搜索nginx
docker pull nginx

docker images

-p 宿主机端口:内部端口
docker run -d --name nginx01 -p 3344:80 nginx

发起请求测试

[root @ VM-8-3-centos ~] # curl localhost:3344
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>

网页测试

端口暴露原理分析

docker帮我们做了一层mapping,主机端口映射到容器内部端口

nginx因为没服务可能会自动关闭

[root @ VM-8-3-centos ~] # docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                          PORTS     NAMES
8d8e7ee85c76   nginx     "/docker-entrypoint.…"   10 minutes ago   Exited (0) About a minute ago             nginx01
fedc8c32b382   centos    "/bin/bash"              25 minutes ago   Exited (0) 22 minutes ago                 wizardly_keldysh
[root @ VM-8-3-centos ~] # docker start 8d8e7ee85c76
8d8e7ee85c76
[root @ VM-8-3-centos ~] #
[root @ VM-8-3-centos ~] # docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                      PORTS                  NAMES
8d8e7ee85c76   nginx     "/docker-entrypoint.…"   10 minutes ago   Up 2 seconds                0.0.0.0:3344->80/tcp   nginx01
fedc8c32b382   centos    "/bin/bash"              25 minutes ago   Exited (0) 23 minutes ago                          wizardly_keldysh
[root @ VM-8-3-centos ~] # docker exec -it nginx01 /bin/bash
root@8d8e7ee85c76:/#

进入容器
docker exec -it nginx01 /bin/bash

[root @ VM-8-3-centos ~] # docker exec -it nginx01 /bin/bash
root@8d8e7ee85c76:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@8d8e7ee85c76:/# cd /etc/nginx
# 查看配置文件
root@8d8e7ee85c76:/etc/nginx# ls
conf.d          koi-utf  mime.types  nginx.conf   uwsgi_params
fastcgi_params  koi-win  modules     scgi_params  win-utf
root@8d8e7ee85c76:/etc/nginx#

停止容器

docker stop nginx01

部署tomcat

官方文档

官方加了 --rm ,一般是用来测试的,用完即删,前期不推荐这么用
–rm Automatically remove the container when it exits


docker pull tomcat:9.0

docker images

docker run -d -p 7010:8080 --name tomcat01 tomcat:9.0

访问测试

进入其查看内容,linux命令少了,webapps内没有网站,只是因为下载的镜像默认最小可用

[root @ VM-8-3-centos ~] # docker exec -it tomcat01 /bin/bash
root@96901ce17c70:/usr/local/tomcat#
root@96901ce17c70:/usr/local/tomcat#
root@96901ce17c70:/usr/local/tomcat# ls -al
total 172
drwxr-xr-x 1 root root  4096 Jan 13 08:25 .
drwxr-xr-x 1 root root  4096 Jan 13 08:19 ..
-rw-r--r-- 1 root root 18982 Dec  3 11:48 BUILDING.txt
-rw-r--r-- 1 root root  5409 Dec  3 11:48 CONTRIBUTING.md
-rw-r--r-- 1 root root 57092 Dec  3 11:48 LICENSE
-rw-r--r-- 1 root root  2333 Dec  3 11:48 NOTICE
-rw-r--r-- 1 root root  3257 Dec  3 11:48 README.md
-rw-r--r-- 1 root root  6898 Dec  3 11:48 RELEASE-NOTES
-rw-r--r-- 1 root root 16507 Dec  3 11:48 RUNNING.txt
drwxr-xr-x 2 root root  4096 Jan 13 08:25 bin
drwxr-xr-x 1 root root  4096 Jan 26 06:21 conf
drwxr-xr-x 2 root root  4096 Jan 13 08:25 lib
drwxrwxrwx 1 root root  4096 Jan 26 06:21 logs
drwxr-xr-x 2 root root  4096 Jan 13 08:25 native-jni-lib
drwxrwxrwx 2 root root  4096 Jan 13 08:25 temp
drwxr-xr-x 2 root root  4096 Jan 13 08:25 webapps
drwxr-xr-x 7 root root  4096 Dec  3 11:45 webapps.dist
drwxrwxrwx 2 root root  4096 Dec  3 11:43 work
root@96901ce17c70:/usr/local/tomcat#

webapps.dist中存放了原来webapps的东西,我们可以把它拷贝过去

root@96901ce17c70:/usr/local/tomcat# cd webapps
root@96901ce17c70:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager

回浏览器查看主页

部署Es+Kibana


因为Elasticsearch 是一个基于 Apache Lucene™ 的开源搜索引擎。无论在开源还是专有领域,Lucene 可以被认为是迄今为止最先进、性能最好的、功能最全的搜索引擎库。

kibana工具是提供了一个可视化的界面。
我们的es需要以基于 HTTP 协议,以 JSON 为数据交互格式的 RESTful API来进行交互!
Kibana 可以看出是一个操作 ElasticSeach 的客户端.
由于Kibana是用nodejs写的一个web项目。所以进程查询使用ps来进行查询区别es用jps查询!


查看官网

$ docker network create somenetwork
Run Elasticsearch:$ docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:tag

下载启动elasticsearch
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2

docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2

启动后就卡住了,es十分的占内存,docker stats查看cpu状态

访问9200端口查看运行状态

停止docker stop,增加内存限制再启动

通过-e进行环境配置修改,限制内存启动

docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2

再次查看一下内存状态,占用率降低

kibana和Es如何通信呢?

通过linux内网转发,而不是通过外网ip,需要了解docker的网络原理

可视化管理面板

使用portainer

Portainer is a lightweight management UI which allows you to easily manage your Docker host or Swarm cluster.

图形化后台管理面板

官方文档
https://documentation.portainer.io/v2.0/deploy/linux/

docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce

访问地址

配置好密码,即可进入主页

查看镜像

查看容器

继续浅入了解docker

docker镜像

镜像是什么?镜像是一种轻量级、可执行的独立软件包,包括代码、运行时库、环境和配置文件。

联合文件系统

UnionFs联合文件系统,Union文件系统是一种分层的轻量级且高性能的文件系统。镜像可以通过分层来进行继承,基于基础镜像,通过叠加文件,可以制作各种具体的应用镜像。

我们在下载镜像时,是一层一层文件下载的,有重复的文件时不会下载,优化存储空间。

docker镜像加载原理

docker的镜像实际上由一层一层的文件系统构成。

bootfs:系统启动需要引导加载,bootfs包含加载器和内核,服务运行起来了bootfs引导就没用了,bootfs会被卸载。bootfs对于所有镜像是公用的。

rootfs:包含/dev、/etc等典型的linux目录,所以说为什么启动一个容器就是启动了一个小的linux

对于一个精简的OS,rootfs可以很小,只需要包含最基本的命令和程序库即可,底层直接用宿主机的内核,镜像只要提供rootfs就可以了。可见对于不同的linux版本,bootfs基本是一致的,rootfs稍有差别。

分层

通过inspect指令查看元数据可以看到分层结构

docker inspect redis

  "RootFS": {"Type": "layers","Layers": ["sha256:cb42413394c4059335228c137fe884ff3ab8946a014014309676c25e3ac86864","sha256:8e14cb7841faede6e42ab797f915c329c22f3b39026f8338c4c75de26e5d4e82","sha256:1450b8f0019c829e638ab5c1f3c2674d117517669e41dd2d0409a668e0807e96","sha256:f927192cc30cb53065dc266f78ff12dc06651d6eb84088e82be2d98ac47d42a0","sha256:a24a292d018421783c491bc72f6601908cb844b17427bac92f0a22f5fd809665","sha256:3480f9cdd491225670e9899786128ffe47054b0a5d54c48f6b10623d2f340632"]},

镜像都基于一个基础镜像,通过叠加文件,可以制作新的镜像。类似于windows打安全补丁。

镜像之间的文件可以复用。镜像之间某层文件是相同的就不用重复下载了。

镜像与容器关联

镜像都是只读的,当容器启动时,一个新的可写层被加载到镜像顶部。
这一层就是就是容器层,容器之下叫镜像层。

docker commit 自己的容器

docker commit 提交容器成为一个新的镜像

类似git的操作

docker commit -m “描述信息” -a “作者” 容器id 目标镜像名[:tag]

首先启动我们的tomcat镜像作为例子
docker run -d -p 7010:8080 --name tomcat01 tomcat:9.0

docker exec -it tomcat01 /bin/bash

cp -r webapps.dist/* webapps

提交我们自己的镜像
docker commit -a="likeghee" -m="add webapps app" tomcat01 mytomcat:1.0
docker commit -m=“描述信息” -a=“作者” 容器id 目标镜像名[:tag]

以后我们可以使用自己镜像了

[root @ VM-8-3-centos ~] # docker images
REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
mytomcat                 1.0       fed9dbf45383   6 seconds ago   654MB

================

| docker入门分界线 |

================


动态添加端口

经常遇到这个问题,就顺便放到这了:https://zhuanlan.zhihu.com/p/65938559

容器数据卷

应用和环境打包成一个镜像,通过镜像可以启动容器,而数据不应该保存在容器中,容器一删除数据都丢失。

比如安装mysql容器,容器删了里面存的数据一块丢了,等于删库跑路了

什么是容器数据卷?
我们希望容器之间有一个数据共享的技术,Docker容器中产生的数据同步到本地,数据存放在本地数据就不会丢失了,简单的说,就是将容器内的目录挂载到linux文件系统上。

作用:容器数据持久化和同步,容器间数共享。

使用-v命令挂载

docker run -it -v 主机目录:容器目录

docker run -it --name centos01 -v /home/test:/home centos /bin/bash

测试文件同步

查看inspect

  "Mounts": [{"Type": "bind","Source": "/home/test","Destination": "/home","Mode": "","RW": true,"Propagation": "rprivate"}],

以后我们修改文件只要在linux主机上修改即可

部署mysql-同步数据

官方文档操作:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

-d 后台运行
-p 端口映射
-v 数据卷挂载
:/etc/mysql/conf 配置文件
:/var/lib/mysql 数据文件
-e 环境配置
MYSQL_ROOT_PASSWORD=配置密码

docker run -d -p 3306:3306 -v /home/mysql01/conf:/etc/mysql/conf -v /home/mysql01/data:/var/lib/mysql --name mysql01 -e MYSQL_ROOT_PASSWORD=■■■■■■■ mysql:5.7

顺便把phpmyadmin开了,访问likeghee.ltd:8888即可
docker run -d --name myadmin -e PMA_HOST=likeghee.ltd -e PMA_PORT=3306 -p 8888:80 phpmyadmin/phpmyadmin

随心玩玩(四)docker从入门到入土相关推荐

  1. docker从入门到入土(基础篇)

    Hello~大家好,这里是KOKO师傅! 今天我们来学习Docker与微服务实战的相关内容. docker简介 docker是什么 docker为什么出现 AB法则: before after bef ...

  2. docker从入门到入土(进阶篇)

    Hello~大家好,这里是KOKO! 之前我们学习了docker的基础篇内容,今天我们来深入了解进阶篇的内容. 在学习进阶篇之前,请大家务必保证基础篇的那些常用命令都进行了练习并且已经熟练掌握! do ...

  3. Docker从入门到实战(四)

    一步一步走,写小白都能看懂的文章,将持续更新中,敬请期待! Docker从入门到实战(四) Docker基础 一:Docker基本操作 一般情况安装Docker之后系统会自动创建一个Docker的用户 ...

  4. Docker从入门到实践

    一般说来 SPA 的项目我们只要启一个静态文件 Server 就可以了,但是针对传统项目就不一样了,一个项目会依赖很多服务端程序.之前我们的开发模式是在一台开发机上部署开发环境,所有人都在这台开发机上 ...

  5. 《 Docker 技术入门与实战 》读书笔记 ( CentOS 安装 Docker )

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. PS :个人所有读书笔记只记录个人想要的内容,很可能原书大量内容没有纳入笔记中... ... 以下全 ...

  6. Docker 精通之入门

    Docker 精通系列 Docker 精通之入门 Docker 精通之微服务 Docker 精通之常用命令 Docker 精通之 Dockerfile 2013年发布至今, Docker 一直广受瞩目 ...

  7. Docker快速入门,看这个就够了

    Docker快速入门 一.Docker介绍 1.1 Docker背景 1.2 Docker概念 1.3 Docker的优势 1.4 Docker的三个基本概念 二.Docker的安装和使用 2.1 安 ...

  8. 《Docker 技术入门与实践》-读书笔记二

    <Docker 技术入门与实践>-读书笔记一 <Docker 技术入门与实践>-读书笔记二 一.数据管理 用户在使用 Docker 的过程中,往往需要能查看容器内应用产生的数据 ...

  9. Docker 快速入门(一文上手 Docker)

    通过本篇文章,就可以达到在 Window 或 Linux 上手 Docker(有点长,可以根据目录选择你需要的内容看) 文章图片没有带过来,涉及的图片较多,就不一一挪了,大家可以直接看我 GitCha ...

最新文章

  1. Ubuntu安装Flash视频插件
  2. android gradle.properties
  3. python----字符串方法
  4. 关于Android中Intent传递Serialzilable数据的问题
  5. [iOS]CIFilter滤镜
  6. Ubuntu9.04更新源
  7. JS根据分数,计算名次(分数相同名次相同)
  8. 【安卓的一个进程等级】
  9. H264--语法及结构--2
  10. 《When you are old》一如苇中的风,轻柔却难忘
  11. matlab lcl滤波器,LCL滤波器参数性能的比较
  12. APP实用总结—Android图书馆
  13. face++实现人脸识别及人脸相似度对比
  14. notifyAll()方法
  15. 写给程序员的 n+1 条建议
  16. Uniforms(uniform变量)
  17. SQLyog Ultimate旗舰版软件安装使用
  18. 51nod 1001
  19. 杯具,万达电商又换CEO
  20. tl494c封装区别_TL494芯片详细资料

热门文章

  1. c语言邻接表做公园导游系统,公园导游管理系统
  2. 怎样做一个企业网站建设规划书?
  3. 制作动态网站比较好用的软件工具
  4. 经典linux c程序,经典 c 程序 100 例
  5. Arduino 控制9g舵机
  6. AUTOSAR DiagnosticLogAndTrace DLT(三)-- 消息的发送、DLT命令的发送与接收
  7. 大数据实时传输组件Maxwellmaxwell中遇到的问题
  8. 解决微信公众账号申请认证方面的问题
  9. 网页平面设计/广州平面UI设计培训就业班课程
  10. 使用stm32产生三角波和正弦波