docker cp Copy File and Enter Container

  • 2021-10-25 00:13:39
  • OfStack

Enter the running container


#  Enter the container   Newly opened 1 Terminals 
# docker exec -it  Container id /bin/bash
docker exec -it eaac94ef6926 /bin/bash

#  Enter the original terminal of the container 
# docker attach  Container id
docker attach eaac94ef6926

File copy of container


#  Copy files from within the container to linux On the mainframe 
# docker cp  Container id: Path in container   Target host path 
docker cp eaac94ef6926:/home/test.txt /root/test.txt

#  Copy files from the host to the container  # docker cp  File path   Container id: Path in container 
docker cp test.txt eaac94ef6926:/home/test.txt

Official document: https://docs.docker.com/engine/reference/commandline/exec/

Supplement: docker cp: Copy files from container to local

Example: Copy 1 test. db file from the container to the local data directory.


#  Hypothetical existence 1 A mirror named  kitty With the label 0.1 , creating 1 A person named  koko Container of 

# 1. create a container first
docker run -itd --name koko kitty:0.1 /bin/bash
# 2. copy test.db from koko tmp directory to local data directory.
docker cp koko:/tmp/test.db ./data/test.db
# 3. rm container koko
docker rm -f koko

docker cp can also be transferred from a native copy file to a container:


#  Taking the above code as an example, you can reverse the container path and the local path .
docker cp ./data/test.db koko:/tmp/test.db

Supplement: docker on Mac is mounted to local files for interoperability

docker Mount to Local File


docker run -itv /Users/XXXX/Sites/docker:/www images : 12121 /bin/bash

Related articles: