当前位置:   article > 正文

随心玩玩(四)docker从入门到入土_79896.pw8

79896.pw8

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



前置学习 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"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

开始学习安装,一起始于文档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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

跟着文档做就好,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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

正确安装显示

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: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/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

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
  • 1
  • 2
  • 3

卸载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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

工作原理

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 --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  只显示id
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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 # 真实地址
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

删除镜像

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

通过docker run命令

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

常用Option

--name centos1  容器名字
--d  后台方式运行
-it  使用交互方式运行
-p  指定端口
  -p 宿主机端口:容器端口(常用)
  -p 容器端口
  -p ip:主机端口:容器端口
-P  随机指定端口
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

启动并进入容器
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

查看运行中的容器

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
  • 1
  • 2
  • 3
  • 4

退出容器

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

删除容器

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

删除所有容器,管道用法
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 ~] #
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

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

查看日志
docker logs [option] container

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --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 42
                       minutes)
  -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 for
                       42 minutes)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

查看容器中的进程

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 ~] #
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

查看镜像的元数据

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

进入当前正在运行的容器

方式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 /]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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

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

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

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

在这里插入图片描述


部署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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

网页测试
在这里插入图片描述

端口暴露原理分析

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:/#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

进入容器
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#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

停止容器

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#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

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

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

回浏览器查看主页
在这里插入图片描述

部署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
  • 1
  • 2
  • 3
  • 4

下载启动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"
            ]
        },
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

镜像都基于一个基础镜像,通过叠加文件,可以制作新的镜像。类似于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
  • 1
  • 2
  • 3

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

| 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"
            }
        ],

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

以后我们修改文件只要在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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/524147
推荐阅读
相关标签