Search code examples
dockerelasticsearchdocker-composedocker-network

How can I run docker-compose multiple times without port issues?


I'm trying to use docker-compose to run continuous integration tests on a Jenkins server.

Here is my docker-compose.yml:

version: '3'

services:
  elasticsearch:
    container_name: elasticsearch_${INSTANCE}
    image: docker.elastic.co/elasticsearch/elasticsearch:6.7.2
    ports:
      - 9200:9200
      - 9300:9300
    command: elasticsearch -E transport.host=0.0.0.0
    environment:
      ES_JAVA_OPTS: "-Xms2g -Xmx2g"
      discovery-type: single-node
  mainapp:
    container_name: mainapp_${INSTANCE}
    image: testbot:${INSTANCE}
    environment:
      ES_ADDRESS: http://elasticsearch_${INSTANCE}:9200
      SUBSET: ${SUBSET}
      DIRECTORY: ${DIRECTORY}
      INSTANCE: ${INSTANCE}
      TEST_CMD: ${TEST_CMD}
    command: /bin/bash /mainapp/build/tests/wrapper.sh

This works great, but when I try to run multiple tests at the same time, the previously running test exits with code 137 immediately. I think this is because the services are binding to the host network, and I can't do that with multiple containers.

For my purposes, the two services that are started only need to communicate with each other, not with the host at all. I'm a bit confused with exactly how to network this.


Solution

  • You can do this by specifying a different project name using the COMPOSE_PROJECT_NAME environment variable or the --project-name flag for docker-compose. All services, networks, and volumes are created and named per-project.