1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Sparkのカスタムイメージ向けのmvnコンテナを使った依存JARの取得方法

Last updated at Posted at 2025-12-01

この記事は Distributed Computing Advent Calendar 2025さくらインターネット Advent Calendar 2025の2日目の記事です。

Sparkのカスタムイメージを作成する際にMavenリポジトリから依存するJARを取得したいケースがたまにあります。

1つにまとまっていれば簡単ですが、依存の影響で複数必要だった場合めんどくさいです。

そこでmvnコンテナ使って取ってくるTips。

例えば、Hadoop-AWSモジュールの依存JARを例にします。

maven-settings.xml ※PROXYが必要な人向け

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
	<proxies>
		<proxy>
			<id>http_proxy</id>
			<active>true</active>
			<protocol>http</protocol>
			<host>proxy.example.com</host>
			<port>8080</port>
			<nonProxyHosts>localhost|127.0.0.1|0.0.0.0</nonProxyHosts>
		</proxy>
	</proxies>
</settings>

pom-spark-aws.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>hadoop-aws-deps</artifactId>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <!-- Hadoop-AWS モジュール (3.3.4) を追加 -->
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-aws</artifactId>
      <version>3.3.4</version>
    </dependency>
  </dependencies>
</project>

あとは、上記をバインドマウントして実行すると ./jars 以下に依存がDLされます。

$ docker run --rm -ti \
  -w /build \
  -v ./jars:/build/dependencies \
  -v ./maven-settings.xml:/build/maven-settings.xml \
  -v ./pom-spark-aws.xml:/build/pom.xml \
  maven:3-eclipse-temurin-11 bash -c "mvn -B -f /build/pom.xml -s /build/maven-settings.xml dependency:copy-dependencies -DoutputDirectory=/build/dependencies"

使い回さないなら、マルチステージビルド使って、以下のようにまとめて対応する手もあります。

Dockerfileを以下のように用意しておいて、、

# syntax=docker/dockerfile:1
ARG base_image=unknown

# Downloader ########################################################
FROM maven:3-eclipse-temurin-17 AS download

ARG hadoop_aws_version=unknown
ENV HADOOP_AWS_VERSION=${hadoop_aws_version}

WORKDIR /download


ENV FILE_PATH=/download/pom.xml

SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN tee ${FILE_PATH} <<EOF
<!-- pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>local.dummy</groupId>
  <artifactId>hadoop-aws-fetcher</artifactId>
  <version>1.0.0</version>

  <dependencies>
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-aws</artifactId>
      <version>${HADOOP_AWS_VERSION}</version>
    </dependency>
  </dependencies>
</project>
EOF

RUN --mount=type=secret,id=mvn,target=/root/.m2/settings.xml \
  mvn --file /download/pom.xml dependency:copy-dependencies \
  -DoutputDirectory=/download

# Main       ########################################################
FROM ${base_image} AS main

(略)

USER spark

## COPY Hadoop-AWSモジュール
COPY --from=download --chown=spark:spark --chmod=755 /download/*.jar ${SPARK_HOME}/jars/

(略)

m2_settings.xml ※PROXY必要な人向け

<settings>
	<proxies>
		<proxy>
			<id>ma-https-proxy</id>
			<active>true</active>
			<protocol>http</protocol>
			<host>proxy.example.com</host>
			<port>8080</port>
			<nonProxyHosts>localhost|127.0.0.1|0.0.0.0</nonProxyHosts>
		</proxy>
	</proxies>
</settings>

んで、以下のように実行

docker buildx build --platform linux/amd64 \
    --secret id=mvn,src=./m2_settings.xml \
	--no-cache \
	--progress=plain \
   (略)
	--build-arg hadoop_aws_version=$(HADOOP_AWS_VERSION) \
   (略)
	-f ./docker/Dockerfile ./docker/ ;

以上、 Distributed Computing Advent Calendar 2025さくらインターネット Advent Calendar 2025 の2日目の記事でした。

P.S. Distributed Computing Advent Calendar 2025 の枠が空いているので、良ければ参加ください!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?