Proof of Concept Tutorial: Setting up Docker, Airflow, and Python with Faker on Ubuntu
This step-by-step tutorial will guide you through setting up a data pipeline using Docker, Python's Faker library to simulate data fetching, and Apache Airflow orchestrated with docker-compose. We will use a Python virtual environment (venv) to manage our Python dependencies.
Prerequisites
An Ubuntu Linux system (18.04 LTS or later recommended)
Administrative (sudo) access to install packages
Basic familiarity with command-line operations
Table of Contents
Install Docker
Install docker-compose
Install Python 3 and Set Up a Virtual Environment
Install the Faker Library
Set Up Apache Airflow with docker-compose
Create a Sample Airflow DAG Using Faker
Run Airflow and Trigger the DAG
Conclusion
1. Install Docker
Docker allows you to run applications in containers. We'll use it to run Airflow in a containerized environment.
Steps:
Update the package index and install prerequisites:
Mounting volumes for dags, logs, plugins, and the virtual environment.
Create the necessary directories:
bash
mkdir -p dags logs plugins
Copy the virtual environment into the Airflow directory:
bash
cp -r venv airflow/
Initialize the Airflow database:
bash
6. Create a Sample Airflow DAG Using Faker
We'll create a simple DAG that uses the Faker library to generate fake data.
Steps:
Create a DAG file:
bash
nano dags/faker_dag.py
Add the following code to faker_dag.py:
python
from airflow import DAG
from airflow.operators.python_operator import PythonOperator
from datetime import datetime, timedelta
from faker import Faker
def generate_fake_data(**kwargs):
faker = Faker()
fake_data = {
'name': faker.name(),
'address': faker.address(),
'email': faker.email(),
'text': faker.text()
}
print(fake_data)
default_args = {
'owner': 'airflow',
'start_date': datetime(2023, 1, 1),
'retries': 1,
'retry_delay': timedelta(minutes=5),
}
with DAG(
'faker_dag',
default_args=default_args,
description='A simple DAG that generates fake data using Faker',
schedule_interval=timedelta(days=1),
) as dag:
generate_data = PythonOperator(
task_id='generate_fake_data',
python_callable=generate_fake_data,
provide_context=True,
)
generate_data
Save and exit the editor (Ctrl+O, Enter, Ctrl+X in nano).
7. Run Airflow and Trigger the DAG
We'll start the Airflow webserver and scheduler, then trigger our DAG.
Steps:
Start Airflow services using docker-compose:
bash
docker-compose up -d
This command starts the Airflow webserver in detached mode.
Access the Airflow web UI:
Open your web browser and navigate to http://localhost:8080. Log in with the credentials you set earlier (admin / admin).
Enable the DAG:
In the Airflow UI, go to the DAGs tab.
Find faker_dag in the list.
Toggle it ON to enable scheduling.
Trigger the DAG manually (optional):
Click on faker_dag.
Click on the Trigger Dag button.
Check the logs:
In the Airflow UI, go to DAGs tab.
Click on the faker_dag.
In the Graph View, click on the generate_fake_data task.
Select View Log to see the output.
You should see the fake data printed in the logs, similar to:
code
{'name': 'John Doe', 'address': '123 Main St\nAnytown, USA', 'email': 'john.doe@example.com', 'text': 'Lorem ipsum dolor sit amet, ...'}
(Optional) Stop Airflow services:
bash
docker-compose down
8. Conclusion
You've successfully set up a proof of concept for using Docker, Python with Faker, and Airflow orchestrated with docker-compose on Ubuntu. This setup allows you to simulate data fetching processes and orchestrate workflows using Airflow in a containerized environment.
Next Steps:
Explore more Airflow operators and functionalities.
Integrate with real data sources or sinks (e.g., databases, APIs).
Extend the DAGs to include more complex workflows.