How do I copy data inside and outside the Docker container

  • 2020-06-01 11:19:48
  • OfStack

Copy files from the container to the host


[root@oegw1 soft]# docker ps
CONTAINER ID    IMAGE        COMMAND        CREATED       STATUS       PORTS        NAMES
8d418a7b6021    postgres      "/docker-entrypoint.  7 hours ago     Up 7 hours               test1  
[root@oegw1 soft]# docker exec -t -i 8d418a7b6021 /bin/bash
root@oegw1:/var/lib/postgresql# pwd
/var/lib/postgresql
root@oegw1:/var/lib/postgresql# ls
data
root@oegw1:/var/lib/postgresql# exit
exit
[root@oegw1 soft]# docker cp 8d418a7b6021:/var/lib/postgresql/data /opt/soft/

Complete copy


docker run -v /opt/soft:/mnt 8d418a7b6021

Copy files from host into container

1. Mount the host data volume into the container with -v

With the -v parameter, before the colon is the host directory, which must be the absolute path, and after the colon is the path mounted in the image.


[root@oegw1 soft]# docker run -it -v /opt/soft:/mnt postgres /bin/bash

The disadvantage of this approach is that it can only be mounted when the container has just started

2. Copy directly to the container physical storage system on the host


[root@oegw1 soft]# docker ps
CONTAINER ID    IMAGE        COMMAND        CREATED       STATUS       PORTS        NAMES
8d418a7b6021    postgres      "/docker-entrypoint.  8 hours ago     Up 8 hours               test1        
[root@oegw1 soft]# docker inspect -f  '{{.Id}}' 8d418a7b6021
8d418a7b60216ed677ada5ca5fe1e82a953af6702837e8e05c95b16d23241d47

In docker, Is mapped to/var/lib/docker / 8 d418a7b60216ed677ada5ca5fe1e82a953af6702837e8e05c95b16d23241d47 devicemapper mnt / / this is ES50 under the folder in the file The contents of the root directory in EN, and then we can copy whatever we want


Related articles: