我是靠谱客的博主 壮观微笑,最近开发中收集的这篇文章主要介绍docker中开发php,phpstorm调试docker中的xdebug3步骤Dockerfilestart.sh构建镜像&&创建容器xdebug比较重要的配置,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
这里写自定义目录标题
- 步骤
- Dockerfile
- start.sh
- 构建镜像&&创建容器
- xdebug比较重要的配置
步骤
- 在同一个目录中分别创建
Dockerfile
和start.sh
文件 - 构建镜像
- 创建容器
- 安装浏览器扩展
- debug
Dockerfile
这里是php7.4版本,如果需要其他版本请修改
alpine
的版本
FROM alpine:3.13
MAINTAINER Paul Smith <pa.ulsmith.net>
# Add repos
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
# Add basics first
RUN apk update && apk upgrade && apk add
bash apache2 php7-apache2 curl ca-certificates git php7 php7-phar php7-json php7-iconv php7-openssl tzdata openntpd nano
RUN cp /usr/bin/php7 /usr/bin/php
# Setup apache and php
RUN apk add
php7-ftp
php7-xdebug
php7-mcrypt
php7-mbstring
php7-soap
php7-gmp
php7-pdo_odbc
php7-dom
php7-pdo
php7-zip
php7-mysqli
php7-sqlite3
php7-pdo_pgsql
php7-bcmath
php7-gd
php7-odbc
php7-pdo_mysql
php7-pdo_sqlite
php7-gettext
php7-xmlreader
php7-xmlrpc
php7-bz2
php7-pdo_dblib
php7-curl
php7-ctype
php7-session
php7-xmlwriter
php7-fileinfo
php7-simplexml
php7-tokenizer
php7-pecl-xlswriter
# Problems installing in above stack
RUN apk add file
RUN apk add postgresql-client
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/v3.13/community/ --allow-untrusted gnu-libiconv
ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php
RUN rm -f /var/cache/apk/*
# Add apache to run and configure
#RUN mkdir /run/apache2
RUN sed -i "s/#LoadModule rewrite_module/LoadModule rewrite_module/" /etc/apache2/httpd.conf
&& sed -i "s/#LoadModule session_module/LoadModule session_module/" /etc/apache2/httpd.conf
&& sed -i "s/#LoadModule session_cookie_module/LoadModule session_cookie_module/" /etc/apache2/httpd.conf
&& sed -i "s/#LoadModule session_crypto_module/LoadModule session_crypto_module/" /etc/apache2/httpd.conf
&& sed -i "s/#LoadModule deflate_module/LoadModule deflate_module/" /etc/apache2/httpd.conf
&& sed -i "s#^DocumentRoot ".*#DocumentRoot "/YouProject/web"#g" /etc/apache2/httpd.conf
&& sed -i "s#/var/www/localhost/htdocs#/YouProject/web#" /etc/apache2/httpd.conf
&& printf "n<Directory "/YouProject/web">ntAllowOverride Alln</Directory>n" >> /etc/apache2/httpd.conf
RUN mkdir /YouProject && mkdir /YouProject/web && chown -R apache:apache /YouProject && chmod -R 755 /YouProject && mkdir bootstrap
ADD start.sh /bootstrap/
RUN chmod +x /bootstrap/start.sh
EXPOSE 80
ENTRYPOINT ["/bootstrap/start.sh"]
start.sh
#!/bin/sh
# Set the timezone. Base image does not contain the setup-timezone script, so an alternate way is used.
if [ "$CONTAINER_TIMEZONE" ]; then
cp /usr/share/zoneinfo/${CONTAINER_TIMEZONE} /etc/localtime &&
echo "${CONTAINER_TIMEZONE}" > /etc/timezone &&
echo "Container timezone set to: $CONTAINER_TIMEZONE"
fi
# Force immediate synchronisation of the time and start the time-synchronization service.
# In order to be able to use ntpd in the container, it must be run with the SYS_TIME capability.
# In addition you may want to add the SYS_NICE capability, in order for ntpd to be able to modify its priority.
ntpd -s
# Apache server name change
if [ ! -z "$APACHE_SERVER_NAME" ]
then
sed -i "s/#ServerName www.example.com:80/ServerName $APACHE_SERVER_NAME/" /etc/apache2/httpd.conf
echo "Changed server name to '$APACHE_SERVER_NAME'..."
else
echo "NOTICE: Change 'ServerName' globally and hide server message by setting environment variable >> 'SERVER_NAME=your.server.name' in docker command or docker-compose file"
fi
# PHP Config
if [ ! -z "$PHP_SHORT_OPEN_TAG" ]; then sed -i "s/;?\s?short_open_tag = .*/short_open_tag = $PHP_SHORT_OPEN_TAG/" /etc/php7/php.ini && echo "Set PHP short_open_tag = $PHP_SHORT_OPEN_TAG..."; fi
if [ ! -z "$PHP_OUTPUT_BUFFERING" ]; then sed -i "s/;?\s?output_buffering = .*/output_buffering = $PHP_OUTPUT_BUFFERING/" /etc/php7/php.ini && echo "Set PHP output_buffering = $PHP_SHORT_OUTPUT_BUFFERING..."; fi
if [ ! -z "$PHP_OPEN_BASEDIR" ]; then sed -i "s/;?\s?open_basedir = .*/open_basedir = $PHP_OPEN_BASEDIR/" /etc/php7/php.ini && echo "Set PHP open_basedir = $PHP_OPEN_BASEDIR..."; fi
if [ ! -z "$PHP_MAX_EXECUTION_TIME" ]; then sed -i "s/;?\s?max_execution_time = .*/max_execution_time = $PHP_MAX_EXECUTION_TIME/" /etc/php7/php.ini && echo "Set PHP max_execution_time = $PHP_MAX_EXECUTION_TIME..."; fi
if [ ! -z "$PHP_MAX_INPUT_TIME" ]; then sed -i "s/;?\s?max_input_time = .*/max_input_time = $PHP_MAX_INPUT_TIME/" /etc/php7/php.ini && echo "Set PHP max_input_time = $PHP_MAX_INPUT_TIME..."; fi
if [ ! -z "$PHP_MAX_INPUT_VARS" ]; then sed -i "s/;?\s?max_input_vars = .*/max_input_vars = $PHP_MAX_INPUT_VARS/" /etc/php7/php.ini && echo "Set PHP max_input_vars = $PHP_MAX_INPUT_VARS..."; fi
if [ ! -z "$PHP_MEMORY_LIMIT" ]; then sed -i "s/;?\s?memory_limit = .*/memory_limit = $PHP_MEMORY_LIMIT/" /etc/php7/php.ini && echo "Set PHP memory_limit = $PHP_MEMORY_LIMIT..."; fi
if [ ! -z "$PHP_ERROR_REPORTING" ]; then sed -i "s/;?\s?error_reporting = .*/error_reporting = $PHP_ERROR_REPORTING/" /etc/php7/php.ini && echo "Set PHP error_reporting = $PHP_ERROR_REPORTING..."; fi
if [ ! -z "$PHP_DISPLAY_ERRORS" ]; then sed -i "s/;?\s?display_errors = .*/display_errors = $PHP_DISPLAY_ERRORS/" /etc/php7/php.ini && echo "Set PHP display_errors = $PHP_DISPLAY_ERRORS..."; fi
if [ ! -z "$PHP_DISPLAY_STARTUP_ERRORS" ]; then sed -i "s/;?\s?display_startup_errors = .*/display_startup_errors = $PHP_DISPLAY_STARTUP_ERRORS/" /etc/php7/php.ini && echo "Set PHP display_startup_errors = $PHP_DISPLAY_STARTUP_ERRORS..."; fi
if [ ! -z "$PHP_LOG_ERRORS" ]; then sed -i "s/;?\s?log_errors = .*/log_errors = $PHP_LOG_ERRORS/" /etc/php7/php.ini && echo "Set PHP log_errors = $PHP_LOG_ERRORS..."; fi
if [ ! -z "$PHP_LOG_ERRORS_MAX_LEN" ]; then sed -i "s/;?\s?log_errors_max_len = .*/log_errors_max_len = $PHP_LOG_ERRORS_MAX_LEN/" /etc/php7/php.ini && echo "Set PHP log_errors_max_len = $PHP_LOG_ERRORS_MAX_LEN..."; fi
if [ ! -z "$PHP_IGNORE_REPEATED_ERRORS" ]; then sed -i "s/;?\s?ignore_repeated_errors = .*/ignore_repeated_errors = $PHP_IGNORE_REPEATED_ERRORS/" /etc/php7/php.ini && echo "Set PHP ignore_repeated_errors = $PHP_IGNORE_REPEATED_ERRORS..."; fi
if [ ! -z "$PHP_REPORT_MEMLEAKS" ]; then sed -i "s/;?\s?report_memleaks = .*/report_memleaks = $PHP_REPORT_MEMLEAKS/" /etc/php7/php.ini && echo "Set PHP report_memleaks = $PHP_REPORT_MEMLEAKS..."; fi
if [ ! -z "$PHP_HTML_ERRORS" ]; then sed -i "s/;?\s?html_errors = .*/html_errors = $PHP_HTML_ERRORS/" /etc/php7/php.ini && echo "Set PHP html_errors = $PHP_HTML_ERRORS..."; fi
if [ ! -z "$PHP_ERROR_LOG" ]; then sed -i "s/;?\s?error_log = .*/error_log = $PHP_ERROR_LOG/" /etc/php7/php.ini && echo "Set PHP error_log = $PHP_ERROR_LOG..."; fi
if [ ! -z "$PHP_POST_MAX_SIZE" ]; then sed -i "s/;?\s?post_max_size = .*/post_max_size = $PHP_POST_MAX_SIZE/" /etc/php7/php.ini && echo "Set PHP post_max_size = $PHP_POST_MAX_SIZE..."; fi
if [ ! -z "$PHP_DEFAULT_MIMETYPE" ]; then sed -i "s/;?\s?default_mimetype = .*/default_mimetype = $PHP_DEFAULT_MIMETYPE/" /etc/php7/php.ini && echo "Set PHP default_mimetype = $PHP_DEFAULT_MIMETYPE..."; fi
if [ ! -z "$PHP_DEFAULT_CHARSET" ]; then sed -i "s/;?\s?default_charset = .*/default_charset = $PHP_DEFAULT_CHARSET/" /etc/php7/php.ini && echo "Set PHP default_charset = $PHP_DEFAULT_CHARSET..."; fi
if [ ! -z "$PHP_FILE_UPLOADS" ]; then sed -i "s/;?\s?file_uploads = .*/file_uploads = $PHP_FILE_UPLOADS/" /etc/php7/php.ini && echo "Set PHP file_uploads = $PHP_FILE_UPLOADS..."; fi
if [ ! -z "$PHP_UPLOAD_TMP_DIR" ]; then sed -i "s/;?\s?upload_tmp_dir = .*/upload_tmp_dir = $PHP_UPLOAD_TMP_DIR/" /etc/php7/php.ini && echo "Set PHP upload_tmp_dir = $PHP_UPLOAD_TMP_DIR..."; fi
if [ ! -z "$PHP_UPLOAD_MAX_FILESIZE" ]; then sed -i "s/;?\s?upload_max_filesize = .*/upload_max_filesize = $PHP_UPLOAD_MAX_FILESIZE/" /etc/php7/php.ini && echo "Set PHP upload_max_filesize = $PHP_UPLOAD_MAX_FILESIZE..."; fi
if [ ! -z "$PHP_MAX_FILE_UPLOADS" ]; then sed -i "s/;?\s?max_file_uploads = .*/max_file_uploads = $PHP_MAX_FILE_UPLOADS/" /etc/php7/php.ini && echo "Set PHP max_file_uploads = $PHP_MAX_FILE_UPLOADS..."; fi
if [ ! -z "$PHP_ALLOW_URL_FOPEN" ]; then sed -i "s/;?\s?allow_url_fopen = .*/allow_url_fopen = $PHP_ALLOW_URL_FOPEN/" /etc/php7/php.ini && echo "Set PHP allow_url_fopen = $PHP_ALLOW_URL_FOPEN..."; fi
if [ ! -z "$PHP_ALLOW_URL_INCLUDE" ]; then sed -i "s/;?\s?allow_url_include = .*/allow_url_include = $PHP_ALLOW_URL_INCLUDE/" /etc/php7/php.ini && echo "Set PHP allow_url_include = $PHP_ALLOW_URL_INCLUDE..."; fi
if [ ! -z "$PHP_DEFAULT_SOCKET_TIMEOUT" ]; then sed -i "s/;?\s?default_socket_timeout = .*/default_socket_timeout = $PHP_DEFAULT_SOCKET_TIMEOUT/" /etc/php7/php.ini && echo "Set PHP default_socket_timeout = $PHP_DEFAULT_SOCKET_TIMEOUT..."; fi
if [ ! -z "$PHP_DATE_TIMEZONE" ]; then sed -i "s/;?\s?date.timezone = .*/date.timezone = $PHP_DATE_TIMEZONE/" /etc/php7/php.ini && echo "Set PHP date.timezone = $PHP_DATE_TIMEZONE..."; fi
if [ ! -z "$PHP_PDO_MYSQL_CACHE_SIZE" ]; then sed -i "s/;?\s?pdo_mysql.cache_size = .*/pdo_mysql.cache_size = $PHP_PDO_MYSQL_CACHE_SIZE/" /etc/php7/php.ini && echo "Set PHP pdo_mysql.cache_size = $PHP_PDO_MYSQL_CACHE_SIZE..."; fi
if [ ! -z "$PHP_PDO_MYSQL_DEFAULT_SOCKET" ]; then sed -i "s/;?\s?pdo_mysql.default_socket = .*/pdo_mysql.default_socket = $PHP_PDO_MYSQL_DEFAULT_SOCKET/" /etc/php7/php.ini && echo "Set PHP pdo_mysql.default_socket = $PHP_PDO_MYSQL_DEFAULT_SOCKET..."; fi
if [ ! -z "$PHP_SESSION_SAVE_HANDLER" ]; then sed -i "s/;?\s?session.save_handler = .*/session.save_handler = $PHP_SESSION_SAVE_HANDLER/" /etc/php7/php.ini && echo "Set PHP session.save_handler = $PHP_SESSION_SAVE_HANDLER..."; fi
if [ ! -z "$PHP_SESSION_SAVE_PATH" ]; then sed -i "s/;?\s?session.save_path = .*/session.save_path = $PHP_SESSION_SAVE_PATH/" /etc/php7/php.ini && echo "Set PHP session.save_path = $PHP_SESSION_SAVE_PATH..."; fi
if [ ! -z "$PHP_SESSION_USE_STRICT_MODE" ]; then sed -i "s/;?\s?session.use_strict_mode = .*/session.use_strict_mode = $PHP_SESSION_USE_STRICT_MODE/" /etc/php7/php.ini && echo "Set PHP session.use_strict_mode = $PHP_SESSION_USE_STRICT_MODE..."; fi
if [ ! -z "$PHP_SESSION_USE_COOKIES" ]; then sed -i "s/;?\s?session.use_cookies = .*/session.use_cookies = $PHP_SESSION_USE_COOKIES/" /etc/php7/php.ini && echo "Set PHP session.use_cookies = $PHP_SESSION_USE_COOKIES..."; fi
if [ ! -z "$PHP_SESSION_COOKIE_SECURE" ]; then sed -i "s/;?\s?session.cookie_secure = .*/session.cookie_secure = $PHP_SESSION_COOKIE_SECURE/" /etc/php7/php.ini && echo "Set PHP session.cookie_secure = $PHP_SESSION_COOKIE_SECURE..."; fi
if [ ! -z "$PHP_SESSION_NAME" ]; then sed -i "s/;?\s?session.name = .*/session.name = $PHP_SESSION_NAME/" /etc/php7/php.ini && echo "Set PHP session.name = $PHP_SESSION_NAME..."; fi
if [ ! -z "$PHP_SESSION_COOKIE_LIFETIME" ]; then sed -i "s/;?\s?session.cookie_lifetime = .*/session.cookie_lifetime = $PHP_SESSION_COOKIE_LIFETIME/" /etc/php7/php.ini && echo "Set PHP session.cookie_lifetime = $PHP_SESSION_COOKIE_LIFETIME..."; fi
if [ ! -z "$PHP_SESSION_COOKIE_PATH" ]; then sed -i "s/;?\s?session.cookie_path = .*/session.cookie_path = $PHP_SESSION_COOKIE_PATH/" /etc/php7/php.ini && echo "Set PHP session.cookie_path = $PHP_SESSION_COOKIE_PATH..."; fi
if [ ! -z "$PHP_SESSION_COOKIE_DOMAIN" ]; then sed -i "s/;?\s?session.cookie_domain = .*/session.cookie_domain = $PHP_SESSION_COOKIE_DOMAIN/" /etc/php7/php.ini && echo "Set PHP session.cookie_domain = $PHP_SESSION_COOKIE_DOMAIN..."; fi
if [ ! -z "$PHP_SESSION_COOKIE_HTTPONLY" ]; then sed -i "s/;?\s?session.cookie_httponly = .*/session.cookie_httponly = $PHP_SESSION_COOKIE_HTTPONLY/" /etc/php7/php.ini && echo "Set PHP session.cookie_httponly = $PHP_SESSION_COOKIE_HTTPONLY..."; fi
# enable xdebug coverage for testing with phpunit (already installed)
echo "Enable XDebug for unit testing..."
echo 'zend_extension=/usr/lib/php7/modules/xdebug.so' >> /etc/php7/php.ini;
echo 'xdebug.coverage_enable=On' >> /etc/php7/php.ini;
echo 'xdebug.remote_enable=1' >> /etc/php7/php.ini;
echo 'xdebug.remote_connect_back=1' >> /etc/php7/php.ini;
echo 'xdebug.remote_log=/tmp/xdebug.log' >> /etc/php7/php.ini;
echo 'xdebug.remote_autostart=true' >> /etc/php7/php.ini;
echo 'xdebug.client_port=9100' >> /etc/php7/php.ini;
echo 'xdebug.idekey=PHPSTORM' >> /etc/php7/php.ini;
echo 'xdebug.client_host=host.docker.internal' >> /etc/php7/php.ini;
echo 'xdebug.mode=debug' >> /etc/php7/php.ini;
# Start (ensure apache2 PID not left behind first) to stop auto start crashes if didn't shut down properly
echo "Clearing any old processes..."
rm -f /run/apache2/apache2.pid
rm -f /run/apache2/httpd.pid
echo "Starting apache..."
httpd -D FOREGROUND
构建镜像&&创建容器
docker build -t my_php_dev:v1 . //再上面两个文件的同级目录下执行,注意最后的点号
docker run -d --name my_dev -p 8080:80 -v d:/data/YourProject:/YourProject my_php_dev:v1 //我是在git客户端创建,如果是在cmd命令行可能需要修改路径,创建容器
xdebug比较重要的配置
xdebug.idekey=PHPSTORM
xdebug.client_host=host.docker.internal //本机调试docker需要这个地址
xdebug.client_port=9100 //修改默认的9000端口
xdebug.mode=debug //开启debug
最后
以上就是壮观微笑为你收集整理的docker中开发php,phpstorm调试docker中的xdebug3步骤Dockerfilestart.sh构建镜像&&创建容器xdebug比较重要的配置的全部内容,希望文章能够帮你解决docker中开发php,phpstorm调试docker中的xdebug3步骤Dockerfilestart.sh构建镜像&&创建容器xdebug比较重要的配置所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复