Copying files To &From Running Docker Container.
Many times we wanted to copy a file to the container or copy our work to & from the container. Using a docker volume and bound mounts can be a good way but what if you forget to attach a volume to the container?
Well, when you caught up in that situation and you do not want to use volume for single copy operation and want to do at runtime. The solution to this is single docker Command:
Docker cp: Copy files/folders between a container and the local filesystem, Just check the docker cp — help:
C:\Users\anand> docker cp — help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATHCopy files/folders between a container and the local filesystem
Options:
-a, — archive Archive mode (copy all uid/gid information)
-L, — follow-link Always follow symbol link in SRC_PATH
Let’s take an example of how you can use this:
Step 1: Run a sample Nginx image:
Step 2: Now I will create a single text file so that I can copy it into a container
Step 3: Copying this file into the Running container:
Step4: Done! Now you can go inside the container and see the file is copied at the destination:
or you can see it from the browser with this path:
You can do this vice-versa as well…!
Hope this simple docker command helps you :) follow me for such short and crisp docker article!