6

I need run command to docker container in a script, i trying with:

docker run -it <container> /bin/bash -c "command"

I had no error but the command is not executed

stecog
  • 2,202
  • 4
  • 30
  • 50
  • 1
    Could you be more specific? e.g. `docker run -it /bin/bash -c "echo 'Hello'"` works fine. is centos in my case – lifus Jan 30 '15 at 15:26
  • I need clear cache on nginx container, i run: docker run -it nginx /bin/bash -c "rm -f /tmp/cache/*/*/*". If i launch this command inside container it work, in this way the cache is not cleared – stecog Jan 30 '15 at 16:17
  • that's because you should [commit you change](https://docs.docker.com/userguide/dockerimages/) – lifus Jan 30 '15 at 16:19
  • no, `docker commit nginx` successful, but the files are still there – stecog Jan 30 '15 at 16:32
  • I can not create a new image, i must not stop the service ... I do it manually – stecog Jan 30 '15 at 16:40
  • Did you try to run `docker exec rm -f /tmp/cache/*/*/*`? – lifus Jan 30 '15 at 16:42

1 Answers1

3

I can not create a new image, i must not stop the service ... I do it manually

It's possible to invoke a command in a running container:

docker exec <container_id> rm -f /tmp/cache/*/*/*
lifus
  • 8,074
  • 1
  • 19
  • 24
  • I have alredy trying, i not get any error but the files were not deleted – stecog Jan 30 '15 at 17:47
  • @user2385354 Are you sure that `rm -f /tmp/cache/*/*/*` is what you actually need? Are you going to remove `/tmp/cache` and all its subfolders? If so, then `rm -rf /tmp/cache` is what you need – lifus Jan 30 '15 at 17:57
  • If i launch `rm -rf` it erase all folder and nginx stop working, i need stop and start container, instead `rm -f` erase only file in all folder and suibfolder. – stecog Jan 30 '15 at 18:10
  • You may remove **all files** as follows `find /tmp/cache/ -type f -delete`. You may also add `-maxdepth ` if you want. – lifus Jan 30 '15 at 18:15
  • 1
    Actually, http://stackoverflow.com/a/6896903/2286990 suggests exactly this. See http://nginx.2469901.n2.nabble.com/best-way-to-empty-nginx-cache-tp3017271p3017429.html – lifus Jan 30 '15 at 18:45
  • I suspect for the answer to work you'd need to do: `docker exec rm -f "/tmp/cache/*/*/*"` – Kevin Lyda Jun 10 '15 at 05:51