4

In a docker-compose.yml file, I have an entrypoint which should copy files to a bind mount so that I can retrieve them from the host:

version: '3.9'

services:
  my-service:
  ....
  entrypoint: cp /foo/*.txt /data
  volumes:
    - ./data:/data

But each time I run this container, the log says: cp: cannot stat '/foo/*.txt': No such file or directory. On the other hand, if I enter a full file name, it works very well.

I also tried: entrypoint: ['cp', '/foo/*.txt', '/data'] but the same error occurs.

Ref: https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact

How could I use a wildcard for copying files in my compose service?

Info:

docker --version
Docker version 20.10.21, build baeda1f

cat /etc/lsb-release | grep DESCRIPTION
DISTRIB_DESCRIPTION="Ubuntu 22.04.1 LTS"
s.k
  • 429
  • 1
  • 4
  • 15
  • I'm not sure if this works (and I cannot test it now): `entrypoint: sh -c 'cp /foo/*.txt /data'` – Edgar Magallon Nov 05 '22 at 22:14
  • It seems to work, but what I don't really understand is that running `entrypoint: cp /foo/*.txt /data` should already be called with `/bin/sh -c` in front in such so-called "shell form" according to: https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact – s.k Nov 05 '22 at 22:25
  • 1
    I'm thinking that docker-compose is parsing the `cp /foo/*.txt /data` as `cp /foo/\*.txt /data`. And therefore the `*` is taken as character and not as wildcard. – Edgar Magallon Nov 05 '22 at 22:49
  • How do you run the container? Are you using `docker run`? If so, try *debugging* with `strace docker run`, maybe you could see the reason why the container is failing. (the wildcard is taken as character perhaps) – Edgar Magallon Nov 05 '22 at 22:51

1 Answers1

2

Characters like * are interpreted by the shell, so you need to invoke a shell to handle it (e.g., sh -c 'cp /foo/*.txt /data'). Otherwise, those parameters are being passed to exec, which does not know how to handle *, and the error you've described happens.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
DevilaN
  • 1,918
  • 10
  • 17