Can't copy files using exec

Hi All,

I can successfully copy files for my container “benfullwave” like this:
docker exec -it benfullwave bash
cp data/*.dat ./

But when I do this:
docker exec benfullwave cp data/*.dat ./
I get an error (cp: cannot stat ‘data/*.dat’: No such file or directory)

I need to copy all the .dat files from /home/data/ to /home/

Any help would be greatly appreciated. Thank you!

data/*.dat is expanded by GNU bash running inside the container.

data/*.dat is expanded by whatever shell you have running outside the container; Docker directly runs /bin/cp without invoking a shell, so nothing inside the container can expand the wildcard.

How did they get to that directory in the first place?

If you built the image from a Dockerfile, change the place you COPY or RUN cp or RUN tar xzf them, rebuild the image, and restart the container.

If you’re trying to use docker exec to copy content from your host system into a container, I’d strongly suggest one of two paths. Either use docker build to build an image with the data files you need, or use docker run -v to make the content you need appear at the right place in the container filesystem, without manually copying it. If you started the container already with the wrong docker run -v option, stop, destroy, and recreate the container with the correct one.

If you really have no other choice than to use docker exec here, you can make Docker run the shell

docker exec 0123456789 sh -c 'cp /home/data/*.dat /home'

but any changes you make will be lost when you destroy the container.

1 Like

Hi David,

Thank you very much! I actually want changes to be lost when I destroy the container, so your last line is perfect. It works great.