zoukankan      html  css  js  c++  java
  • docker容器与宿主交互数据

    1、查看容器

    [root@localhost ~]# docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
    cd6957191c52        nginx               "nginx -g 'daemon ..."   8 hours ago         Up 7 hours          192.168.51.227:9999->80/tcp   webserver

    2、使用容器名字webserver进行文件复制

    3、从宿主机复制到容器,命令:docker cp ceshi.txt webserver:/home/

    4、进入容器,查看文件:docker exec -it webserver bash

    [root@localhost docker]# docker exec -it webserver bash
    root@cd6957191c52:/# cat /home/ceshi.txt 
    hello,bob!
    nice to meet you!
    hello jack!
    nice to meet you too!

    5、修改容器中的文件,并复制到宿主机,命令:docker cp webserver:/home/ceshi.txt ./

    root@cd6957191c52:/# cd /home/
    root@cd6957191c52:/home# echo "this is my bike" >> ceshi.txt
    root@cd6957191c52:/home# cat ceshi.txt 
    hello,bob!
    nice to meet you!
    hello jack!
    nice to meet you too!
    this is my bike
    root@cd6957191c52:/home# exit
    exit
    [root@localhost docker]# docker cp webserver:/home/ceshi.txt ./
    [root@localhost docker]# cat ceshi.txt 
    hello,bob!
    nice to meet you!
    hello jack!
    nice to meet you too!
    this is my bike

    或者,把容器的名字改为容器的ID,获取容器ID的方法:

    方法1、

    [root@localhost docker]# docker inspect -f   '{{.Id}}'  webserver
    cd6957191c52b25d29319b8ad450313931f2a8c730e4f1052704be957f8c573d

     方法2、

    [root@localhost docker]# docker ps 
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
    cd6957191c52        nginx               "nginx -g 'daemon ..."   8 hours ago         Up 7 hours          192.168.51.227:9999->80/tcp   webserver
    [root@localhost docker]# docker inspect -f '{{.Id}}' cd6957191c52
    cd6957191c52b25d29319b8ad450313931f2a8c730e4f1052704be957f8c573d

    用容器ID复制,如下:

    [root@localhost docker]# docker inspect -f   '{{.Id}}'  webserver
    cd6957191c52b25d29319b8ad450313931f2a8c730e4f1052704be957f8c573d
    [root@localhost docker]# echo "abc">>ceshi.txt
    [root@localhost docker]# cat ceshi.txt 
    hello,bob!
    nice to meet you!
    hello jack!
    nice to meet you too!
    this is my bike
    abc
    [root@localhost docker]# docker cp ceshi.txt cd6957191c52b25d29319b8ad450313931f2a8c730e4f1052704be957f8c573d:/home/
    [root@localhost docker]# docker exec -it webserver bash
    root@cd6957191c52:/# cd /home/
    root@cd6957191c52:/home# cat ceshi.txt 
    hello,bob!
    nice to meet you!
    hello jack!
    nice to meet you too!
    this is my bike
    abc

    不用容器全ID也可以

    [root@localhost docker]# docker ps 
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                         NAMES
    cd6957191c52        nginx               "nginx -g 'daemon ..."   8 hours ago         Up 7 hours          192.168.51.227:9999->80/tcp   webserver
    [root@localhost docker]# docker cp cd6957191c52:/home/ceshi.txt ./abc.txt
    [root@localhost docker]# cat abc.txt 
    hello,bob!
    nice to meet you!
    hello jack!
    nice to meet you too!
    this is my bike
    abc
  • 相关阅读:
    如何在 Knative 中部署 WebSocket 和 gRPC 服务?
    全球首个开放应用模型 OAM 开源 | 云原生生态周报 Vol. 23
    从零开始入门 K8s | Kubernetes 网络概念及策略控制
    重磅发布 | 全球首个云原生应用标准定义与架构模型 OAM 正式开源
    成都,我们来啦 | Dubbo 社区开发者日
    一文读懂分布式架构知识体系(内含超全核心知识大图)
    阿里巴巴开源 Dragonwell JDK 最新版本 8.1.1-GA 发布
    可能是国内第一篇全面解读 Java 现状及趋势的文章
    从零开始入门 K8s | 可观测性:监控与日志
    阿里巴巴的云原生与开发者
  • 原文地址:https://www.cnblogs.com/dyh004/p/9122639.html
Copyright © 2011-2022 走看看