Docker 练习

docker安装Nginx

docker search nginx 搜索镜像

或者在docker Hub上搜索

网站:https://hub.docker.com/

https://hub.docker.com/search?q=nginx&type=image

image-20200705161923317

我们可以进入了解详情!

docker pull nginx 下载镜像

root@ai1:~# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
8559a31e96f4: Pull complete 
8d69e59170f7: Pull complete 
3f9f1ec1d262: Pull complete 
d1f5ff4f210d: Pull complete 
1e22bfa8652e: Pull complete 
Digest: sha256:21f32f6c08406306d822a0e6e8b7dc81f53f336570e852e25fbe1e3e3d0d0133
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

启动镜像

docker run -d --name nginx01 -p 3344:80 nginx
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
0828c6be1dd5        nginx               "/docker-entrypoint.…"   24 seconds ago      Up 23 seconds       0.0.0.0:3344->80/tcp   nginx01

image-20200706090509177

在流量器可以访问到,页面显示如上,表示我们的nginx安装(容器启动)成功了

进入容器

docker exec -it nginx01 /bin/bash

root@0828c6be1dd5:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@0828c6be1dd5:/# cd /etc/nginx
root@0828c6be1dd5:/etc/nginx# ls
conf.d		koi-utf  mime.types  nginx.conf   uwsgi_params
fastcgi_params	koi-win  modules     scgi_params  win-utf

停止容器

docker stop 容器id

启动容器

docker start 容器id(或容器名)


docker安装tomcat

下载并运行

下载镜像

elfin@master1-machine:~$ docker pull tomcat:9.0
9.0: Pulling from library/tomcat
e9afc4f90ab0: Already exists 
989e6b19a265: Already exists 
af14b6c2f878: Already exists 
5573c4b30949: Already exists 
fb1a405f128d: Pull complete 
612a9f566fdc: Pull complete 
cf63ebed1142: Pull complete 
fbb20561cd50: Pull complete 
e99c920870d7: Pull complete 
b7f793f2be47: Pull complete 
Digest: sha256:81c2a95e5b1b5867229d75255abe54928d505deb81c8ff8949b61fde1a5d30a1
Status: Downloaded newer image for tomcat:9.0
docker.io/library/tomcat:9.0

启动容器

# 没有指定tomcat的版本所以会下载最新版
$ docker run -d --name tomcat01 -p 3344:8080 tomcat
Unable to find image 'tomcat:latest' locally
latest: Pulling from library/tomcat
Digest: sha256:81c2a95e5b1b5867229d75255abe54928d505deb81c8ff8949b61fde1a5d30a1
Status: Downloaded newer image for tomcat:latest
63e9b165d2b992630f28e8844a0b36cbcad16043dee7ba6c4020e50c4534de82

# 之前我们已经下载了9.0版本,所以有两个版本
$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              9.0                 2eb5a120304e        3 weeks ago         647MB
tomcat              latest              2eb5a120304e        3 weeks ago         647MB
nginx               latest              2622e6cca7eb        3 weeks ago         132MB
hello-world         latest              bf756fb1ae65        6 months ago        13.3kB

# 查看当前的容器
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                              NAMES
63e9b165d2b9        tomcat              "catalina.sh run"   34 seconds ago      Up 31 seconds       8080/tcp, 0.0.0.0:3344->1002/tcp   tomcat01
$

进入容器

# 进入容器
$ docker exec -it tomcat01 /bin/bash
root@63e9b165d2b9:/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

# linux中的ll命令被阉割了
root@63e9b165d2b9:/usr/local/tomcat# ll
bash: ll: command not found

# tomcat的webapps下面没有任何应用;原因是:默认最小可运行的环境!
root@63e9b165d2b9:/usr/local/tomcat# cd webapps
root@63e9b165d2b9:/usr/local/tomcat/webapps# ls
root@63e9b165d2b9:/usr/local/tomcat/webapps# 

# 将项目复制到webapps目录
root@63e9b165d2b9:/usr/local/tomcat# cp -r webapps.dist/* webapps/
root@63e9b165d2b9:/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@63e9b165d2b9:/usr/local/tomcat# cd webapps
root@63e9b165d2b9:/usr/local/tomcat/webapps# ls
ROOT  docs  examples  host-manager  manager

添加应用之前:

image-20200706114951234

添加应用之后:

image-20200706115307342

思考

我们以后要部署项目,如果每次都要进入容器不是十分麻烦?我要是可以在容器外部提供一个映射路径,webapps,我们在外部放置项目能自动同步到内部就好了!

docker部署es + Kibana

es 暴露的端口很多!

es 十分耗内存

es 的数据一般需要放置到安全目录

下载并启动

--net somenetwork  配置网络
# 创建用户定义的网络(用于连接到连接到同一网络的其他服务(例如,Kibana))
$ docker network create somenetwork

# 启动elasticsearch
$ docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:tag

# 查看cpu的状态
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT    MEM %               NET I/O             BLOCK I/O           PIDS
59b5ccfa71b0        elasticsearch       0.36%               1.237GiB / 3.83GiB   32.31%              7.07kB / 0B         1.16MB / 729kB      48

# 启动之后机器就很卡了?怎么解决?
# 我这里并不卡,如果你的资源有限可能会很卡,此时elasticsearch内存直接占了32.31%
# 增加ES的内存限制 
-e ES_JAVA_OPTS="-Xms64m -Xmx1024m"
$ docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx1024m" elasticsearch:7.6.2

# 内存占用
$ docker stats
----------------------------------------------------------------------
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT    MEM %               NET I/O             BLOCK I/O           PIDS
eeb9d777725f        elasticsearch02     1.09%               383.3MiB / 3.83GiB   9.77%               2.9kB / 0B          4.1kB / 729kB       46
----------------------------------------------------------------------
# 上面通过添加参数启动,我们控制了内存的占用

思考

elasticsearch已经安装了,Kibana如何安装连接?(两个容器)------>见docker的网络原理!

原文地址:https://www.cnblogs.com/dan-baishucaizi/p/13257700.html