Skip to content
Advertisement

How to Create PHP Envirnoment in Docker?

I am trying to dockerize php web-app with apache2 server. any better way to create environment for php and apache2. I have search on internet cloud not able to find any solution.

I have used ubuntu:latest as base image,I have tried to create image in step if first one work than go ahead. I have already tried to create image without php and i was succesful to run docker container but without PHP there is no meaning to run than I have take next step to add PHP into image, now I am stuck.

There is image of Error below.

enter image description here

Here is my 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"]

Thanks!!

Advertisement

Answer

The symbolic links for g++ & gcc are not good.

The following worked for me:

#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"]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement