Docker Community Forums

Share and learn in the Docker community.

LAMP with Docker with Debian


#1

Hello,

I’m beginning with Docker, first message here.
I’m trying to create a LAMP image that I could use on several computer.

After some research, I’ve got a Debian LAMP server functionnal. But I have some questions. How could I have more than 1 virtualhost ? How modify the security.conf file (for ServerToken and signature) ?
If you have some suggestion about my files, I appreciate, thanks a lot.

Here’s the Dockerfile for Apache :

FROM httpd:2.4.38

RUN apt-get update;

COPY demo.apache.conf /usr/local/apache2/conf/demo.apache.conf
RUN echo "Include /usr/local/apache2/conf/demo.apache.conf" \
>> /usr/local/apache2/conf/httpd.conf

COPY security.conf /etc/apache2/conf-enabled/ 

The file demo.apache.conf

ServerName localhost

LoadModule deflate_module /usr/local/apache2/modules/mod_deflate.so
LoadModule proxy_module /usr/local/apache2/modules/mod_proxy.so
LoadModule proxy_fcgi_module /usr/local/apache2/modules/mod_proxy_fcgi.so

<VirtualHost *:80>
# Proxy .php requests to port 9000 of the php-fpm container
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
DocumentRoot /var/www/html/
<Directory /var/www/html/>
    DirectoryIndex index.php
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

# Send apache logs to stdout and stderr
#CustomLog /proc/self/fd/1 common
#ErrorLog /proc/self/fd/2
CustomLog /var/log/apache2/access.log combined
ErrorLog /var/log/apache2/error.log
</VirtualHost>

The Dockerfile for PHP and Debian

FROM php:7.3.1-fpm-stretch

RUN apt-get update;

RUN docker-php-ext-install mysqli

COPY php.ini /usr/local/etc/php/php.ini

And the docker-compose.yml

version: “3.2”

services:

php:
build:
context: ‘./php/’
networks:

  • backend
    volumes:
  • ~/lamp/public_html/:/var/www/html/
    container_name: php

apache:
build:
context: ‘./apache/’
depends_on:

  • php
  • mysql
    networks:
  • frontend
  • backend
    ports:
  • “80:80”
    volumes:
  • ~/lamp/public_html/:/var/www/html/
  • ~/lamp/log/:/var/log/apache2/
    container_name: apache

mysql:
image: mysql:${MYSQL_VERSION:-latest}
restart: always
ports:

  • “3306:3306”
    volumes:
    - ~/lamp/data:/var/lib/mysql
    networks:
  • backend
    environment:
    MYSQL_ROOT_PASSWORD: “{DB_ROOT_PASSWORD}" MYSQL_DATABASE: "{DB_NAME}”
    MYSQL_USER: “{DB_USERNAME}" MYSQL_PASSWORD: "{DB_PASSWORD}”
    container_name: mysql
    networks:
    frontend:
    backend:
    volumes:
    data:

Thanks a lot for your help,
Cordially,

ANDRE Ani


(Ove "Lime" Lindström) #2

Hi Andre and welcome to the world of the whale. :wink:

The first thing you should start understanding is that a container is NOT a host. If I was setting up multiple websites using the LAMP-stack, I would actually create them as different services/stacks. If needed, I would mount the different data volumes needed into the same base image but creating multiple containers.

If you do need multiple virtual hosts, you just need to create your own apache.conf and add it either when you build your image or when you start your container.


#3

Thanks :wink:

Ok, it’s clear now :wink:

I just need 2/3 virtualhost, and create an image to share it.

So, if I understand, I “just” need to add a new demo.apache.conf to add a new virtualhost and create a new directory if I use my example of code ?

Cordially,

andre0ani


(Tekki) #4

Don’t know if it helps you but this moment I stumbled over this interesting video by Bret Fisher that discusses the question whether you should run different vhosts in individual containers or all in a single one. “…using different Apache configs for each website. To me that’s old school, that’s pre-container.”


#5

Thanks, i’ll watch it, it could be interesting.
For now, I just need 2/3 vhost, and I don’t know how could I do that with my code…


#6

In my dockerfile, I can’t put a2enmod rewrite, command not found. The same thing if I tried to set the locales on fr_FR with locale-gen, and also for this : CMD /usr/sbin/apache2ctl -D FOREGROUND

What’s wrong with my files, I don’t understand what’s happening…
A little help please ?

Thanks a lot,

andre0ani


(Gary Forghetti) #7

Time to for you to debug.
After your build fails, you need to run a container and bring up a command shell from the most recent docker image displayed in a docker image ls command. You can then “snoop” around and try and locate the “missing” commands. If they are not found, you will need to install them, if you find them then you will need to specify the full path in your Dockerfile.

Check out this post. I provided detailed instructions to another person who had a similar problem.


#8

Ok, thanks for the informations :wink:


#9

Hello,

I’m trying to configure some informations about my host, like timezone and date.
I’m using this in a dockerfile, is it a good way to do ?

RUN apt-get -y install locales && rm -rf /var/lib/apt/lists/* \
&& localedef -i fr_FR -c -f UTF-8 -A /usr/share/locale/locale.alias fr_FR.UTF-8

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

ENV LC_ALL fr_FR.UTF-8
ENV LANG fr_FR.UTF-8
ENV LANGUAGE fr_FR.UTF-8
ENV TZ=Europe/Paris

And in my docker-compose I use this for the the hostname : hostname: mon-site

And, if I try to activate mod_rewrite, I have an error with the a2enmod command, not found.

I don’t understand why.

Thanks in advance :wink:

andre0ani


(Tekki) #10

a2enmod is a Debian-specific command and not available in the httpd image. If you want to use the rewrite module, you have to uncomment the following line in /usr/local/apache2/conf/httpd.conf:

#LoadModule rewrite_module modules/mod_rewrite.so

You find an example in the description of the httpd image under SSL/HTTPS that shows how to achieve this using sed.
There is quite a difference between the httpd image and what you know from Debian/Ubuntu.


#11

Thanks a lot for this precisions, I didn’t thought about that.