LoginSignup
0
2

More than 1 year has passed since last update.

Sailを使ってインストールしたLaravelのTinkerで日本語が使えないときの対処

Last updated at Posted at 2021-10-27

この記事について

sailを使って、laravelの環境を構築したところTinkerで日本語の入力ができませんでした。症状としては以下の記事と同様。
Laravel の tinker で日本語が入力できなくて困ったけどなんとかなった話
Laravel の tinkerで日本語入力ができず色々迷走していたらもっと根本的な問題だった件

違いは、./vender/bin/sail tinkerとしてTinkerを起動している点です。

本記事では最終的には、Dockerコンテナ内でTinkerを起動すれば日本語入力できるようになったというところまで紹介します。

(良い方法がありましたらご教授ください。記事の一番下に参考として検証した事項をまとめています。)

環境(PC)

$ ./vendor/bin/sail -v                                               
Docker Compose version v2.0.0

環境(コンテナ内)

# php -v
PHP 7.4.25 (cli) (built: Oct 22 2021 12:34:59) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.25, Copyright (c), by Zend Technologies
    with Xdebug v3.1.1, Copyright (c) 2002-2021, by Derick Rethans

php artisan --version
Laravel Framework 8.57.0

対処法

  1. docker-compose.yml でlaravel.testのcontextを./vendor/laravel/sail/runtimes/7.4に変更(8.0になっていることがある)。

  2. ./vendor/laravel/sail/runtimes/7.4/Dockerfileに以下の部分を追加する

(略)

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

+ # コンテナ内を日本語入力に対応させる
+ RUN apt-get update && \
+     apt-get install -y locales && \
+     echo "ja_JP UTF-8" > /etc/locale.gen && \
+     locale-gen && \
+     echo "export LANG=ja_JP.UTF-8" >> ~/.bashrc

RUN apt-get update \
    && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 \

(略)
  1. ./vendor/bin/sail build./vendor/bin/sail up -dを実行
  2. docker compose pssail-8.0/appのコンテナIDを取得
  3. docker container exec -it c7b1ff0f152d bashでコンテナを起動
  4. php artisan tinkerとしてTinkerを立ち上げる。./vendor/bin/sail tinkerは不可

composerのエラーでTinkerが立ち上がらない場合は、phpのバージョンを下げたことが原因となっています。sail composer updateとして、パッケージをダウングレードしてください。

以上で、日本語入力ができるようになりました。

この方法ではコンテナに入らず、./vendor/bin/sail tinkerとした場合は、日本語入力はできませんでした😭(良い方法がありましたらご教授ください。以下に参考として検証した事項をまとめています。)

参考

検証したこと

コンテナ内のphpがlibeditに依存していない。多くの記事で問題の発端となっていたlibeditがphpの依存関係に入っていませんでした。が、libedit自体は割と新しそうなものが入っているようでした。(以下、コンテナ内で検証した結果)

# which php
/usr/bin/php

# ldd /usr/bin/php | grep libedit
(出力なし)

# apt-cache search libedit
libedit2 - BSD editline and history libraries

# apt-cache show libedit2
Package: libedit2
Status: install ok installed
Priority: optional
Section: libs
Installed-Size: 254
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Multi-Arch: same
Source: libedit
Version: 3.1-20191231-2
Replaces: libedit-dev (<< 3.1-20180525-2~)
Depends: libbsd0 (>= 0.1.3), libc6 (>= 2.14), libtinfo6 (>= 6)
Description: BSD editline and history libraries
 Command line editor library provides generic line editing,
 history, and tokenization functions.
 .
 It slightly resembles GNU readline.
Description-md5: 16f9b7e3d0830d8027f7f78c9be66a88
Original-Maintainer: LLVM Packaging Team <pkg-llvm-team@lists.alioth.debian.org>
Homepage: https://www.thrysoee.dk/editline/

Dockerfileの全体

vendor/laravel/sail/runtime/7.4/Dockerfile
FROM ubuntu:21.04

LABEL maintainer="Taylor Otwell"

ARG WWWGROUP

WORKDIR /var/www/html

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# コンテナ内を日本語入力に対応させる
RUN apt-get update && \
    apt-get install -y locales && \
    echo "ja_JP UTF-8" > /etc/locale.gen && \
    locale-gen && \
    echo "export LANG=ja_JP.UTF-8" >> ~/.bashrc

RUN apt-get update \
    && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 \
    && mkdir -p ~/.gnupg \
    && chmod 600 ~/.gnupg \
    && echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf \
    && apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E5267A6C \
    && apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C300EE8C \
    && echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu hirsute main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
    && apt-get update \
    && apt-get install -y php7.4-cli php7.4-dev \
       php7.4-pgsql php7.4-sqlite3 php7.4-gd \
       php7.4-curl php7.4-memcached \
       php7.4-imap php7.4-mysql php7.4-mbstring \
       php7.4-xml php7.4-zip php7.4-bcmath php7.4-soap \
       php7.4-intl php7.4-readline php7.4-pcov \
       php7.4-msgpack php7.4-igbinary php7.4-ldap \
       php7.4-redis php7.4-xdebug \
    && php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && curl -sL https://deb.nodesource.com/setup_16.x | bash - \
    && apt-get install -y nodejs \
    && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
    && apt-get update \
    && apt-get install -y yarn \
    && apt-get install -y mysql-client \
    && apt-get install -y postgresql-client \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN setcap "cap_net_bind_service=+ep" /usr/bin/php7.4

RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail

COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/7.4/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container

EXPOSE 8000

ENTRYPOINT ["start-container"]


docker-compose.ymlの全体

docker-compose.yml
# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/7.4
            # context: ./vendor/laravel/sail/runtimes/8.0 <- うまくいかない
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - redis
            - meilisearch
            - selenium
    mysql:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
          retries: 3
          timeout: 5s
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "redis-cli", "ping"]
          retries: 3
          timeout: 5s
    meilisearch:
        image: 'getmeili/meilisearch:latest'
        ports:
            - '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
        volumes:
            - 'sailmeilisearch:/data.ms'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "wget", "--no-verbose", "--spider",  "http://localhost:7700/health"]
          retries: 3
          timeout: 5s
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - '${FORWARD_MAILHOG_PORT:-1025}:1025'
            - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
    selenium:
       image: 'selenium/standalone-chrome'
       volumes:
            - '/dev/shm:/dev/shm'
       networks:
           - sail
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
    sailredis:
        driver: local
    sailmeilisearch:
        driver: local
0
2
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
0
2