Monday, November 11, 2019

Run php 7 project in docker container where php 5.6 project is running on Host

Run multiple php versions in docker and host with apache / Run multiple websites on php 7 docker without affecting php 5.6 of host.

If one of your php project is running on php 5.6 of ubuntu/centos/fedora, Now if you want to run another project with php 7 inside docker container. It is quite good idea to run multiple php projects on same machine.

1) Suppose you have container where apache2 and php 7 is running

2) You have already launched this container with mapped port and htdocs path.
docker run -it -v /var/www/html:/var/www/html -p 7030:80 ubuntu:16.04 /bin/bash
As our 80 port of host is already busy, php 5.6 project is running there so we mapped 80 port of docker with 7030 port of host.
We have also mounted /var/www/html of host in /var/www/html of docker. We do not need to go inside docker container to access project files, it can be accessed in html folder of host as we already mapped host and docker directory.

3) Point your subdomain to the public IP of host and add a virtualhost in apache config of host.
<VirtualHost *:80>
ServerName php7.project.com
ProxyPreserveHost On
ProxyRequests off
ProxyPass / http://127.0.0.1:7030/
ProxyPassReverse / http://127.0.0.1:7030/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
We have proxy passed to port 7030 as 80 port of docker apache is mapped with 7030 port of host.
'ProxyPreserveHost On' parameter, does not show proxy-passed ip for js and css. It keeps the servername on every page.

5) Now create a file php7project.conf in /etc/apache2/sites-available of docker-apache with following content
<VirtualHost *:80>
ServerName php7.project.com
DocumentRoot /var/www/html/projectfoldername
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

6) Enable the site.
cd /etc/apache2/sites-available
a2ensite php7project.conf

7) Now virtualhostof host apache is mapped with virtualhost of docker apache and it will open the site.
http://php7.project.com/

8) Once you make the correct database connection in your docker apache project, your site is ready to access.

Metabase - Shifting your metabase docker container with existing data

1) Export the docker container from source instance as .gz
docker export containerid | gzip > containername.gz
2) Import the docker container into destination instance.
zcat containername.gz | docker import - imagename
3) Run the container as mapped port 3000
sudo docker run -it -p 3000:3000 6cf7f6ca9351 /bin/bash
where 6cf7f6ca9351 is image id.
4) Start the container and attach yourself into it.
docker start containerid
docker attach containerid

5) If you will run the file metabase.jar
java -jar metabase.jar
and if it creates new files metabase.db.mv.db and metabase.db.trace.db, it means metabase is starting the setup. It will be the fresh metabase installation so your old created dashboards and questions will not be visible.
You need to find the files of existing database i.e. metabase.db.mv.db, metabase.db.trace.db in the container which will be large in size. These are existing db files, you need to make metabase used these files as a database then only metabase will be run from the current state.
If you have started metabase and it has created new db files, stop the metabase and replace new files with existing files.

6) Start metabase again.
Once "Metabase Initialization COMPLETE" message appears and you go on login page instead of setup, old metabase is back.
You can login with existing details. Now make changes in database credentials from admin panel if database is changed on new instance, dashboards and questions will start showing your data from changed database.

Errors :
1) Connections could not be acquired from the underlying database.
Solution : Make sure metabase is able to make connection to database.

2) Java heap space memory issues.
Solution : RAM should be sufficient.
You can assign RAM to metabase while running it.
Generally, leaving 1-2 GB of RAM for these other processes should be enough; for example, you might set -Xmx to 1g for an instance with 2 GB of RAM, 2g for one with 4 GB of RAM, 6g for an instance with 8 GB of RAM, and so forth. You may need to experiment with these settings a bit to find the right number.
For 2GB RAM
java -Xmx1g -jar metabase.jar
More accute solution :
java -Xmx1g -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path/to/a/directory -jar metabase.jar