我有两个容器,分别是“ mysql-1 ”和“ ubuntu-1 ”。
我互相联系用过
docker run -p 127.0.0.1:3010:80 -itd --link mysql-1 --name=ubuntu-1 my-ubuntu-image:latest
注意 my-ubuntu-image 已经具有 nginx 和 php ,并且我在其中更改了 config ubuntu-1 如下:
默认
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
当我尝试运行不带mysql的简单php文件时,它起作用。 但是现在我尝试运行如下所示的php文件:
<?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";
?>
如何连接到mysql-1容器?
ubuntu-1内部的环境变量如下:
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