1

I am trying to create a shell script that will check for a new file then cp to a Docker Container. The code I have so far is...

#!/bin/sh


source="/var/www/html/"
dest="dev_ubuntu:/var/www/html/"

inotifywait -m "/var/www/html" -e create -e moved_to |
while read file; do
    sudo docker cp /var/www/html/$file dev_ubuntu:/var/www/html
done

But this code gives the following error:

Setting up watches.
Watches established.
"docker cp" requires exactly 2 argument(s).
See '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

What am I doing wrong?

ThomasC
  • 97
  • 1
  • 11

1 Answers1

0

Do you have spaces in your file names? Use double quotes to avoid separating filenames by words:

echo $file
sudo docker cp "$file" dev_ubuntu:"$file"

I've also echoed the file name to see what is happening.

Robert
  • 33,429
  • 8
  • 90
  • 94
  • Name of file did have a space, I added a "_" for the space. and changed the code to `sudo docker cp "/var/www/html/$file" dev_ubuntu:/var/www/html` now I get the following error `lstat /var/www/html/var: no such file or directory` – ThomasC Jun 07 '17 at 16:13
  • With this you want to avoid docker volumes, right? Because volumes are intended to your use case – Robert Jun 07 '17 at 16:14
  • You're welcome. I have added double quotes to the second $file too – Robert Jun 07 '17 at 16:34
  • this one also works: `#!/bin/sh MONITORDIR="/var/www/html/" inotifywait -m -r -e create --format '%w%f' "${MONITORDIR}" | while read NEWFILE do echo "File ${NEWFILE} has been created TEST" sudo docker cp ${NEWFILE} dev_ubuntu:/var/www/html done` – ThomasC Jun 07 '17 at 16:38
  • Great. And why you don't use docker volume? – Robert Jun 07 '17 at 16:39
  • added quotes to ${NEWFILE} allows for spaced filenames `sudo docker cp "${NEWFILE}" dev_ubuntu:/var/www/html` – ThomasC Jun 07 '17 at 16:43