PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, May 15, 2022

[FIXED] How to copy a directory over the docker cp?

 May 15, 2022     docker, docker-copy, linux, shell, ubuntu     No comments   

Issue

I want to copy a directory from the host to a container, but I couldn't use the following command:

$ docker cp -r <a-dir> <a-container-ID>:/destination/path
unknown shorthand flag: 'r' in -r

With scp -r shell command I can do it, so I expected the same with docker cp -r.

How do I overcome it without using make a compress .tar file and the extraction?


Solution

Quoting the docs:

The cp command behaves like the Unix cp -a command in that directories are copied recursively with permissions preserved if possible.

-- https://docs.docker.com/engine/reference/commandline/#extended-description

So docker cp doesn't have nor need a -r flag. Just omit it. Like this:

# Create a directory and put two files in it
$ mkdir testdir && touch testdir/aaa testdir/bbb

# Make sure files are there
$ ls -la testdir
total 384
drwxr-xr-x 1 jannisbaratheon 197121 0 paź 10 15:15 ./
drwxr-xr-x 1 jannisbaratheon 197121 0 paź 10 15:15 ../
-rw-r--r-- 1 jannisbaratheon 197121 0 paź 10 15:15 aaa
-rw-r--r-- 1 jannisbaratheon 197121 0 paź 10 15:15 bbb

# Create container and fetch its ID
$ export CONTAINER_ID=`docker run -di alpine:3.8 sh`

# Make sure the target directory does not exist
$ docker exec "$CONTAINER_ID" sh -c 'ls -la /containertestdir'
ls: /containertestdir: No such file or directory

# Copy the files with 'docker cp'
$ docker cp testdir "$CONTAINER_ID":/containertestdir

# Verify the files were copied by running 'ls -la' in the container
$ docker exec "$CONTAINER_ID" sh -c 'ls -la /containertestdir'
total 8
drwxr-xr-x    2 root     root          4096 Oct 10 13:15 .
drwxr-xr-x    1 root     root          4096 Oct 10 13:15 ..
-rwxr-xr-x    1 root     root             0 Oct 10 13:15 aaa
-rwxr-xr-x    1 root     root             0 Oct 10 13:15 bbb


Answered By - jannis
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing