How to copy files from host to Docker container?

前端 未结 30 2476
南方客
南方客 2020-11-22 06:58

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

相关标签:
30条回答
  • 2020-11-22 07:26

    This is a onliner for copying a single file while running a tomcat container.

    docker run -v /PATH_TO_WAR/sample.war:/usr/local/tomcat/webapps/myapp.war -it -p 8080:8080 tomcat
    

    This will copy the war file to webapps directory and get your app running in no time.

    0 讨论(0)
  • 2020-11-22 07:28

    tar and docker cp are a good combo for copying everything in a directory.

    Create a data volume container

    docker create --name dvc --volume /path/on/container cirros
    

    To preserve the directory hierarchy

    tar -c -C /path/on/local/machine . | docker cp - dvc:/path/on/container
    

    Check your work

    docker run --rm --volumes-from dvc cirros ls -al /path/on/container
    
    0 讨论(0)
  • 2020-11-22 07:29

    Container Up Syntax:

     docker run -v /HOST/folder:/Container/floder 
    

    In docker File

    COPY hom* /myFolder/        # adds all files starting with "hom"
    COPY hom?.txt /myFolder/    # ? is replaced with any single character, e.g., "home.txt"
    
    0 讨论(0)
  • 2020-11-22 07:29

    Many that find this question may actually have the problem of copying files into a Docker image while it is being created (I did).

    In that case, you can use the COPY command in the Dockerfile that you use to create the image.

    See the documentation.

    0 讨论(0)
  • 2020-11-22 07:30

    If the host is CentOS or Fedora, there is a proxy NOT in /var/lib/docker/aufs, but it is under /proc:

    cp -r /home/user/mydata/* /proc/$(docker inspect --format "{{.State.Pid}}" <containerid>)/root
    

    This cmd will copy all contents of data directory to / of container with id "containerid".

    0 讨论(0)
  • 2020-11-22 07:30

    Docker cp command is a handy utility that allows to copy files and folders between a container and the host system.

    If you want to copy files from your host system to the container, you should use docker cp command like this:

          docker cp host_source_path container:destination_path
    

    List your running containers first using docker ps command:

      abhishek@linuxhandbook:~$ sudo docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              
      PORTS               NAMES
      8353c6f43fba        775349758637        "bash"              8 seconds ago       Up 7 
      seconds                            ubu_container
    

    You need to know either the container ID or the container name. In my case, the docker container name is ubu_container. and the container ID is 8353c6f43fba.

    If you want to verify that the files have been copied successfully, you can enter your container in the following manner and then use regular Linux commands:

            docker exec -it ubu_container bash
    

    Copy files from host system to docker container Copying with docker cp is similar to the copy command in Linux.

    I am going to copy a file named a.py to the home/dir1 directory in the container.

            docker cp a.py ubu_container:/home/dir1
    

    If the file is successfully copied, you won’t see any output on the screen. If the destination path doesn’t exist, you would see an error:

            abhishek@linuxhandbook:~$ sudo docker cp a.txt ubu_container:/home/dir2/subsub
            Error: No such container:path: ubu_container:/home/dir2
    

    If the destination file already exists, it will be overwritten without any warning.

    You may also use container ID instead of the container name:

            docker cp a.py 8353c6f43fba:/home/dir1
    
    0 讨论(0)
提交回复
热议问题