Docker初学者入门详细学习(1+)

84 阅读3分钟

安装 linux以及Es 熟悉操作,跑通服务实战

安装linux

docker search linux # 搜索镜像

docker pull nginx # 下载镜像

[root@iZwz909zuxxmim3sok3i3qZ /]# docker run -d --name nginx01 -p 3344:80 nginx
# 启动容器 命名为nginx01  开放3344端口 映射到容器内端口80
# -d 后台运行 --name 命名 -p 宿主机端口: 容器内端口  更详细可看下面示例1

[root@iZwz909zuxxmim3sok3i3qZ /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
ceb3dbe420a3   nginx     "/docker-entrypoint.…"   48 seconds ago   Up 47 seconds   0.0.0.0:3344->80/tcp, :::3344->80/tcp   nginx01

# 运行测试
[root@iZwz909zuxxmim3sok3i3qZ /]# 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>

这时候确认你的服务器安全组开启了3344端口后 访问你的服务器公网ip:3344即可看到nginx网页

image.png
查看nginx

# 进入容器
[root@iZwz909zuxxmim3sok3i3qZ /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
ceb3dbe420a3   nginx     "/docker-entrypoint.…"   11 minutes ago   Up 11 minutes   0.0.0.0:3344->80/tcp, :::3344->80/tcp   nginx01
[root@iZwz909zuxxmim3sok3i3qZ /]# docker exec -it ceb3dbe420a3 /bin/bash
root@ceb3dbe420a3:/# whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx
root@ceb3dbe420a3:/# cd /etc/nginx
root@ceb3dbe420a3:/etc/nginx# ls
conf.d	fastcgi_params	mime.types  modules  nginx.conf  scgi_params  uwsgi_param s

# 停止容器
root@ceb3dbe420a3:/etc/nginx# exit
exit
[root@iZwz909zuxxmim3sok3i3qZ /]# docker stop ceb3dbe420a3
ceb3dbe420a3
## 断开后就访问不到页面了

示例1 端口暴露的概念 解释 -p

image.png

思考问题:

  1. 每次改动nginx配置文件,都需要进入容器内部?可以在容器外部提供一个映射路径,达到在容器修改文件名,容器内部就可以自动修改? -v 数据卷

ps: 阿里云镜像安装的镜像默认是最小的景象,所有有不必要的都剔除

安装ES(elasticsearch)+Kibana

问题:es暴露的端口很多;很耗内存;数据放置到安全目录

# 下载启动
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2

# 查看es
[root@iZwz909zuxxmim3sok3i3qZ /]# curl localhost:9200
{
  "name" : "045bbb5a7b4d",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "KCaZZHDUTVqkurDcDcSyJQ",
  "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"
}

# 启动后会发现linux很卡
docker stats # 查看cpu的状态

image.png

# 增加内存限制,修改配置文件 -e 环境配置修改
docker run -d --name elasticsearch01 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2

# 再次查看stats
docker stats

image.png

思考问题 Kibana如何连接到ES

image.png

参考资料:www.bilibili.com/video/BV1og… 学习于B站狂神老师的教学