• How to copy files from host to Docker container?


    https://stackoverflow.com/questions/22907231/how-to-copy-files-from-host-to-docker-container

    1822

    I am trying to build a backup and restore solution for the Docker containers that we work with.

    I have Docker base image that I have created, ubuntu:base, and do not want have to rebuild it each time with a Docker file to add files to it.

    I want to create a script that runs from the host machine and creates a new container using the ubuntu:base Docker image and then copies files into that container.

    How can I copy files from the host to the container?

     
    • 12
      If you don't want to rebuild, why not "docker commit" ? That saves your image. – Berend de Boer Apr 28 '14 at 22:24
    • 36
      Just a remark on a notion nobody has addressed: in general, treat containers as "ephemeral". There ARE use cases to copy files into/from a running container (testing, prototyping). But if you find yourself in a position where you can't rebuild what you need using Dockerfiles and/or compose, then you may be in a bad place. You generally don't want to be backing up containers as if they were OS or even VM objects. Generally speaking :-) – Scott Prive Oct 16 '17 at 18:04 

    42 Answers

    3090
     

    The cp command can be used to copy files.

    One specific file can be copied TO the container like:

    docker cp foo.txt mycontainer:/foo.txt
    

    One specific file can be copied FROM the container like:

    docker cp mycontainer:/foo.txt foo.txt
    

    For emphasis, mycontainer is a container ID, not an image ID.

    Multiple files contained by the folder src can be copied into the target folder using:

    docker cp src/. mycontainer:/target
    docker cp mycontainer:/src/. target
    

    Reference: Docker CLI docs for cp

    In Docker versions prior to 1.8 it was only possible to copy files from a container to the host. Not from the host to a container.

     
    194
    1. Get container name or short container id:

      $ docker ps
      
    2. Get full container id:

      $ docker inspect -f   '{{.Id}}'  SHORT_CONTAINER_ID-or-CONTAINER_NAME
      
    3. Copy file:

      $ sudo cp path-file-host /var/lib/docker/aufs/mnt/FULL_CONTAINER_ID/PATH-NEW-FILE
      

    EXAMPLE:

    $ docker ps
    
    CONTAINER ID      IMAGE    COMMAND       CREATED      STATUS       PORTS        NAMES
    
    d8e703d7e303   solidleon/ssh:latest      /usr/sbin/sshd -D                      cranky_pare
    
    $ docker inspect -f   '{{.Id}}' cranky_pare
    

    or

    $ docker inspect -f   '{{.Id}}' d8e703d7e303
    
    d8e703d7e3039a6df6d01bd7fb58d1882e592a85059eb16c4b83cf91847f88e5
    
    $ sudo cp file.txt /var/lib/docker/aufs/mnt/**d8e703d7e3039a6df6d01
    What Doesn't Kill Me Makes Me Stronger
  • 相关阅读:
    Bootstrap学习笔记系列2-------Bootstrap简单表格处理
    Bootstrap学习笔记系列1-------Bootstrap网格系统
    前端代码规范
    Dev TreeList设置焦点失败解决方法
    las数据集加载las数据
    c# 文件另存为代码
    Dev 饼图
    ASP.NET MVC Json的序列化和反序列化
    服务器重启后导致访问ArcServer地图服务须登录
    jQuery回调函数
  • 原文地址:https://www.cnblogs.com/kungfupanda/p/14590864.html
Copyright © 2020-2023  润新知