How to run a new dependent container with docker-compose?

Hi,

Just a question. What I want to achieve is to run 3 exactly the same stacks based on the same docker-compose file.

For example, start the app+db 3 times which would be completely independent so that we will end up with 3 apps and 3 dbs overall.

This is what I naturally tried (start 3 apps in parallel):

`
for ((n=0;n<3;n++))
do
docker-compose -f tmp/compose-example.yml run --rm app sleep 10&
sleep 1
done

docker ps
`

which produces this:

tmp/example.sh CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b62cb6eabb3b debian "sleep 10" 18 minutes ago Up Less than a second tmp_app_run_10 0cc4d5fd63f3 debian "sleep 10" 18 minutes ago Up 1 seconds tmp_app_run_9 101258f7cb04 debian "sleep 10" 18 minutes ago Up 2 seconds tmp_app_run_8 847a99f7ca52 mongo:2.4 "/entrypoint.sh mongo" 47 minutes ago Up 28 minutes 27017/tcp tmp_db_1

As we can see there are indeed 3 apps, but the dependency (db) is shared across those. So I want to end up with 3 db-s - each matching the appropriate app.

The example compose file is this:

version: '2' services: app: image: debian depends_on: - db db: image: mongo:2.4

So the question is can I stop sharing the same dependency?

Thanks in advance.

You could specify the -p flag to docker-compose to set the project name.

(pseudocode)

for var in [one, two, three]:
docker-compose -p $i up -d

This brings up three ‘project’ instances for me, each with two containers (app & db).

1 Like

That seems to work well. Thanks for that!
The only problem is that it leaves behind the dependencies (db in my case).

But that’s probably because I’m using docker-compose -p app$i run instead of up?