首页 > 解决方案 > 如何在 Docker 中创建 PHP 环境?

问题描述

我正在尝试使用apache2服务器对php web-app进行 dockerize。为 php 和 apache2 创建环境的任何更好的方法。我在互联网云上搜索找不到任何解决方案。

我已经使用 ubuntu:latest 作为基础映像,如果第一个工作比继续进行,我会尝试逐步创建映像。我已经尝试在没有 php 的情况下创建图像并且我成功地运行了 docker 容器但是没有 PHP 没有运行的意义比我采取下一步将 PHP 添加到图像中,现在我被卡住了。

下面有错误的图像。

在此处输入图像描述

这是我的 Dockerfile

#Download base image
FROM ubuntu:latest

#Install tzdata and set timezone.
#Update all packages run the 'apt update' command before installing any packages.
#we need to use -y flag otherwise it will ask for yes/no?
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata && apt-get install -y software-properties-common

#Install C/C++ 
RUN add-apt-repository -y ppa:ubuntu-toolchain-r/test
#RUN apt install build-essential
RUN apt-get update -y
RUN apt-get install -y gcc
RUN apt-get install -y g++
RUN ln -f -s /usr/bin/gcc /usr/bin/gcc
RUN ln -f -s /usr/bin/g++ /usr/bin/g++

RUN apt-get update -y
RUN add-apt-repository ppa:ondrej/php

# Install apache, PHP, and supplimentary programs. openssh-server, curl, and lynx-cur are for debugging the container.
RUN apt-get update && apt-get -y upgrade && DEBIAN_FRONTEND=noninteractive apt-get -y install \
    apache2 php7.4 php7.4-mysql libapache2-mod-php7.4 curl lynx-common

# Enable apache mods.
RUN a2enmod php7.4
RUN a2enmod rewrite

#Remove any unnecessary files
RUN apt-get clean

#Setup Apache2 servers                                               
#Debian configuration requires the environment variables APACHE_RUN_USER, APACHE_RUN_GROUP, and APACHE_PID_FILE to be set
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_PID_FILE /var/run/apache2.pid
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2

#Expose ports
EXPOSE 80

#Change Permission
RUN chmod -R 777 /var/www/html/

#Copy files to webserver 
COPY editor /var/www/html/

# Remove Default index.html
RUN rm /var/www/html/index.html

#Start services
CMD ["/usr/sbin/apache2ctl","-D","FOREGROUND"]

谢谢!!

标签: phplinuxdocker

解决方案


g++ 和 gcc 的符号链接不好。

以下对我有用:

#Download base image
FROM ubuntu:latest

#Install tzdata and set timezone.
#Update all packages run the 'apt update' command before installing any packages.
#we need to use -y flag otherwise it will ask for yes/no?
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata && apt-get install -y software-properties-common

#Install C/C++ 
RUN add-apt-repository -y ppa:ubuntu-toolchain-r/test
#RUN apt install build-essential
RUN apt-get update -y && apt-get install -y gcc g++
#RUN apt-get install -y gcc
#RUN apt-get install -y g++
#RUN ln -f -s /usr/bin/gcc /usr/bin/gcc
#RUN ln -f -s /usr/bin/g++ /usr/bin/g++

#RUN apt-get update -y
RUN add-apt-repository ppa:ondrej/php

# Install apache, PHP, and supplimentary programs. openssh-server, curl, and lynx-cur are for debugging the container.
RUN apt-get update && apt-get -y upgrade && DEBIAN_FRONTEND=noninteractive apt-get -y install apache2 php7.4 php7.4-mysql libapache2-mod-php7.4 curl lynx-common

# Enable apache mods.
RUN a2enmod php7.4
RUN a2enmod rewrite

#Remove any unnecessary files
RUN apt-get clean

#Setup Apache2 servers                                               
#Debian configuration requires the environment variables APACHE_RUN_USER, APACHE_RUN_GROUP, and APACHE_PID_FILE to be set
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_PID_FILE /var/run/apache2.pid
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2

#Expose ports
EXPOSE 80

#Change Permission
RUN chmod -R 777 /var/www/html/

#Copy files to webserver 
COPY editor /var/www/html/

# Remove Default index.html
RUN rm /var/www/html/index.html

#Start services
CMD ["/usr/sbin/apache2ctl","-D","FOREGROUND"]

推荐阅读