Of "different" from a standalone setup is that you will have running more than one process in your container. Not that containers have been made to run a single process/service, but do everything in one, in addition to "plaster" the structure of the container and make it have several responsibilities ends up taking some advantages that they bring, as service monitoring facility/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 to run, reliably, more than one process in the container. For this I will use the Honcho, but it has other forms like supervisor or even a script that starts all the service, commonly seen.
The option to Honcho is the simplicidada in the use and management of services, with supervisor the configuration is more extensive and the pattern constantly generates error in containers. Here’s an example using supervisor that even talks about not using many processes in one container: https://docs.docker.com/engine/admin/multi-service_container/
These would be all the necessary steps for what you need:
- installation and configuration of Honcho
- installation and configuration of Apache2.4
- installation and configuration of Maria DB 10.3
- PHP 7.0 installation and configuration
I will not install and not configure everything, just part with the Honcho configured and running Apache and Maria DB services. The rest, with PHP installation and configuration, dynamic settings, and other details you need to do as needed.
Let’s start adding the Maria DB repository, plus some utilities for the apt. 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'
Once done we will install Apache, Maria DB and pip
, Python package manager, which we will use to install the 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 of the Maria DB password configuration, so you don’t need to interact with the console when building the Docker, comment on some settings of Maria DB that usually generate errors and finally delete the cache of apt, for the image to be smaller.
Finally the Honcho, for him we will need a Procfile
, file where we specify which commands should be executed to start the services. Its contents 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 name of the process 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 celebrate our Procfile
for the image and set the working directory in /
, which is where by default the Honcho search for Procfile
:
ADD Procfile /Procfile
WORKDIR /
To start the Honcho and he raise the services of Apache and Maria DB, we will use the CMD
even so:
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 organize the commands as you see fit to generate less layers and have a smaller image.
Our Docker-Compose example 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 the Docker-Compose you can use whatever you need, how to map the volumes, have a network specific, deploy, 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 access the default Apache page in http://localhost:90 - may be that yours is not routing properly, so it goes by the IP of Docker machine.
Finally, there are several examples of LAMP in a single container, such as the linode/Lamp and the dell/Lamp, then you can assemble yours as you find most interesting. In addition, a search for LAMP Docker Hub/Store will return a lot,
My first reaction would have been to create a custom image with the stack. Like I’ve never used
Docker
, I can’t tell you how to do this, the difficulty involved, the reliability of this "homemade" solution, or how to actually do it– Jefferson Quesado
your idea would be to create 3 containers, each with a service or maintain the 3 services in one container?
– zwitterion
Together. I know that 99% of scenarios are not right, but this is a personal need.
– Fábio Jânio