私はdocker-composeでcodeigniter(localhost)とlaravel(api.localhost)の環境を構築しています。
codeigniter(localhost)からlaravel(api.localhost)へのapiリクエストをテストしていますが、NULLになってしまいます。
同じdocker-compose内だとIPアドレスが被ってしまうことが原因みたいなのですが、そこを解決する方法がわからないです。
その方法を教えていただければ幸いです。
試した事
codeigniterコンテナから以下を試してみましたが
*curl http://laravel(コンテナ名)/api/test
ダメでした。
*curl http://google.com
だと取得できます。
codeigniter
header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS"); header("Access-Control-Allow-Headers: *"); $opt = array("http" => array('ignore_errors' => true)); //FALSE $result = file_get_contents('http://api.localhost/api/test/', false, stream_context_create($opt)); //TRUE $result = file_get_contents('https://yahoo.com', false, stream_context_create($opt)); var_dump($result);
・laravel(api.localhost)route api.php
Route::resource('test/', 'TestController');
TestController
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class TestController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { return response()->json(['apple' => 'red', 'peach' => 'pink']); } }
docker-compose.yml
version: '2' services: #MySQLのデータが保存されるので、消さない! data: image: busybox volumes: - ./misc/data:/var/lib/mysql nginx: image: nginx:1.10 ports: #ポート番号は変更OK→ただし、nginxフォルダのdefault.confの設定も変更すること! - "80:80" #volumesがローカルからコンテナにマウントされるディレクトリ volumes: - ./nginx/default.conf:/etc/nginx/conf.d/default.conf #各自切り替える事 - /Users/alberorana/:/var/www/html/ - /Users/alberorana/api:/var/www/html/api links: - code - laravel container_name: nginx code: build: ./codeigniter volumes: - /Users/alberorana/:/var/www/html/ - ./php/php.ini:/usr/local/etc/php/conf.d/php.ini working_dir: /var/www/html/ links: - mysql - laravel ports: - "9000:9000" container_name: code laravel: build: ./laravel #volumesがローカルからコンテナにマウントされるディレクトリ volumes: #各自切り替える事 - /Users/alberorana/api:/var/www/html/api - ./php/php.ini:/usr/local/etc/php/conf.d/php.ini working_dir: /var/www/html/ links: - mysql ports: - "9001:9001" container_name: laravel mysql: image: mysql:5.7 environment: #各自のMuSQLコンテナに合わせる事 MYSQL_ROOT_PASSWORD: **** MYSQL_DATABASE: **** MYSQL_ROOT_USER: root ports: - "13306:3306" volumes_from: - data volumes: - ./misc/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf container_name: mysql
追記:ホストからhttpにて接続できる事は確認しております。
そして、イメージ画像に誤りがありました。
イメージ画像ではクライアント、APIサーバーに各々nginxがあるイメージですが、正しくは1つのnginxに両方のサーバーがあり、ドメインで振り分けている設定になります。
もちろん本番環境は各サーバーでIPアドレスもドメインも違うのですが、開発環境は1つのdocker-composeで完結させたいため、このような構成を検討している状態になります。
default.conf(nginx)
server { listen 80; index index.php index.html; server_name localhost; include mime.types; rewrite ^/(css|js)/(.*).\d+.(css|js)$ http://localhost/$1/$2.$3 redirect; root /var/www/html/public_html; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ .php$ { root /var/www/html/public_html; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass code:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param CI_ENV development; #各自切り替える事 fastcgi_param SERVER_NAME uchida; } location ~* .(ico|css|js|gif|jpe?g|png)(?[0-9]+)?$ { root /var/www/html/public_html; expires 0; access_log off; #rewrite ^/(css|js)/(.*).\d+.(css|js)$ http://localhost/$1/$2.$3 redirect; sendfile off; } } server { listen 80; index index.php index.html; server_name api.localhost; include mime.types; root /var/www/html/api/public; location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ .php$ { root /var/www/html/api/public; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass laravel:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param CI_ENV development; #各自切り替える事 fastcgi_param SERVER_NAME uchida; } }

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/05 12:52
2018/04/05 13:56 編集
2018/04/05 14:13
2018/04/05 14:27
2018/04/05 15:03 編集
2018/04/05 23:32
2018/04/05 23:51
2018/04/06 06:25