バーチャルマシンみたいに使う場合の構成。
プロセス毎にコンテナ作るのが正当ですが、開発環境なら1つにまとめちゃって配布してもOK。
docker-composeインストール
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose 一度ログインし直す(重要) $ exit
$ docker-compose -v docker-compose version 1.22.0, build f46880fe
Dockerインストール
$ sudo yum install -y docker $ sudo usermod -aG docker ${USER} sudo systemctl start docker sudo systemctl enable docker sudo systemctl status docker
$ mkdir -p $HOME/docker-lamp/{mysql,php,centos} $ mkdir -p $HOME/docker-lamp/data/{html,db}
Docker 構成ファイル作成
$ vi $HOME/docker-lamp/data/html/index.php <?php echo "Hello Docker-Lamp!";
$ vi $HOME/docker-lamp/docker-compose.yml version: '2' services: mysql: build: ./mysql container_name: "mysql" ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: dbRootPassword MYSQL_DATABASE: appdb MYSQL_USER: appdbuser MYSQL_PASSWORD: appDbPassword volumes: - ./data/db:/var/lib/mysql restart: always privileged: true centos: depends_on: - mysql build: ./centos container_name: "centos" ports: - "80:80" working_dir: /var/www/html volumes: - ./data/html:/var/www/html restart: always privileged: true volumes: data:
$ vi $HOME/docker-lamp/mysql/my.cnf [mysqld] character-set-server=utf8
$ vi $HOME/docker-lamp/mysql/Dockerfile FROM mysql:5.7 COPY ./my.cnf /etc/mysql/conf.d/ EXPOSE 3306
$ vi $HOME/docker-lamp/php/php.ini [Date] date.timezone = "Asia/Tokyo" [mbstring] mbstring.internal_encoding = "UTF-8" mbstring.language = "Japanese"
$ vi $HOME/docker-lamp/centos/Dockerfile FROM centos:7 ## update yum RUN yum -y update RUN yum -y install yum-utils RUN yum clean all RUN yum -y install epel-release RUN yum -y groupinstall "Development Tools" RUN yum -y install wget git zsh curl ## install remi repo RUN wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm RUN rpm -Uvh remi-release-7*.rpm RUN yum-config-manager --enable remi-php70 ## install php7" RUN \ yum -y install \ php php-common \ php-mbstring \ php-mcrypt \ php-devel \ php-xml \ php-mysqlnd \ php-pdo \ php-opcache --nogpgcheck \ php-bcmath ## install composer RUN curl -sS https://getcomposer.org/installer | php && \ mv composer.phar /usr/local/bin/composer ## timezone setting RUN cp /etc/localtime /etc/localtime.org RUN ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime ## Apache install and Start RUN yum -y install httpd RUN systemctl enable httpd # exec. CMD ["/sbin/init"]
docker-lampのビルドと起動
$ cd $HOME/docker-lamp/ $ docker-compose build && docker-compose up -d
http://IPアドレス/
サービスを停止させる $ docker-compose stop コンテナの停止、削除、さらにネットワーク、記憶領域を全て削除 $ docker-compose down コンテナをすべて削除 $ docker rm -f `docker ps -a -q` イメージをすべて削除 $ docker rmi $(docker images -q) -f コンテナの全停止とイメージの削除 $ docker stop $(docker ps -q) && docker rmi $(docker images -q) -f