• Docker 命令


    获取容器/镜像的元数据

    docker inspect [容器ID]

    进入正在运行的容器

    docker exec -it [容器ID] /bin/bash
    docker attach [容器ID]

     容器内拷贝带主机上

    docker cp [容器ID]:[容器文件路径] [主机存储路径]
    docker cp 747dd11910bf:/home/test.txt /home

     提交容器生成一个新的镜像 (commit镜像创建)

    docker commit -m="test_commit" -a="zjj" 2cdca6f5c914 commit_nginx:1.0
    -m # 提交描述     
    -a # 作者
    [root@docker home]# docker images
    REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
    commit_nginx          1.0                 e5777d289799        34 seconds ago      133MB
    redis                 6.2                 bc8d70f9ef6c        11 days ago         105MB
    php                   7.3.28-fpm          2692864592ed        11 days ago         399MB
    nginx                 1.20.0              7ab27dbbfbdf        12 days ago         133MB
    mysql                 5.7.34              2c9028880e58        12 days ago         447MB
    portainer/portainer   latest              580c0e4e98b0        2 months ago        79.1MB
    centos                7                   8652b9f0cb4c        6 months ago        204MB

     卷挂载

    -v 卷挂载
    -e 环境变量配置

    docker run -it -v [主机目录]:[容器目录] [root@iZ2zejfv6wfxfponzj4w1zZ home]# docker run -it -v /home/dock_v:/home centos [root@iZ2zejfv6wfxfponzj4w1zZ home]# ll drwxr-xr-x 4 root root 4096 4月 25 15:39 data drwxr-xr-x 2 root root 4096 5月 27 11:17 dock_v # 查看挂载信息 [root@iZ2zejfv6wfxfponzj4w1zZ home]# docker inspect e68d989665a7

     docker volume ls 查看所有挂载的卷

    [root@iZ2zejfv6wfxfponzj4w1zZ home]# docker volume ls
    DRIVER    VOLUME NAME
    local     fc49b532a6d1c618c753a9e85600cd3f4f0c5941551dbb46aae325a85be27008
    
    # 写名字的 , 相对路径
    [root@iZ2zejfv6wfxfponzj4w1zZ home]# docker run -d -P --name nginx03 -v nginx03:/etc/nginx nginx
    61d9628a9659e968fbc1fc37804b8dbef1a193d1a77e7601ef983559cb788cb4
    [root@iZ2zejfv6wfxfponzj4w1zZ home]# docker volume ls DRIVER VOLUME NAME local fc49b532a6d1c618c753a9e85600cd3f4f0c5941551dbb46aae325a85be27008 local nginx03

    docker volume inspect [卷名字] 查看挂载卷的地址

    [root@iZ2zejfv6wfxfponzj4w1zZ nginx02]# docker volume ls
    DRIVER    VOLUME NAME
    local     fc49b532a6d1c618c753a9e85600cd3f4f0c5941551dbb46aae325a85be27008
    local     nginx03
    [root@iZ2zejfv6wfxfponzj4w1zZ nginx02]# docker volume inspect nginx03 # 没有指定目录情况下默认地址 :
    /var/lib/docker/volumes/xxx/_data [ { "CreatedAt": "2021-05-27T14:21:19+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/nginx03/_data", "Name": "nginx03", "Options": null, "Scope": "local" } ]

     --volumes-from 数据卷之间的挂载

    # 创建nginx01镜像 和挂载volume_nginx01卷
    [root@iZ2zejfv6wfxfponzj4w1zZ home]# docker run -d -P -v volume_nginx01:/etc/nginx --name nginx01 nginx
    322d4d3ef3b57dfeb6e5858531117e00d5167b90981774a0ddd2bb157cefb5f4
    [root@iZ2zejfv6wfxfponzj4w1zZ home]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 322d4d3ef3b5 nginx
    "/docker-entrypoint.…" 4 seconds ago Up 2 seconds 0.0.0.0:49166->80/tcp, :::49166->80/tcp nginx01
    [root@iZ2zejfv6wfxfponzj4w1zZ home]# docker volume ls DRIVER VOLUME NAME local fc49b532a6d1c618c753a9e85600cd3f4f0c5941551dbb46aae325a85be27008 local nginx03 local volume_nginx01 # 通过
    --volumes-from 新建的nginx02 继承 nginx01 数据卷 [root@iZ2zejfv6wfxfponzj4w1zZ home]# docker run -d -P --volumes-from nginx01 --name nginx02 nginx

    Docker网络

    创建自己的网络 docker network create

    [root@iZ2zejfv6wfxfponzj4w1zZ ~]# docker network create --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
    1abbcbb263e5c2e7e88380284152b90e57d0d89180a0cc8aa2dc08374adb136b
    
    [root@iZ2zejfv6wfxfponzj4w1zZ ~]# docker network ls
    NETWORK ID     NAME      DRIVER    SCOPE
    873c312ecefe   bridge    bridge    local
    c544019b10e8   host      host      local
    1abbcbb263e5   mynet     bridge    local
    362640eea876   none      null      local
    
    [root@iZ2zejfv6wfxfponzj4w1zZ ~]# docker network inspect mynet
    [
        {
            "Name": "mynet",
            "Id": "1abbcbb263e5c2e7e88380284152b90e57d0d89180a0cc8aa2dc08374adb136b",
            "Created": "2021-05-28T16:17:12.163030931+08:00",
            "Scope": "local",
            "Driver": "bridge",
            "EnableIPv6": false,
            "IPAM": {
                "Driver": "default",
                "Options": {},
                "Config": [
                    {
                        "Subnet": "192.168.0.0/16",
                        "Gateway": "192.168.0.1"
                    }
                ]
            },
            "Internal": false,
            "Attachable": false,
            "Ingress": false,
            "ConfigFrom": {
                "Network": ""
            },
            "ConfigOnly": false,
            "Containers": {},
            "Options": {},
            "Labels": {}
        }
    ]

    使用自己创建的网络

    [root@iZ2zejfv6wfxfponzj4w1zZ ~]# docker run -d -P --name tomcat-net-01 --net mynet tomcat
    edd774545b140c32790e3845721b67819c56a1e93527cbe80b8c496fb5e8a434
    [root@iZ2zejfv6wfxfponzj4w1zZ ~]# docker run -d -P --name tomcat-net-02 --net mynet tomcat
    32ee18f06c603ecf7fc53528c0dbd5c2649caedbc2a1f61c6dad84dbaeb598a8
    
    # 查看mynet网络
    [root@iZ2zejfv6wfxfponzj4w1zZ ~]# docker network inspect mynet
    [
        {
            "Name": "mynet",
            "Id": "1abbcbb263e5c2e7e88380284152b90e57d0d89180a0cc8aa2dc08374adb136b",
            "Created": "2021-05-28T16:17:12.163030931+08:00",
            "Scope": "local",
            "Driver": "bridge",
            "EnableIPv6": false,
            "IPAM": {
                "Driver": "default",
                "Options": {},
                "Config": [
                    {
                        "Subnet": "192.168.0.0/16",
                        "Gateway": "192.168.0.1"
                    }
                ]
            },
            "Internal": false,
            "Attachable": false,
            "Ingress": false,
            "ConfigFrom": {
                "Network": ""
            },
            "ConfigOnly": false,
            "Containers": {
                "32ee18f06c603ecf7fc53528c0dbd5c2649caedbc2a1f61c6dad84dbaeb598a8": {
                    "Name": "tomcat-net-02",
                    "EndpointID": "391408f4ed2c0503fce649e8de8bfbd92331afc6fea28539d795321e7f2a8a14",
                    "MacAddress": "02:42:c0:a8:00:03",
                    "IPv4Address": "192.168.0.3/16",
                    "IPv6Address": ""
                },
                "edd774545b140c32790e3845721b67819c56a1e93527cbe80b8c496fb5e8a434": {
                    "Name": "tomcat-net-01",
                    "EndpointID": "4a182ccbfd2a62b4314af979ef69d4cf0d6f9371b60b4723d9fc67d69b38c0a9",
                    "MacAddress": "02:42:c0:a8:00:02",
                    "IPv4Address": "192.168.0.2/16",
                    "IPv6Address": ""
                }
            },
            "Options": {},
            "Labels": {}
        }
    ]

    把一个容器链接到网络中  docker network connect [网络名] [容器名]

    [root@iZ2zejfv6wfxfponzj4w1zZ ~]# docker network connect mynet tomcat01
    [root@iZ2zejfv6wfxfponzj4w1zZ ~]# docker exec -it tomcat01 ping tomcat-net-01
    PING tomcat-net-01 (192.168.0.2) 56(84) bytes of data.
    64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.064 ms
    64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.066 ms
    ^C
  • 相关阅读:
    java+phantomjs实现动态网页抓取
    windows 安装 cordova
    windows系统安装 ionic
    windows系统 安装 mysql.fx
    安装 Navicat for MySQL
    windows 安装 MySQL
    调用百度地图api隐藏版权信息
    ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
    Angular 调用百度地图API接口
    Angular 使用 frame 加载网络资源显示路径不安全问题
  • 原文地址:https://www.cnblogs.com/zjj1990/p/16050118.html
Copyright © 2020-2023  润新知