No response from localhost

First time using docker. I used docker-composeI and made two simple docker images to connect each other.

  1. WhaleDocker3.py
import requests

response = requests.get('http://whaledocker4:8004/')

if response.ok:
    print("Success! Response from whaledocker4:")
    print(response.text)
else:
    print("Failed to get response from whaledocker4")


1-1 WhaleDocker3 docker file

FROM python:3
COPY WhaleDocker3.py /app/
RUN pip install --no-cache-dir requests
RUN pip install --no-cache-dir flask
CMD [ "python", "/app/WhaleDocker3.py" ]
  1. WhaleDocker4.py (also made docker image)
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "🐳 Hello from whaledocker4! 🐳"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8004)

2-1 WhaleDocker4 docker file

FROM python:3
COPY WhaleDocker4.py /app/
RUN pip install --no-cache-dir requests
RUN pip install --no-cache-dir flask
CMD [ "python", "/app/WhaleDocker4.py" ]
  1. docker-compose.yml
version: '3'

services:
  whaledocker3:
    image: whaledocker3
    ports:
      - "8003:8003"
    networks:
      - my_network

  whaledocker4:
    image: whaledocker4
    ports:
      - "8004:8004"
    networks:
      - my_network

networks:
  my_network:
    driver: bridge

whaledocker4 works very well but whaledocker3 don’t work…
error code here

2024-05-09 23:14:26 Traceback (most recent call last):
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/urllib3/connection.py", line 198, in _new_conn
2024-05-09 23:14:26     sock = connection.create_connection(
2024-05-09 23:14:26            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/urllib3/util/connection.py", line 60, in create_connection
2024-05-09 23:14:26     for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
2024-05-09 23:14:26                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/socket.py", line 963, in getaddrinfo
2024-05-09 23:14:26     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
2024-05-09 23:14:26                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-09 23:14:26 socket.gaierror: [Errno -2] Name or service not known
2024-05-09 23:14:26 
2024-05-09 23:14:26 The above exception was the direct cause of the following exception:
2024-05-09 23:14:26 
2024-05-09 23:14:26 Traceback (most recent call last):
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/urllib3/connectionpool.py", line 793, in urlopen
2024-05-09 23:14:26     response = self._make_request(
2024-05-09 23:14:26                ^^^^^^^^^^^^^^^^^^^
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/urllib3/connectionpool.py", line 496, in _make_request
2024-05-09 23:14:26     conn.request(
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/urllib3/connection.py", line 400, in request
2024-05-09 23:14:26     self.endheaders()
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/http/client.py", line 1331, in endheaders
2024-05-09 23:14:26     self._send_output(message_body, encode_chunked=encode_chunked)
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/http/client.py", line 1091, in _send_output
2024-05-09 23:14:26     self.send(msg)
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/http/client.py", line 1035, in send
2024-05-09 23:14:26     self.connect()
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/urllib3/connection.py", line 238, in connect
2024-05-09 23:14:26     self.sock = self._new_conn()
2024-05-09 23:14:26                 ^^^^^^^^^^^^^^^^
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/urllib3/connection.py", line 205, in _new_conn
2024-05-09 23:14:26     raise NameResolutionError(self.host, self, e) from e
2024-05-09 23:14:26 urllib3.exceptions.NameResolutionError: <urllib3.connection.HTTPConnection object at 0x7fe91ba3f7d0>: Failed to resolve 'whaledocker' ([Errno -2] Name or service not known)
2024-05-09 23:14:26 
2024-05-09 23:14:26 The above exception was the direct cause of the following exception:
2024-05-09 23:14:26 
2024-05-09 23:14:26 Traceback (most recent call last):
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/requests/adapters.py", line 486, in send
2024-05-09 23:14:26     resp = conn.urlopen(
2024-05-09 23:14:26            ^^^^^^^^^^^^^
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/urllib3/connectionpool.py", line 847, in urlopen
2024-05-09 23:14:26     retries = retries.increment(
2024-05-09 23:14:26               ^^^^^^^^^^^^^^^^^^
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/urllib3/util/retry.py", line 515, in increment
2024-05-09 23:14:26     raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
2024-05-09 23:14:26     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-09 23:14:26 urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='whaledocker', port=8004): Max retries exceeded with url: / (Caused by NameResolutionError("<urllib3.connection.HTTPConnection object at 0x7fe91ba3f7d0>: Failed to resolve 'whaledocker' ([Errno -2] Name or service not known)"))
2024-05-09 23:14:26 
2024-05-09 23:14:26 During handling of the above exception, another exception occurred:
2024-05-09 23:14:26 
2024-05-09 23:14:26 Traceback (most recent call last):
2024-05-09 23:14:26   File "/app/WhaleDocker3.py", line 3, in <module>
2024-05-09 23:14:26     response = requests.get('http://whaledocker:8004/')
2024-05-09 23:14:26                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/requests/api.py", line 73, in get
2024-05-09 23:14:26     return request("get", url, params=params, **kwargs)
2024-05-09 23:14:26            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/requests/api.py", line 59, in request
2024-05-09 23:14:26     return session.request(method=method, url=url, **kwargs)
2024-05-09 23:14:26            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/requests/sessions.py", line 589, in request
2024-05-09 23:14:26     resp = self.send(prep, **send_kwargs)
2024-05-09 23:14:26            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/requests/sessions.py", line 703, in send
2024-05-09 23:14:26     r = adapter.send(request, **kwargs)
2024-05-09 23:14:26         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-09 23:14:26   File "/usr/local/lib/python3.12/site-packages/requests/adapters.py", line 519, in send
2024-05-09 23:14:26     raise ConnectionError(e, request=request)
2024-05-09 23:14:26 requests.exceptions.ConnectionError: HTTPConnectionPool(host='whaledocker', port=8004): Max retries exceeded with url: / (Caused by NameResolutionError("<urllib3.connection.HTTPConnection object at 0x7fe91ba3f7d0>: Failed to resolve 'whaledocker' ([Errno -2] Name or service not known)"))

This error appears to be caused by the inability to resolve the hostname ‘whaledocker4’. I don’t know how to fix it. Is it a priority issue running within Docker Compose?

The error logs of the whaledocker3 container do not match the shared code of WhaleDocker3.py from above.

Please run this command to find out, if your image actually uses the exact same code that you shared:

docker run --rm --entrypoint sh whaledocker3 -c 'cat /app/WhaleDocker3.py'

I think I shared another whaledocker3 file as I fixed the code to fix the error. now i updated new error log!
can you check it again please?

new error log checked, same code

docker run --rm --entrypoint sh whaledocker3 -c 'cat /app/WhaleDocker3.py'
import requests

response = requests.get('http://whaledocker:8004/')

if response.ok:
    print("Success! Response from whaledocker4:")
    print(response.text)
else:
    print("Failed to get response from whaledocker4")```

You run

docker-compose

or

docker compose

CLI command?

Note that you don’t need to expose ports of all the containers, as all ports are reachable within the Docker network.

Also note that you start both apps in parallel. Maybe the request app is faster and fails connecting to the server app, as the server app is still starting up with the larger framework used.

why should this work when there is no whaledocker container only whaledocker3 and whaledocker4?