linux – Container LAMP com Debian Stretch

Question:

I did some internet searches and couldn't find instructions for mounting a LAMP container using the official Debian Stretch image ( https://hub.docker.com/_/debian/ ). The idea would be to upload a Docker container with Apache2.4, PHP7.0 and MariaDB 10.3 (docker-compose 3.3 and Dockerfile would be used for that).

Note: I understand that the ideal would be not to upload everything in a single container, however, if possible, in this specific scenario, I would like to integrate the LAMP structure in a single container, in the worst case scenario the bank could be in a separate container.

Any idea how to do this?

Answer:

What is "different" from a standalone setup is that you will have more than one process running in your container . Not that containers were made to run a single process/service, but doing everything in one, in addition to "plastering" the container structure and making it have several responsibilities, ends up taking some advantages they bring, such as ease of monitoring the service/ container , process management complexity, image maintainability – even using a provisioner like puppet or chef -, etc.

Anyway, an example of how to make this available would be using something that allows running, reliably, more than one process in the container . For this I will use Honcho , but there are other forms like the supervisord itself or even a script that starts all the services, commonly seen.

The option for Honcho is simplified in the use and management of services, with supervisord the configuration is more extensive and the default constantly generates errors in containers . Here's an example using supervisord that even talks about not using too many processes in the same container : https://docs.docker.com/engine/admin/multi-service_container/


These would be all the necessary steps for what you need:

  • Honcho installation and configuration
  • Apache2.4 installation and configuration
  • installation and configuration of Maria DB 10.3
  • PHP 7.0 installation and configuration

I'm not going to install or configure everything, just part with Honcho configured and running the services of Apache and Maria DB. The rest, with PHP installation and configuration, dynamic configurations, and other details that you need, just do as you need.


Let's get started adding the Maria DB repository, plus some apt utilities. This part will look like this and is self-explanatory:

RUN apt-get update --fix-missing; \
    apt-get upgrade -y; \
    apt-get install -y software-properties-common gnupg; \
    apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xF1656F24C74CD1D8; \
    add-apt-repository 'deb http://mariadb.biz.net.id/repo/10.3/debian stretch main'

That done we will install Apache, Maria DB and pip , Python package manager, which we will use to install Honcho , in the Dockerfile we would have something like this:

RUN { \
        echo "mariadb-server-10.3" mysql-server/root_password password 'unused'; \
        echo "mariadb-server-10.3" mysql-server/root_password_again password 'unused'; \
    } | debconf-set-selections; \
    apt-get install -y apache2 mariadb-common mariadb-server python-pip; \
    sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf; \
    rm -rf /var/lib/apt/lists/*

Otherwise, we just set the default values ​​for the Maria DB password configuration, so as not to interact with the console when building the docker image, we commented on some Maria DB settings that usually generate errors and finally we cleared the apt cache, to make the image smaller.

Finally Honcho , for it we will need a Procfile , file where we specify which commands must be executed to start the services. Its content will be this:

apache: /usr/sbin/apachectl -DFOREGROUND
mariadb: /usr/bin/mysqld_safe --timezone=UTC

Its format is very simple, the first element is the process name and the second is the command to run the process.

In the Dockerfile to install we will have this:

RUN pip install honcho

And this to copy our Procfile to the image and set the working directory to / , which is where by default Honcho looks for the Procfile :

ADD Procfile /Procfile

WORKDIR /

To start Honcho and it raise the services of Apache and Maria DB, we will use the CMD , like this:

CMD honcho start

This would be the full version of our Dockerfile :

FROM debian:stretch

ENV DEBIAN_FRONTEND=noninteractive

# adiciona repositório do mariadb
RUN apt-get update --fix-missing; \
    apt-get upgrade -y; \
    apt-get install -y software-properties-common gnupg; \
    apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xF1656F24C74CD1D8; \
    add-apt-repository 'deb http://mariadb.biz.net.id/repo/10.3/debian stretch main'

# instalando apache2, mariadb e python-pip
RUN { \
        echo "mariadb-server-10.3" mysql-server/root_password password 'unused'; \
        echo "mariadb-server-10.3" mysql-server/root_password_again password 'unused'; \
    } | debconf-set-selections; \
    apt-get install -y apache2 mariadb-common mariadb-server python-pip; \
    sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf; \
    rm -rf /var/lib/apt/lists/*

# instalando o honcho
RUN pip install honcho

ADD Procfile /Procfile

EXPOSE 80 443 3306

WORKDIR /

CMD honcho start

You can arrange the commands as you see fit to generate fewer layers and have a smaller image.

Our example docker-compose would look like this:

version: '3'
services:
  lamp:
    build:
      context: .
    container_name: sample_multi_process
    image: brunocesar/sample-multi-process:0.0.1-snapshot
    ports:
      - 90:80

In docker-compose you can use whatever you need, like mapping the volumes , having a specific network , deploying , etc.

Checking that everything is ok with docker-compose ps :

[bruno@bruno docker-multi-process]$ docker-compose ps
        Name                   Command           State                   Ports                
----------------------------------------------------------------------------------------------
sample_multi_process   /bin/sh -c honcho start   Up      3306/tcp, 443/tcp, 0.0.0.0:90->80/tcp

You can see the details with docker-compose logs -f --tail 20 , for example, or go to Apache's default page at http://localhost:90 – yours may not be routing correctly, so go by IP of the docker machine .

Finally, there are several LAMP examples in a single container, such as aa linode/lamp and dell/lamp , so you can assemble yours as you find most interesting. Also, a search for LAMP in the Docker Hub/Store will return a lot,

Scroll to Top
AllEscort