2016-10-16 3 views
5

Docker를 사용하여 Mac에서 PHP와 MySQL로 작성된 웹 응용 프로그램을 테스트 할 컨테이너를 만듭니다. 내 PHP 응용 프로그램은 뚱뚱한 - 무료 프레임 워크 MVC와 라우팅을 사용하여 작성됩니다. 두 개의 Dockerfiles (하나는 MySQL 용이고 다른 하나는 PHP 용)가 있습니다. 나는 테스트 도커 응용 프로그램을 성공적으로 사용 했으므로 이미지가 올바르게 설치되었다고 생각합니다.Docker 사용 "SQLSTATE [HY000] [2002] 해당 파일이나 디렉토리가 없습니다."

오류의 주요 부분 : SQLSTATE[HY000] [2002] Connection refused

내 PHP Dockerfile을 :

Internal Server Error 

SQLSTATE[HY000] [2002] No such file or directory 

[fatfree/lib/DB/SQL.php:466] PDO->__construct('mysql:host=127.0.0.1;port=3306;dbname=robohome','root','password',array(1002=>'SET NAMES utf8;')) 
[fatfree/app/Controllers/Controller.php:24] DB\SQL->__construct('mysql:host=127.0.0.1;port=3306;dbname=robohome','root','password') 

참고, 내가 대신 localhost127.0.0.1을 사용하여 연결하면 나는 말한다 약간 다른 오류가

FROM php:5.6-apache 

RUN docker-php-ext-install mysqli pdo pdo_mysql 
RUN a2enmod rewrite 

내 MySQL Docker 파일 :

오류가 라인 (24) 언급 691,363,210
FROM mysql:5.7 

ENV MYSQL_ROOT_PASSWORD password 
ENV MYSQL_DATABASE robohome 

COPY ./schema.sql /docker-entrypoint-initdb.d/ 

Controller.php 파일 :

MYSQL_SERVERNAME = "localhost" <-This is what I've tried changing to 127.0.0.1 
MYSQL_USERNAME = "root" 
MYSQL_PASSWORD = "password" 
MYSQL_DBNAME = "robohome" 

내 도커 구성 파일 :

version: '2' 

services: 
    web: 
    build: ./docker/php 
    ports: 
     - 80:80 
    volumes: 
     - .:/var/www/html/ 
    links: 
     - db 
    db: 
    build: ./docker/mysql 
    ports: 
     - 3306 

<?php 

namespace Controllers; 

class Controller 
{ 
    protected $f3; 
    protected $db; 

    public function __construct() 
    { 
     $f3 = \Base::instance(); 
     $this->f3 = $f3; 

     $mysqlServerName = $f3->get("MYSQL_SERVERNAME"); 
     $mysqlDatabseName = $f3->get("MYSQL_DBNAME"); 

     //$container = \DI\ContainerBuilder::buildDevContainer(); <-Not used currently 

     //Below is line 24 referred to in the error 
     $db = new \DB\SQL(
      "mysql:host={$mysqlServerName};port=3306;dbname={$mysqlDatabseName}", 
      $f3->get("MYSQL_USERNAME"), 
      $f3->get("MYSQL_PASSWORD") 
     ); 

     $this->db = $db; 
    } 

MYSQL_* 값은 .ini 파일에서 가져온 있습니다

를 실행하여 실행합니다.. 그때 docker ps에서 얻을 수있는 출력은 다음과 같습니다

내가 대신 포 그라운드에서 실행하면, 나는 다음과 같은 출력을 얻을
CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS    PORTS      NAMES 
f35066a16586  robohomeweb_mysql "docker-entrypoint.sh" 3 minutes ago  Up 2 seconds  0.0.0.0:32777->3306/tcp robohomeweb_mysql_1 
86d34eb34583  robohomeweb_php  "apache2-foreground"  3 minutes ago  Up 2 seconds  0.0.0.0:80->80/tcp  robohomeweb_php_1 

: 내 연구에서

Building php 
Step 1 : FROM php:5.6-apache 
---> 8f9b7e57129a 
Step 2 : RUN docker-php-ext-install mysqli pdo pdo_mysql 
---> Using cache 
---> fadd8f9e7207 
Step 3 : RUN a2enmod rewrite 
---> Using cache 
---> 9dfed7fdc60f 
Successfully built 9dfed7fdc60f 
Building mysql 
Step 1 : FROM mysql:5.7 
---> eda6a4884645 
Step 2 : ENV MYSQL_ROOT_PASSWORD password 
---> Using cache 
---> 759895ac5772 
Step 3 : ENV MYSQL_DATABASE robohome 
---> Using cache 
---> e926c5ecc088 
Step 4 : COPY ./schema.sql /docker-entrypoint-initdb.d/ 
---> Using cache 
---> cf5d00aa8020 
Successfully built cf5d00aa8020 
Starting robohomeweb_php_1 
Starting robohomeweb_mysql_1 
Attaching to robohomeweb_mysql_1, robohomeweb_php_1 
php_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3. Set the 'ServerName' directive globally to suppress this message 
php_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3. Set the 'ServerName' directive globally to suppress this message 
php_1 | [Sun Oct 16 20:21:17.944575 2016] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.10 (Debian) PHP/5.6.26 configured -- resuming normal operations 
php_1 | [Sun Oct 16 20:21:17.946919 2016] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND' 
mysql_1 | 2016-10-16T20:21:18.036272Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 
mysql_1 | 2016-10-16T20:21:18.038330Z 0 [Note] mysqld (mysqld 5.7.16) starting as process 1 ... 
mysql_1 | 2016-10-16T20:21:18.043331Z 0 [Note] InnoDB: PUNCH HOLE support available 
mysql_1 | 2016-10-16T20:21:18.043603Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 
mysql_1 | 2016-10-16T20:21:18.043951Z 0 [Note] InnoDB: Uses event mutexes 
mysql_1 | 2016-10-16T20:21:18.044077Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier 
mysql_1 | 2016-10-16T20:21:18.044260Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3 
mysql_1 | 2016-10-16T20:21:18.044414Z 0 [Note] InnoDB: Using Linux native AIO 
mysql_1 | 2016-10-16T20:21:18.045150Z 0 [Note] InnoDB: Number of pools: 1 
mysql_1 | 2016-10-16T20:21:18.045620Z 0 [Note] InnoDB: Using CPU crc32 instructions 
mysql_1 | 2016-10-16T20:21:18.047629Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M 
mysql_1 | 2016-10-16T20:21:18.057705Z 0 [Note] InnoDB: Completed initialization of buffer pool 
mysql_1 | 2016-10-16T20:21:18.059988Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). 
mysql_1 | 2016-10-16T20:21:18.074670Z 0 [Note] InnoDB: Highest supported file format is Barracuda. 
mysql_1 | 2016-10-16T20:21:18.101209Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables 
mysql_1 | 2016-10-16T20:21:18.101433Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ... 
mysql_1 | 2016-10-16T20:21:18.354806Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB. 
mysql_1 | 2016-10-16T20:21:18.356928Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active. 
mysql_1 | 2016-10-16T20:21:18.357158Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active. 
mysql_1 | 2016-10-16T20:21:18.358049Z 0 [Note] InnoDB: Waiting for purge to start 
mysql_1 | 2016-10-16T20:21:18.412987Z 0 [Note] InnoDB: 5.7.16 started; log sequence number 12179647 
mysql_1 | 2016-10-16T20:21:18.414470Z 0 [Note] Plugin 'FEDERATED' is disabled. 
mysql_1 | 2016-10-16T20:21:18.421833Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool 
mysql_1 | 2016-10-16T20:21:18.424144Z 0 [Note] InnoDB: Buffer pool(s) load completed at 161016 20:21:18 
mysql_1 | 2016-10-16T20:21:18.425607Z 0 [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key 
mysql_1 | 2016-10-16T20:21:18.427018Z 0 [Note] Server hostname (bind-address): '*'; port: 3306 
mysql_1 | 2016-10-16T20:21:18.427581Z 0 [Note] IPv6 is available. 
mysql_1 | 2016-10-16T20:21:18.427749Z 0 [Note] - '::' resolves to '::'; 
mysql_1 | 2016-10-16T20:21:18.428019Z 0 [Note] Server socket created on IP: '::'. 
mysql_1 | 2016-10-16T20:21:18.456023Z 0 [Warning] 'db' entry 'sys [email protected]' ignored in --skip-name-resolve mode. 
mysql_1 | 2016-10-16T20:21:18.456354Z 0 [Warning] 'proxies_priv' entry '@ [email protected]' ignored in --skip-name-resolve mode. 
mysql_1 | 2016-10-16T20:21:18.480237Z 0 [Warning] 'tables_priv' entry 'sys_config [email protected]' ignored in --skip-name-resolve mode. 
mysql_1 | 2016-10-16T20:21:18.488758Z 0 [Note] Event Scheduler: Loaded 0 events 
mysql_1 | 2016-10-16T20:21:18.490880Z 0 [Note] mysqld: ready for connections. 
mysql_1 | Version: '5.7.16' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL) 

을, 나는 두 localhost를 사용하여 연결하려고했습니다 기술적으로 다르게 취급되기 때문에 127.0.0.1입니다. 또한 TCP 대신 소켓을 통해 대화하려고하는 것과 관련이 있습니다. 이상적으로 나는 Dockerfiles에 구울 수있는 솔루션을 원합니다. 따라서 명령 기억이나 내가 어떻게했는지 걱정할 필요가 없습니다.

+1

글쎄, mysql이 듣기 위해 설정 한 것은 무엇인가? 포트 3306 (TCP의 기본값) 또는 소켓 파일? – Devon

+0

위의 내 의견에 따라 (오른쪽으로 스크롤해야 함)'0.0.0.0:32777-> 3306/tcp' – roundtheworld

+0

Docker 작성 파일을 포함하도록 편집되었습니다. – roundtheworld

답변

24

의견에서 지적한 바와 같이 제공된 도커 작성 파일은 귀하의 질문과 매우 관련이 있습니다.

고정 표시기-작성 파일 links에 대한 문서, 데이터베이스 컨테이너가 그래서 MySQL의 컨테이너에서 당신을 지적해야 PHP 컨테이너에서 db 호스트를 해결, db를 귀하의 경우에는

Containers for the linked service will be reachable at a hostname identical to the alias, or the service name if no alias was specified.

라는 말한다.구성 파일에서 localhostdb으로 바꾸면 PHP 컨테이너가 MySQL에 연결할 수 있어야합니다.

+1

그것은 내가 누락되었거나 이해하지 못했던 정보 조각이었습니다. 데이터베이스의 위치가 도커 작성 파일의 이름과 일치해야합니다. 나는 그것을 작동하게했다! – roundtheworld

+1

나는'links' 섹션이 필요 없다고 덧붙이고 싶었습니다. 나의 문제는 나의 설정 파일에있는 데이터베이스의 위치가 yml 파일'db'에있는 내 서비스의 이름이었을 때'localhost'였습니다. – roundtheworld

+1

@ aroundtheworld 사실, 최근 (사실) Docker는 컨테이너가 서로를 찾을 수있게 해주는 기본 [DNS] (https://docs.docker.com/engine/userguide/networking/default_network/configure-dns/)에 대한 지원을 받았다. 이름으로. – R0MANARMY

1

PHP는 mysql과 다른 도커 이미지에 있습니다. localhost와 php의 127.0.0.1은 mysql을 가리 키지 않습니다. mysql 도커 인스턴스의 ip에 연결해야한다.

또한 mysql이 모든 인터페이스에서 수신 대기하는지 확인하십시오. mysql.ini에서 사용 가능한 모든 인터페이스를 수신 대기하려면 0.0.0.0 수신 대기해야합니다. 기본적으로 localhost (및 PHP 도커 컨테이너는 다른 호스트)의 연결 만 허용합니다.

+1

다른 곳에서 필요로하지 않는 한 게시하지 말고 보안을 설정해야하는 다른 끝점 만 게시하십시오. 그들을 연결하십시오. https://docs.docker.com/compose/compose-file/#links – user2105103

+0

php와 mysql이 서로 다른 Docker 이미지에 있다는 것을 알고 있습니다. 그러나 필자가 작성한 온라인에서 찾은 샘플에는 아무 것도 볼 수 없었습니다. 그거 했어. 나는이 작품을 광산과 매우 흡사하게 만들었다. https://github.com/mikesir87/docker-compose-demo – roundtheworld

+0

나는 응답을 업데이트했다. 컨테이너를 연결하면 원하는 것이고 링크 이름으로 참조됩니다. – user2105103

관련 문제