Build a good looking Docker for Mac (+Docker Compose) at LAMP development environment

What is Docker Compose?

When using multiple content relatively, we can devide content into parts, so that we can manage multiple content at the same time by using file which is defined in YAML (Start-Stop-construction...)

Specification

・One for Web, One for DB, the 2 contents will be defined in one docker-compose.yml file

・Start/Stop the above 2 container at the same time at docker-compose.yml command

・Mount the Directory on the host (Mac) to the document route of web content (Web server), we can edit directly on our Host (Mac)

・We can also add new, edit the settings of Apache on Host (Mac) (even the content that can not be changed at .htacces)

・For debugging, i did make an input function for access log/error log of WEB server on HOST (Mac)

File Organization

./
 ├ docker-compose.yml
 ├ web/
 │  └ Dockerfile
 ├ var/
 │  ├ logs/
 │  └ public_html/ 
 └ files/
    └ apache_conf/
       └ 00_virtualhost.conf

./docker-compose.yml

・ I will describe the files that related at docker-compose later on

./web/Dockerfile

・ I will describe the file that we use to make container image later on

./var/logs/

・ We save the log of Apache of inner content (at docker-compose.yml, we define the mount to container so that we don't have to re-build the container image)

./var/public_html/

・ This is the Document route of Apache of inner content (at docker-compose.yml, we define the mount to container so that we don't have to re-build the container image)

files/apache_conf/00_virtualhost.conf

・ This is the added new setting of Apache (at docker-compose.yml, we define the mount to container so that we don't have to re-build the container image)

Dockerfileの詳細

Base on official CentOS, i did a briefly sum up

FROM centos:latest

# Install & Add repo
RUN rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
RUN rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

# Install PHP5.6 & Apache
RUN yum --enablerepo=remi-php56 install php php-devel php-gd php-xml php-mbstring php-opcache php-mcrypt php-mysqlnd -y

# Clear yum cache
RUN yum clean all

# for web
EXPOSE 80

CMD /usr/sbin/httpd -DFOREGROUND

docker-compose.yml

version: '2'
services:
  # mysql
  db01:
    image: mysql:5.6
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root

  # web
  web01:
    build: ./web
    image: web:24
    ports:
      - "80:80"
    links:
      - "db01:db01"
    volumes:
      - ./var/public_html:/var/www/public_html
      - ./var/logs:/var/log/httpd
      - ./files/apache_conf/00_virtualhost.conf:/etc/httpd/conf.d/00_virtualhost.conf

docker-compose.yml explanation

  • version '2'

・We can choose Version 1 or 2 but this time i use Ver.2

  • services: db01: / web01:

・I define the service job. we must put in Ver.2

  • image: mysql:5.6

・ i refer to the working container image. If there is no image in the Docker host, automatically download based on DockerHUB

  • ports:

・ Set contents [Port of link forward:(At Container) Port release]

  • environment:

・ Command implement when start container (Set Password )

  • build: ./web

・ Set Link save of DockerFile

  • image: web:24

・ After building images: below of build:, we set the image name, we can write out only Ver.2

  • links:

・コンテナ間のリンクの指定

・web01コンテナとdb01をリンク

・web01からは、ホスト名db01にてdb01に接続が可能

  • volumes:

・ Mount the file directory upon container of Host(Mac) ・ ./var/public_html:/var/www/public_html ・ ./var/logs:/var/log/httpd ・ ./files/apache_conf/00_virtualhost.conf:/etc/httpd/conf.d/00_virtualhost.conf

Start/Stop container

  • Start container $ docker-compose up -d

At the first time start, implement the docker-compose.yml to build image, start container...

Implement container at background by -d option

  • Stop container

docker-compose stop

Stop all things related to container

  • Edit Apache settings

After fixing 00_virtualhost.conf, restart container Web01

Implement only Web01 container by $ docker-compose restart web01

  • How to use
  1. We can see the file distributed by public_html by openning localhost on browser on Host (Mac)
  2. If we use DB from PHP program (web01 container) for reference, please set the host name to db01
  3. If we use DB from Host (Mac), we can refer at 127.0.0.1:3306