I had two container called "mysql-1" and "ubuntu-1".
And I connect each other used
docker run -p 127.0.0.1:3010:80 -itd --link mysql-1 --name=ubuntu-1 my-ubuntu-image:latest
Notice that my-ubuntu-image already had nginx and php, and I changed the config inside the ubuntu-1 like following:
default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/test/;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass 127.0.0.1:9999;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
}
www.conf
listen = 127.0.0.1:9999
It works, when I try to run a simple php file without mysql. But now I try to run a php file like following:
<?php
$servername = "localhost";
$username = "root";
$password = "123456";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
How to connect to mysql-1 container?
The environment Variables inside ubuntu-1 is like following:
MYSQL_1_PORT_33060_TCP=tcp://172.17.0.4:33060
MYSQL_1_PORT_33060_TCP_PORT=33060
MYSQL_1_PORT_3306_TCP_ADDR=172.17.0.4
HOSTNAME=632b415c540e
OLDPWD=/root
MYSQL_1_ENV_MYSQL_ROOT_PASSWORD=123456
PWD=/var/www/html/test
HOME=/root
MYSQL_1_PORT_3306_TCP_PORT=3306
MYSQL_1_ENV_MYSQL_MAJOR=8.0
MYSQL_1_ENV_GOSU_VERSION=1.7
MYSQL_1_PORT_3306_TCP_PROTO=tcp
MYSQL_1_PORT_33060_TCP_ADDR=172.17.0.4
MYSQL_1_ENV_MYSQL_VERSION=8.0.12-1debian9
MYSQL_1_PORT_33060_TCP_PROTO=tcp
TERM=xterm
SHLVL=1
MYSQL_1_NAME=/ubuntu-4/mysql-1
MYSQL_1_PORT_3306_TCP=tcp://172.17.0.4:3306
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MYSQL_1_PORT=tcp://172.17.0.4:3306
_=/usr/bin/env