部署Nginx
寻找镜像
docker search nginx
:默认最新版下载镜像
docker pull nginx
[root@iZwz9hv1phm24s3jicy8x1Z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 3 months ago 141MB
centos latest 5d0da3dc9764 6 months ago 231MB
启动镜像
docker run -d --name nginx01 -p 3344:80 nginx
-d
表示后台启动--name 名称
为服务起一个名字,这样我们可以启动多个服务--p 主机端口:镜像端口
镜像端口到主机端口的映射,通过外网的端口就可以直接访问到docker镜像里面的服务。查看运行的容器
[root@iZwz9hv1phm24s3jicy8x1Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c1c91b662574 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01
尝试本机访问
[root@iZwz9hv1phm24s3jicy8x1Z ~]# curl localhost:3344
<!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>
端口暴露详情(狂神)
通过外网访问测试
注意:阿里云的安全组要打开这个端口才可以。
进入docker
[root@iZwz9hv1phm24s3jicy8x1Z ~]# docker exec -it nginx01 /bin/bash
root@c1c91b662574:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
root@c1c91b662574:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
这样子部署好简单,但是我们每次想要修改配置都需要进入容器内部来修改nginx的配置,这样很麻烦,怎么样才能不进入容器直接在外部进行修改呢?(可以使用数据卷 -v的技术)
部署Tomcat
官方的配置:docker run -it --rm tomcat:9.0
docker ps
查看是没有的。启动tomcat镜像
[root@iZwz9hv1phm24s3jicy8x1Z ~]# docker run -d -p 3344:8080 --name tomcat01 b8e65a4d736d
b0bc12cf3f803fa2850986a1f2b7e706d6e4acd4ac72828c51c639a3d7a72d4a
进入容器
[root@iZwz9hv1phm24s3jicy8x1Z ~]# docker exec -it tomcat01 /bin/bash
root@b0bc12cf3f80:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
查看我们的容器内部的tomcat的内部文件
root@b0bc12cf3f80:/usr/local/tomcat# ls -al
total 172
drwxr-xr-x 1 root root 4096 Dec 22 17:16 .
drwxr-xr-x 1 root root 4096 Apr 13 09:11 ..
-rw-r--r-- 1 root root 18970 Dec 2 14:30 BUILDING.txt
-rw-r--r-- 1 root root 6210 Dec 2 14:30 CONTRIBUTING.md
-rw-r--r-- 1 root root 57092 Dec 2 14:30 LICENSE
-rw-r--r-- 1 root root 2333 Dec 2 14:30 NOTICE
-rw-r--r-- 1 root root 3378 Dec 2 14:30 README.md
-rw-r--r-- 1 root root 6898 Dec 2 14:30 RELEASE-NOTES
-rw-r--r-- 1 root root 16507 Dec 2 14:30 RUNNING.txt
drwxr-xr-x 2 root root 4096 Dec 22 17:16 bin
drwxr-xr-x 1 root root 4096 Apr 13 09:11 conf
drwxr-xr-x 2 root root 4096 Dec 22 17:16 lib
drwxrwxrwx 1 root root 4096 Apr 13 09:11 logs
drwxr-xr-x 2 root root 4096 Dec 22 17:16 native-jni-lib
drwxrwxrwx 2 root root 4096 Dec 22 17:16 temp
drwxr-xr-x 2 root root 4096 Dec 22 17:16 webapps
drwxr-xr-x 7 root root 4096 Dec 2 14:30 webapps.dist
drwxrwxrwx 2 root root 4096 Dec 2 14:30 work
进入webapps,发现是空的,所以上面的404很正常
root@b0bc12cf3f80:/usr/local/tomcat# cd webapps
root@b0bc12cf3f80:/usr/local/tomcat/webapps# ls
由于只是保证最小的可运行环境,所以很多的不必须的文件都没有,所以才会出现命令和文件没有的情况。
我们可以发现webapps里面的文件好像是原来的问价,我们拷贝一下
root@b0bc12cf3f80:/usr/local/tomcat# ls
BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist
CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
root@b0bc12cf3f80:/usr/local/tomcat# cp -r webapps.dist/* webapps/
root@b0bc12cf3f80:/usr/local/tomcat# cd webapps
root@b0bc12cf3f80:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
root@b0bc12cf3f80:/usr/local/tomcat/webapps#
但是这里还是存在一个可以优化的地方,就是我们每次部署项目都要进入容器,很麻烦,怎么能够做到不进入容器就可以部署我们的项目。
也就是可以把容器外部的配置或者部署直接映射到容器的内部就好了。
部署ES
注意的问题:
ES暴漏的端口很多
ES十分消耗内存
ES的数据一般要放置在安全目录
--net somenetwork
:docker的网络命令(把这个先去掉)
$ docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
查看启动过的容器
[root@iZwz9hv1phm24s3jicy8x1Z ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
978533b71b0a elasticsearch:7.6.2 "/usr/local/bin/dock…" About a minute ago Exited (137) About a minute ago elasticsearch
b0bc12cf3f80 b8e65a4d736d "catalina.sh run" 29 minutes ago Up 29 minutes 0.0.0.0:3344->8080/tcp, :::3344->8080/tcp tomcat01
c1c91b662574 nginx "/docker-entrypoint.…" About an hour ago Exited (0) 45 minutes ago nginx01
0c0ad128fbc9 centos "/bin/bash" 7 hours ago Exited (0) 7 hours ago gallant_wu
es启动之后由于我的服务器很垃圾。直接卡死,所以要设置内存限制。
重新使用命令
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@iZwz9hv1phm24s3jicy8x1Z ~]# 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
013e22f9e71af48d20cf4f731767d4380299acc5bd463b3cdab0197e16c84c45
[root@iZwz9hv1phm24s3jicy8x1Z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
013e22f9e71a elasticsearch:7.6.2 "/usr/local/bin/dock…" 20 seconds ago Up 16 seconds 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp elasticsearch
[root@iZwz9hv1phm24s3jicy8x1Z ~]# docker stats 013e22f9e71a
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
013e22f9e71a elasticsearch 0.00% 388.2MiB / 1.923GiB 19.72% 2.16kB / 0B 151MB / 324kB 40
可以看到这个时候只是占用一部分的内存,不是那么卡了。
我们访问本地的
[root@iZwz9hv1phm24s3jicy8x1Z ~]# curl localhost:9200
{
"name" : "013e22f9e71a",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "RCgh6R-BQ8K2uLDbYI0YQA",
"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"
}
可以看到elastic Search已经启动了。
可视化
Docker的图形化界面管理工具
命令:
docker run -d -p 8088:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
记得把阿里云的安全组打开之后,即可通过外网访问8088端口
登录之后即可使用。