Docker Compose for AirFlow: A Step-by-Step Guide to Connecting SQL Server and MongoDB
Image by Egidus - hkhazo.biz.id

Docker Compose for AirFlow: A Step-by-Step Guide to Connecting SQL Server and MongoDB

Posted on

Are you struggling to get Docker Compose to work with AirFlow, especially when it comes to connecting your SQL Server and MongoDB databases? Don’t worry, you’re not alone! In this article, we’ll take a deep dive into the world of Docker Compose and AirFlow, providing you with a clear, step-by-step guide to help you overcome this common hurdle.

What is Docker Compose and AirFlow?

The Problem: Docker Compose for AirFlow not working with SQL Server and MongoDB

So, you’ve tried to set up Docker Compose for AirFlow, but it just won’t work with your SQL Server and MongoDB databases. You’ve scoured the internet for solutions, but to no avail. Don’t worry, we’ve got you covered. In this article, we’ll identify the common pitfalls and provide you with a foolproof guide to overcome them.

Prerequisites

  • Docker (running on your local machine)
  • Docker Compose ( installed on your system)
  • AirFlow ( installed on your system)
  • SQL Server ( running on your local machine or remotely)
  • MongoDB ( running on your local machine or remotely)

Step 1: Create a Docker Compose File


version: '3'
services:
  airflow:
    build: .
    restart: always
    ports:
      - "8080:8080"
    depends_on:
      - db
      - mongo
    environment:
      - AIRFLOW_DB_HOST=db
      - AIRFLOW_DB_PORT=5432
      - AIRFLOW_DB_USER=airflow
      - AIRFLOW_DB_PASSWORD=airflow
      - MONGO_HOST=mongo
      - MONGO_PORT=27017
  db:
    image: postgres
    environment:
      - POSTGRES_USER=airflow
      - POSTGRES_PASSWORD=airflow
      - POSTGRES_DB=airflow
  mongo:
    image: mongo
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=password

Step 2: Configure AirFlow to Connect to SQL Server


[core]
sql_alchemy_conn = postgresql://airflow:airflow@db:5432/airflow

Step 3: Configure AirFlow to Connect to MongoDB


MONGO_URI = 'mongodb://root:password@mongo:27017/'

Step 4: Create a DAG to Test the Connection


from datetime import datetime, timedelta
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from airflow.providers.microsoft.mssql.operators.mssql import MsSqlOperator
from airflow.providers.mongo.operators.mongo import MongoOperator

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime(2023, 3, 21),
    'retries': 1,
    'retry_delay': timedelta(minutes=5),
}

dag = DAG(
    'my_dag',
    default_args=default_args,
    schedule_interval=timedelta(days=1),
)

def test_sql_server():
    MsSqlOperator(
        task_id='test_sql_server',
        conn_id='mssql_default',
        sql='SELECT * FROM sys.tables'
    )

def test_mongo():
    MongoOperator(
        task_id='test_mongo',
        mongo_conn_id='mongo_default',
        mongo_collection='test_collection',
        operation='insert',
        data=[{'name': 'John', 'age': 30}]
    )

test_sql_server_task = PythonOperator(
    task_id='test_sql_server_task',
    python_callable=test_sql_server
)

test_mongo_task = PythonOperator(
    task_id='test_mongo_task',
    python_callable=test_mongo
)

dag.append(test_sql_server_task)
dag.append(test_mongo_task)

Step 5: Run Docker Compose


docker-compose up -d

Step 6: Trigger the DAG


airflow dags trigger my_dag
Common Issues Solutions
AirFlow not connecting to SQL Server Check the SQL Server credentials in the `airflow.cfg` file. Ensure that the database credentials are correct and the database is reachable.
AirFlow not connecting to MongoDB Check the MongoDB credentials in the `mongodb_default.py` file. Ensure that the database credentials are correct and the database is reachable.
Docker Compose not starting services Check the `docker-compose.yml` file for any syntax errors. Ensure that the services are defined correctly and the dependencies are met.

Conclusion

Here are 5 Questions and Answers about “Docker Compose for AirFlow (Source: SQL Server and destination: MongoDB) not working” in HTML format:

Frequently Asked Question

Get the answers to the most common questions about Docker Compose for AirFlow not working with SQL Server as the source and MongoDB as the destination.

Why is my Docker Compose for AirFlow not working with SQL Server as the source and MongoDB as the destination?

This could be due to various reasons such as incorrect configuration, incompatible versions, or missing dependencies. Make sure to check the Docker Compose file and the AirFlow configuration for any syntax errors or inconsistencies.

How do I troubleshoot the issue with my Docker Compose for AirFlow?

Start by checking the Docker Compose logs for any error messages. You can do this by running the command `docker-compose logs` in the terminal. Additionally, check the AirFlow logs for any errors or warnings. Also, make sure to check the SQL Server and MongoDB connections are working correctly.

What are the minimum requirements for SQL Server and MongoDB to work with AirFlow using Docker Compose?

For SQL Server, you need to have the ODBC driver installed and configured correctly. For MongoDB, you need to have the MongoDB Python driver (pymongo) installed. Also, make sure to have the correct versions of SQL Server and MongoDB that are compatible with your AirFlow version.

How do I configure my Docker Compose file to connect to SQL Server and MongoDB?

You need to define the SQL Server and MongoDB services in your Docker Compose file and configure the environment variables for AirFlow to connect to them. For example, you can define the SQL Server service as `sql-server: latest` and the MongoDB service as `mongo: latest`. Then, you can configure the AirFlow service to connect to them using environment variables such as `AIRFLOW_DATABASE_USERNAME` and `AIRFLOW_DATABASE_PASSWORD` for SQL Server, and `MONGODB_HOST` and `MONGODB_PORT` for MongoDB.

What are some common mistakes to avoid when using Docker Compose for AirFlow with SQL Server and MongoDB?

Some common mistakes to avoid include forgetting to expose the correct ports for SQL Server and MongoDB, not defining the correct environment variables for AirFlow, and not configuring the correct connection strings for SQL Server and MongoDB. Also, make sure to use the correct versions of SQL Server and MongoDB that are compatible with your AirFlow version.

Leave a Reply

Your email address will not be published. Required fields are marked *