# How to Run a Spark Job From an Airflow DAG (Free, in Docker)

> Run a PySpark job from an Airflow DAG on your laptop: put it in code/, call it from a task, and watch the Airflow UI and Spark UI. Free, in Docker.

Page: https://datautility.in/blog/run-spark-job-from-airflow-dag-docker
This is the Markdown copy of the page. The tool itself runs in the browser: your files are processed on your own device and are not uploaded.

Published 2026-11-29 · Analyze & protect · airflow spark, run spark job from airflow, airflow pyspark example, airflow spark docker, airflow orchestrate spark, airflow bashoperator spark, spark airflow tutorial

Short answer: keep the Spark code in its own Python file, then call that file from an Airflow task. Airflow decides when and in what order things run, and Spark does the data work. In the Airflow Lab both are installed in one Docker container, so you can run the DAG and open the Airflow UI and the Spark UI on your laptop.

Open the tool: [Airflow Lab](https://datautility.in/airflow-playground)

## Airflow orchestrates, Spark computes

A common beginner mistake is writing the data work inside the DAG file. Keep them apart: the DAG only says "run the cleaning step, then the Spark job, then check the result". The Spark job is an ordinary PySpark file that you can also run on its own.

## The two folders

| Folder | What goes there |
|---|---|
| `dags/` | Airflow DAG files |
| `code/` | Your Python and PySpark files. It is on the Python path, so a task can also import functions from it |

## Step 1: a Spark job in code/

```python
from pyspark.sql import SparkSession

spark = SparkSession.builder.appName("orders").getOrCreate()
df = spark.createDataFrame([("north", 120), ("south", 80), ("north", 45)], ["region", "amount"])
df.groupBy("region").sum("amount").show()
spark.stop()
```

Run it on its own first: tick **Keep Spark UI open**, press **Run file**, then press **Spark UI**. You see the job, its stages and the shuffle that `groupBy` causes. If you want to learn to read that page, see [how to read the Spark UI](https://datautility.in/blog/learn-pyspark-and-the-spark-ui-free).

## Step 2: call it from a DAG

```python
import pendulum
from airflow.sdk import DAG
from airflow.providers.standard.operators.bash import BashOperator

with DAG(dag_id="spark_in_dag", start_date=pendulum.datetime(2026, 1, 1, tz="UTC"), schedule=None, catchup=False) as dag:
    clean = BashOperator(task_id="clean", bash_command="echo cleaning the input")
    spark_job = BashOperator(task_id="spark_job", bash_command="python /workspace/code/job.py")
    show = BashOperator(task_id="show_result", bash_command="ls -l /workspace/code")

    clean >> spark_job >> show
```

The `spark_job` task runs the file with `python`. The `clean` task runs first and `show_result` waits until the Spark job has finished successfully.

## Step 3: trigger it and watch

1. Press **Trigger** so the scheduler runs the DAG like a real run.

2. Open the DAGs tab, or the Airflow UI on `localhost:8080`, and watch the three tasks go green one after another.

3. Click `spark_job` and read its log: the Spark output is there.

4. If the Spark job fails, the DAG stops at that task and `show_result` is not run. That is the behaviour you want from an orchestrator.

## Next steps

- Add a [retry](https://datautility.in/blog/airflow-schedule-catchup-retries-explained) to the Spark task so one failed attempt does not fail the whole DAG.

- Use [dynamic task mapping](https://datautility.in/blog/airflow-branching-dynamic-task-mapping-task-groups) to run the same job once for each file.

- Lesson 11, Call your own Python, shows a task that imports a function from `code/`.

## A note on production

This runs Spark inside the Airflow container, which is perfect for learning. In production, Airflow usually sends the job to a Spark cluster (for example through a provider operator) instead of running Spark itself. The idea of the DAG is the same.

## How to use the Airflow Lab: step by step

1. Start the Airflow Lab
2. Put a PySpark file in code/ and run it with Keep Spark UI open
3. Write a DAG that calls it
4. Trigger the DAG and read the task log

## When to use the Airflow Lab

- Learning how Airflow and Spark fit together
- Practising the data engineering interview question: orchestrate a Spark job
- Testing a Spark job and its DAG on sample data
- Teaching a team the difference between orchestration and compute

## Example

Input:

```
a DAG that cleans data, runs a Spark job and lists the result
```

Output:

```
Task states in the lab, the DAG in the Airflow UI, the job in the Spark UI
```

## Frequently asked questions

### How do I run a Spark job from an Airflow DAG?
Put the job in a Python file and call it from a task, for example BashOperator with python /workspace/code/job.py in the Airflow Lab. Airflow runs the task and shows its log, and the Spark UI shows the job.

### Do I need a Spark cluster to practise this?
No. The Airflow Lab has PySpark in the same container, so the whole pipeline runs on your laptop.

### Should the Spark code live inside the DAG file?
No. Keep it in its own file so you can test it alone, and let the DAG only decide the order and the retries.

### Can I see the Spark UI for a job started by Airflow?
Yes. Tick Keep Spark UI open in the lab before you run, then press Spark UI. The Spark UI is available while the job is running.

Tip: Run the Spark file on its own before you put it in a DAG. When the DAG fails you then know the problem is in the DAG, not in Spark.

## Related tool pages

- [Learn Apache Airflow Free: Hands-On Airflow Lab](https://datautility.in/learn-airflow-free): Practise Apache Airflow hands-on, free: DAGs, dependencies, TaskFlow, schedules, retries, branching and mapping, with 11 lessons and the real Airflow UI on your laptop.

## Also try

- [A free Airflow playground](https://datautility.in/blog/free-airflow-playground-learn-apache-airflow-docker)
- [Learn PySpark and the Spark UI](https://datautility.in/blog/learn-pyspark-and-the-spark-ui-free)
- [Schedules, catchup and retries](https://datautility.in/blog/airflow-schedule-catchup-retries-explained)

## More guides

- [Data Profiler: Check Data Quality Before You Use It](https://datautility.in/blog/data-profiler-check-data-quality)
- [Compare Two CSV or Excel Files: Find Added, Removed and Changed Rows](https://datautility.in/blog/compare-two-csv-files-find-differences)
- [Data Masking: Hide Emails, Phones and Names Before Sharing](https://datautility.in/blog/data-masking-hide-emails-phones-pii)
