官方命令图
docker 命令图
1. 用docker部署nginx
补充:端口暴露的概念==docker run -d -p 宿主主机端口:容器内端口 [镜像名称] [cmd]
1.搜索nginx镜像===建议去官网搜索
https://hub.docker.com/
[root@docker ~]# docker search nginx
......
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 15175 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 2048 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 816 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 217
linuxserver/nginx An Nginx container, brought to you by LinuxS… 149
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 135 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 123 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 102 [OK]
jasonrivers/nginx-rtmp Docker images to host
......
2.获取nginx镜像
[root@docker ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
b4d181a07f80: Pull complete
66b1c490df3f: Pull complete
d0f91ae9b44c: Pull complete
baf987068537: Pull complete
6bbc76cbebeb: Pull complete
32b766478bc2: Pull complete
Digest: sha256:353c20f74d9b6aee359f30e8e4f69c3d7eaea2f610681c4a95849a2fd7c497f9
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
3.查看镜像列表
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 4cdc5dd7eaad 8 days ago 133MB
4.以映射端口和后台形式启动nginx
[root@docker ~]# docker run -d -p 3322:80 --name nginx01 nginx
4cdc9dff795f6e4b8a7239f907467c7b38abab980654cfe569e2c71b74247682
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4cdc9dff795f nginx "/docker-entrypoint.…" 4 seconds ago Up 2 seconds 0.0.0.0:3322->80/tcp, :::3322->80/tcp nginx01
5.测试访问
内网
[root@docker ~]# curl 127.0.0.1:3322
.....
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
.....
or
[root@docker ~]# curl localhost:3322
外网
http://10.0.0.30:3322/
6.进入容器
[root@docker ~]# docker exec -it 4cdc9dff795f /bin/bash
root@4cdc9dff795f:/etc# cd /etc/nginx/
root@4cdc9dff795f:/etc/nginx# ls
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
root@4cdc9dff795f:/etc/nginx# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@4cdc9dff795f:/etc/nginx# exit
exit
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4cdc9dff795f nginx "/docker-entrypoint.…" 14 minutes ago Up 14 minutes 0.0.0.0:3322->80/tcp, :::3322->80/tcp nginx01
[root@docker ~]# docker stop nginx01
这时候外网进不能访问
思考:
每次修改nginx的配置文件都是非常麻烦的,我要是在容器外部提供一个映射路径,达到在容器外修改文件名,容器内部就可以自动修改? -v:数据卷
docker run -it -v /etc/nginx:/etc/nginx/nginx.conf nginx
2.用docker部署Tomcat
官方的使用:
docker run --it -rm tomcat:9.0
我们之前的启动都是后台,停止了容器后台还可以看见。docker run --it -rm tomcat:9.0 一般用来测试你,用完即删除。
1.获取Tomcat镜像
[root@docker ~]# docker pull tomcat
9.0: Pulling from library/tomcat
0bc3020d05f1: Pull complete
a110e5871660: Pull complete
83d3c0fa203a: Pull complete
a8fd09c11b02: Pull complete
96ebf1506065: Pull complete
b8bf70f9cc4d: Pull complete
3f6da67b9e68: Pull complete
257407776119: Pull complete
7bd0a187fb92: Pull complete
307fc4df04c9: Pull complete
Digest: sha256:a5abf192aceed45620dbb2e09f8abdec2b96108b86365a988c85e753c28cd36b
Status: Downloaded newer image for tomcat:9.0
docker.io/library/tomcat:9.0
2.以映射端口和后台形式启动Tomcat
[root@docker ~]# docker run -d -p 3355:8080 --name tomcat01 tomcat
Unable to find image 'tomcat:latest' locally
latest: Pulling from library/tomcat
Digest: sha256:a5abf192aceed45620dbb2e09f8abdec2b96108b86365a988c85e753c28cd36b
Status: Downloaded newer image for tomcat:latest
bbac1a99bb7fabef7aea3604e67773af46a01b5c620bd86549efd4bf4a78b765
3.查看容器列表
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 4cdc5dd7eaad 8 days ago 133MB
tomcat latest 36ef696ea43d 12 days ago 667MB
4.查看正在运行的容器
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bbac1a99bb7f tomcat "catalina.sh run" 6 minutes ago Up 6 minutes 0.0.0.0:3355->8080/tcp, :::3355->8080/tcp tomcat01
4cdc9dff795f nginx "/docker-entrypoint.…" 50 minutes ago Up 50 minutes 0.0.0.0:3322->80/tcp, :::3322->80/tcp nginx01
5.测试访问
[root@docker ~]# curl localhost:3355
<!doctype html><html lang="en"><head><title>HTTP Status 404 ? Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 ? Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.50</h3></body></html>[root@docker ~]#
外网:10.0.0.30:3355
由于这时候访问页面报错404,因为webapps目录下没有文件导致。
发现问题:1.linux的命令少了
2.没有webapps======阿里云镜像的原因,默认是最小的镜像,保证最小可运行环境。
1.进入容器
[root@docker ~]# docker exec -it tomcat01 /bin/bash
root@bbac1a99bb7f:/usr/local/tomcat# ls -al
total 128
drwxr-xr-x 1 root root 30 Jul 2 19:58 .
drwxr-xr-x 1 root root 20 Jun 26 05:00 ..
-rw-r--r-- 1 root root 18949 Jun 28 08:46 BUILDING.txt
-rw-r--r-- 1 root root 6210 Jun 28 08:46 CONTRIBUTING.md
-rw-r--r-- 1 root root 57092 Jun 28 08:46 LICENSE
-rw-r--r-- 1 root root 2333 Jun 28 08:46 NOTICE
-rw-r--r-- 1 root root 3372 Jun 28 08:46 README.md
-rw-r--r-- 1 root root 6898 Jun 28 08:46 RELEASE-NOTES
-rw-r--r-- 1 root root 16507 Jun 28 08:46 RUNNING.txt
drwxr-xr-x 2 root root 4096 Jul 2 19:58 bin
drwxr-xr-x 1 root root 22 Jul 15 09:46 conf
drwxr-xr-x 2 root root 4096 Jul 2 19:58 lib
drwxrwxrwx 1 root root 177 Jul 15 09:46 logs
drwxr-xr-x 2 root root 134 Jul 2 19:58 native-jni-lib
drwxrwxrwx 2 root root 30 Jul 2 19:58 temp
drwxr-xr-x 2 root root 6 Jul 2 19:58 webapps
drwxr-xr-x 7 root root 81 Jun 28 08:46 webapps.dist
drwxrwxrwx 2 root root 6 Jun 28 08:46 work
2.拷贝webapps.dist输出目录的文件到webapps目录下
root@bbac1a99bb7f:/usr/local/tomcat# cp -r webapps.dist/* webapps
root@bbac1a99bb7f:/usr/local/tomcat# cd webapps
root@bbac1a99bb7f:/usr/local/tomcat/webapps# ls -l
total 4
drwxr-xr-x 3 root root 223 Jul 15 10:00 ROOT
drwxr-xr-x 15 root root 4096 Jul 15 10:00 docs
drwxr-xr-x 7 root root 99 Jul 15 10:00 examples
drwxr-xr-x 6 root root 79 Jul 15 10:00 host-manager
drwxr-xr-x 6 root root 114 Jul 15 10:00 manager
root@bbac1a99bb7f:/usr/local/tomcat/webapps#
3.重新刷新web页面
用docker 部署ES+kibana
ES 暴露的端口特别多
ES 特别消耗内存
ES 的数据一般需要放在安全目录 挂载
1.下载启动ES
[root@docker ~]# 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
57cdfdcfc6b908d06f5fa497c72303a1e1ab74ab5b71f7fbf8cc7221f50a4573
-e:单节点
-p:端口
9300端口:ES节点之间通讯使用
9200端口:ES节点与外部通讯使用
2.启动了linux非常卡,用docker stats 查看cpu的状态
ES 非常消耗内存,1.xG 1核两G
停止整个docker
测试一下ES成功了
赶紧关闭ES,增加内存的限制
参考ES的官网:
https://www.elastic.co/guide/en/elasticsearch/reference/7.6/index.html
修改配置文件 -e 环境配置修改
3.访问测试
[root@docker ~]# curl localhost:9200
{
"name" : "57cdfdcfc6b9",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "6p-Aa8XyTOio2peqwCuFiw",
"version" : {
"number" : "7.6.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
"build_date" : "2020-03-26T06:34:37.794943Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
4.查看CPU的状态
5.赶紧关闭ES
[root@docker ~]# docker stop 57cdfdcfc6b9
57cdfdcfc6b9
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4cdc9dff795f nginx "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:3322->80/tcp, :::3322->80/tcp nginx01
6.增加内存的限制并重新启动
[root@docker ~]# docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xmx=64m -Xmx=512m" elasticsearch:7.6.2
docker: Error response from daemon: Conflict. The container name "/elasticsearch" is already in use by container "57cdfdcfc6b908d06f5fa497c72303a1e1ab74ab5b71f7fbf8cc7221f50a4573". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
这时候需要换名字即可
[root@docker ~]# docker run -d --name elasticsearch04 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
f40267a6f702f8d6bc08a2b702fb57a9855320cb3ff1b6075b0fb2211c3beaa2
[root@docker ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f40267a6f702 elasticsearch:7.6.2 "/usr/local/bin/dock…" 3 seconds ago Up 3 seconds 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp elasticsearch04
7.再次查看CPU、内存的状态
这时候的效果是我们想要的
测试访问:
[root@docker ~]# curl 127.0.0.1:9200
{
“name” : “f40267a6f702”,
“cluster_name” : “docker-cluster”,
“cluster_uuid” : “YM8tULljRei1dfL07k1uDQ”,
“version” : {
“number” : “7.6.2”,
“build_flavor” : “default”,
“build_type” : “docker”,
“build_hash” : “ef48eb35cf30adf4db14086e8aabd07ef6fb113f”,
“build_date” : “2020-03-26T06:34:37.794943Z”,
“build_snapshot” : false,
“lucene_version” : “8.4.0”,
“minimum_wire_compatibility_version” : “6.8.0”,
“minimum_index_compatibility_version” : “6.0.0-beta1”
},
“tagline” : “You Know, for Search”
}
作业:使用kibana连接ES 思考网络如何连接过去
portainer可视化安装
[root@docker ~]# docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
portainer 先用着
Rancher (cicd)
什么是portainer?
是docker的图形化界面管理工具。提供一个后台面板供我们操作!
[root@docker ~]# docker run -d -p 8080:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
访问测试:内网
[root@docker ~]# curl 10.0.0.30:8088
<meta name="msapplication-config" content="4806ce9049e1e082dd3da4063ceb0eea.xml" />
<meta name="theme-color" content="#ffffff" />
<link href="vendor.1.css" rel="stylesheet"><link href="main.50213a000e42f5c05bcc.css" rel="stylesheet"></head>
<body ng-controller="MainController">
<div
id="page-wrapper"
ng-class="{
open: toggle && ['portainer.auth', 'portainer.updatePassword', 'portainer.init.admin', 'portainer.init.endpoint'].indexOf($state.current.name) === -1,
nopadding: ['portainer.auth', 'portainer.updatePassword', 'portainer.init.admin', 'portainer.init.endpoint'].indexOf($state.current.name) > -1 || applicationState.loading
}"
ng-cloak
>
<div id="sideview" ui-view="sidebar" ng-if="!applicationState.loading"></div>
<div id="content-wrapper">
<div class="page-content">
<div class="page-wrapper" ng-if="applicationState.loading">
<!-- loading box -->
<div class="container simple-box">
<div class="col-md-6 col-md-offset-3 col-sm-6 col-sm-offset-3">
<!-- loading box logo -->
<div class="row">
<img ng-if="logo" ng-src="{{ logo }}" class="simple-box-logo" />
<img ng-if="!logo" src="5da83cfb4883a59354abeff852cb7394.png" class="simple-box-logo" alt="Portainer" />
</div>
<!-- !loading box logo -->
<!-- panel -->
<div class="row" style="text-align: center;">
Loading Portainer...
<i class="fa fa-cog fa-spin" style="margin-left: 5px;"></i>
</div>
<!-- !panel -->
</div>
</div>
<!-- !loading box -->
</div>
<!-- Main Content -->
<div id="view" ui-view="content" ng-if="!applicationState.loading"></div> </div
><!-- End Page Content --> </div
><!-- End Content Wrapper --> </div
><!-- End Page Wrapper -->
<script type="text/javascript" src="vendor.50213a000e42f5c05bcc.js"></script><script type="text/javascript" src="main.50213a000e42f5c05bcc.js"></script></body></html
>
访问测试:外网:8088
选择本地的
进入之后的面板
可视化面板平时不会使用,自己玩玩即可。