LoginSignup
23
26

More than 5 years have passed since last update.

mecab Web API な Docker コンテナを作る

Posted at

概要

JSON でリクエストすると mecab パースして結果を JSON で返してくれる、シンプルな WEB API サーバーを Docker コンテナとして構築します。

製作手順としての Dockerfile をご参照ください。

関連記事

製作の経緯

mecab を使うのに mecab-ruby バインドを利用していたのですが、この ruby がビルドできる環境がイマイチシビアなようで動く版動かない版とかがあって環境とバージョンを探すのに苦労していました。
また、mecab 自体 cygwin でビルドするのに骨が折れたりとか、Linux 以外の環境で扱うのにずいぶん苦労した記憶があります。

こういったバージョン合わせの苦労から逃れるために「バインドは libmecab を直接操作できる go にする」とか「HTTP&JSOC経由でどの環境からも利用できるサーバー型インタフェースにする」といった事をめざすことにしました。

そして出来上がった環境を使い続けたり、メンテナンスを簡易にするため Docker コンテナにするというのが、今回のチャレンジになります。

使い方

ベースにする Docker image

Debian:jessie を使います、
docker pull debian
なりでイメージを pull しておいてください。

コンテナの作成

下記の Dockerfile をファイルとして作成しておいて、それが存在するディレクトリで
docker build -t [タグ名] .
としてコンテナをビルドしてください。

サーバー(コンテナ)の起動

コンテナの webmecab を起動すると、サーバーとして動作します。
docker run -d -p 80:80 -t [タグ名] webmecab -p 80
ポート指定はお好きなように。

Dockerfile

Dockerfile
FROM debian:jessie
MAINTAINER rerofumi

RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get -y install wget g++ make golang mercurial

RUN cd /root; wget https://mecab.googlecode.com/files/mecab-0.996.tar.gz
RUN cd /root; wget https://mecab.googlecode.com/files/mecab-ipadic-2.7.0-20070801.tar.gz

RUN mkdir -p /root/go/src
RUN mkdir -p /root/go/bin
ENV GOPATH /root/go
ENV PATH $PATH:/root/go/bin

# --- mecab install
RUN cd /root; \
    tar xvf mecab-0.996.tar.gz; \
    cd /root/mecab-0.996; \
    ./configure --enable-utf8-only; \
    make; \
    make install

RUN echo '/usr/local/lib' > /etc/ld.so.conf.d/mecab
RUN ldconfig

# --- mecab dic
RUN cd /root; \
   tar xvf mecab-ipadic-2.7.0-20070801.tar.gz

RUN cd /root/mecab-ipadic-2.7.0-20070801; \
    ./configure --with-charset=utf8; \
    make; \
    make install

# --- mecab server
run cd /root; \
    go get bitbucket.org/rerofumi/webmecab

EXPOSE 80
CMD ["/bin/bash"]
23
26
1

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
23
26