4

Each time I run the command "docker-compose run web ..." it results in another container being added to Kitematic, so that I have a list of containers like "image-name_web_run_1" and so on with each run of docker-compose.

Is this expected behaviour? If so, how do I work around it?

ldg
  • 9,112
  • 2
  • 29
  • 44
Dobler
  • 821
  • 2
  • 10
  • 23

3 Answers3

11

It is expected. You can use docker-compose run --rm to have the container removed when it exits.

You can clean up these old containers using docker-compose down or docker-compose rm -a.

dnephin
  • 25,944
  • 9
  • 55
  • 45
2

Do you mean new container? That is the expected behavior for docker-compose run, see: https://docs.docker.com/compose/reference/run/

Did you mean to use docker-compose exec? e.g., docker-compose exec web /bin/sh See: https://docs.docker.com/compose/reference/exec/

ldg
  • 9,112
  • 2
  • 29
  • 44
  • I was trying to run a container command directly from the command line. Yes I mean container. – Dobler Jul 06 '16 at 02:41
  • To run a container command you can use `docker-compose exec` e.g., `docker-compose exec web /bin/sh` which is equivalent to `docker exec -ti web /bin/sh`. See: https://docs.docker.com/compose/reference/exec/ . (answer updated to reflect your comment.) – ldg Jul 06 '16 at 19:07
  • If you were trying to run a command on a running container, then `docker exec` (or `docker-compose exec`) is what you are looking for. `docker-compose run --rm` will create and destroy a temporary container, which is more expensive and may have some side-effects. In basic cases, they will pretty much do the same thing, so it may not matter -- just depends what you are trying to do. – ldg Jul 08 '16 at 20:40
0

I think the issue here is that some users want something in the middle of the out of the box functionality for exec vs run.

docker-compose run stands up a new container, that then needs to be cleaned up.

docker-compose exec only works on an existing and RUNNING container, which errors out if the container is not running.

As far as I know there is not a command or "one-liner" to accomplish the desired effect of starting a potentially stopped container and running a one-off command.

If you want to avoid the expense of creating a new container and then destroying it every time as suggested in the accepted answer by using the --rm flag; as pointed out in ldg's comment.

Then you have to manually start the container and then run exec to get the desired effect.

I recommend the following concatenated commands:

 docker-compose start web; docker-compose exec web bin/rails c

NOTE: assuming you have dependencies set up in your docker-compose file, those will also be started with the start command.

SMAG
  • 652
  • 6
  • 12