Skip to content

构建 apache_php7_mysql 的dockerfile

字数
503 字
阅读时间
4 分钟
更新日期
3/6/2021

记录一下,纯dockerfile

docker
From alpine:3.8

# Timezone
ENV TIMEZONE            Asia/Shanghai
ENV PHP_MEMORY_LIMIT    512M
ENV MAX_UPLOAD          50M
ENV PHP_MAX_FILE_UPLOAD 200
ENV PHP_MAX_POST        100M
ENV XDEBUG_REMOTE_ENABLE 1
ENV XDEBUG_REMOTE_CONNECT_BACK 0
ENV XDEBUG_REMOTE_HOST  host.docker.internal
ENV XDEBUG_REMOTE_PORT    9000
ENV XDEBUG_IDEKEY        PHPSTORM

# 替换为阿里云的源,构建速度更快
RUN sed -i 's#dl-cdn\.alpinelinux\.org#mirrors\.aliyun\.com#' /etc/apk/repositories

# install mysql, apache and php and php extensions, tzdata, wget
RUN apk update && \
    apk add \
    mysql mysql-client \
    apache2 \
    curl wget \
    tzdata \
    php7-apache2 \
    php7-cli \
    php7-phar \
    php7-zlib \
    php7-zip \
    php7-bz2 \
    php7-ctype \
    php7-mysqli \
    #php7-mysql \
    php7-pdo_mysql \
    php7-opcache \
    php7-pdo \
    php7-json \
    php7-curl \
    php7-gd \
    php7-gmp \
    php7-mcrypt \
    php7-openssl \
    php7-dom \
    php7-xml \
    php7-iconv \
    php7-xdebug \
    php7-mbstring \
    openssh

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

# configure timezone, mysql, apache
RUN cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && \
    echo "${TIMEZONE}" > /etc/timezone && \
    mkdir -p /run/mysqld && \
    chown -R mysql:mysql /run/mysqld /var/lib/mysql && \
    mysql_install_db --user=mysql --verbose=1 --basedir=/usr --datadir=/var/lib/mysql --rpm > /dev/null && \
    sed -i '/skip-external-locking/a log_error = \/var\/lib\/mysql\/error.log' /etc/mysql/my.cnf && \
    sed -i '/skip-external-locking/a general_log = ON' /etc/mysql/my.cnf && \
    sed -i '/skip-external-locking/a general_log_file = \/var\/lib\/mysql\/query.log' /etc/mysql/my.cnf && \
    ln -s /usr/lib/libxml2.so.2 /usr/lib/libxml2.so && \
    sed -i 's#AllowOverride None#AllowOverride All#' /etc/apache2/httpd.conf && \
    sed -i 's#ServerName www.example.com:80#\nServerName localhost:80#' /etc/apache2/httpd.conf && \
    sed -i 's#^DocumentRoot ".*#DocumentRoot "/www"#g' /etc/apache2/httpd.conf && \
    sed -i 's#/var/www/localhost/htdocs#/www#g' /etc/apache2/httpd.conf && \
    sed -i "s|;*date.timezone =.*|date.timezone = ${TIMEZONE}|i" /etc/php7/php.ini && \
    sed -i "s|;*memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|i" /etc/php7/php.ini && \
    sed -i "s|;*upload_max_filesize =.*|upload_max_filesize = ${MAX_UPLOAD}|i" /etc/php7/php.ini && \
    sed -i "s|;*max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|i" /etc/php7/php.ini && \
    sed -i "s|;*post_max_size =.*|post_max_size = ${PHP_MAX_POST}|i" /etc/php7/php.ini && \
    sed -i "s|;*cgi.fix_pathinfo=.*|cgi.fix_pathinfo= 0|i" /etc/php7/php.ini && \
    mkdir -p /run/apache2 && \
    chown -R apache:apache /run/apache2 && \
    mkdir /www && \
    echo "<?php phpinfo(); ?>" > /www/phpinfo.php


COPY upload /www
RUN chown -R apache:apache /www

# Configure xdebug
RUN echo "zend_extension=xdebug.so" > /etc/php7/conf.d/xdebug.ini && \ 
    echo -e "\n[XDEBUG]"  >> /etc/php7/conf.d/xdebug.ini && \ 
    echo "xdebug.remote_enable=${XDEBUG_REMOTE_ENABLE}" >> /etc/php7/conf.d/xdebug.ini && \  
    echo "xdebug.remote_handler=dbgp" >> /etc/php7/conf.d/xdebug.ini && \  
    echo "xdebug.remote_host=${XDEBUG_REMOTE_HOST}" >> /etc/php7/conf.d/xdebug.ini && \  
    echo "xdebug.remote_port=${XDEBUG_REMOTE_PORT}" >> /etc/php7/conf.d/xdebug.ini && \  
    echo "xdebug.remote_autostart=1" >> /etc/php7/conf.d/xdebug.ini && \ 
    echo "xdebug.remote_connect_back=${XDEBUG_REMOTE_CONNECT_BACK}" >> /etc/php7/conf.d/xdebug.ini && \ 
    echo "xdebug.idekey=${XDEBUG_IDEKEY}" >> /etc/php7/conf.d/xdebug.ini && \ 
    echo "xdebug.remote_log=\"/tmp/xdebug.log\"" >> /etc/php7/conf.d/xdebug.ini

# Configure sshd
RUN sed -i s/#PermitRootLogin.*/PermitRootLogin\ yes/ /etc/ssh/sshd_config
RUN /usr/bin/ssh-keygen -A
RUN ssh-keygen -t rsa -b 4096 -f  /etc/ssh/ssh_host_key

#start apache
RUN echo "#!/bin/sh" > /start.sh && \
    echo "httpd" >> /start.sh && \
    echo "/usr/sbin/sshd" >> /start.sh && \
    echo "nohup mysqld --skip-grant-tables --bind-address 0.0.0.0 --user mysql > /dev/null 2>&1 &" >> /start.sh && \
    echo "sleep 3 && mysql -uroot -e \"create database db;\"" >> /start.sh && \
    echo "tail -f /var/log/apache2/access.log" >> /start.sh && \
    chmod u+x /start.sh


WORKDIR /www

EXPOSE 80
EXPOSE 3306

ENTRYPOINT ["/start.sh"]

撰写