Hamza Halabi
Hamza Halabi

Reputation: 21

Call ArangoDB docker container from a python script container fails

I have a python script that I dockerized, inside the script i make a call to ArangoDB. I have another container running an ArangoDB image. When i run both containers, I'm able to access ArangoDB on my localhost and if I run my python script from outside its container it can call ArangoDB successfully. However when i run the script from inside a docker container, it fails to connect to the containerized ArangoDB because it does not identify that there is a server up and running, i get the following error:

File "/usr/local/lib/python3.7/site-packages/arango/client.py", line 167, in db
    raise ServerConnectionError('bad connection: {}'.format(err))
arango.exceptions.ServerConnectionError: bad connection: HTTPConnectionPool(host='127.0.0.1', port=8529): Max retries exceeded with url: /_db/dox/_api/collection (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f17a4db3450>: Failed to establish a new connection: [Errno 
111] Connection refused'))

Here is my dockerfile for the python script

FROM python:3.7-buster

WORKDIR /usr/src/app

COPY requirements.txt ./
RUN pip install -r requirements.txt

COPY . .

CMD [ "python", "main.py" ]

And this is where i call ArangoDB from my python script:

class ArangoDB():
    def __init__(self, db, username, password):
        """
        Creates an ArangoDB session and connects to a specified Arango database.

        Parameters
        ----------
        db : str
            Arango database name
        username : str
            Arango database username
        password : str
            Arango database password

        Returns
        -------
        An instance of ArangoDB API Wrapper
        """
        logger.info(f'User {username} Connecting to database: {db}')
        self._conn = ArangoClient()
        self._db = self._conn.db(name=db, username=username, password=password,verify=True)
        logger.info(f"Successfully connected to database: {db}")
    

Finally, this is my docker-compose.yml

version: "3.8"
services: 
    arangodb:
        image: arangodb:latest
        container_name: db
        environment: 
            ARANGO_ROOT_PASSWORD: route66
        ports:
            - "8529:8529"
        volumes:
            - arangodb_data:/var/lib/arangodb3
            - arangodb_apps_data:/var/lib/arangodb3-apps
    python:
        build: .
        depends_on: 
            - arangodb
        ports:
            - "5000:5000"
        container_name: myapp
networks:
    default:
        external:
            name: back

volumes:
    arangodb_data:
    arangodb_apps_data:

Upvotes: 1

Views: 660

Answers (1)

Joe Moraca
Joe Moraca

Reputation: 11

I had apache / php in a container and arangodb in a different container.

I had to use the "bridge" network that docker auto creates for all running containers. these commands will show you what you have: docker network ll docker network inspect bridge:

I got the IPv4Addresss from the arangodb container. In my case it is 172.17.0.2

I had to change the endpoint to match that IP address ArangoConnectionOptions::OPTION_ENDPOINT => 'tcp://172.17.0.2:8529',

Upvotes: 0

Related Questions