I am trying to share Compose configurations between different projects using the same postgres database and redis server. For that, I have three different Compose configurations.
./docker-compose.base.yml
./apps/app1/docker-compose.extended.yml
./apps/app2/docker-compose.extended.yml
I create and start the containers with the following command:
docker-compose -f docker-compose.base.yml -f apps/app1/docker-compose.extended.yml -f apps/app2/docker-compose.extended.yml up -d
All the services in three configuration files are in the same network: myapp-backend
. All services (postgres, redis, elasticsearch, kafka, zookeeper, app1, app2
) run without any problems, except one catch. app2
does not show up when I write docker-compose ps
or it does not stop when I type docker-compose down
.
Creating network "myapp_myapp-backend" with the default driver
Creating myapp_postgres_1 ... done
Creating myapp_zookeeper_1 ... done
Creating myapp_redis_1 ... done
Creating myapp_elasticsearch_1 ... done
Creating myapp_kafka_1 ... done
Creating myapp_app1_1 ... done
Creating myapp_app2_1 ... done
docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------------------
myapp_elasticsearch_1 /usr/local/bin/docker-entr ... Up 127.0.0.1:9200->9200/tcp, 9300/tcp
myapp_kafka_1 start-kafka.sh Up 127.0.0.1:9092->9092/tcp
myapp_postgres_1 docker-entrypoint.sh postgres Up 127.0.0.1:5434->5432/tcp
myapp_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp
myapp_app1_1 /bin/sh -c bundle install ... Up 127.0.0.1:3000->3000/tcp
myapp_zookeeper_1 /bin/sh -c /usr/sbin/sshd ... Up 2181/tcp, 22/tcp, 2888/tcp, 3888/tcp
As you can see, app2
service does not show up here. But it shows up on docker ps
:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75bc1704c559 myapp_app1 "/bin/sh -c '(bundle…" 33 minutes ago Up 22 minutes 127.0.0.1:3020->3000/tcp myapp_app1_1
4ab2294f7a2c myapp_app2 "/bin/sh -c 'bundle …" 33 minutes ago Up 32 minutes 127.0.0.1:3000->3000/tcp myapp_app2_1
...
I can share the compose files if necessary, but I cannot understand why this is happening.
2
Answers
docker-compose ps
doesn’t remember the full stack of Compose files you initially randocker-compose up
with. You need to repeat all of the-f
options on everydocker-compose
command.If you don’t want to repeat this, Compose also supports a
COMPOSE_FILE
environment variable that is the same as the-f
options, so you should also be able to:That’s part of a feature: Multiple Isolated environments on a single host.
Compose is using the project app_1 or _2 to isolate environments from each other. From the docs
The default project name is the basename of the project directory. And that’s the reason you can see the output
myapp_app2_1
meaningservice_folder_servicenumber
Multiple isolated environments on a single host