PHP、Laravel、Docker などの話がメインです

Docker コンテナに ssh でログインする

下記の記事で作成した Apache + PHP 環境のコンテナに ssh ログインしたいケースが出てきたので sshd も起動するように変更したときの手順メモ

Dockerfile の変更

Docker は 1 コンテナ 1 プロセスが推奨されていますが、今回のように複数プロセス起動させる場合、supervisord を使うのがいろいろと便利

前回の内容から supervisordsshd のインストールと初期設定が追加されている

# Apache + PHP
# 20160622
#   CentOS 6.6 + epel,remi,rpmforge
#   Apache 2.2.15
#   PHP 5.5.25

FROM centos:6
MAINTAINER takaya030

# update yum
RUN yum update -y && \
    yum clean all

# epel repo
RUN yum install -y http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm && \
    yum clean all
RUN sed -i -e "s/enabled *= *1/enabled=0/g" /etc/yum.repos.d/epel.repo

# remi repo
RUN yum install -y http://rpms.famillecollet.com/enterprise/remi-release-6.rpm && \
    yum clean all
RUN sed -i -e "s/enabled *= *1/enabled=0/g" /etc/yum.repos.d/remi.repo

# rpmforge repo
RUN yum install -y http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm && \
    yum clean all
RUN sed -i -e "s/enabled *= *1/enabled=0/g" /etc/yum.repos.d/rpmforge.repo

# mysql community repo
RUN yum install -y http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm && \
    yum clean all
RUN sed -i -e "s/enabled *= *1/enabled=0/g" /etc/yum.repos.d/mysql-community.repo

# httpd, sshd, scp, sudo, which
RUN yum install -y httpd httpd-tools openssh-server openssh-clients sudo which && \
    yum clean all

# mysql-community-client
RUN yum install --enablerepo=mysql56-community -y mysql-community-client && \
    yum clean all

# libmcrypt, s3cmd, supervisor
RUN yum install --enablerepo=epel -y libmcrypt s3cmd supervisor && \
    yum clean all

# gd-last (for php-gd)
RUN yum install --enablerepo=remi -y gd-last && \
    yum clean all

# php-pecl-memcached
RUN yum install --enablerepo=remi,remi-php55 -y php-pecl-memcached && \
    yum clean all

# php
RUN yum install --enablerepo=remi-php55 -y php php-devel php-gd php-mbstring php-mcrypt php-mysqlnd php-pear php-xml php-opcache && \
    yum clean all

# modify /etc/php.ini
RUN sed -i -e "s/;date.timezone *=.*$/date.timezone = Asia\/Tokyo/" /etc/php.ini

# composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# phpunit
RUN curl -L https://phar.phpunit.de/phpunit.phar > /usr/local/bin/phpunit && \
	chmod +x /usr/local/bin/phpunit

# initialize for ssh
RUN sed -i '/pam_loginuid\.so/s/required/optional/' /etc/pam.d/sshd
RUN /sbin/service sshd start
RUN /sbin/service sshd stop

# create login user
RUN useradd -d /home/laravel -m -s /bin/bash laravel
RUN echo laravel:password | chpasswd
RUN echo 'laravel ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers

# timezone
RUN cp -p /usr/share/zoneinfo/Japan /etc/localtime


ADD ./httpd.conf /etc/httpd/conf/httpd.conf
ADD ./index.html /webapp/public/index.html
ADD ./phpinfo.php /webapp/public/phpinfo.php
ADD ./supervisord.conf /etc/supervisord.conf

EXPOSE 22 80

CMD ["/usr/bin/supervisord"]

supervisord.conf の追加

必ず "nodaeman=true" とすること

http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)
;http_port=  ; (alternately, ip_address:port specifies AF_INET)
;sockchmod=0700              ; AF_UNIX socketmode (AF_INET ignore, default 0700)
;sockchown=nobody.nogroup     ; AF_UNIX socket uid.gid owner (AF_INET ignores)
;umask=022                   ; (process file creation umask;default 022)
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (logging level;default info; others: debug,warn)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=true               ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)

;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;http_username=user          ; (default is no username (open system))
;http_password=123           ; (default is no password (open system))
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;user=chrism                 ; (default is current user, required if root)
;directory=/tmp              ; (default is not to cd during start)
;environment=KEY=value       ; (key value pairs to add to environment)

serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl= ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

command=/usr/sbin/sshd -D

command=/usr/sbin/httpd -DFOREGROUND

docker-compose.yml の変更

コンテナのポート 22 をホストのポート 2022 にフォワードさせる設定追加

  build: ./webapp
  hostname: webapp
    - "80:80"
    - "2022:22"
    - /workspace/laravel:/webapp
    - mysql
    - memcached

 image: mysql:5.6
    - MYSQL_ROOT_PASSWORD=password
    - "3306:3306"
    - /mnt/sda1/var/lib/mysql:/var/lib/mysql

 image: memcached



docker@dev:~$ docker rm `docker ps -a -q`
docker@dev:~$ cd /home/docker/laravel
docker@dev:~/laravel$ docker-compose up -d


C:\>ssh laravel@ -p 2022
laravel@'s password:
Last login: Sun Sep  6 10:19:51 2015 from
[laravel@webapp ~]$ php --version
PHP 5.5.29 (cli) (built: Sep  3 2015 11:00:33)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies