这是我的 Docker 文件的外观。
到目前为止,一切看起来都很好。但我意识到我有多个版本/功能的源代码。所以我希望创建多个实例,每个实例都有自己的网络。给容器命名是否会阻止在不同网络中使用相同名称的容器创建?此外,我不确定如何在同一主机上启动具有类似容器的另一个网络。我发现了单个主机上的多个隔离环境,但无法获得示例。任何帮助都将不胜感激。
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
...
基本上它有两个服务/容器:controller和test_fixture。 controller有运行的源代码,而test_fixture包含所有测试用例。 test_fixture需要通过套接字与控制器通信。由于docker-compose在其容器之间创建网络,在我的py-test案例中,我只是简单地使用
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("controller_g5", 30001)) # controller_g5 is name of the controller container
到目前为止,一切看起来都很好。但我意识到我有多个版本/功能的源代码。所以我希望创建多个实例,每个实例都有自己的网络。给容器命名是否会阻止在不同网络中使用相同名称的容器创建?此外,我不确定如何在同一主机上启动具有类似容器的另一个网络。我发现了单个主机上的多个隔离环境,但无法获得示例。任何帮助都将不胜感激。
s.connect(("dev_controller_1", 30001))
的情况下访问网络中其他容器的方法? - BhanuKirancontainer_name
字段来解决容器名称冲突。Compose 通常会自动将项目名称前缀添加到容器中,除非指定了container_name
。在删除container_name
字段后,你的服务将获得与服务名称相同的主机名(controller 和 test_fixture)。如果你在代码中硬编码了服务主机名为controller_g5
,那么你需要将服务重命名为controller_g5
。 - Shashank Vdev_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 ascontroller_g5
THe container name would becontroller_g5_1
- BhanuKirancontroller_g5
将正常工作。 - Shashank V