云边日落 十年窗下无人问,一举成名天下知。(元·高明·琵琶记)
歌曲封面 未知作品
  • 歌曲封面“魔女の宅急便”~ルージュの伝言松任谷由実

渝ICP备2021002886号-1

渝公网安备50022502000591号

网站已运行 3 年 330 天 19 小时 26 分

Powered by Typecho & Sunny

3 online · 40 ms

Title

docker—基本命令小结

酒笙

·

Article
⚠️ 本文最后更新于2021年09月26日,已经过了964天没有更新,若内容或图片失效,请留言反馈

小结

f27d6a7d3f1f098f727223b8c7b16fe8.png
勤能补拙,温故而知新
docker命令导图.png♾️ shell 代码:
attach    Attach to a running container #当前 shell 下 attach 连接指定运行镜像
build      Build an image from a Dockerfile #通过 Dockerfile 定制镜像
commit  create a new image from a container changes #提交当前容器为新的镜像
cp          copy files/folders from the containers filesystem to the host path #从容器中拷贝指定文件或者目录到宿主机中
create    Create a new container #创建一个新的容器,同 run,但不启动容器
diff         Inspect changes on a container's filesystem #查看 docker 容器变化
events    Get real time events from the server #从docker 服务获取容器实时事件
exec       Run a command in an existing container #在已存在的容器上运行命令
export   Stream the contents of a container as a tar archive #导出容器的内容流作为一个 tar 归档文件[对应import ]
history  Show the history of an image #展示一个镜像形成历史
images  List images # 列出系统当前镜像
import  create a new filesystem image from the contents of a tarball #从tar包中的内容创建一个新的文件系统映像[对应export]
info       Display system-wide information # 显示系统相关信息
inspect  Return low-level information on a container #查看容器详细信息
kill         Kill a running container #kill 指定 docker 容器
load      Load an image from a tar archive # 从一个 tar 包中加载一个镜像[对应 save]
login     Register or Login to the docker registry server # 注册或者登陆一个 docker 源服务器
logout  Log out from a Docker registry server # 从当前 Docker registry 退出
logs      Fetch the logs of a container #输出当前容器日志信息
port      Lookup the public-facing port which is NAT-ed to PRIVATE_PORT #查看映射端口对应的容器内部源端
pause   pause all processes within a container #暂停容器
ps         List containers #列出容器列表
pull        the docker registry server #从docker镜像源服务器拉取指定镜像
push      Push an image or a repository to the docker registry server # 推送指定镜像或者库镜像至docker源服务器
restart    Restart a running container #重启运行的容器
rm           Remove one or more containers # 移除一个或者多个容器
rmi           Remove one or more images #移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run          Run a command in a new container #创建一个新的容器并运行一个命令
save       Save an image to a tar archive # 保存一个镜像为一个 tar 包[对应 load]
search    Search for an image on the Docker Hub #在 docker hub 中搜索镜像
start         Start a stopped containers # 启动容器
stop        Stop a running containers #停止容器
tag          Tag an image into a repository #给源中镜像打标签
top         Lookup the running processes of a container #查看容器中运行的进程信息
unpause    Unpause a paused container # 取消暂停容器
version    Show the docker version information #查看 docker 版本号
wait          Block until a container stops, then print its exit code #截取容器停止时的退出状态值

格式

♾️ shell 代码:
# 1. 拉取镜像到本地
docker pull 镜像名称[:tag]
# 举个例子 tomcat
docker pull daocloud.io/library/tomcat:8.5.15-jre8

# 2. 查看全部本地的镜像
docker images

# 3. 删除本地镜像
docker rmi 镜像的标识

# 4. 镜像的导入导出(不规范)
# 将本地的镜像导出
docker save -o 导出的路径 镜像id
# 加载本地的镜像文件
docker load -i 镜像文件
# 修改镜像名称
docker tag 镜像id 新镜像名称:版本

# 1. 运行容器
# 简单操作
docker run 镜像的标识|镜像名称[tag]
# 常用的参数
docker run -d -p  宿主机端口:容器端口 --name 容器名称 镜像的标识|镜像名称[tag]
# -d: 代表后台运行容器
# -p: 宿主机端口:容器端口: 为了映射当前Linux的端口和容器的端口
# --name 容器名称: 指定容器的名称

# 2. 查看正在运行的容器
docker ps [OPTIONS]
# OPTIONS说明:
# -a: 代表查看全部的容器,包括没有运行
# -q: 只查看容器的标识
# -f: 根据条件过滤显示的内容
# --format: 指定返回值的模板文件
# -l: 显示最近创建的容器
# -n: 列出最近创建的n个容器
# --no-trunc: 不截断输出
# -s: 显示总的文件大小

# 3. 查看容器的日志
docker logs -f 容器id
# -f: 可以滚动查看日志的最后几行

# 4. 进入到容器内部
docker exec -it 容器id bash

# 5. 删除容器(删除容器前,需要先停止容器)
docker stop 容器id
# 停止指定的容器
docker stop $(docker ps -qa)
# 停止全部容器
docker rm 镜像id
# 删除指定容器
docker rm $(docker ps -qa)
# 删除全部容器

#6. 启动容器
docker start 容器id

作业练习

安装Nginx
  1. 安装nginx

    ♾️ shell 代码:
    docker search nginx
  2. 下载镜像

    ♾️ shell 代码:
    docker pull nginx
  3. 运行镜像

    ♾️ shell 代码:
    # -d 后台运行
    # --name  给容器命名
    # -p     端口映射,本机端口:容器内部端口
    
    docker run -d --name jiusheng -p 520:80 nginx
    
    #查看是否启动
    [root@iZj6c5ctiawsbu90vbskdwZ ~]# docker ps
    CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                 NAMES
    82e88fd7529c   nginx     "/docker-entrypoint.…"   12 seconds ago   Up 10 seconds   0.0.0.0:520->80/tcp   jiusheng
    
    #查看端口是否连通
    [root@iZj6c5ctiawsbu90vbskdwZ ~]# curl localhost:520
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
     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>
  4. 进入容器

    ♾️ smalltalk 代码:
    docker exec -it jiusheng /bin/bach
  5. 端口映射概念
    ku14hfb9.png
安装tomcat
注意,在官网上的使用方法如下:
♾️ shell 代码:
docker run -it --rm tomcat:9.0
这里我们不推荐,我们一般是使用后台启动,停止了容器后,还可以查找到, 但是docker run -it --rm ,使用之后就会直接删除,查找不到,也就是说是一个即用即删的,多用于测试!
  1. 搜索tomcat镜像
♾️ xshell 代码:
docker searce tomcat
  1. 下载tomcat
♾️ shell 代码:
docker pull tomcat
  1. 启动运行
♾️ shell 代码:
docker run -d -p 250:8080 --name tomcat01 tomcat
  1. 测试访问
♾️ shell 代码:
curl localhost:250
访问ip:250
  1. 访问的通,没有出现想要的页面
♾️ shell 代码:
#进入容器
docker exec -it tomcat01 /bin/bash
#发现少了命令,同时没有web。

#原因:默认最小安装,剔除了不必要的东西,保证最小化运行环境。

#解决方法
cp -r webapps.dist/* webapps

#之后就可以访问量
部署elasticsearch简称es+kibana
♾️ shell 代码:
#官方命令
#--net somenetwork 网络配置
#"discovery.type=single-node"   集群配置,默认单节点
docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:tag
#我们做一些修改
docker run -d --name elasticsearch  -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e EC_JAVA_OPTS="=-Xms64m -Xm512m" elasticsearch:7.6.2
#去掉了网络服务
#改了名字
#增加了环境限制,别问,问就是这个非常耗内存,我的机子托不动,然后指定了版本号

docker run -d --name=es2 -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m"  elasticsearch:7.6.2
  1. 查看容器占用内存
♾️ shell 代码:
    docker stats 容器id
  1. 检查是否启动成功
♾️ shell 代码:
    curl localhost:9200
    #参考
    [root@iZj6c5ctiawsbu90vbskdwZ ~]# curl localhost:9200
    {
      "name" : "62ee34017bec",
      "cluster_name" : "docker-cluster",
      "cluster_uuid" : "3OGmDOBLSOOYOeQQ23F7dQ",
      "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"
    }
    
现在已有 0 条评论,5 人点赞
Comment
发表
搜 索 消 息 足 迹
你还不曾留言过..
你还不曾留下足迹..
博主