Sandbox with container

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

from algosdk.v2client import algod

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 above:

FROM python:latest

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

ADD /files/

WORKDIR /files/


While this is the docker-compose.yml file:

version: "3"


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

But I get the following error:

testing-sistemacentrale-1 | Traceback (most recent call last):
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/urllib/”, line 1348, in do_open
testing-sistemacentrale-1 | h.request(req.get_method(), req.selector,, headers,
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/http/”, 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/”, 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/”, 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/”, line 1037, in _send_output
testing-sistemacentrale-1 | self.send(msg)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/http/”, line 975, in send
testing-sistemacentrale-1 | self.connect()
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/http/”, line 941, in connect
testing-sistemacentrale-1 | self.sock = self._create_connection(
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/”, line 845, in create_connection
testing-sistemacentrale-1 | raise err
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/”, 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/./”, 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/”, 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/”, line 78, in algod_request
testing-sistemacentrale-1 | resp = urlopen(req)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/urllib/”, line 216, in urlopen
testing-sistemacentrale-1 | return, data, timeout)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/urllib/”, line 519, in open
testing-sistemacentrale-1 | response = self._open(req, data)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/urllib/”, 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/”, line 496, in _call_chain
testing-sistemacentrale-1 | result = func(*args)
testing-sistemacentrale-1 | File “/usr/local/lib/python3.10/urllib/”, 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/”, 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:

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!