Execute a command after run
Hi,
It will be very helpful to have something like "onrun" in the YAML to be able to run commands after the run. Similar to https://github.com/docker/docker/issues/8860
mongodb:
image: mongo:3.0.2
hostname: myhostname
domainname: domain.lan
volumes:
- /data/mongodb:/data
ports:
- "27017:27017"
onrun:
- mongodump --host db2dump.domain.lan --port 27017 --out /data/mongodb/dumps/latest
- mongorestore -d database /data/mongodb/dumps/latest/database
After the mongodb start, It will dump db2dump.domain.lan and restore it.
When I will stop and then start the container, onrun part will no be executed to preserve idempotency.
EDIT 15 June 2020
5 years later, Compose wan't to "standardize" specifications, please check https://github.com/compose-spec/compose-spec/issues/84
I think these should be steps in the Dockerfile
FROM mongo:3.0.2
ADD data/mongodb/dumps/latest /data/mongodb/dumps/latest
RUN mongorestore -d database /data/mongodb/dumps/latest/database
That way you also get it cached when you rebuild.
Thanks @dnephin. Of course I can make a Dockerfile and use it in build instead of images, or I can use docker exec. MongoDB is just an example, you can have this example with mysql and account creation, or with rabbitmq and queue creation etc.
onrun will permits flexibility on compose orchestration, compose will read onrun list and make docker exec on each item.
The point is that putting commands to docker exec in docker-compose.yml is unnecessary when you can either do it in the Dockerfile or in the container's startup script, both of which will also make your container more useful when not being run with Compose.
Alternatively, start your app with a shell script or Makefile that runs the appropriate docker and docker-compose commands.
The functionality isn't worth adding to Compose unless it would add significant value over doing either of those, and I don't think it would for the use cases you've cited.
So, to manage my docker, you suggest me to use a Script or a Makefile. So why compose was created ? We can manage, scale etc. container with script || dockerfile ?
Ok, I take this example, it's what I used to deploy my application testing environment in the CI process.
rabbitmq:
image: rabbitmq:3.5.1-management
environment:
RABBITMQ_NODENAME: rabbit
hostname: rabbitmq
domainname: domain.lan
volumes:
- /data/rabbitmq/db:/var/lib/rabbitmq
ports:
- "5672:5672"
- "15672:15672"
- "25672:25672"
- "4369:4369"
mongodb:
image: mongo:3.0.2
hostname: mongo
domainname: domain.lan
volumes:
- /data/mongodb:/data
ports:
- "27017:27017"
appmaster:
image: appmaster
hostname: master
domainname: domain.lan
environment:
...
ports:
- "80:80"
- "8080:8080"
links:
- mongodb
- rabbitmq
celery:
image: celery
hostname: celery
domainname: domain.lan
environment:
...
links:
- rabbitmq
After container starts, I must provision mongodb, manage queue and account in rabbitmq
What i'm doing today is a script with:
#!/bin/bash
PROJECT=appmaster
docker-compose -f appmaster.yml -p appmaster up -d
docker exec appmaster_rabbitmq_1 rabbitmqctl add_user user password
docker exec appmaster_rabbitmq_1 rabbitmqctl add_vhost rabbitmq.domain.lan
docker exec appmaster_rabbitmq_1 rabbitmqctl set_permissions -p rabbitmq.domain.lan password ".*" ".*" ".*"
docker exec appmaster_mongodb_1 mongodump --host mongo-prd.domain.lan --port 27017 --out /data/mongodb/dumps/latest
docker exec appmaster_mongodb_1 mongorestore -d database /data/mongodb/dumps/latest/database
With onrun instruction I can directly make docker-compose -f appmaster.yml -p appmaster up -d
and the yml file become more readable
rabbitmq:
...
onrun:
- rabbitmqctl add_user user password
- rabbitmqctl add_vhost rabbitmq.domain.lan
- rabbitmqctl set_permissions -p rabbitmq.domain.lan password ".*" ".*" ".*"
mongodb:
...
onrun:
- mongodump --host mongo-prd.domain.lan --port 27017 --out /data/mongodb/dumps/latest
- mongorestore -d database /data/mongodb/dumps/latest/database
This would be rather useful and solves a use case.
:+1:
It will make using docker-compose more viable for gated tests as part of a CD pipeline
:+1:
This is a duplicate of #877, #1341, #468 (and a few others).
I think the right way to support this is #1510 and allow external tools to perform operations when you hit the event you want.
Closing as a duplicate
This would be very useful. I don't understand the argument of "oh you could do this with a bash script". Of course we could do it with a bash script. I could also do everything that Docker-compose does with a bash script. But the point is that there is one single YAML file that controls your test environment and it can be spun up with a simple docker-compose up command.
It is not the remit of Compose to do everything that could be done with a shell script or Makefile - we have to draw a line somewhere to strike a balance between usefulness and avoiding bloat.
Furthermore, one important property of the Compose file is that it's pretty portable across machines - even Mac, Linux and Windows machines. If we enable people to put arbitrary shell commands in the Compose file, they're going to get a lot less portable.
@aanand To be fair, being able to execute a docker exec does not automatically imply x-plat incompatibility.
Apologies - I misread this issue as being about executing commands on the host machine. Still, my first point stands.
I understand your point @aanand. It doesn't seem out of scope to me, since already docker-compose does a lot of the same things that the regular docker engine already does, like command, expose, ports, build, etc. Adding the exec functionality would add more power to docker-compose to make it a true one stop shop for setting up dev environments.
@aanand the main problem for many devs and CI pipelines is to have a data very close to the production env. Like a dump from a DB. I create this ticket 1 year ago and nothing move in docker compose.
So you suggest a Makefile or a Bashcript just to run some exec https://github.com/docker/compose/issues/1809#issuecomment-128073224
What I originally suggest is onrun (or oncreate) who keep idempotency. Just run at the first start. If the container is stopped or paused, the new start will not run onrun (or oncreate)
Finally, in my git repository I will have a compose file, a dockerfile and a makefile with idempotency management (may makefile could create a statefile). Genius!
There's a big difference between command, expose, etc and exec. The first group are container options, exec is a command/api endpoint. It's a separate function, not options to the create container function.
There are already a couple ways to accomplish this with Compose (https://github.com/docker/compose/issues/1809#issuecomment-128059030). onrun already exists. It is command.
Regarding the specific problem of dumping or loading data from a database, those are more "workflow" or "build automation" type tasks, that are generally done in a Makefile. I've been prototyping a tool for exactly those use-cases called dobi, which runs all tasks in containers. It also integrates very well with Compose. You might be interested in trying it out if you aren't happy with Makefiles. I'm working on an example of a database init/load use case.
@dnephin onrun is not a simple command because you just miss the idempotency.
Let's imagine. create on container creation and will never be exec again (dump & restore).
exec:
create:
- echo baby
destroy:
- echo keny
start:
- echo start
stop:
- echo bye
If you need more examples:
- Create a cmd file with this issue https://github.com/LarryEitel/phpdock/blob/master/dc.cmd -> juste compose up + exec
- This person will use a 'trowel' to resolve this https://stackoverflow.com/questions/29303904/docker-compose-initialize-vs-run
Thanks for dobi, but if you need to create a tool to enhance compose, compose is bad and it's better to use a more powerfull tool.
but if you need to create a tool to enhance compose, compose is bad and it's better to use a more powerful tool.
That's like saying "if you need applications to enhance your operating system, your OS is bad". No one tool should do everything. The unix philosophy is do one thing, and do it well. That is what we're doing here. Compose does its one thing "orchestrate containers for running an application". It is not a build automation tool.
That's like saying "if you need applications to enhance your operating system, your OS is bad". No one tool should do everything. The unix philosophy is do one thing, and do it well. That is what we're doing here.
Wow I think that we reached the best bad faith.
- So why exec is in the code and the cli reference ? https://docs.docker.com/compose/reference/exec/ ?
- I think it's make a long time that docker don't respect the unix philosophy https://coreos.com/blog/rocket
Unfortunately, a simple re-usable component is not how things are playing out. Docker now is building tools for launching cloud servers, systems for clustering, and a wide range of functions: building images, running images, uploading, downloading, and eventually even overlay networking, all compiled into one monolithic binary running primarily as root on your server. The standard container manifesto was removed. We should stop talking about Docker containers, and start talking about the Docker Platform. It is not becoming the simple composable building block we had envisioned.
So you can guarantee that we will never see "docker compose" wrote in Go inside in the docker monolithic binary to keep the unix philosophy ? https://www.orchardup.com/blog/orchard-is-joining-docker
To continue towards that original goal, we’re joining Docker. Among other things, we’re going to keep working on making Docker the best development experience you’ve ever seen – both with Fig, and by incorporating the best parts of Fig into Docker itself.
So in short there is no way to do things like loading fixtures with compose..? I have to say I'm surprised.. The official way is to add fixture loading to my production container? Or to write a shell script around my compose file? In the later case I could also just execute 'docker run' as I did before.