docker cp命令实现容器和宿主操作系统的文件相互复制拷贝
1.查询容器id[root@localhost ubuntu]# docker psCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES158ee41a55c8postgres:10.17“docker-entrypoint.s…”2 hours agoUp 2 hours5432/tcppeaceful_cartwri
1.查询容器id
[root@localhost ubuntu]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
158ee41a55c8 postgres:10.17 “docker-entrypoint.s…” 2 hours ago Up 2 hours 5432/tcp peaceful_cartwright
2.docker容器文件拷贝到宿主操作系统(docker container–>host system)
docker cp 158ee41a55c8:/var/lib/postgresql/system_pg_settings.csv /home/ubuntu/
或者 使用docker name
docker cp peaceful_cartwright:/var/lib/postgresql/system_pg_settings.csv /home/ubuntu/
3.宿主操作系统拷贝文件到docker容器(host system–>docker container)
docker cp /home/ubuntu/system_pg_settings.csv 158ee41a55c8 :/var/lib/postgresql/
或者 使用docker name
docker cp /home/ubuntu/system_pg_settings.csv peaceful_cartwright :/var/lib/postgresql/
但是要注意拷贝过来的文件权限是root用户,如果要正常被postgres用户使用,需要再进行用户变更,否则会文件访问权限问题。
postgres@158ee41a55c8:/$ ls -al /var/lib/postgresql/
total 64
drwxr-xr-x. 1 postgres postgres 78 Oct 7 19:54 .
drwxr-xr-x. 1 root root 24 Jul 22 14:33 …
-rw-------. 1 postgres postgres 275 Oct 7 19:50 .bash_history
drwx------. 19 postgres postgres 4096 Oct 7 17:36 data
-rw-------. 1 postgres postgres 2243 Oct 7 19:54 .psql_history
-rw-r–r–. 1 root root 53138 Oct 7 18:59 system_pg_settings.csv
root@158ee41a55c8:/# chown postgres:postgres /var/lib/postgresql/system_pg_settings.csv
root@158ee41a55c8:/# ls -al /var/lib/postgresql/
total 64
drwxr-xr-x. 1 postgres postgres 78 Oct 7 19:54 .
drwxr-xr-x. 1 root root 24 Jul 22 14:33 …
-rw-------. 1 postgres postgres 540 Oct 7 19:58 .bash_history
drwx------. 19 postgres postgres 4096 Oct 7 17:36 data
-rw-------. 1 postgres postgres 2243 Oct 7 19:54 .psql_history
-rw-r–r–. 1 postgres postgres 53138 Oct 7 18:59 system_pg_settings.csv
root@158ee41a55c8:/#
4.docker cp命令帮助信息
[root@localhost ubuntu]# docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
Use '-' as the source to read a tar archive from stdin
and extract it to a directory destination in a container.
Use '-' as the destination to stream a tar archive of a
container source to stdout.
Options:
-a, --archive Archive mode (copy all uid/gid information)
-L, --follow-link Always follow symbol link in SRC_PATH
更多推荐
所有评论(0)