PHP container connect to external DB - php

I would like to connect my project php with an external MYSQL database.
Currently, i use this docker-compose wtih Dockerfile :
Dockefile
FROM php:5.6-apache-jessie
RUN apt-get update
RUN apt-get install -y apt-file
RUN apt-file update
RUN apt-get install -y vim bash net-tools
RUN docker-php-ext-install mysqli pdo pdo_mysql && docker-php-ext-enable pdo_mysql
EDIT docker-compose :
version: '3.8'
services:
db:
image: mariadb:10.1
ports:
- "3306:3306"
restart: always
environment:
- MYSQL_HOST=IP_EXTERNAL_DB
- MYSQL_ROOT_PASSWORD=AMAZING_PASSWORD
- MYSQL_DATABASE=NAME_OF_DATABASE_TRY_TO_CONNECT
- MYSQL_USER=USER
- MYSQL_PASSWORD=PASSWORD
php:
build:
context: .
dockerfile: Dockerfile
ports:
- "80:80"
volumes:
- .:/var/www/html
environment:
- PHP_FPM_CLEAR_ENV=no
links:
- db
PHP is ok, but the connection to external DB doesn't work.
I know i'm missing something, but i can't find it.
Any ideas ? :)

Related

Getting Class "mysqli" not found when using docker container to load mysql and php

First time working with creating a docker container, i am trying to add a mysql container which seems to be added fine according to the command line however in the browser i get error class mysqli not found
I have added a docker file which includes the following
Dockerfile
FROM php:8.0-apache
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
RUN apt-get update && apt-get upgrade -y
docker-compose.yml
version: '3.6'
services:
php-apache-environment:
container_name: php-apache
image: php:8.0-apache
volumes:
- ./php/src:/var/www/html/
ports:
- 8000:80
db:
container_name: db
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: MYSQL_ROOT_PASSWORD
MYSQL_DATABASE: MY_DATABASE
MYSQL_USER: MYSQL_USER
MYSQL_PASSWORD: MYSQL_PASSWORD
ports:
- "9906:3306"
I have also ran docker compose up
However i am still getting error class mysqli not found
Add in Dockerfile:
# Install mysqli
RUN docker-php-ext-install mysqli pdo pdo_mysql && docker-php-ext-enable pdo_mysql

docker-compose wp cli doesn't print anything on commands

I have this docker-compose configuration:
mysql:
image: mariadb:latest
container_name: ${APP_NAME}-mysql
command: --lower_case_table_names=2
volumes:
- './data/db:/var/lib/mysql:delegated'
environment:
- MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
- MYSQL_DATABASE=${DB_NAME}
restart: unless-stopped
ports:
- '3307:3306'
networks:
- app
wordpress:
build:
context: .
dockerfile: Dockerfile
container_name: ${APP_NAME}-wordpress
volumes:
- ./src:/var/www/html:rw,cached
- ./config/php.ini:/usr/local/etc/php/conf.d/php.ini
environment:
XDEBUG_ENABLED: 1
XDEBUG_CONFIG: remote_host=host.docker.internal
PHP_IDE_CONFIG: serverName=localhost
env_file:
- src/.env
depends_on:
- mysql
links:
- mysql:db
restart: always
networks:
- app
My Docker file looks like the following (after installing wordpress and mariadb)
# Install wp-cli
RUN apt-get update && apt-get install -y sudo less mariadb-client
RUN curl -o /bin/wp-cli.phar https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
RUN chmod +x /bin/wp-cli.phar
RUN cd /bin && mv wp-cli.phar wp
RUN mkdir -p /var/www/.wp-cli/cache && chown www-data:www-data /var/www/.wp-cli/cache
When I access the wordpress container I correctly see wp cli installed since I get this:
As you can see when I run the help command with --allow-root it doesn't print anything. Any suggestions?

PHP docker and using MySQL PDO

When creating a LEMP stack using PHP-FPM I can't seem to load the PDO dirver it keeps defaulting to the sqlite driver.
Docker compose:
version: "3.5"
services:
redis:
image: redis:alpine
container_name: errors-redis
mysql:
image: mysql:8.0
container_name: errors-mysql
working_dir: /application
volumes:
- .:/application
environment:
- MYSQL_ROOT_PASSWORD=test
- MYSQL_DATABASE=errors
- MYSQL_USER=test
- MYSQL_PASSWORD=test
ports:
- "9011:3306"
webserver:
image: nginx:latest
container_name: errors-nginx
working_dir: /application
volumes:
- .:/application
- ./dev/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
ports:
- "9010:80"
php-fpm:
image: php:fpm
container_name: errors-php-fpm
working_dir: /application
volumes:
- .:/application
- ./dev/php-fpm/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini
Docker file:
FROM phpdockerio/php72-fpm:latest
WORKDIR "/application"
# Install selected extensions and other stuff
RUN apt-get update \
&& apt-get -y --no-install-recommends install php-memcached php7.2-mysql php-redis php-xdebug \
&& apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*
PHP ini
upload_max_filesize = 100M
post_max_size = 108M
Now when I check phpinfo()
PDO support enabled
PDO drivers sqlite
I've not installed sqlite at any point so I'm not sure how it's gotten it. How do I change it to use MySQL instead of sqlite?
You need to add the PDO bits to your install as well...
apt-get -y --no-install-recommends install php-memcached php-redis php-xdebug mysqli pdo pdo_mysql \
Specifically the last 3. You can probably miss out the php7.2-mysql as this should be covered with the mysqli option.

How to set PHP $_SERVER variable with Docker?

I've created a PHP/Apache/MySQL development environment with Docker and would like to set variable that I can use with $_SERVER in PHP.
Usually I will configure something like that in my virtual host
SetEnv ENV "developement"
Is there a way to do it with my docker_compose.yml file ?
I'll try by using environment: - ENV=developement in my docker-compose file but it doesn't work.
Here is my Dockerfile
FROM php:5.6-apache
RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
RUN docker-php-ext-install pdo pdo_mysql gd curl
RUN a2enmod rewrite
RUN service apache2 restart
and my docker-compose.yml
version: '2'
services:
webserver:
build: ./docker/webserver
image: dev_web
ports:
- "80:80"
- "443:443"
volumes:
- /pathtodev/www:/var/www/html
links:
- db
environment:
- ENV=developement
db:
image: mysql:5.7
ports:
- "3306:3306"
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=******
- MYSQL_DATABASE=db_dev
Consider the below Dockerfile
FROM php:5.6-apache
RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
RUN docker-php-ext-install pdo pdo_mysql gd curl
RUN a2enmod rewrite
RUN service apache2 restart
RUN echo 'PassEnv FIRST_NAME' > /etc/apache2/conf-enabled/expose-env.conf
RUN echo '<?php echo $_SERVER["FIRST_NAME"];' > /var/www/html/first.php && echo '<?php echo $_SERVER["LAST_NAME"];' > /var/www/html/last.php
Now run the container using
docker run -it -e FIRST_NAME=TARUN -e LAST_NAME=LALWANI -p 80:80 4ba2aa50347b
Testing
$ curl localhost/first.php
TARUN
$ curl localhost/last.php
$
As you can see the only FIRST_NAME can be accessed, because we exposed the same using PassEnv directive in our apache config insider the container
I know this is a old thread, however, another solution could also be that inside your docker compose, you need to remove the "-" in front of ENV and then instead of "=" it needs to be ":".
It will end up looking like this then:
version: '2'
services:
webserver:
build: ./docker/webserver
image: dev_web
ports:
- "80:80"
- "443:443"
volumes:
- /pathtodev/www:/var/www/html
links:
- db
environment:
ENV: developement
db:
image: mysql:5.7
ports:
- "3306:3306"
volumes:
- ./db:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=******
- MYSQL_DATABASE=db_dev
At least it's working like that in version 3.

cannot connect mysql and php

Having this lamp docker setup (Im a sort of docker newbie):
docker-compose.yml
version: '2'
services:
webserver:
build: .
ports:
- "8080:80"
- "443:443"
volumes:
- ./:/var/www/html
links:
- db
db:
image: mysql:5.6
ports:
- "3306:3306"
volumes:
- /var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=adminpasswd
- MYSQL_DATABASE=se_racken_dev
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- "88:80"
links:
- db:db
Dockerfile
FROM php:5.6-apache
RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
RUN docker-php-ext-install pdo pdo_mysql gd curl
RUN a2enmod rewrite
RUN service apache2 restart
Just cant get my local environment to work.
Get this error message at localhost:8088:
SQLSTATE[HY000] [2002] No such file or directory
How can I configure my docker setup to get past this connection problem?
Got some hints here:
Starting with Zend Tutorial - Zend_DB_Adapter throws Exception: "SQLSTATE[HY000] [2002] No such file or directory"
Do I need to install vim and do what they suggest in above or can I solve it in my docker files?
On webserver image you should define the connection values such as database's hostname,database name, username and password.
What can you can is to specify a .env file as seen in https://docs.docker.com/compose/env-file/
In your case that should be:
DB_HOSTNAME=db:/var/run/mysqld/mysqld.sock
DB_USER=root
DB_PASSWORD=somepassword
DB_NAME=se_racken_dev
Then to your Dockerfile specify:
FROM php:5.6-apache
ENV MYSQL_HOSTNAME="localhost"
ENV MYSQL_PASSWORD=""
ENV MYSQL_USERNAME="root"
ENV MYSQL_DATABASE_NAME=""
RUN apt-get update -y && apt-get install -y libpng-dev curl libcurl4-openssl-dev
RUN docker-php-ext-install pdo pdo_mysql gd curl
RUN a2enmod rewrite
RUN service apache2 restart
Then modify your docker-compose.yml like that:
version: '2'
services:
webserver:
build: .
ports:
- "8080:80"
- "443:443"
volumes:
- ./:/var/www/html
links:
- db
environment:
- MYSQL_HOSTNAME=$DB_HOSTNAME
- MYSQL_PASSWORD=$DB_USER
- MYSQL_USERNAME=$DB_PASSWORD
- MYSQL_DATABASE_NAME=$DB_NAME
db:
image: mysql:5.6
ports:
- "3306:3306"
volumes:
- /var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=$DB_PASSWORD
- MYSQL_DATABASE=$DB_NAME
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
ports:
- "88:80"
links:
- db:db
Then you can use php's getenv to retreive the values from enviromental variables you specified. eg In your case the getenv('MYSQL_DATABASE_NAME') will retreive the value "se_racken_dev" as a string. So you need to modify the database configuration file in order to retreive correctly the database connection credentials using the function mewntioned above.
A simple way to remember is whatever you specify via ENV in your dockerfile you can retreive via getenv.

Categories