Docker Setup

Getting Started

This Getting Started section guides you through the local setup (on one machine, but in separate containers) of a Flink cluster using Docker containers.

Introduction

Docker is a popular container runtime. There are official Docker images for Apache Flink available on Docker Hub. You can use the Docker images to deploy a Session or Application cluster on Docker. This page focuses on the setup of Flink on Docker and Docker Compose.

Deployment into managed containerized environments, such as standalone Kubernetes or native Kubernetes, are described on separate pages.

Starting a Session Cluster on Docker

A Flink Session cluster can be used to run multiple jobs. Each job needs to be submitted to the cluster after the cluster has been deployed. To deploy a Flink Session cluster with Docker, you need to start a JobManager container. To enable communication between the containers, we first set a required Flink configuration property and create a network:

  1. $ FLINK_PROPERTIES="jobmanager.rpc.address: jobmanager"
  2. $ docker network create flink-network

Then we launch the JobManager:

  1. $ docker run \
  2. --rm \
  3. --name=jobmanager \
  4. --network flink-network \
  5. --publish 8081:8081 \
  6. --env FLINK_PROPERTIES="${FLINK_PROPERTIES}" \
  7. flink:1.15.0-scala_2.12 jobmanager

and one or more TaskManager containers:

  1. $ docker run \
  2. --rm \
  3. --name=taskmanager \
  4. --network flink-network \
  5. --env FLINK_PROPERTIES="${FLINK_PROPERTIES}" \
  6. flink:1.15.0-scala_2.12 taskmanager

The web interface is now available at localhost:8081.

Submission of a job is now possible like this (assuming you have a local distribution of Flink available):

  1. $ ./bin/flink run ./examples/streaming/TopSpeedWindowing.jar

To shut down the cluster, either terminate (e.g. with CTRL-C) the JobManager and TaskManager processes, or use docker ps to identify and docker stop to terminate the containers.

Deployment Modes

The Flink image contains a regular Flink distribution with its default configuration and a standard entry point script. You can run its entry point in the following modes:

This allows you to deploy a standalone cluster (Session or Application Mode) in any containerised environment, for example:

Note The native Kubernetes also runs the same image by default and deploys TaskManagers on demand so that you do not have to do it manually.

The next chapters describe how to start a single Flink Docker container for various purposes.

Once you’ve started Flink on Docker, you can access the Flink Web UI on localhost:8081 or submit jobs like this ./bin/flink run ./examples/streaming/TopSpeedWindowing.jar.

We recommend using Docker Compose for deploying Flink in Session Mode to ease system configuration.

Application Mode

For high-level intuition behind the application mode, please refer to the deployment mode overview.

A Flink Application cluster is a dedicated cluster which runs a single job. In this case, you deploy the cluster with the job as one step, thus, there is no extra job submission needed.

The job artifacts are included into the class path of Flink’s JVM process within the container and consist of:

  • your job jar, which you would normally submit to a Session cluster and
  • all other necessary dependencies or resources, not included into Flink.

To deploy a cluster for a single job with Docker, you need to

  • make job artifacts available locally in all containers under /opt/flink/usrlib,
  • start a JobManager container in the Application cluster mode
  • start the required number of TaskManager containers.

To make the job artifacts available locally in the container, you can

  • either mount a volume (or multiple volumes) with the artifacts to /opt/flink/usrlib when you start the JobManager and TaskManagers:

    1. $ FLINK_PROPERTIES="jobmanager.rpc.address: jobmanager"
    2. $ docker network create flink-network
    3. $ docker run \
    4. --mount type=bind,src=/host/path/to/job/artifacts1,target=/opt/flink/usrlib/artifacts1 \
    5. --mount type=bind,src=/host/path/to/job/artifacts2,target=/opt/flink/usrlib/artifacts2 \
    6. --rm \
    7. --env FLINK_PROPERTIES="${FLINK_PROPERTIES}" \
    8. --name=jobmanager \
    9. --network flink-network \
    10. flink:1.15.0-scala_2.12 standalone-job \
    11. --job-classname com.job.ClassName \
    12. [--job-id <job id>] \
    13. [--fromSavepoint /path/to/savepoint [--allowNonRestoredState]] \
    14. [job arguments]
    15. $ docker run \
    16. --mount type=bind,src=/host/path/to/job/artifacts1,target=/opt/flink/usrlib/artifacts1 \
    17. --mount type=bind,src=/host/path/to/job/artifacts2,target=/opt/flink/usrlib/artifacts2 \
    18. --env FLINK_PROPERTIES="${FLINK_PROPERTIES}" \
    19. flink:1.15.0-scala_2.12 taskmanager
  • or extend the Flink image by writing a custom Dockerfile, build it and use it for starting the JobManager and TaskManagers:

    1. FROM flink
    2. ADD /host/path/to/job/artifacts/1 /opt/flink/usrlib/artifacts/1
    3. ADD /host/path/to/job/artifacts/2 /opt/flink/usrlib/artifacts/2
    1. $ docker build --tag flink_with_job_artifacts .
    2. $ docker run \
    3. flink_with_job_artifacts standalone-job \
    4. --job-classname com.job.ClassName \
    5. [--job-id <job id>] \
    6. [--fromSavepoint /path/to/savepoint [--allowNonRestoredState]] \
    7. [job arguments]
    8. $ docker run flink_with_job_artifacts taskmanager

The standalone-job argument starts a JobManager container in the Application Mode.

JobManager additional command line arguments

You can provide the following additional command line arguments to the cluster entrypoint:

  • --job-classname <job class name>: Class name of the job to run.

    By default, Flink scans its class path for a JAR with a Main-Class or program-class manifest entry and chooses it as the job class. Use this command line argument to manually set the job class. This argument is required in case that no or more than one JAR with such a manifest entry is available on the class path.

  • --job-id <job id> (optional): Manually set a Flink job ID for the job (default: 00000000000000000000000000000000)

  • --fromSavepoint /path/to/savepoint (optional): Restore from a savepoint

    In order to resume from a savepoint, you also need to pass the savepoint path. Note that /path/to/savepoint needs to be accessible in all Docker containers of the cluster (e.g., storing it on a DFS or from the mounted volume or adding it to the image).

  • --allowNonRestoredState (optional): Skip broken savepoint state

    Additionally you can specify this argument to allow that savepoint state is skipped which cannot be restored.

If the main function of the user job main class accepts arguments, you can also pass them at the end of the docker run command.

Session Mode

For high-level intuition behind the session mode, please refer to the deployment mode overview.

Local deployment in the Session Mode has already been described in the Getting Started section above.

Back to top

Image Hosting

There are two distribution channels for the Flink Docker images:

  1. Official Flink images on Docker Hub (reviewed and build by Docker)
  2. Flink images on Docker Hub apache/flink (managed by the Flink developers)

We recommend using the official images on Docker Hub, as they are reviewed by Docker. The images on apache/flink are provided in case of delays in the review process by Docker.

Launching an image named flink:latest will pull the latest image from Docker Hub. In order to use the images hosted in apache/flink, replace flink by apache/flink. Any of the image tags (starting from Flink 1.11.3) are available on apache/flink as well.

Image Tags

The Flink Docker repository is hosted on Docker Hub and serves images of Flink version 1.2.1 and later. The source for these images can be found in the Apache flink-docker repository.

Images for each supported combination of Flink and Scala versions are available, and tag aliases are provided for convenience.

For example, you can use the following aliases:

  • flink:latestflink:<latest-flink>-scala_<latest-scala>
  • flink:1.11flink:1.11.<latest-flink-1.11>-scala_2.12

Note It is recommended to always use an explicit version tag of the docker image that specifies both the needed Flink and Scala versions (for example flink:1.11-scala_2.12). This will avoid some class conflicts that can occur if the Flink and/or Scala versions used in the application are different from the versions provided by the docker image.

Note Prior to Flink 1.5 version, Hadoop dependencies were always bundled with Flink. You can see that certain tags include the version of Hadoop, e.g. (e.g. -hadoop28). Beginning with Flink 1.5, image tags that omit the Hadoop version correspond to Hadoop-free releases of Flink that do not include a bundled Hadoop distribution.

Docker Compose is a way to run a group of Docker containers locally. The next sections show examples of configuration files to run Flink.

General

  • Create the docker-compose.yaml file. Please check the examples in the sections below:

  • Launch a cluster in the foreground (use -d for background)

    1. $ docker-compose up
  • Scale the cluster up or down to N TaskManagers

    1. $ docker-compose scale taskmanager=<N>
  • Access the JobManager container

    1. $ docker exec -it $(docker ps --filter name=jobmanager --format={{.ID}}) /bin/sh
  • Kill the cluster

    1. $ docker-compose down
  • Access Web UI

    When the cluster is running, you can visit the web UI at http://localhost:8081.

Application Mode

In application mode you start a Flink cluster that is dedicated to run only the Flink Jobs which have been bundled with the images. Hence, you need to build a dedicated Flink Image per application. Please check here for the details. See also how to specify the JobManager arguments in the command for the jobmanager service.

docker-compose.yml for Application Mode.

  1. version: "2.2"
  2. services:
  3. jobmanager:
  4. image: flink:1.15.0-scala_2.12
  5. ports:
  6. - "8081:8081"
  7. command: standalone-job --job-classname com.job.ClassName [--job-id <job id>] [--fromSavepoint /path/to/savepoint [--allowNonRestoredState]] [job arguments]
  8. volumes:
  9. - /host/path/to/job/artifacts:/opt/flink/usrlib
  10. environment:
  11. - |
  12. FLINK_PROPERTIES=
  13. jobmanager.rpc.address: jobmanager
  14. parallelism.default: 2
  15. taskmanager:
  16. image: flink:1.15.0-scala_2.12
  17. depends_on:
  18. - jobmanager
  19. command: taskmanager
  20. scale: 1
  21. volumes:
  22. - /host/path/to/job/artifacts:/opt/flink/usrlib
  23. environment:
  24. - |
  25. FLINK_PROPERTIES=
  26. jobmanager.rpc.address: jobmanager
  27. taskmanager.numberOfTaskSlots: 2
  28. parallelism.default: 2

Session Mode

In Session Mode you use docker-compose to spin up a long-running Flink Cluster to which you can then submit Jobs.

docker-compose.yml for Session Mode:

  1. version: "2.2"
  2. services:
  3. jobmanager:
  4. image: flink:1.15.0-scala_2.12
  5. ports:
  6. - "8081:8081"
  7. command: jobmanager
  8. environment:
  9. - |
  10. FLINK_PROPERTIES=
  11. jobmanager.rpc.address: jobmanager
  12. taskmanager:
  13. image: flink:1.15.0-scala_2.12
  14. depends_on:
  15. - jobmanager
  16. command: taskmanager
  17. scale: 1
  18. environment:
  19. - |
  20. FLINK_PROPERTIES=
  21. jobmanager.rpc.address: jobmanager
  22. taskmanager.numberOfTaskSlots: 2

In this example, you spin up a long-running session cluster and a Flink SQL CLI which uses this clusters to submit jobs to.

docker-compose.yml for Flink SQL Client with Session Cluster:

  1. version: "2.2"
  2. services:
  3. jobmanager:
  4. image: flink:1.15.0-scala_2.12
  5. ports:
  6. - "8081:8081"
  7. command: jobmanager
  8. environment:
  9. - |
  10. FLINK_PROPERTIES=
  11. jobmanager.rpc.address: jobmanager
  12. taskmanager:
  13. image: flink:1.15.0-scala_2.12
  14. depends_on:
  15. - jobmanager
  16. command: taskmanager
  17. scale: 1
  18. environment:
  19. - |
  20. FLINK_PROPERTIES=
  21. jobmanager.rpc.address: jobmanager
  22. taskmanager.numberOfTaskSlots: 2
  23. sql-client:
  24. image: flink:1.15.0-scala_2.12
  25. command: bin/sql-client.sh
  26. depends_on:
  27. - jobmanager
  28. environment:
  29. - |
  30. FLINK_PROPERTIES=
  31. jobmanager.rpc.address: jobmanager
  • In order to start the SQL Client run

    1. docker-compose run sql-client

    You can then start creating tables and queries those.

  • Note, that all required dependencies (e.g. for connectors) need to be available in the cluster as well as the client. For example, if you would like to use the Kafka Connector create a custom image with the following Dockerfile

    1. FROM flink:1.15.0-scala_2.12
    2. RUN wget -P /opt/flink/lib https://repo.maven.apache.org/maven2/org/apache/flink/flink-sql-connector-kafka_2.12/1.15.0/flink-sql-connector-kafka_scala_2.12-1.15.0.jar

    and reference it (e.g via the build) command in the Dockerfile. and reference it (e.g via the build) command in the Dockerfile. SQL Commands like ADD JAR will not work for JARs located on the host machine as they only work with the local filesystem, which in this case is Docker’s overlay filesystem.

To build a custom image which has Python and PyFlink prepared, you can refer to the following Dockerfile:

  1. FROM flink:1.15.0
  2. # install python3: it has updated Python to 3.9 in Debian 11 and so install Python 3.7 from source
  3. # it currently only supports Python 3.6, 3.7 and 3.8 in PyFlink officially.
  4. RUN apt-get update -y && \
  5. apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev libffi-dev && \
  6. wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz && \
  7. tar -xvf Python-3.7.9.tgz && \
  8. cd Python-3.7.9 && \
  9. ./configure --without-tests --enable-shared && \
  10. make -j6 && \
  11. make install && \
  12. ldconfig /usr/local/lib && \
  13. cd .. && rm -f Python-3.7.9.tgz && rm -rf Python-3.7.9 && \
  14. ln -s /usr/local/bin/python3 /usr/local/bin/python && \
  15. apt-get clean && \
  16. rm -rf /var/lib/apt/lists/*
  17. # install PyFlink
  18. RUN pip3 install apache-flink==1.15.0

Note For Debian 10 and below, Python 3 could also be installed alternatively as following:

  1. RUN apt-get update -y && \
  2. apt-get install -y python3.7 python3-pip python3.7-dev && rm -rf /var/lib/apt/lists/*
  3. RUN ln -s /usr/bin/python3 /usr/bin/python

Build the image named as pyflink:latest:

  1. $ docker build --tag pyflink:latest .

Via Environment Variables

When you run Flink image, you can also change its configuration options by setting the environment variable FLINK_PROPERTIES:

  1. $ FLINK_PROPERTIES="jobmanager.rpc.address: host
  2. taskmanager.numberOfTaskSlots: 3
  3. blob.server.port: 6124
  4. "
  5. $ docker run --env FLINK_PROPERTIES=${FLINK_PROPERTIES} flink:1.15.0-scala_2.12 <jobmanager|standalone-job|taskmanager>

The jobmanager.rpc.address option must be configured, others are optional to set.

The environment variable FLINK_PROPERTIES should contain a list of Flink cluster configuration options separated by new line, the same way as in the flink-conf.yaml. FLINK_PROPERTIES takes precedence over configurations in flink-conf.yaml.

The configuration files (flink-conf.yaml, logging, hosts etc) are located in the /opt/flink/conf directory in the Flink image. To provide a custom location for the Flink configuration files, you can

  • either mount a volume with the custom configuration files to this path /opt/flink/conf when you run the Flink image:

    1. $ docker run \
    2. --mount type=bind,src=/host/path/to/custom/conf,target=/opt/flink/conf \
    3. flink:1.15.0-scala_2.12 <jobmanager|standalone-job|taskmanager>
  • or add them to your custom Flink image, build and run it:

    1. FROM flink
    2. ADD /host/path/to/flink-conf.yaml /opt/flink/conf/flink-conf.yaml
    3. ADD /host/path/to/log4j.properties /opt/flink/conf/log4j.properties

The mounted volume must contain all necessary configuration files. The flink-conf.yaml file must have write permission so that the Docker entry point script can modify it in certain cases.

Using Filesystem Plugins

As described in the plugins documentation page: In order to use plugins they must be copied to the correct location in the Flink installation in the Docker container for them to work.

If you want to enable plugins provided with Flink (in the opt/ directory of the Flink distribution), you can pass the environment variable ENABLE_BUILT_IN_PLUGINS when you run the Flink image. The ENABLE_BUILT_IN_PLUGINS should contain a list of plugin jar file names separated by ;. A valid plugin name is for example flink-s3-fs-hadoop-1.15.0.jar

  1. $ docker run \
  2. --env ENABLE_BUILT_IN_PLUGINS=flink-plugin1.jar;flink-plugin2.jar \
  3. flink:1.15.0-scala_2.12 <jobmanager|standalone-job|taskmanager>

There are also more advanced ways for customizing the Flink image.

Switching the Memory Allocator

Flink introduced jemalloc as default memory allocator to resolve memory fragmentation problem (please refer to FLINK-19125).

You could switch back to use glibc as the memory allocator to restore the old behavior or if any unexpected memory consumption or problem observed (and please report the issue via JIRA or mailing list if you found any), by setting environment variable DISABLE_JEMALLOC as true:

  1. $ docker run \
  2. --env DISABLE_JEMALLOC=true \
  3. flink:1.15.0-scala_2.12 <jobmanager|standalone-job|taskmanager>

For users that are still using glibc memory allocator, the glibc bug can easily be reproduced, especially while savepoints or full checkpoints with RocksDBStateBackend are created. Setting the environment variable MALLOC_ARENA_MAX can avoid unlimited memory growth:

  1. $ docker run \
  2. --env MALLOC_ARENA_MAX=1 \
  3. flink:1.15.0-scala_2.12 <jobmanager|standalone-job|taskmanager>

Further Customization

There are several ways in which you can further customize the Flink image:

  • install custom software (e.g. python)
  • enable (symlink) optional libraries or plugins from /opt/flink/opt into /opt/flink/lib or /opt/flink/plugins
  • add other libraries to /opt/flink/lib (e.g. Hadoop)
  • add other plugins to /opt/flink/plugins

You can customize the Flink image in several ways:

  • override the container entry point with a custom script where you can run any bootstrap actions. At the end you can call the standard /docker-entrypoint.sh script of the Flink image with the same arguments as described in supported deployment modes.

    The following example creates a custom entry point script which enables more libraries and plugins. The custom script, custom library and plugin are provided from a mounted volume. Then it runs the standard entry point script of the Flink image:

    1. # create custom_lib.jar
    2. # create custom_plugin.jar
    3. $ echo "
    4. # enable an optional library
    5. ln -fs /opt/flink/opt/flink-queryable-state-runtime-*.jar /opt/flink/lib/
    6. # enable a custom library
    7. ln -fs /mnt/custom_lib.jar /opt/flink/lib/
    8. mkdir -p /opt/flink/plugins/flink-s3-fs-hadoop
    9. # enable an optional plugin
    10. ln -fs /opt/flink/opt/flink-s3-fs-hadoop-*.jar /opt/flink/plugins/flink-s3-fs-hadoop/
    11. mkdir -p /opt/flink/plugins/custom_plugin
    12. # enable a custom plugin
    13. ln -fs /mnt/custom_plugin.jar /opt/flink/plugins/custom_plugin/
    14. /docker-entrypoint.sh <jobmanager|standalone-job|taskmanager>
    15. " > custom_entry_point_script.sh
    16. $ chmod 755 custom_entry_point_script.sh
    17. $ docker run \
    18. --mount type=bind,src=$(pwd),target=/mnt
    19. flink:1.15.0-scala_2.12 /mnt/custom_entry_point_script.sh
  • extend the Flink image by writing a custom Dockerfile and build a custom image:

    1. FROM flink
    2. RUN set -ex; apt-get update; apt-get -y install python
    3. ADD /host/path/to/flink-conf.yaml /container/local/path/to/custom/conf/flink-conf.yaml
    4. ADD /host/path/to/log4j.properties /container/local/path/to/custom/conf/log4j.properties
    5. RUN ln -fs /opt/flink/opt/flink-queryable-state-runtime-*.jar /opt/flink/lib/.
    6. RUN mkdir -p /opt/flink/plugins/flink-s3-fs-hadoop
    7. RUN ln -fs /opt/flink/opt/flink-s3-fs-hadoop-*.jar /opt/flink/plugins/flink-s3-fs-hadoop/.
    8. ENV VAR_NAME value

Commands for building:

  1. ```sh
  2. $ docker build --tag custom_flink_image .
  3. # optional push to your docker image registry if you have it,
  4. # e.g. to distribute the custom image to your cluster
  5. $ docker push custom_flink_image
  6. ```

Back to top