Help us understand the problem. What is going on with this article?

LaradockにMecabを入れる

More than 1 year has passed since last update.

LaradockにMecabを入れる

入れる箇所は workspacephp-fpm の二カ所.
(php-worker は一旦除外)

インストール対象はmecab本体 と、php-mecab .
Mecabの辞書には mecab-ipadic-NEologd を採用.

workspace

Dockerfile-71

laradock/workspace/Dockerfile-71 を修正.
場所はどこでもいいので、以下を挿入

#####################################
# Mecab:
#####################################
USER root
ARG INSTALL_MECAB=false
ENV INSTALL_MECAB ${INSTALL_MECAB}
RUN if [ ${INSTALL_MECAB} = true ]; then \
    cd /usr/local/src && \
    apt-get update -yqq && \
    apt-get install -y sudo git mecab libmecab-dev mecab-ipadic-utf8 && \
    git clone --depth 1 https://github.com/neologd/mecab-ipadic-neologd.git && \
    cd /usr/local/src/mecab-ipadic-neologd && \
    ./bin/install-mecab-ipadic-neologd -n -y && \
    cd /usr/local/src && \
    git clone https://github.com/rsky/php-mecab && \
    cd /usr/local/src/php-mecab/mecab && \
    phpize && ./configure && make && make install && \
    echo "extension=mecab.so" >> /etc/php/7.1/mods-available/mecab.ini && \
    echo "mecab.default_dicdir=/usr/lib/mecab/dic/mecab-ipadic-neologd/" >> /etc/php/7.1/mods-available/mecab.ini && \
    ln -s /etc/php/7.1/mods-available/mecab.ini /etc/php/7.1/cli/conf.d/25-mecab.ini \
;fi

上記の通り、 INSTALL_MECAB が true の場合にインストールを実行する.
INSTALL_MECABdocker-compose.yml に定義なので後述.

Dockerfile-70

基本は同じ.
Dockerfile-70 の 場合は mecab.ini の置く場所が /etc/php/7.0/mods-available/mecab.ini で、シンボリックリンクも /etc/php/7.0/cli/conf.d/25-mecab.ini になるのでそこだけ修正.

Dockerfile-56

基本は同じ.
Dockerfile-56 の 場合は mecab.ini の置く場所が /etc/php/5.6/mods-available/mecab.ini で、シンボリックリンクも /etc/php/5.6/cli/conf.d/25-mecab.ini になるのでそこだけ修正.

php-fpm

Dockerfile-71

laradock/php-fpm/Dockerfile-71 を修正.

場所はどこでもいいので、以下を挿入

#####################################
# Mecab:
#####################################
USER root
ARG INSTALL_MECAB=false
ENV INSTALL_MECAB ${INSTALL_MECAB}
RUN if [ ${INSTALL_MECAB} = true ]; then \
    cd /usr/local/src && \
    apt-get update -yqq && \
    apt-get install -y sudo git mecab libmecab-dev mecab-ipadic-utf8 && \
    git clone --depth 1 https://github.com/neologd/mecab-ipadic-neologd.git && \
    cd /usr/local/src/mecab-ipadic-neologd && \
    ./bin/install-mecab-ipadic-neologd -n -y && \
    cd /usr/local/src && \
    git clone https://github.com/rsky/php-mecab && \
    cd /usr/local/src/php-mecab/mecab && \
    phpize && ./configure && make && make install \
;fi
COPY ./docker-php-ext-mecab.ini /usr/local/etc/php/conf.d/docker-php-ext-mecab.ini

上記の通り、 INSTALL_MECAB が true の場合にインストールを実行する.

INSTALL_MECABdocker-compose.yml に定義してある.

同時に laradock/php-fpm/docker-php-ext-mecab.ini を用意し、これを所定の場所にコピーする.

docker-php-ext-mecab.ini の中身は以下のとおり.

; Enable mecab extension module
extension=mecab.so
mecab.default_dicdir=/usr/lib/mecab/dic/mecab-ipadic-neologd/

Dockerfile-70

Dockerfile-71と同じ設定を書く.

Dockerfile-56

Dockerfile-71と同じ設定を書く.

環境設定ファイル

対象は .envdocker-compose.yml.
基本的には INSTALL_MECAB というフラグを設け、ここが true の場合はインストールを実行するようにする.

.env

workspace 用は WORKSPACE_INSTALL_MECAB
php-fpm 用は PHP_FPM_INSTALL_MECAB
で設定する.

(〜中略〜)
WORKSPACE_INSTALL_MECAB=true
(〜中略〜)
PHP_FPM_INSTALL_MECAB=true
(〜中略〜)

docker-compose.yml

WORKSPACE_INSTALL_MECABPHP_FPM_INSTALL_MECABINSTALL_MECAB として扱う.

    workspace:
      build:
        context: ./workspace
        args:
          (〜中略〜)
          - INSTALL_MECAB=${WORKSPACE_INSTALL_MECAB}
          (〜中略〜)

    php-fpm:
      build:
        context: ./php-fpm
        args:
          (〜中略〜)
          - INSTALL_MECAB=${PHP_FPM_INSTALL_MECAB}
          (〜中略〜)

docker-compose 実行

これで

$ docker-compose up -d nginx

で、mecab本体とphpでphp-mecab が利用可能となり docker-php-ext-mecab.ini の設定に従い、その辞書は mecab-ipadic-NEologd となる.

tomokove
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした