1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Docker CentOS7にPHP7.1+MySQL+Apache環境構築

Posted at

概要

Dockerでタイトルの環境を構築する設定ファイルを残しておく。
CentOS7上に構築すると躓くところが多かった…

環境

  • OS
    • CentOS 7
  • 言語
    • PHP 7.1.
  • DB
    • MySQL 8.0.32

設定ファイル

docker-compose.yaml

docker-compose.yaml
services:
  mysql:
    image: mysql:8.0.32
    container_name: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: mysql
      MYSQL_PORT: 3306
    ports:
      - 3307:3306
    volumes:
      - type: volume
        source: mysql_data
        target: /var/lib/mysql
      - type: bind
        source: ./docker/db/conf
        target: /etc/mysql/conf.d
      - type: bind
        source: ./docker/db/script/
        target: /docker-entrypoint-initdb.d
      
  app:
    privileged: true
    command: /sbin/init
    container_name: containerXXX
    build:
      context: .
      dockerfile: ./Dockerfile
    image: imageXXX
    ports:
      - 8080:80
    depends_on:
      - mysql

volumes:
  mysql_data:

Dockerfile

FROM centos:centos7

COPY . /var/www/

RUN sed -i 's|mirrorlist|#mirrorlist|g' /etc/yum.repos.d/CentOS-*
RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*

RUN yum -y update \
    && yum -y install httpd httpd-tools \
    && yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm \
    && yum -y install --enablerepo=remi-php71 php php-simplexml php-gd php-mbstring php-zip php-pdo php-mysqlnd \
    && yum -y install git \
    && yum clean all && rm -rf /var/cache/yum/*

RUN cp /var/www/docker/web/php.ini /etc/php.ini
WORKDIR /var/www

# Composer install
RUN php composer.phar install

# Apache start
RUN cp /var/www/docker/web/docker.conf /etc/httpd/conf.d/
RUN rm -f /etc/httpd/conf.d/welcome.conf
EXPOSE 80
RUN systemctl enable httpd.service
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

Apacheコンフィグ

docker.conf
LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %D %{X-Forwarded-For}i %{X-Forwarded-Proto}i" combined
SetEnvIf Remote_Addr 127.0.0.1 nolog
SetEnvIf User-Agent "internal dummy connection" nolog
SetEnvIf Request_URI "\.(gif|jpg|png|css|ico|js)$" nolog
SetEnvIf User-Agent "ELB-HealthChecker/[0-9]+\.[0-9]+" nolog

<Files "robots.txt">
    Satisfy any
    order allow,deny
    allow from all
</Files>

<VirtualHost *:80>
    Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preloa
    ServerAdmin root@localhost
    DocumentRoot /var/www/public
    ServerName localhost
    ErrorLog logs/error_log
    CustomLog logs/access_log combined env=!nolog
    <Directory "/var/www/public" >
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

躓きポイント

CentOS7はサポートが終わっているためか扱いづらかった

  • yum updateするだけでもデフォルトだと参照するURLが古くなっているため、書き換える必要があった
RUN sed -i 's|mirrorlist|#mirrorlist|g' /etc/yum.repos.d/CentOS-*
RUN sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*

ApacheをDockerビルド後にそのまま起動させるのにコツが必要だった

  • サーバー上だとapachectl startなどすればApacheが起動されるが、Dockefileにそのまま記載しても実行されない(エラーもなくただ実行されない)
  • 以下を記載する必要があり、この情報を見つけるまでが大変だった…
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
  • /usr/sbin/httpdのところはフルパスがいいらしい
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?