LoginSignup
22
17

More than 5 years have passed since last update.

mavenでDocker Imageをbuildしよう

Posted at

MavenでDockerImageを作ろう

  • 最近書いてないのでちょっとした小ネタをPOSTしてみます

はじめに

  • SpringBootで作ったアプリケーションは可搬性がウリなわけで、Dockerとも相性が良いのは容易に想像できるわけです。
  • 私自身これまでDockerfileからDockerImageを作っていたのですがこの作業がmavenのプラグインを使うことで実現が可能です。

Dockerをインストール

  • ローカルのWindows端末で試してみました。
  • Vagrantを使ってCentOS7のイメージを引っ張ってきたことを前提にしています。
  • Dockerのインストールは簡単です。
yum -y install docker

pomに追記する

    <plugin>
      <groupId>com.spotify</groupId>
      <artifactId>docker-maven-plugin</artifactId>
      <version>0.4.10</version>
      <configuration>
        <imageName>uzresk/spring-boot-examples:v1.0</imageName>
        <baseImage>java</baseImage>
        <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
        <!-- copy the service's jar file from target into the root directory of the image -->
        <resources>
           <resource>
             <targetPath>/</targetPath>
             <directory>${project.build.directory}</directory>
             <include>${project.build.finalName}.jar</include>
           </resource>
        </resources>
      </configuration>
    </plugin>
  • imageNameにはDockerのImage名を設定します。
  • baseImageには元となるDockerImageを指定します。ここではjavaと指定されていますがタグ名が省略されていますので実際にはjava:latestが指定されています。
  • 自身が作ったimageを指定したい場合は、その値を指定しましょう。
  • 手前味噌ですがcentos:latestをベースとして、oracke-jdkがインストール済のdocker imageをあげておきました。これを利用する場合uzresk/docker-oracle-jdk:latestを指定すれば良いです。

さっそくbuildしてみましょう。

  • step1でdocker.io/uzresk/docker-oracle-jdkからダウンロード
  • step2でパッケージングされたjarファイルをコンテナにadd
  • step3でコンテナ起動時に実行するコマンドが定義されていることがわかります。
# mvn docker:build
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for jp.gr.java_conf.uzresk:sbex:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 14, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SpringBootExamples 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- docker-maven-plugin:0.4.10:build (default-cli) @ sbex ---
[INFO] Copying /root/springboot-example/target/sbex-0.0.1-SNAPSHOT.jar -> /root/springboot-example/target/docker/sbex-0.0.1-SNAPSHOT.jar
[INFO] Building image uzresk/spring-boot-examples:v1.0
Step 1 : FROM uzresk/docker-oracle-jdk:latest
Trying to pull repository docker.io/uzresk/docker-oracle-jdk ...
Pulling from uzresk/docker-oracle-jdk
607c08bc7c1f: Pull complete
99a2522ba072: Pull complete
a09b2c0a1654: Pull complete
Digest: sha256:60db98e3647576f4ea9d44921f68bfa83259a05b267e7879282dbfd10d2438e6
Status: Downloaded newer image for docker.io/uzresk/docker-oracle-jdk:latest
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
 ---> a09b2c0a1654
Step 2 : ADD /sbex-0.0.1-SNAPSHOT.jar //
 ---> 87bd34407671
Removing intermediate container 5a837bd2a6f7
Step 3 : ENTRYPOINT java -jar /sbex-0.0.1-SNAPSHOT.jar
 ---> Running in 3a95e065a972
 ---> 29b7d351ba72
Removing intermediate container 3a95e065a972
Successfully built 29b7d351ba72
[INFO] Built uzresk/spring-boot-examples:v1.0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:13 min
[INFO] Finished at: 2016-06-02T01:05:59+00:00
[INFO] Final Memory: 13M/36M
[INFO] ------------------------------------------------------------------------

動かしてみよう

  • 今回のアプリケーションはport8080で起動しているので、80でアクセスさせるにはポートフォワードの設定が必要になります。
# docker run -it -p 80:8080 --name spring-boot-examples uzresk/spring-boot-examples:v1.0

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.2.1.RELEASE)

その他

  • この他、DockerHubやPrivateリポジトリにもimageをpushすることができます。
  • mavenのタスクで完結するのでjenkinsでimageを作ってDockerHubにpushしておくといい感じですね
22
17
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
22
17