Airflow running inside a container cannot call another container - python

I'm trying to run a DAG that calls a docker container and executes a command inside it, but Airflow cannot execute the task. Follows the error launched:
*** Reading local file: /opt/airflow/logs/docker_operator_dag/docker_command_hello/2021-05-26T02:40:13.171571+00:00/2.log
[2021-05-26 02:45:26,001] {taskinstance.py:877} INFO - Dependencies all met for <TaskInstance: docker_operator_dag.docker_command_hello 2021-05-26T02:40:13.171571+00:00 [queued]>
[2021-05-26 02:45:26,030] {taskinstance.py:877} INFO - Dependencies all met for <TaskInstance: docker_operator_dag.docker_command_hello 2021-05-26T02:40:13.171571+00:00 [queued]>
[2021-05-26 02:45:26,031] {taskinstance.py:1068} INFO -
--------------------------------------------------------------------------------
[2021-05-26 02:45:26,031] {taskinstance.py:1069} INFO - Starting attempt 2 of 2
[2021-05-26 02:45:26,032] {taskinstance.py:1070} INFO -
--------------------------------------------------------------------------------
[2021-05-26 02:45:26,060] {taskinstance.py:1089} INFO - Executing <Task(DockerOperator): docker_command_hello> on 2021-05-26T02:40:13.171571+00:00
[2021-05-26 02:45:26,080] {standard_task_runner.py:52} INFO - Started process 67 to run task
[2021-05-26 02:45:26,083] {standard_task_runner.py:76} INFO - Running: ['airflow', 'tasks', 'run', 'docker_operator_dag', 'docker_command_hello', '2021-05-26T02:40:13.171571+00:00', '--job-id', '11', '--pool', 'default_pool', '--raw', '--subdir', 'DAGS_FOLDER/docker_job/docker-job.py', '--cfg-path', '/tmp/tmp3vl5dv7x', '--error-file', '/tmp/tmptazwx_tc']
[2021-05-26 02:45:26,084] {standard_task_runner.py:77} INFO - Job 11: Subtask docker_command_hello
[2021-05-26 02:45:26,181] {logging_mixin.py:104} INFO - Running <TaskInstance: docker_operator_dag.docker_command_hello 2021-05-26T02:40:13.171571+00:00 [running]> on host f1e5cfe4a07f
[2021-05-26 02:45:26,219] {taskinstance.py:1283} INFO - Exporting the following env vars:
AIRFLOW_CTX_DAG_OWNER=airflow
AIRFLOW_CTX_DAG_ID=docker_operator_dag
AIRFLOW_CTX_TASK_ID=docker_command_hello
AIRFLOW_CTX_EXECUTION_DATE=2021-05-26T02:40:13.171571+00:00
AIRFLOW_CTX_DAG_RUN_ID=manual__2021-05-26T02:40:13.171571+00:00
[2021-05-26 02:45:26,227] {taskinstance.py:1482} ERROR - Task failed with exception
Traceback (most recent call last):
File "/home/airflow/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 677, in urlopen
chunked=chunked,
File "/home/airflow/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 392, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/local/lib/python3.6/http/client.py", line 1287, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/local/lib/python3.6/http/client.py", line 1333, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/local/lib/python3.6/http/client.py", line 1282, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/local/lib/python3.6/http/client.py", line 1042, in _send_output
self.send(msg)
File "/usr/local/lib/python3.6/http/client.py", line 980, in send
self.connect()
File "/home/airflow/.local/lib/python3.6/site-packages/docker/transport/unixconn.py", line 43, in connect
sock.connect(self.unix_socket)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/airflow/.local/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/home/airflow/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 727, in urlopen
method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
File "/home/airflow/.local/lib/python3.6/site-packages/urllib3/util/retry.py", line 410, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/home/airflow/.local/lib/python3.6/site-packages/urllib3/packages/six.py", line 734, in reraise
raise value.with_traceback(tb)
File "/home/airflow/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 677, in urlopen
chunked=chunked,
File "/home/airflow/.local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 392, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/local/lib/python3.6/http/client.py", line 1287, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/local/lib/python3.6/http/client.py", line 1333, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/local/lib/python3.6/http/client.py", line 1282, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/local/lib/python3.6/http/client.py", line 1042, in _send_output
self.send(msg)
File "/usr/local/lib/python3.6/http/client.py", line 980, in send
self.connect()
File "/home/airflow/.local/lib/python3.6/site-packages/docker/transport/unixconn.py", line 43, in connect
sock.connect(self.unix_socket)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionRefusedError(111, 'Connection refused'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/airflow/.local/lib/python3.6/site-packages/docker/api/client.py", line 207, in _retrieve_server_version
return self.version(api_version=False)["ApiVersion"]
File "/home/airflow/.local/lib/python3.6/site-packages/docker/api/daemon.py", line 181, in version
return self._result(self._get(url), json=True)
File "/home/airflow/.local/lib/python3.6/site-packages/docker/utils/decorators.py", line 46, in inner
return f(self, *args, **kwargs)
File "/home/airflow/.local/lib/python3.6/site-packages/docker/api/client.py", line 230, in _get
return self.get(url, **self._set_request_timeout(kwargs))
File "/home/airflow/.local/lib/python3.6/site-packages/requests/sessions.py", line 555, in get
return self.request('GET', url, **kwargs)
File "/home/airflow/.local/lib/python3.6/site-packages/requests/sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "/home/airflow/.local/lib/python3.6/site-packages/requests/sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "/home/airflow/.local/lib/python3.6/site-packages/requests/adapters.py", line 498, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionRefusedError(111, 'Connection refused'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/airflow/.local/lib/python3.6/site-packages/airflow/models/taskinstance.py", line 1138, in _run_raw_task
self._prepare_and_execute_task_with_callbacks(context, task)
File "/home/airflow/.local/lib/python3.6/site-packages/airflow/models/taskinstance.py", line 1311, in _prepare_and_execute_task_with_callbacks
result = self._execute_task(context, task_copy)
File "/home/airflow/.local/lib/python3.6/site-packages/airflow/models/taskinstance.py", line 1341, in _execute_task
result = task_copy.execute(context=context)
File "/home/airflow/.local/lib/python3.6/site-packages/airflow/providers/docker/operators/docker.py", line 287, in execute
self.cli = self._get_cli()
File "/home/airflow/.local/lib/python3.6/site-packages/airflow/providers/docker/operators/docker.py", line 319, in _get_cli
return APIClient(base_url=self.docker_url, version=self.api_version, tls=tls_config)
File "/home/airflow/.local/lib/python3.6/site-packages/docker/api/client.py", line 190, in __init__
self._version = self._retrieve_server_version()
File "/home/airflow/.local/lib/python3.6/site-packages/docker/api/client.py", line 215, in _retrieve_server_version
'Error while fetching server API version: {0}'.format(e)
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', ConnectionRefusedError(111, 'Connection refused'))
[2021-05-26 02:45:26,230] {taskinstance.py:1532} INFO - Marking task as FAILED. dag_id=docker_operator_dag, task_id=docker_command_hello, execution_date=20210526T024013, start_date=20210526T024526, end_date=20210526T024526
[2021-05-26 02:45:26,261] {local_task_job.py:146} INFO - Task exited with return code 1
I'm using the Airflow docker-compose found here https://airflow.apache.org/docs/apache-airflow/stable/start/docker.html and trying to run the following DAG:
from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from airflow.operators.docker_operator import DockerOperator
from airflow.operators.python_operator import BranchPythonOperator
from airflow.operators.dummy_operator import DummyOperator
default_args = {
'owner' : 'airflow',
'description' : 'Use of the DockerOperator',
'depend_on_past' : False,
'start_date' : datetime(2021, 5, 1),
'email_on_failure' : False,
'email_on_retry' : False,
'retries' : 1,
'retry_delay' : timedelta(minutes=5)
}
with DAG('docker_operator_dag', default_args=default_args, schedule_interval="5 * * * *", catchup=False) as dag:
start_dag = DummyOperator(
task_id='start_dag'
)
end_dag = DummyOperator(
task_id='end_dag'
)
t1 = BashOperator(
task_id='print_current_date',
bash_command='date'
)
t2 = DockerOperator(
task_id='docker_command_sleep',
image='docker_image_task',
container_name='task___command_sleep',
api_version='auto',
auto_remove=True,
command="/bin/sleep 30",
docker_url="unix://var/run/docker.sock",
network_mode="bridge"
)
t3 = DockerOperator(
task_id='docker_command_hello',
image='docker_image_task',
container_name='task___command_hello',
api_version='auto',
auto_remove=True,
command="/bin/sleep 40",
docker_url="unix://var/run/docker.sock",
network_mode="bridge"
)
t4 = BashOperator(
task_id='print_hello',
bash_command='echo "hello world"'
)
start_dag >> t1
t1 >> t2 >> t4
t1 >> t3 >> t4
t4 >> end_dag
Also, I'm using Windows 10, I've tried adding the following in volumes: - "/var/run/docker.sock:/var/run/docker.sock" and I've succeeded in Ubuntu, but in Windows doesn't.
As requested, follow the docker-compose file:
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
# Basic Airflow cluster configuration for CeleryExecutor with Redis and PostgreSQL.
#
# WARNING: This configuration is for local development. Do not use it in a production deployment.
#
# This configuration supports basic configuration using environment variables or an .env file
# The following variables are supported:
#
# AIRFLOW_IMAGE_NAME - Docker image name used to run Airflow.
# Default: apache/airflow:master-python3.8
# AIRFLOW_UID - User ID in Airflow containers
# Default: 50000
# AIRFLOW_GID - Group ID in Airflow containers
# Default: 50000
# _AIRFLOW_WWW_USER_USERNAME - Username for the administrator account.
# Default: airflow
# _AIRFLOW_WWW_USER_PASSWORD - Password for the administrator account.
# Default: airflow
#
# Feel free to modify this file to suit your needs.
---
version: '3'
x-airflow-common:
&airflow-common
image: ${AIRFLOW_IMAGE_NAME:-apache/airflow:2.0.2}
environment:
&airflow-common-env
AIRFLOW__CORE__EXECUTOR: CeleryExecutor
AIRFLOW__CORE__SQL_ALCHEMY_CONN: postgresql+psycopg2://airflow:airflow#postgres/airflow
AIRFLOW__CELERY__RESULT_BACKEND: db+postgresql://airflow:airflow#postgres/airflow
AIRFLOW__CELERY__BROKER_URL: redis://:#redis:6379/0
AIRFLOW__CORE__FERNET_KEY: ''
AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION: 'true'
AIRFLOW__CORE__LOAD_EXAMPLES: 'false'
AIRFLOW__SCHEDULER__DAG_DIR_LIST_INTERVAL: 5 # Just to have a fast load in the front-end. Do not use it in production with those configurations.
AIRFLOW__API__AUTH_BACKEND: 'airflow.api.auth.backend.basic_auth'
AIRFLOW__CORE__ENABLE_XCOM_PICKLING: 'true' # "_run_image of the DockerOperator returns now a python string, not a byte string" Ref: https://github.com/apache/airflow/issues/13487
volumes:
- ./dags:/opt/airflow/dags
- ./logs:/opt/airflow/logs
- ./plugins:/opt/airflow/plugins
- "/var/run/docker.sock:/var/run/docker.sock" # We will pass the Docker Deamon as a volume to allow the webserver containers start docker images. Ref: https://stackoverflow.com/q/51342810/7024760
user: "${AIRFLOW_UID:-50000}:${AIRFLOW_GID:-50000}"
depends_on:
redis:
condition: service_healthy
postgres:
condition: service_healthy
services:
postgres:
image: postgres:13
environment:
POSTGRES_USER: airflow
POSTGRES_PASSWORD: airflow
POSTGRES_DB: airflow
volumes:
- postgres-db-volume:/var/lib/postgresql/data
healthcheck:
test: ["CMD", "pg_isready", "-U", "airflow"]
interval: 5s
retries: 5
restart: always
redis:
image: redis:latest
ports:
- 6379:6379
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 5s
timeout: 30s
retries: 50
restart: always
airflow-webserver:
<<: *airflow-common
command: webserver
ports:
- 8080:8080
healthcheck:
test: ["CMD", "curl", "--fail", "http://localhost:8080/health"]
interval: 10s
timeout: 10s
retries: 5
restart: always
airflow-scheduler:
<<: *airflow-common
command: scheduler
restart: always
airflow-worker:
<<: *airflow-common
command: celery worker
restart: always
airflow-init:
<<: *airflow-common
command: version
environment:
<<: *airflow-common-env
_AIRFLOW_DB_UPGRADE: 'true'
_AIRFLOW_WWW_USER_CREATE: 'true'
_AIRFLOW_WWW_USER_USERNAME: ${_AIRFLOW_WWW_USER_USERNAME:-airflow}
_AIRFLOW_WWW_USER_PASSWORD: ${_AIRFLOW_WWW_USER_PASSWORD:-airflow}
flower:
<<: *airflow-common
command: celery flower
ports:
- 5555:5555
healthcheck:
test: ["CMD", "curl", "--fail", "http://localhost:5555/"]
interval: 10s
timeout: 10s
retries: 5
restart: always
volumes:
postgres-db-volume:

Related

Flask render_template error: TemplateNotFound when using Docker Swarm/Stack

Currently we use Docker Compose to start up services but we need secrets to secure passwords and the like, therefore the use of Docker Swarm & secrets is required.
Everything works perfectly with Compose but when we switch to Swarm a templating issue appears when loading the UI.
docker-compose.yml stripped down for simplicity
version: '3.7'
services:
postgresql:
image: docker.io/bitnami/postgresql:12.11.0
volumes:
- 'postgresql_data:/bitnami/postgresql'
environment:
#- ALLOW_EMPTY_PASSWORD=yes
- POSTGRESQL_USERNAME=user
- POSTGRESQL_DATABASE=user
- POSTGRESQL_PASSWORD_FILE=/path/to/docker/secret
secrets:
- secret
keycloak:
image: docker.io/bitnami/keycloak:16.1.1
depends_on:
- postgresql
environment:
- KEYCLOAK_ADMIN_USER=user
- KEYCLOAK_ADMIN_PASSWORD_FILE=/path/to/docker/secret
- KEYCLOAK_DATABASE_PORT=5432
- KEYCLOAK_DATABASE_HOST=postgresql
- KEYCLOAK_DATABASE_NAME=user
- KEYCLOAK_DATABASE_USER=user
- KEYCLOAK_DATABASE_PASSWORD_FILE=/path/to/docker/secret
- KEYCLOAK_HTTP_PORT=8083
- KEYCLOAK_HTTPS_PORT=8443
- KEYCLOAK_ENABLE_TLS=true
- KEYCLOAK_TLS_KEYSTORE_FILE=/path/to/keystore.jks
- KEYCLOAK_TLS_KEYSTORE_PASSWORD_FILE=/path/to/docker/secret
- KEYCLOAK_TLS_TRUSTSTORE_FILE=/path/to/truststore.jks
- KEYCLOAK_TLS_TRUSTSTORE_PASSWORD_FILE=/path/to/docker/secret
volumes:
- ".path/to/keycloak.jks:/opt/bitnami/keycloak/certs/keystore.jks"
- ".path/to/keycloak.jks:/opt/bitnami/keycloak/certs/truststore.jks"
networks:
default:
aliases:
- keycloak.host.net
ports:
- 8083:8083
- 8443:8443
secrets:
- secret
ui:
image: <image from AWS ECR>
labels:
COMPOSE_PATH: ${PWD}
depends_on:
- keycloak
ports:
- 8000:8000
restart: always
environment:
- HOSTNAME=ui
- NAME=UI
- PORT=8000
- REGISTER_WITH_KEYCLOAK=True
- ENVIRONMENT=prod
- CONSUL_URL=http://consul-server-bootstrap:8500
- MONGO_URI=mongodb://user:password#mongodb:27017/db
- QUEUE_HOSTNAME=queue
UI code from config.py
# Landing page
#app.route("/")
def index():
try:
current_app.logger.info(f"public/index.html page being displayed")
return Response(render_template("public/index.html"), mimetype="text/html")
except exception_with_data as ex:
return ex.repr_as_dict(), 400
The error
2022-07-26T08:21:03.252452 Thread-83 UI app ERROR Exception on / [GET]
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File "/usr/local/lib/python3.9/site-packages/ui/main/config.py", line 209, in index
return Response(render_template("public/index.html"), mimetype="text/html")
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 148, in render_template
ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1068, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 997, in get_template
return self._load_template(name, globals)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 958, in _load_template
template = self.loader.load(self, name, self.make_globals(globals))
File "/usr/local/lib/python3.9/site-packages/jinja2/loaders.py", line 125, in load
source, filename, uptodate = self.get_source(environment, name)
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 59, in get_source
return self._get_source_fast(environment, template)
File "/usr/local/lib/python3.9/site-packages/flask/templating.py", line 95, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: public/index.html
Code tree
ui
/main
app.py
config.py
/templates
/admin
<all admin html pages>
/public
index.html
<all other public html pages>
All the files are in the correct location in the docker container.
The only difference between the yml files is the one for swarm has secrets.
docker-compose up -d works as expected and the UI pages render but when
docker stack deploy -c docker-compose-swarm.yml name is ran, the UI pages do not render.
The pages that do not use render_template (debugging pages) work fine and the UI registers with consul as expected.
Any help is appreciated!
Versions:
Flask==2.0.1
Werkzeug==2.0.1
Jinja2==3.0.1
itsdangerous==2.0.1

python script return error after running redis as a job

I have a script writen in a python:
#!/usr/bin/env python
import time
import rediswq
from datetime import datetime
import os
#import subprocess
#host="redis"
# Uncomment next two lines if you do not have Kube-DNS working.
import os
host = os.getenv("REDIS_SERVICE_HOST")
q = rediswq.RedisWQ(name="dealer_codes", host=host)
print("Worker with sessionID: " + q.sessionID())
print("Initial queue state: empty=" + str(q.empty()))
while not q.empty():
item = q.lease(lease_secs=10, block=True, timeout=2)
if item is not None:
itemstr = item.decode("utf-8")
time.sleep(10) # Put your actual work here instead of sleep.
q.complete(item)
else:
print("Waiting for work")
print("Queue empty, exiting")
which is working when I run a redis pod with configuration:
apiVersion: v1
kind: Pod
metadata:
name: redis-master
namespace: sandbox
labels:
app: redis
spec:
containers:
- name: master
image: redis
command:
- redis-server
env:
- name: MASTER
value: "true"
ports:
- containerPort: 6379
when I convert this pod definition file to job like it follows:
apiVersion: batch/v1
kind: Job
metadata:
name: redis-master
namespace: sandbox
spec:
parallelism: 1
ttlSecondsAfterFinished: 10
template:
spec:
containers:
- name: master
image: redis
command:
- redis-server
env:
- name: MASTER
value: "true"
ports:
- containerPort: 6379
restartPolicy: Never
apiVersion: v1
kind: Service
metadata:
name: redis
namespace: sandbox
spec:
ports:
- port: 6379
targetPort: 6379
selector:
app: redis
I get an error from script that says ConnectionRefusedError: [Errno 111] Connection refused:
Worker with sessionID: c7537dc0-0a7c-4d8e-a845-b863441f6433
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 614, in connect
sock = self.retry.call_with_retry(
File "/usr/local/lib/python3.10/site-packages/redis/retry.py", line 45, in call_with_retry
return do()
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 615, in <lambda>
lambda: self._connect(), lambda error: self.disconnect(error)
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 680, in _connect
raise err
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 668, in _connect
sock.connect(socket_address)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "//worker.py", line 16, in <module>
print("Initial queue state: empty=" + str(q.empty()))
File "/rediswq.py", line 56, in empty
return self._main_qsize() == 0 and self._processing_qsize() == 0
File "/rediswq.py", line 45, in _main_qsize
return self._db.llen(self._main_q_key)
File "/usr/local/lib/python3.10/site-packages/redis/commands/core.py", line 2498, in llen
return self.execute_command("LLEN", name)
File "/usr/local/lib/python3.10/site-packages/redis/client.py", line 1215, in execute_command
conn = self.connection or pool.get_connection(command_name, **options)
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 1386, in get_connection
connection.connect()
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 620, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 111 connecting to 10.104.106.102:6379. Connection refused.
I see that is something regarding connection. how can i make this script is working?

aiohttp.client_exceptions.ClientConnectorError with Discord bot using Docker-compose and Lavalink

I'm making a Discord bot in python, and I get an error when my bot tries to connect with lavalink. I use Docker-compose for the bot and lavalink, and my lavalink client is wavelink. I have checked that lavalink is on the right port.
Does anyone have any idea what's wrong?
My docker-compose.yml:
version: "3.9"
services:
bot:
build:
context: .
dockerfile: Dockerfile
restart: on-failure
depends_on:
lavalink:
condition: service_started
lavalink:
container_name: lavalink
image: fredboat/lavalink:master
volumes:
- ./Lavalink/application.yml:/opt/Lavalink/application.yml
ports:
- 2333
restart: always
My music.py:
class Music(commands.Cog, wavelink.WavelinkMixin, name="music"):
def __init__(self, client):
self.client = client
self.fancy_name = "Music"
if not hasattr(client, 'wavelink'):
self.client.wavelink = wavelink.Client(bot=self.client)
self.client.loop.create_task(self.start_nodes())
async def start_nodes(self):
await self.client.wait_until_ready()
await self.client.wavelink.initiate_node(host='127.0.0.1',
port=2333,
rest_uri='http://127.0.0.1:2333',
password="pass",
identifier='MAIN',
region='us_central')
And my error:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/wavelink/websocket.py", line 76, in _connect
self._websocket = await self._node.session.ws_connect(uri, headers=self.headers, heartbeat=self._node.heartbeat)
File "/usr/local/lib/python3.9/site-packages/aiohttp/client.py", line 721, in _ws_connect
resp = await self.request(method, url,
File "/usr/local/lib/python3.9/site-packages/aiohttp/client.py", line 480, in _request
conn = await self._connector.connect(
File "/usr/local/lib/python3.9/site-packages/aiohttp/connector.py", line 523, in connect
proto = await self._create_connection(req, traces, timeout)
File "/usr/local/lib/python3.9/site-packages/aiohttp/connector.py", line 858, in _create_connection
_, proto = await self._create_direct_connection(
File "/usr/local/lib/python3.9/site-packages/aiohttp/connector.py", line 1004, in _create_direct_connection
raise last_exc
File "/usr/local/lib/python3.9/site-packages/aiohttp/connector.py", line 980, in _create_direct_connection
transp, proto = await self._wrap_create_connection(
File "/usr/local/lib/python3.9/site-packages/aiohttp/connector.py", line 943, in _wrap_create_connection
raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host 127.0.0.1:2333 ssl:default [Connect call failed ('127.0.0.1', 2333)]
Your bot container needs to connect to http://lavalink:2333, not itself on 127.0.0.1
You mapping is wrong you declared the port but didn't mapped it to you localhost port.
change this
ports:
- 2333
to
ports:
- "2333:2333"
this will map the port from your docker container running lavalink to your localhost port 2333 or you can assign anyport you want.
ports:
"first_arg:second_arg"
first : localhost port
second: docker container port

Localstack lambda is not triggered by SQS

I'm trying to use localstack to test a lambda triggered by an SQS event.
Here it is my localstack docker-compose:
version: "3.8"
services:
localstack:
container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
image: localstack/localstack:latest
network_mode: bridge
ports:
- "127.0.0.1:53:53"
- "127.0.0.1:53:53/udp"
- "127.0.0.1:443:443"
- "127.0.0.1:4566:4566"
- "127.0.0.1:4571:4571"
environment:
- SERVICES=${SERVICES- }
- DEBUG=1
- DATA_DIR=${DATA_DIR- }
- LAMBDA_EXECUTOR=docker
- LAMBDA_REMOTE_DOCKER=false
- LOCALSTACK_API_KEY=${LOCALSTACK_API_KEY- }
- KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
- DOCKER_HOST=unix:///var/run/docker.sock
- HOST_TMP_FOLDER=${TMPDIR}
volumes:
- "${TMPDIR:-/tmp/localstack}:/tmp/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
And here is my python lambda code in the handler.py file:
import json
import logging
import os
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def handler(event, context):
try:
logger.info(f"### ENV VARS {os.environ}")
logger.info(f"### EVENT {event}")
logger.info(f"### ENV VARS {context}")
if event and "Records" in event:
for elt in event["Records"]:
logger.info(elt["body"])
return True
except Exception as e:
logger.error(e)
raise e
Here it is the lambda Dockerfile
FROM public.ecr.aws/lambda/python:3.9
RUN python -m pip install --upgrade pip
COPY requirements.txt ${LAMBDA_TASK_ROOT}
RUN pip install -r ${LAMBDA_TASK_ROOT}/requirements.txt --quiet
COPY src/ ${LAMBDA_TASK_ROOT}
CMD ["handler.handler"]
I run the following commands to create the lambda and the event
aws --endpoint http://localhost:4566 sqs create-queue --queue-name TEST_QUEUE
aws --endpoint http://localhost:4566 lambda create-function --timeout 120 --function-name python-lambda-test --handler handler.handler --code ImageUri=python-lambda-test:latest --role arn:aws:iam::000000000:role/lambda-ex
aws --endpoint http://localhost:4566 lambda create-event-source-mapping --function-name python-lambda-test --batch-size 1 --event-source-arn arn:aws:sqs:sa-east-1:000000000000:TEST_QUEUE
And finally, this command to test the Lambda function
aws --endpoint http://localhost:4566 sqs send-message --queue-url http://localhost:4566/000000000000/TEST_QUEUE --message-body 'this is a message'
But all this without success
I'm receiving this message in the logs
2021-08-30 18:30:23,439:API: 127.0.0.1 - - [30/Aug/2021 18:30:23] "POST / HTTP/1.1" 200 -
2021-08-30T18:30:23:DEBUG:localstack.services.awslambda.lambda_api: Found 1 source mappings for event from SQS queue arn:aws:sqs:sa-east-1:000000000000:TEST_QUEUE: ['arn:aws:lambda:sa-east-1:000000000000:function:python-lambda-test']
2021-08-30T18:30:23:DEBUG:localstack.services.awslambda.lambda_api: Starting SQS message polling thread for Lambda API
2021-08-30 18:30:23,467:API: 127.0.0.1 - - [30/Aug/2021 18:30:23] "POST / HTTP/1.1" 200 -
2021-08-30 18:30:23,520:API: 127.0.0.1 - - [30/Aug/2021 18:30:23] "POST / HTTP/1.1" 200 -
2021-08-30T18:30:23:DEBUG:localstack.services.awslambda.lambda_api: Sending event from event source arn:aws:sqs:sa-east-1:000000000000:TEST_QUEUE to Lambda arn:aws:lambda:sa-east-1:000000000000:function:python-lambda-test
2021-08-30T18:30:23:DEBUG:localstack.services.awslambda.lambda_executors: Lambda executed in Event (asynchronous) mode, no response will be returned to caller
2021-08-30T18:30:23:INFO:localstack.services.awslambda.lambda_executors: Determined main container target IP: 172.17.0.2
2021-08-30T18:30:23:INFO:localstack.services.awslambda.lambda_executors: Running lambda: arn:aws:lambda:sa-east-1:000000000000:function:python-lambda-test
2021-08-30T18:30:23:DEBUG:localstack.utils.docker: Running container with image: lambci/lambda:
2021-08-30T18:30:23:DEBUG:localstack.utils.docker: Creating container with image: lambci/lambda:
2021-08-30T18:30:23:DEBUG:localstack.services.awslambda.lambda_executors: Lambda arn:aws:lambda:sa-east-1:000000000000:function:python-lambda-test result / log output:
>
2021-08-30 18:30:23,635:API: 127.0.0.1 - - [30/Aug/2021 18:30:23] "POST / HTTP/1.1" 200 -
2021-08-30 18:30:23,658:API: 127.0.0.1 - - [30/Aug/2021 18:30:23] "POST / HTTP/1.1" 200 -
2021-08-30 18:30:23,677:API: 127.0.0.1 - - [30/Aug/2021 18:30:23] "POST / HTTP/1.1" 200 -
2021-08-30 18:30:23,701:API: 127.0.0.1 - - [30/Aug/2021 18:30:23] "POST / HTTP/1.1" 200 -
2021-08-30 18:30:23,734:API: 127.0.0.1 - - [30/Aug/2021 18:30:23] "POST / HTTP/1.1" 200 -
2021-08-30 18:30:23,754:API: 127.0.0.1 - - [30/Aug/2021 18:30:23] "POST / HTTP/1.1" 200 -
2021-08-30T18:30:23:INFO:localstack.utils.run: Thread run method <function LambdaExecutor.execute.<locals>.do_execute at 0x7f69de3770e0>(None) failed: Lambda process returned with error. Result: . Output:
Traceback (most recent call last):
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/docker/api/client.py", line 268, in _raise_for_status
response.raise_for_status()
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/requests/models.py", line 943, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http+docker://localhost/v1.41/containers/create
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/code/localstack/localstack/utils/docker.py", line 1230, in create_container
container = create_container()
File "/opt/code/localstack/localstack/utils/docker.py", line 1226, in create_container
**kwargs,
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/docker/models/containers.py", line 870, in create
resp = self.client.api.create_container(**create_kwargs)
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/docker/api/container.py", line 430, in create_container
return self.create_container_from_config(config, name)
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/docker/api/container.py", line 441, in create_container_from_config
return self._result(res, True)
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/docker/api/client.py", line 274, in _result
self._raise_for_status(response)
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/docker/api/client.py", line 270, in _raise_for_status
raise create_api_error_from_http_exception(e)
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 400 Client Error for http+docker://localhost/v1.41/containers/create: Bad Request ("invalid reference format")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 336, in run_lambda_executor
func_details, env_vars, command, docker_flags=docker_flags, stdin=event_stdin_bytes
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 815, in execute_in_container
stdin=stdin,
File "/opt/code/localstack/localstack/utils/docker.py", line 1280, in run_container
additional_flags=additional_flags,
File "/opt/code/localstack/localstack/utils/docker.py", line 1238, in create_container
raise ContainerException()
localstack.utils.docker.ContainerException
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/code/localstack/localstack/utils/run.py", line 138, in run
result = self.func(self.params, **kwargs)
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 247, in do_execute
return _run(func_arn=func_arn)
File "/opt/code/localstack/localstack/utils/cloudwatch/cloudwatch_util.py", line 157, in wrapped
raise e
File "/opt/code/localstack/localstack/utils/cloudwatch/cloudwatch_util.py", line 153, in wrapped
result = func(*args, **kwargs)
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 234, in _run
raise e
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 220, in _run
result = self._execute(func_arn, func_details, event, context, version)
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 450, in _execute
event=stdin, env_vars=environment, func_details=func_details, docker_flags=docker_flags
File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 375, in run_lambda_executor
) from error
localstack.services.awslambda.lambda_executors.InvocationException: Lambda process returned with error. Result: . Output:
What is my mistake in this case?
Thanks in advance.

Not able to connect to Elasticsearch in Docker

I am not able to connect to elasticsearch in kubernetes inside docker. My elasticsearch is accessed via kubernetes and I have an index called 'radius_ml_posts'. I am using elasticsearch's python library to connect to elasticsearch. When I run the whole process on my python IDE (Spyder), it works just fine. However, when I try to run it inside a docker container, I get connection issues. What am I missing? Below are my configs and code:
The localhost:9200:
{
"name" : "elasticsearch-dev-client-6858c5f9dc-zbz8p",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "lJJbPJpJRaC1j7k5IGhj7g",
"version" : {
"number" : "6.7.0",
"build_flavor" : "oss",
"build_type" : "docker",
"build_hash" : "8453f77",
"build_date" : "2019-03-21T15:32:29.844721Z",
"build_snapshot" : false,
"lucene_version" : "7.7.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
My python code to connect to elasticsearch host:
def get_data_es(question):
es = Elasticsearch(hosts=[{"host": "elastic", "port": 9200}], connection_class=RequestsHttpConnection, max_retries=30,
retry_on_timeout=True, request_timeout=30)
#es = Elasticsearch(hosts='http://host.docker.internal:5000', connection_class=RequestsHttpConnection, max_retries=30, timeout=30)
doc = {'author': 'gunner','text': 'event', "timestamp": datetime.now()}
es.indices.refresh(index="radius_ml_posts")
res = es.index(index="radius_ml_posts", id = 1, body = doc)
res = es.search(index="radius_ml_posts", size = 30, body={ "query": {
"query_string": {
"default_field": "search_text",
"query": question
}
}
}
)
return res
My docker-compose.yml file:
version: '2.2'
services:
elastic:
image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.7.0
container_name: elastic
environment:
- discovery.type=single-node
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9300:9300
- 9200:9200
networks:
- elastic
myimage:
image: myimage:myversion
ports:
- 5000:5000
expose:
- 5000
networks:
- elastic
volumes:
data01:
driver: local
networks:
elastic:
driver: bridge
My Dockerfile:
FROM python:3.7.4
COPY . /app
WORKDIR /app
RUN pip install --upgrade pip
RUN pip3 install -U nltk
RUN python3 -m nltk.downloader all
RUN pip --default-timeout=100 install -r requirements.txt
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["main.py"]
The docker commands I am running stepwise:
docker build -t myimage:myversion .
docker-compose up
The error I am getting:
myimage_1 | Traceback (most recent call last):
myimage_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
myimage_1 | response = self.full_dispatch_request()
myimage_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
myimage_1 | rv = self.handle_user_exception(e)
myimage_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
myimage_1 | reraise(exc_type, exc_value, tb)
myimage_1 | File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
myimage_1 | raise value
myimage_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
myimage_1 | rv = self.dispatch_request()
myimage_1 | File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
myimage_1 | return self.view_functions[rule.endpoint](**req.view_args)
myimage_1 | File "main.py", line 41, in launch_app
myimage_1 | ques = get_data_es(ques1)
myimage_1 | File "/app/Text_Cleaning.py", line 32, in get_data_es
myimage_1 | es.indices.refresh(index="radius_ml_posts")
myimage_1 | File "/usr/local/lib/python3.7/site-packages/elasticsearch/client/utils.py", line 92, in _wrapped
myimage_1 | return func(*args, params=params, headers=headers, **kwargs)
myimage_1 | File "/usr/local/lib/python3.7/site-packages/elasticsearch/client/indices.py", line 42, in refresh
myimage_1 | "POST", _make_path(index, "_refresh"), params=params, headers=headers
myimage_1 | File "/usr/local/lib/python3.7/site-packages/elasticsearch/transport.py", line 362, in perform_request
myimage_1 | timeout=timeout,
myimage_1 | File "/usr/local/lib/python3.7/site-packages/elasticsearch/connection/http_requests.py", line 157, in perform_request
myimage_1 | raise ConnectionError("N/A", str(e), e)
myimage_1 | elasticsearch.exceptions.ConnectionError: ConnectionError(HTTPConnectionPool(host='elastic', port=9200): Max retries exceeded with url: /radius_ml_posts/_refresh (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f967a9b1710>: Failed to establish a new connection: [Errno -2] Name or service not known'))) caused by: ConnectionError(HTTPConnectionPool(host='elastic', port=9200): Max retries exceeded with url: /radius_ml_posts/_refresh (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f967a9b1710>: Failed to establish a new connection: [Errno -2] Name or service not known')))
Please help in fixing the issue.
Thanks in advance.
I fixed it by using the host as:
host:"host.docker.internal"
Code change,
es = Elasticsearch(hosts=[{"host": "host.docker.internal", "port": 9200}], connection_class=RequestsHttpConnection, max_retries=30,
retry_on_timeout=True, request_timeout=30)
You can try to set the ELASTICSEARCH_NODES variable in your application environment section as and then consume the variable in your python code as http://ELASTICSEARCH_NODES:
version: '2.2'
services:
elastic:
image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.7.0
container_name: elastic
environment:
- discovery.type=single-node
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- data01:/usr/share/elasticsearch/data
ports:
- 9300:9300
- 9200:9200
networks:
- elastic
myimage:
image: myimage:myversion
depends_on:
- elastic
environment:
- ELASTICSEARCH_NODES=http://elastic:9200
ports:
- 5000:5000
expose:
- 5000
networks:
- elastic
volumes:
data01:
driver: local
networks:
elastic:
driver: bridge

Categories