menu
阿波銘木 公式ブログ

有限会社 阿波銘木 公式ブログ

docker勉強

ブログを書くのは本当に久しぶりのwebシステム担当です^^

 

と言っても阿波銘木の仕事もぼちぼちしています。

今は僕が東京の現場で仕事をしているので思うようには出来ませんが。。。

 

このブログシステムも僕が作りました(ナイショ)。

 

まぁ、エンジニアなんてそんなもんだろうと思うワケですw

 

今日はdockerの勉強をしていました。

OSはCentOSを指定したのですが、まさかまさかのsystemctlで怒られるという、とんでも事件に巻き込まれていますwww

 

いつもはvagrantで開発していたのですがGCPがdockerとの連携を強めたので、これはやらないとダメだと^^;

 

以下はDockerfile

 

-----Dockerfile-----

 

# Pull base image.
FROM centos

MAINTAINER development

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

# yum update
RUN yum -y install yum-plugin-priorities
RUN yum -y update

# SELinux(入ってないっぽいのでコメントアウト)
# RUN setenforce 0

# kernel
RUN yum -y update kernel*
RUN yum -y install kernel-devel kernel-headers dkms gcc gcc-c++

# repository
RUN yum -y install epel-release
RUN curl -sL https://rpm.nodesource.com/setup_4.x | bash -
RUN rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

# wget
RUN yum -y install wget

# zip unzip
RUN yum install -y zip unzip bzip2
    
# make
RUN yum -y install make

# Git(makeで死んだのでとりあえずyumで落とす)
RUN yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker
RUN yum -y install git

#uninstall apache
RUN yum -y remove httpd

# nginx for centos7
RUN rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
RUN yum -y install nginx

# PHP7
RUN yum -y install --enablerepo=remi-php70 php php php-mbstring php-pear php-mcrypt php-mysql php-fpm

# composer
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer

# rbenv
RUN yum -y install readline-devel
RUN git clone https://github.com/sstephenson/rbenv.git /root/.rbenv
RUN git clone https://github.com/sstephenson/ruby-build.git /root/.rbenv/plugins/ruby-build
RUN ./root/.rbenv/plugins/ruby-build/install.sh
ENV PATH /root/.rbenv/bin:$PATH
RUN echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh
RUN echo 'eval "$(rbenv init -)"' >> .bashrc

# add Ruby version
RUN rbenv install 2.4.0
RUN rbenv global 2.4.0

# rybygames
RUN bash -l -c "gem install bundler"
RUN rbenv rehash

# node.js
RUN yum -y install nodejs

# mariaDB
RUN yum -y install mariadb-server mariadb-devel galera

# iptables
RUN yum -y install iptables-services

 

# Java
RUN yum -y install java-1.8.0-openjdk

# jenkins
RUN wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
RUN rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
RUN yum -y install jenkins

 
# postfix(SMTPはログイン後に設定)
RUN yum -y install postfix
RUN yum -y install cyrus-sasl*
RUN yum -y install telnet
RUN yum -y install mailx

#root no password(とりあえずrootだけでやってみるのでコメントアウト)
#RUN echo 'LoginGraceTime 120' >> /etc/ssh/sshd_config
#RUN echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config
#RUN echo 'StrictModes yes' >> /etc/ssh/sshd_config

# docker user setup(とりあえずrootだけでやってみるのでコメントアウト)
#RUN useradd docker
#USER docker
#ORKDIR /home/docker
#ENV HOME /home/docker

#RUN mkdir .ssh
#RUN chmod 700 .ssh

# waiting port
EXPOSE 22
EXPOSE 80
EXPOSE 443

EXPOSE 3306
EXPOSE 8080

#mount
VOLUME [ '/var/www/html' ]

 

-----/Dockerfile-----

 

 

以下はdockerのimageにログインしてcommitしたいもの

 

-----docker images-----

 

# vi /etc/nginx/nginx.conf

------vi /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    index   index.php index.html index.htm;

    upstream phpfcgi {
        server 127.0.0.1:9000;
        # server unix:/var/run/php5-fpm.sock; #for PHP-FPM running on UNIX socket
    }

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
}
------/vi /etc/nginx/nginx.conf


# vi /etc/nginx/conf.d/web.conf

------vi /etc/nginx/conf.d/web.conf
server {
    listen 80;

    server_name localhost;
    root /var/www/html/public;

    fastcgi_read_timeout 120;

    client_max_body_size 5M;


    location / {
        try_files $uri /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
        include        fastcgi_params;
    }
}
------/vi /etc/nginx/conf.d/web.conf

# mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
# systemctl start nginx
# systemctl enable nginx

# vi /etc/php-fpm.d/www.conf
user = root
group = root

# systemctl start php-fpm
# systemctl enable php-fpm

 

# systemctl start mariadb
# systemctl enable mariadb
# mysql -u root
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO administrator IDENTIFIED BY 'admin' WITH GRANT OPTION;
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> quit;

# iptables -P FORWARD DROP
# systemctl stop iptables
# systemctl disable iptables

# systemctl stop firewalld
# systemctl disable firewalld

 

-----docker images-----

 

 

systemctlさえ問題なければすぐに開発環境の構築に成功するのですが、こう言ったアプリケーションはなんだかんだでうまくいかないものです^^;

大きな問題ではないですが。

 

これからはvagrantよりdockerのほうが主流になると信じてdockerで環境を作ろうと思っています。

 

こぼれ話で今までJavaとPHPの開発環境で使用していたVagrantfileも載せておきます。

 

-----Vagrantfile-----

# development Vagrantfile

Vagrant.configure("2") do |config|

  # OS
  config.vm.box = "bento/centos-7.2"
  config.vm.provider :virtualbox do |vb|
     vb.gui = true
     vb.customize ["modifyvm", :id, "--ostype", "RedHat_64"]
  end

  config.vm.provision :shell, inline: <<-EOT

    # timezone
    cp -fp /usr/share/zoneinfo/Japan /etc/localtime

    # yum update
    sudo yum -y install yum-plugin-priorities
    sudo yum -y update

    # SELinux
    sudo setenforce 0

    # kernel
    sudo yum update -y kernel*
    sudo yum -y install kernel-devel kernel-headers dkms gcc gcc-c++

    # repository
    sudo yum -y install epel-release
    sudo curl -sL https://rpm.nodesource.com/setup_4.x | bash -
    sudo rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
    sudo yum -y install wget

    # zip unzip
    sudo yum install -y zip unzip

    # Git
    sudo yum -y install wget gcc
    sudo yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker
    sudo yum -y install git
    cd /usr/local/src/
    git clone git://git.kernel.org/pub/scm/git/git.git
    cd git
    sudo make prefix=/usr/local all
    sudo make prefix=/usr/local install

    #uninstall apache
    sudo yum -y remove httpd

    # nginx for centos7
    sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    sudo yum -y install nginx
    sudo systemctl enable nginx
    sudo systemctl start nginx

    # PHP7
    sudo wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
    sudo rpm -ivh ./remi-release-7.rpm
    sudo yum --enablerepo=remi-php70 -y install php php php-mbstring php-pear php-mcrypt php-mysql php-fpm
    sudo systemctl start php-fpm
    sudo systemctl enable php-fpm

    # composer
    cd /var/www/html/
    curl -sS https://getcomposer.org/installer | php
    sudo mv composer.phar /usr/local/bin/composer

    # mariaDB
    sudo yum -y install mariadb-server mariadb-devel galera
    sudo systemctl start mariadb
    sudo systemctl enable mariadb

    #sudo mysql -u root
    #MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO administrator IDENTIFIED BY 'admin' WITH GRANT OPTION;
    #MariaDB [(none)]> FLUSH PRIVILEGES;
    #MariaDB [(none)]> quit;

    # iptables
    sudo yum -y install iptables-services

    sudo iptables -P FORWARD DROP
    sudo systemctl restart iptables
    sudo systemctl enable iptables

    # firewall
    sudo systemctl stop firewalld
    sudo systemctl disable firewalld

 

    #Java

    sudo yum -y install java-1.8.0-openjdk

    # jenkins
    sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo
    sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
    sudo yum -y install jenkins
    sudo systemctl start jenkins
    sudo systemctl enable jenkins
    sudo yum -y install cyrus-sasl*

    # postfix
    sudo systemctl start saslauthd
    sudo systemctl enable saslauthd
    sudo yum -y install telnet
    sudo yum -y install mailx
    
    #ネットワーク再起動
    sudo systemctl restart network.service
  EOT

  config.vm.network :forwarded_port, guest: 22,   host: 22, id: "ssh"
  config.vm.network :forwarded_port, guest: 80,   host: 80
  config.vm.network :forwarded_port, guest: 3306, host: 3306
  config.vm.network :forwarded_port, guest: 8080, host: 8080
  config.vm.synced_folder ".", "/vagrant", mount_options: ['dmode=777','fmode=755']

  config.vm.synced_folder "local directory', "host directory", owner: "vagrant", group: "vagrant", mount_options: ["dmode=777", "fmode=755"]
end

 

-----/Vagrantfile-----

 

これだけでは動きません。実際にはvagrantにログインしてサーバ設定してます。

 

 

参考になる方はどうぞ使って下さいwww

 

東京は今日は19度まで上がりました。

 

いよいよ春ですね。

GWに北海道に帰省したら何しようかな^^

 

 

カテゴリー
最新記事
アーカイブ
カテゴリー
最新記事
アーカイブ
facebook
Twitter
TOPへ戻る