From 8adc65e25a662c0fec65aef7cf25140935933947 Mon Sep 17 00:00:00 2001 From: whitemark Date: Wed, 6 Nov 2024 11:03:18 +0900 Subject: [PATCH] [bugfix/#66] Connect container with network --- .../blueprint/service/DockerSyncServiceImpl.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/api/goraebab/domain/blueprint/service/DockerSyncServiceImpl.java b/src/main/java/api/goraebab/domain/blueprint/service/DockerSyncServiceImpl.java index eabdb43..17b76b8 100644 --- a/src/main/java/api/goraebab/domain/blueprint/service/DockerSyncServiceImpl.java +++ b/src/main/java/api/goraebab/domain/blueprint/service/DockerSyncServiceImpl.java @@ -111,7 +111,7 @@ private void customNetworkValidationCheck(List customNetworkList) for (CustomNetwork customNetwork : customNetworkList) { for (CustomConfig config : customNetwork.getCustomIpam().getCustomConfig()) { if (customNetwork.getName().equals(DEFAULT_BRIDGE_NETWORK_NAME) - && config.getSubnet().equals(DEFAULT_BRIDGE_NETWORK_SUBNET)) { + && !config.getSubnet().equals(DEFAULT_BRIDGE_NETWORK_SUBNET)) { throw new CustomException(ErrorCode.NETWORK_CREATION_FAILED); } } @@ -222,15 +222,25 @@ private List> syncContainers(DockerClient dockerClient, List .withBinds(binds) .withMounts(mounts); + ContainerNetwork containerNetwork = new ContainerNetwork() + .withIpamConfig(new ContainerNetwork.Ipam()) + .withIpv4Address(customContainer.getCustomNetworkSettings().getIpAddress()); // IP 주소 설정 + // 컨테이너 생성 CreateContainerResponse containerResponse = dockerClient.createContainerCmd(imageName) .withName(containerName) .withHostConfig(hostConfig) .withEnv(customContainer.getCustomEnv()) .withCmd(customContainer.getCustomCmd()) - .withNetworkMode(customNetwork.getName()) .exec(); + + dockerClient.connectToNetworkCmd() + .withContainerId(containerResponse.getId()) + .withNetworkId(customNetwork.getName()) + .withContainerNetwork(containerNetwork) + .exec(); + dockerClient.startContainerCmd(containerResponse.getId()).exec(); containerResult.put(CONTAINER_RESULT_STATUS_KEY, CONTAINER_STATUS_SUCCESS);