BhanuKiran
BhanuKiran

Reputation: 3151

Docker-compose multiple instances on a single host machine

This is how my docker file looks like

version: "3"
services:
  controller:
    build: ./cd_controller
    image: cd_controller:latest
    env_file: $PWD/robot-configurations/.env
    cap_add:
      - ALL
    links:
      - test_fixture
    volumes:
      - dependencypackages:/root/dependency-packages
      - robotconfigurations:/root/robot-configurations
    container_name: controller_g5
  test_fixture:
    image: docker-standard-all.ur-update.dk/ur/pytest-ur:0.7.1
    volumes:
      - pytestfixture:/workspace
      - controllertests:/workspace/controller-tests
    entrypoint: /workspace/entrypoint.sh
    container_name: test_fixture
    stdin_open: true # docker run -i
    tty: true        # docker run -t
volumes:
  robotconfigurations:
    driver_opts:
      type: none
      device: $PWD/robot-configurations
      o: bind
  ...

Basically it has two two services/containers controller&test_fixture. controller has source code running and test_fixture contains all the test_cases. test_fixture needs to talk to controller through a socket. Since docker-compose creates a network among its containers, in my py-test cases I am simply using

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("controller_g5", 30001)) # controller_g5 is name of the controller container 

So far every thing look fine. But I realized that I have multiple versions/features of the source code. So I would like to create a multiple instances of a same thing each have their own network. Does naming the container blocks the container creation with same name in different network ? Also I am not sure how to spin up one more network with similar container on the same host machine. I came across Multiple isolated environments on a single host But couldn't manage to get a sample example.

Any help is greatly appreciated.

Upvotes: 8

Views: 3388

Answers (1)

Shashank V
Shashank V

Reputation: 11193

You just need to use a different project name. By default compose uses the directory name in which your project is as the project name. docker-compose takes care of setting up the isolated network appropriately for each project.

To create two instances of your project namespaced as "dev" and "test", you can run it as follows:

docker-compose -p dev up
docker-compose -p test up 

From https://docs.docker.com/compose/reference/overview/

-p, --project-name NAME     Specify an alternate project name
                              (default: directory name)

You need to remove container_name field for multiple project instances to work. Compose prepends the container names with project name automatically but it won't do it if container_name is specified. You would get container name conflict when starting another project from the compose file if container_name is used. After removing container_name, your services will get service names as hostnames.

Upvotes: 14

Related Questions