Docker学习(二)例子练习

部署Nginx

  • 搜索镜像去docker hub上
  • 下载镜像 docker pull nginx
  • docker run -d 后台运行 —name nginx01 -p 10024:80
1
2
(base) root@localhost:/home/cpss# docker run -d --name nginx01 -p 10024:80 nginx
84960293d8409dc9f7e70be88027c2149ece57d7cf02dc4d71eb81fe1651fc96
1
2
3
(base) root@localhost:/home/cpss# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84960293d840 nginx "/docker-entrypoint.…" 21 seconds ago Up 20 seconds 0.0.0.0:10024->80/tcp, :::10024->80/tcp nginx01
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
(base) root@localhost:/home/cpss# curl localhost:10024
<!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>

-p 暴露端口的概念

1
2
3
4
5
6
7
8
9
10
docker exec -it nginx01 /bin/bash 进入容器
root@84960293d840:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx


(base) root@localhost:/home/cpss# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
84960293d840 nginx "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 0.0.0.0:10024->80/tcp, :::10024->80/tcp nginx01
(base) root@localhost:/home/cpss# docker stop 84960293d840
84960293d840

思考:每次改动nginx配置文件,都需要进入容器内部,十分麻烦

可以在容器外部提供一个映射路径,达到在容器修改文件名,内部容器就可以自动修改。

这个技术是 -v 数据卷技术

部署 ES+Kibana

ES暴露端口很多,也耗内存,数据一般需要放到安全目录,挂载

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
# --net somenetwork 网络配置
# --rm 用完就删掉

# 启动 elasticsearch 比较耗内存
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.14.0


CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a9057d9c6e50 elasticsearch 2.54% 32.49GiB / 125.8GiB 25.83% 11.1kB / 1.94kB 100MB / 292MB 98

(base) root@linux:/home/cpss# curl localhost:9200
{
"name" : "a9057d9c6e50",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "lkLPT_ssQ2CV30B55gn4bg",
"version" : {
"number" : "7.14.0",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "dd5a0a2acaa2045ff9624f3729fc8a6f40835aa1",
"build_date" : "2021-07-29T20:49:32.864135063Z",
"build_snapshot" : false,
"lucene_version" : "8.9.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

# 增加内存限制,修改卑职文件 -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.14.0

CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
e40105c39e81 elasticsearch1 281.21% 676.5MiB / 125.8GiB 0.53% 2.84kB / 0B 26.9MB / 1.22MB 103