Docker - 安装常用服务
防火墙
对外开放访问,需要开放指定的端口提供对外访问
# 防火墙状态
systemctl status firewalld
# 开启防火墙
systemctl start firewalld
# 关闭防火墙
systemctl stop firewalld
# 开放端口
firewall-cmd --zone=public --add-port=10002/tcp --permanent
# 命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效( —permanent放在前面与后面都行)
# 重启防火墙
firewall-cmd --reload
# 验证修改是否生效
firewall-cmd --zone= public --query-port=10002/tcp
# 移除指定端口
firewall-cmd --permanent --remove-port=123/tcp
# 查看已开放的端口
firewall-cmd --zone=public --list-ports
Nginx
- 搜索镜像 Search 【建议去网页搜索】
- 下载镜像 pull
# 运行 nginx 容器
[root@iZ2zeg7mctvft5renx1qvbZ home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c20060033e06 8 days ago 187MB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@iZ2zeg7mctvft5renx1qvbZ home]# docker run -d --name nginx-docker -p 80:80 nginx
97721fc83a19f5907d0bbfe47d5fd898285fe7aac1146f952a77b7dd20cd2d63
[root@iZ2zeg7mctvft5renx1qvbZ home]# curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
# 进入 nginx
[root@iZ2zeg7mctvft5renx1qvbZ home]# docker exec -it nginx-docker /bin/bash
root@97721fc83a19:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@97721fc83a19:/# cd /etc/nginx/
root@97721fc83a19:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
端口暴露的概念
使用阿里云ECS需要配置安全组入方向
Tomcat
# 官方使用
docker run -it --rm tomcat:9.0
# 我们之前的启动都是后台,停止了容器之后,容器还是可以查到 docker run -it --rm 一般用来测试,用完就删除
# 下载在启动
docker pull tomcat
# 启动运行
[root@iZ2zeg7mctvft5renx1qvbZ home]# docker run -d -p 8080:8080 tomcat
889e4b97acb2004fa98d8e2ec77f4ccca670fabeac3feb4a5d057bd770b724a7
# 进入容器
[root@iZ2zeg7mctvft5renx1qvbZ home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
889e4b97acb2 tomcat "catalina.sh run" 6 minutes ago Up 6 minutes 0.0.0.0:8081->8080/tcp, :::8081->8080/tcp lucid_matsumoto
[root@iZ2zeg7mctvft5renx1qvbZ home]# docker exec -it 889e4b97acb2 /bin/bash
root@889e4b97acb2:/usr/local/tomcat# ls -a
. .. bin BUILDING.txt conf CONTRIBUTING.md lib LICENSE logs native-jni-lib NOTICE README.md RELEASE-NOTES RUNNING.txt temp webapps webapps.dist work
root@889e4b97acb2:/usr/local/tomcat# ls -al
total 128
drwxr-xr-x 1 root root 30 Oct 31 03:36 .
drwxr-xr-x 1 root root 20 Oct 31 03:34 ..
drwxr-xr-x 2 root root 4096 Oct 31 03:36 bin
-rw-r--r-- 1 root root 20122 Oct 11 23:38 BUILDING.txt
drwxr-xr-x 1 root root 22 Nov 9 05:43 conf
-rw-r--r-- 1 root root 6210 Oct 11 23:38 CONTRIBUTING.md
drwxr-xr-x 2 root root 4096 Oct 31 03:36 lib
-rw-r--r-- 1 root root 60393 Oct 11 23:38 LICENSE
drwxrwxrwt 1 root root 80 Nov 9 05:43 logs
drwxr-xr-x 2 root root 158 Oct 31 03:36 native-jni-lib
-rw-r--r-- 1 root root 2333 Oct 11 23:38 NOTICE
-rw-r--r-- 1 root root 3398 Oct 11 23:38 README.md
-rw-r--r-- 1 root root 6776 Oct 11 23:38 RELEASE-NOTES
-rw-r--r-- 1 root root 16076 Oct 11 23:38 RUNNING.txt
drwxrwxrwt 2 root root 30 Oct 31 03:36 temp
drwxr-xr-x 2 root root 6 Oct 31 03:36 webapps
drwxr-xr-x 7 root root 81 Oct 11 23:38 webapps.dist
drwxrwxrwt 2 root root 6 Oct 11 23:38 work
# 将 webapps.dist 拷贝到 webapps 下使用
root@889e4b97acb2:/usr/local/tomcat# ls -a
. .. bin BUILDING.txt conf CONTRIBUTING.md lib LICENSE logs native-jni-lib NOTICE README.md RELEASE-NOTES RUNNING.txt temp webapps webapps.dist work
root@889e4b97acb2:/usr/local/tomcat# cp -r webapps.dist/* webapps/
root@889e4b97acb2:/usr/local/tomcat# cd webapps
root@889e4b97acb2:/usr/local/tomcat/webapps# ls
docs examples host-manager manager ROOT
root@889e4b97acb2:/usr/local/tomcat/webapps# ls -a
. .. docs examples host-manager manager ROOT
ES + kibana
# ES 暴露端口多
# ES 十分耗内存
# ES 的数据一般需要放置到安全目录!挂载
# --net somenetwork ? 网络配置
# 启动 elasticsearch
# 命令 【如果是内存配置较低不建议直接使用,参考下面的添加内存使用限制大小的命令启动】
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
Unable to find image 'elasticsearch:7.6.2' locally
7.6.2: Pulling from library/elasticsearch
ab5ef0e58194: Pull complete
c4d1ca5c8a25: Pull complete
941a3cc8e7b8: Pull complete
43ec483d9618: Pull complete
c486fd200684: Pull complete
1b960df074b2: Pull complete
1719d48d6823: Pull complete
Digest: sha256:1b09dbd93085a1e7bca34830e77d2981521a7210e11f11eda997add1c12711fa
Status: Downloaded newer image for elasticsearch:7.6.2
7331b21d4c7bda10271d8ff769b7580e6d53166c63f97ec7509b92ed172207e7
# elasticsearch 本身是非常占用内存的,通过 docker stats 查看内存占用【这里使用命令时,内存占用过大,命令结果加载不出来。】
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker stats
# 如果启动了,关闭后增加内存的限制,修改配置文件 -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
[root@iZ2zeg7mctvft5renx1qvbZ ~]# 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
26d606f02c30a3643f39c8b2795c9e7872766f25e4f4d9c150f9e6a1ad55b0ed
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
26d606f02c30 elasticsearch 1.55% 232.2MiB / 1.678GiB 13.51% 962B / 0B 120MB / 0B 19
可视化
-
portainer (先用这个)
Docker图形化界面管理工具!提供一个后台面板供我们操作!
# 命令
docker run -d -p 8080:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
[root@iZ2zeg7mctvft5renx1qvbZ ~]# docker run -d -p 8080:9000 \
> --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
Unable to find image 'portainer/portainer:latest' locally
latest: Pulling from portainer/portainer
772227786281: Pull complete
96fd13befc87: Pull complete
0bad1d247b5b: Pull complete
b5d1b01b1d39: Pull complete
Digest: sha256:47b064434edf437badf7337e516e07f64477485c8ecc663ddabbe824b20c672d
Status: Downloaded newer image for portainer/portainer:latest
9ef7881ea7b7e59b949d5da57cebcd5da6d7eb282fccb0ce6be838b0f19ad75f
访问测试:
-
Rancher (CI/CD再用)
Rancher是一个开源的企业级容器管理平台。Rancher提供了在生产环境中使用的管理Docker和Kubernetes的全栈化容器部署与管理平台,简单的说就是可以用rancher来管理docker和kubernetes集群,比如管理主机,容器的启动、停止和删除等。
说明:摘抄B站 “狂神说” 视频