Sandbox with container

I tried starting the sandbox and subsequently ran the following program:

from algosdk.v2client import algod
    
my_address = "YVNCNM6TEBWTLN6OO5UYSZZNPPOH2WOJBG3S6YNT6FDJRVA6YWNKNL767Q"

algod_address = "http://localhost:4001"
algod_token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
algod_client = algod.AlgodClient(algod_token, algod_address)

account_info = algod_client.account_info(my_address)
print("Account balance: {} microAlgos".format(account_info.get('amount')) + "\n")

The result is: Account balance: 25959000 microAlgos.
From this I deduce that everything works correctly.

But I would like to put this program in a Docker container. I wrote the following Dockerfile which is located in the ‘tests’ folder along with the main.py above:

FROM python:latest

RUN pip3 install py-algorand-sdk
RUN pip install psycopg2

ADD main.py /files/

WORKDIR /files/

EXPOSE 4001

While this is the docker-compose.yml file:

version: "3"

services:

  sistemacentrale:
    stdin_open: true # docker run -i
    tty: true        # docker run -t
    build: files/
    command: python ./main.py

But I get the following error:

testing-sistemacentrale-1 | Traceback (most recent call last):
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/urllib/request.py”, line 1348, in do_open
testing-sistemacentrale-1 | h.request(req.get_method(), req.selector, req.data, headers,
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/http/client.py”, line 1282, in request
testing-sistemacentrale-1 | self._send_request(method, url, body, headers, encode_chunked)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/http/client.py”, line 1328, in _send_request
testing-sistemacentrale-1 | self.endheaders(body, encode_chunked=encode_chunked)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/http/client.py”, line 1277, in endheaders
testing-sistemacentrale-1 | self._send_output(message_body, encode_chunked=encode_chunked)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/http/client.py”, line 1037, in _send_output
testing-sistemacentrale-1 | self.send(msg)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/http/client.py”, line 975, in send
testing-sistemacentrale-1 | self.connect()
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/http/client.py”, line 941, in connect
testing-sistemacentrale-1 | self.sock = self._create_connection(
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/socket.py”, line 845, in create_connection
testing-sistemacentrale-1 | raise err
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/socket.py”, line 833, in create_connection
testing-sistemacentrale-1 | sock.connect(sa)
testing-sistemacentrale-1 | OSError: [Errno 99] Cannot assign requested address
testing-sistemacentrale-1 |
testing-sistemacentrale-1 | During handling of the above exception, another exception occurred:
testing-sistemacentrale-1 |
testing-sistemacentrale-1 | Traceback (most recent call last):
testing-sistemacentrale-1 | File “/files/./main.py”, line 9, in
testing-sistemacentrale-1 | account_info = algod_client.account_info(my_address)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/site-packages/algosdk/v2client/algod.py”, line 107, in account_info
testing-sistemacentrale-1 | return self.algod_request(“GET”, req, query, **kwargs)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/site-packages/algosdk/v2client/algod.py”, line 78, in algod_request
testing-sistemacentrale-1 | resp = urlopen(req)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/urllib/request.py”, line 216, in urlopen
testing-sistemacentrale-1 | return opener.open(url, data, timeout)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/urllib/request.py”, line 519, in open
testing-sistemacentrale-1 | response = self._open(req, data)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/urllib/request.py”, line 536, in _open
testing-sistemacentrale-1 | result = self._call_chain(self.handle_open, protocol, protocol +
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/urllib/request.py”, line 496, in _call_chain
testing-sistemacentrale-1 | result = func(*args)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/urllib/request.py”, line 1377, in http_open
testing-sistemacentrale-1 | return self.do_open(http.client.HTTPConnection, req)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/urllib/request.py”, line 1351, in do_open
testing-sistemacentrale-1 | raise URLError(err)
testing-sistemacentrale-1 | urllib.error.URLError: <urlopen error [Errno 99] Cannot assign requested address>
testing-sistemacentrale-1 exited with code 1

The program can be found here: https://drive.google.com/drive/folders/1IZjaksIW4aGDs9I4LVUffulu-1puPIQu

EXPOSE 4001 indicates to docker that you have a service inside the container that is listening to port 4001 (Dockerfile reference | Docker Documentation)

You actually want to do the exact opposite: you want your container to access the host/sandbox port 4001

In that case, you just need to replace localhost in algod_address by host.docker.internal.
This requires a recent enough version of Docker though. If this does not work, update Docker.

See From inside of a Docker container, how do I connect to the localhost of the machine? - Stack Overflow

1 Like

I solved it thanks to you!