Spark Docker Java gateway process exited before sending its port number

Viewed 254

I am fairly new to docker and am trying to get a docker-compose file running with both airflow and pyspark. Below is what I have so far:

version: '3.7'
services:
    master:
      image: gettyimages/spark
      command: bin/spark-class org.apache.spark.deploy.master.Master -h master
      hostname: master
      environment:
        MASTER: spark://master:7077
        SPARK_CONF_DIR: /conf
        SPARK_PUBLIC_DNS: localhost
      expose:
        - 7001
        - 7002
        - 7003
        - 7004
        - 7005
        - 7077
        - 6066
      ports:
        - 4040:4040
        - 6066:6066
        - 7077:7077
        - 8080:8080
      volumes:
        - ./conf/master:/conf
        - ./data:/tmp/data

    worker:
      image: gettyimages/spark
      command: bin/spark-class org.apache.spark.deploy.worker.Worker spark://master:7077
      hostname: worker
      environment:
        SPARK_CONF_DIR: /conf
        SPARK_WORKER_CORES: 2
        SPARK_WORKER_MEMORY: 1g
        SPARK_WORKER_PORT: 8881
        SPARK_WORKER_WEBUI_PORT: 8081
        SPARK_PUBLIC_DNS: localhost
      links:
        - master
      expose:
        - 7012
        - 7013
        - 7014
        - 7015
        - 8881
      ports:
        - 8081:8081
      volumes:
        - ./conf/worker:/conf
        - ./data:/tmp/data
    postgres:
        image: postgres:9.6
        environment:
            - POSTGRES_USER=airflow
            - POSTGRES_PASSWORD=airflow
            - POSTGRES_DB=airflow
        logging:
            options:
                max-size: 10m
                max-file: "3"

    webserver:
        image: puckel/docker-airflow:1.10.9
        restart: always
        depends_on:
            - postgres
        environment:
            - LOAD_EX=y
            - EXECUTOR=Local
        logging:
            options:
                max-size: 10m
                max-file: "3"
        volumes:
            - ./dags:/usr/local/airflow/dags
            # Add this to have third party packages
            - ./requirements.txt:/requirements.txt
            # - ./plugins:/usr/local/airflow/plugins
        ports:
            - "8082:8080"
        command: webserver
        healthcheck:
            test: ["CMD-SHELL", "[ -f /usr/local/airflow/airflow-webserver.pid ]"]
            interval: 30s
            timeout: 30s
            retries: 3

And I am trying to run the following simple DAG just to confirm pyspark is operating correctly:

import pyspark
from airflow.models import DAG
from airflow.utils.dates import days_ago, timedelta
from airflow.operators.python_operator import PythonOperator
from airflow.contrib.operators.spark_submit_operator import SparkSubmitOperator

import random

args = {
    "owner": "ian",
    "start_date": days_ago(1)
}

dag = DAG(dag_id="pysparkTest", default_args=args, schedule_interval=None)


def run_this_func(**context):
    sc = pyspark.SparkContext()
    print(sc)

with dag:
    run_this_task = PythonOperator(
        task_id='run_this',
        python_callable=run_this_func,
        provide_context=True,
        retries=10,
        retry_delay=timedelta(seconds=1)
    )

When I do this, it fails with the error Java gateway process exited before sending its port number. I have found several posts that say to run the command export PYSPARK_SUBMIT_ARGS="--master local[2] pyspark-shell" which I have tried to run as a command like so:

version: '3.7'
services:
    master:
      image: gettyimages/spark
      command: >
        sh -c "bin/spark-class org.apache.spark.deploy.master.Master -h master
        && export PYSPARK_SUBMIT_ARGS="--master local[2] pyspark-shell""
      hostname: master
...

But I still get the same error. Any ideas what I am doing wrong?

1 Answers

I don't think you need to modify the master's command. Leave it as they did here.

In addition, how do you expect the python code which runs on a different container - to connect the master container. I think you should add it to the spark-context, something like:

def run_this_func(**context):
    sc = pyspark.SparkContext("spark://master:7077")
    print(sc)
Related