8

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.

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for reply. It is very much close to the solution that I am looking for. Running the docker-compose with project name is not allowing to set fixed container names: test_fixture/controller_g5 It looks like the container name has to be unique irrespective of network. Can you suggest a way to access other container in the network with out specifying container name s.connect(("dev_controller_1", 30001))
You need to remove container_name field to fix the container name conflict. Compose will prepend the project name automatically to containers usually unless container_name is specified. Your service will get hostnames same as the service names (controller and test_fixture) after removing the container_name field. If you have hardcoded the service host name as controller_g5 in code, then you rename the service to controller_g5.
dev_controller_1 . I am worried about the tailing number 1. The first container is name ends with _1 and the second _2. If i name the service as controller_g5 THe container name would be controller_g5_1
You don't need to worry about the numbers. Within a project, you can still use the service names as hostnames i.e., controller_g5 will just work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.