Repository: localstack/localstack

Issue:
bug: PRO - lambda cannot comunicate with ext after upgrading to 0.12.17 or latest. -  [Errno -3] Temporary failure in name resolution
### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current Behavior

this is my docker-config.yml
```

localstack:
    image: localstack/localstack:latest
    restart : always
    environment:
      - AWS_DEFAULT_REGION=eu-central-1
      - SERVICES=neptune,route53,events,stepfunctions,kinesis,cloudtrail,ses,elbv2,es,ec2,secretsmanager,firehose,ecs,ecr,sns,sqs,appsync,edge,cognito,lambda,s3,docdb,apigateway,apigatewayv2,cloudformation,sts,iam,dynamodb:4701,cloudfront
      - START_WEB=0
      - DEBUG=1
      - NODE_TLS_REJECT_UNAUTHORIZED=0
      - DISABLE_EVENTS=true
      - TEST_AWS_ACCOUNT_ID=123456789012
      - LAMBDA_EXECUTOR=docker-reuse
      - HOSTNAME_EXTERNAL=localstack
      - LAMBDA_DOCKER_NETWORK=localstack_default
      - LAMBDA_REMOTE_DOCKER=true
      - LAMBDA_REMOVE_CONTAINERS=false
      - DATA_DIR=/tmp/localstack/data
      - HOST_TMP_FOLDER=/tmp/localstack
      - LOCALSTACK_API_KEY=xxxxxxxxxx
      - DOCKER_HOST=unix:///var/run/docker.sock
      - DYNAMODB_PORT_EXTERNAL=4701
      - HOSTNAME_EXTERNAL=localstack
      - LAMBDA_DOCKER_DNS=8.8.8.8
 ```

the  lambda container cannot comunicate with external service, but only with service inside docker network named localstack_default




### Expected Behavior

lambda container can comunicate with external service

### How are you starting LocalStack?

Custom (please describe below)

### Steps To Reproduce

executing this py script inside lambda container 

```
import requests
print (requests.get('https://ifconfig.co/json’).text)

```

i got " [Errno -3] Temporary failure in name resolution'"

work replacing the idfconfig.co with ip addess



### Environment

```markdown
- OS: debian 11
- LocalStack: 0.12.17 or latest
```


### Anything else?

the /etc/hosts file  into the lambda container started from localstack:0.12.17
```

127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.18.0.4	77c41d53db2b
172.17.0.1 localhost.localstack.cloud

```

the /etc/hosts file  into the lambda container started from localstack:0.12.16

```
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
172.18.0.3	8e545993dcf9
172.18.0.8 localhost.localstack.cloud
```





Discussion:

Resolving patch:
diff --git a/localstack/utils/docker.py b/localstack/utils/docker.py
--- a/localstack/utils/docker.py
+++ b/localstack/utils/docker.py
@@ -256,7 +256,10 @@ def get_container_id(self, container_name: str) -> str:
 
     @abstractmethod
     def get_container_ip(self, container_name_or_id: str) -> str:
-        """Get the IP address of a given container"""
+        """Get the IP address of a given container
+
+        If container has multiple networks, it will return the IP of the first
+        """
         pass
 
     def get_image_cmd(self, docker_image: str) -> str:
@@ -1116,7 +1119,11 @@ def inspect_image(self, image_name: str) -> Dict[str, Union[Dict, str]]:
             raise ContainerException()
 
     def get_container_ip(self, container_name_or_id: str) -> str:
-        return self.inspect_container(container_name_or_id)["NetworkSettings"]["IPAddress"]
+        networks = self.inspect_container(container_name_or_id)["NetworkSettings"]["Networks"]
+        network_names = list(networks)
+        if len(network_names) > 1:
+            LOG.info("Container has more than one assigned network. Picking the first one...")
+        return networks[network_names[0]]["IPAddress"]
 
     def has_docker(self) -> bool:
         try:


Test patch:
diff --git a/tests/integration/docker/test_docker.py b/tests/integration/docker/test_docker.py
--- a/tests/integration/docker/test_docker.py
+++ b/tests/integration/docker/test_docker.py
@@ -42,10 +42,12 @@ def dummy_container(create_container):
 
 
 @pytest.fixture
-def create_container(docker_client: ContainerClient):
+def create_container(docker_client: ContainerClient, create_network):
     """
     Uses the factory as fixture pattern to wrap ContainerClient.create_container as a factory that
     removes the containers after the fixture is cleaned up.
+
+    Depends on create network for correct cleanup order
     """
     containers = list()
 
@@ -65,6 +67,29 @@ def _create_container(image_name: str, **kwargs):
             LOG.warning("failed to remove test container %s", c)
 
 
+@pytest.fixture
+def create_network():
+    """
+    Uses the factory as fixture pattern to wrap the creation of networks as a factory that
+    removes the networks after the fixture is cleaned up.
+    """
+    networks = list()
+
+    def _create_network(network_name: str):
+        network_id = safe_run([config.DOCKER_CMD, "network", "create", network_name]).strip()
+        networks.append(network_id)
+        return network_id
+
+    yield _create_network
+
+    for network in networks:
+        try:
+            LOG.debug("Removing network %s", network)
+            safe_run([config.DOCKER_CMD, "network", "remove", network])
+        except CalledProcessError:
+            pass
+
+
 class TestDockerClient:
     def test_container_lifecycle_commands(self, docker_client: ContainerClient):
         container_name = _random_container_name()
@@ -746,3 +771,19 @@ def test_get_container_ip(self, docker_client: ContainerClient, dummy_container)
             ip,
         )
         assert "127.0.0.1" != ip
+
+    def test_get_container_ip_with_network(
+        self, docker_client: ContainerClient, create_container, create_network
+    ):
+        network_name = "ls_test_network_%s" % short_uid()
+        create_network(network_name)
+        container = create_container(
+            "alpine", network=network_name, command=["sh", "-c", "while true; do sleep 1; done"]
+        )
+        docker_client.start_container(container.container_id)
+        ip = docker_client.get_container_ip(container.container_id)
+        assert re.match(
+            r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$",
+            ip,
+        )
+        assert "127.0.0.1" != ip