skip to Main Content

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


  1. docker-compose ps doesn’t remember the full stack of Compose files you initially ran docker-compose up with. You need to repeat all of the -f options on every docker-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:

    export COMPOSE_FILE=docker-compose.base.yml:apps/app1/docker-compose.extended.yml:apps/app2/docker-compose.extended.yml
    docker-compose up -d
    docker-compose ps
    docker-compose down
    
    Login or Signup to reply.
  2. it shows up on docker ps:

    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

    • on a shared host or dev host, to prevent different projects, which may use the same service names, from interfering with each other

    The default project name is the basename of the project directory. And that’s the reason you can see the output myapp_app2_1 meaning service_folder_servicenumber

    You can set a custom project name by using the -p command line option or the COMPOSE_PROJECT_NAME environment variable.

    Multiple isolated environments on a single host

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search